-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuniversal.sh
335 lines (239 loc) · 6.87 KB
/
universal.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/bin/bash
if [ $EUID != 0 ]; then
sudo "$0" "$@"
exit $?
fi
temp_dir=.cac-setup
os_ubuntu=$temp_dir/ubuntu
os_centos=$temp_dir/centos
packman_apt=$temp_dir/apt-get
packman_yum=$temp_dir/yum
create_temp_directory() {
if [ ! -d $temp_dir ]; then
mkdir $temp_dir
if grep -i -q "ubuntu" /etc/os-release; then
touch $os_ubuntu
elif grep -i -q "centos" /etc/os-release; then
touch $os_centos
else
echo "Unsupported OS"
fi
if [ -n "$(command -v apt-get)" ]; then
touch $packman_apt
elif [ -n "$(command -v yum)" ]; then
touch $packman_yum
else
echo "Unsupported Package Manager"
fi
fi
}
clear_temp_directory() {
rm -rf $temp_dir
}
prepare_system() {
if [ ! -f $os_centos ]; then
# http://utdream.org/post.cfm/yum-couldn-t-resolve-host-mirrorlist-centos-org-for-centos-6
echo "nameserver 8.8.8.8" >> /etc/resolv.conf
echo "nameserver 8.8.4.4" >> /etc/resolv.conf
echo "nameserver 127.0.0.1" >> /etc/resolv.conf
fi
}
install_prerequisites() {
if [ -f $packman_yum ]; then
yum install yum-utils policycoreutils-python vim-common -y
fi
}
upgrade_machine() {
if [ -f $packman_apt ]; then
apt-get update
apt-get upgrade -y
apt-get dist-upgrade -y
apt-get autoremove -y
elif [ -f $packman_yum ]; then
yum update -y
fi
}
update_machine_name() {
echo "machine name [jeric]:"
read machine_name
if [[ -z "${machine_name// }" ]]; then
machine_name=jeric
fi
if [ -f $os_ubuntu ]; then
sed -i s/ubuntu/$machine_name/ /etc/hosts
sed -i s/ubuntu/$machine_name/ /etc/hostname
hostname $machine_name
elif [ -f $os_centos ]; then
hostnamectl set-hostname $machine_name
fi
}
create_new_account() {
echo "username:"
read new_account
echo "password:"
read -s new_account_password
# account update
echo "change root password"
echo "root:$new_account_password" | chpasswd
echo "deleting default user"
if [ -f $os_ubuntu ]; then
deluser --remove-home user
elif [ -f $os_centos ]; then
userdel -r user
fi
echo "creating new user $new_account"
if [ -f $os_ubuntu ]; then
adduser --quiet --disabled-password --gecos "" $new_account
elif [ -f $os_centos ]; then
adduser $new_account
fi
echo "setting password for $new_account"
echo "$new_account:$new_account_password" | chpasswd
if [ -f $os_ubuntu ]; then
adduser $new_account sudo
elif [ -f $os_centos ]; then
gpasswd -a $new_account wheel
fi
}
cleanup_old_kernels__ubuntu() {
mapfile -t kernels < <(dpkg -l | tail -n +6 | grep -E 'linux-image-[0-9]+' | grep -Fv $(uname -r) | awk '{print $2}' | sed s/-generic//)
for kernel in "${kernels[@]}"
do
echo "=========================================="
echo "removing $kernel"
echo "=========================================="
sudo dpkg --purge $kernel-generic
sudo dpkg --purge $kernel-header $kernel
done
echo
echo "=========================================="
echo "deleting old linux images from boot partition..."
echo "=========================================="
echo
ls /boot | grep "\-generic" | grep -Fv $(uname -r) | awk '{print "/boot/" $1}' | xargs rm
}
cleanup_old_kernels__centos() {
package-cleanup --oldkernels --count=1 -y
}
cleanup_old_kernels() {
# clean up old kernels
echo
echo "=========================================="
echo "cleaning up old kernels..."
echo "=========================================="
echo
if [ -f $os_ubuntu ]; then
cleanup_old_kernels__ubuntu
elif [ -f $os_centos ]; then
cleanup_old_kernels__centos
fi
}
regenerate_ssh_server_keys() {
mapfile -t ssh_key_types < <(ls -l /etc/ssh | grep .pub | awk '{print $9}' | sed -r 's/ssh_host_([a-zA-Z0-9]+)_key.pub/\1/')
echo "new ssh server keys:"
for ssh_key_type in "${ssh_key_types[@]}"
do
rm /etc/ssh/ssh_host_"$ssh_key_type"_key
rm /etc/ssh/ssh_host_"$ssh_key_type"_key.pub
ssh-keygen -q -N "" -t $ssh_key_type -f /etc/ssh/ssh_host_"$ssh_key_type"_key
echo
echo $ssh_key_type | awk '{print toupper($1)}'
if [ -f $os_ubuntu ]; then
ssh-keygen -E sha256 -lf /etc/ssh/ssh_host_"$ssh_key_type"_key
ssh-keygen -E md5 -lf /etc/ssh/ssh_host_"$ssh_key_type"_key
elif [ -f $os_centos ]; then
awk '{print $2}' /etc/ssh/ssh_host_"$ssh_key_type"_key.pub | base64 -d | sha256sum -b | awk '{print $1}' | xxd -r -p | base64
ssh-keygen -lf /etc/ssh/ssh_host_"$ssh_key_type"_key
fi
done
}
configure_ssh() {
echo
echo "=========================================="
echo "Configuring SSH..."
echo "=========================================="
echo
echo "disabling root login..."
sed -i "s/#PermitRootLogin yes/PermitRootLogin no/" /etc/ssh/sshd_config
sed -i "s/#PermitRootLogin no/PermitRootLogin no/" /etc/ssh/sshd_config
sed -i "s/PermitRootLogin yes/PermitRootLogin no/" /etc/ssh/sshd_config
echo "new ssh port [22]:"
read ssh_port
if [[ -z "${ssh_port// }" ]]; then
ssh_port=22
fi
sed -i "s/#Port 22/Port 22/" /etc/ssh/sshd_config
sed -i "s/Port 22/Port $ssh_port/" /etc/ssh/sshd_config
if [ -f $os_centos ]; then
echo "running semanage..."
semanage port -a -t ssh_port_t -p tcp $ssh_port
fi
echo "creating new ssh server keys..."
regenerate_ssh_server_keys
}
create_temp_directory
prepare_system
if [ ! -f $temp_dir/kernel_remove_ready ]; then
install_prerequisites
echo "=========================================="
echo "basic information"
echo "=========================================="
echo
update_machine_name
create_new_account
echo
echo "=========================================="
echo "upgrading to latest version before doing a release upgrade..."
echo "=========================================="
echo
# upgrade everything before release upgrade
upgrade_machine
touch $temp_dir/kernel_remove_ready
echo "press any key to reboot machine, rerun script after rebooting"
read confirm_key
reboot -h now
exit
fi
if [ ! -f $temp_dir/release_upgrade_done ]; then
cleanup_old_kernels
if [ -f $os_ubuntu ]; then
# manually select upgrade options (important)
echo
echo "=========================================="
echo "begin release upgrade..."
echo "=========================================="
echo
do-release-upgrade
touch $temp_dir/release_upgrade_done
echo "press any key to reboot machine, rerun script after rebooting"
read confirm_key
reboot -h now
exit
fi
touch $temp_dir/release_upgrade_done
fi
if [ -f $os_ubuntu ]; then
cleanup_old_kernels
# check for newer updates
echo
echo "=========================================="
echo "check for further updates..."
echo "=========================================="
echo
upgrade_machine
echo
echo "=========================================="
echo "cleaning up temporary files..."
echo "=========================================="
echo
fi
clear_temp_directory
configure_ssh
echo
echo "=========================================="
echo "System is Ready"
echo "=========================================="
echo
echo "press any key to reboot machine"
read confirm_key
reboot -h now