-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathubuntu.sh
executable file
·201 lines (143 loc) · 4.33 KB
/
ubuntu.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
#!/bin/bash
if [ $EUID != 0 ]; then
sudo "$0" "$@"
exit $?
fi
upgrade_machine() {
apt-get update
apt-get upgrade -y
apt-get dist-upgrade -y
apt-get autoremove -y
}
cleanup_old_kernels() {
# clean up old kernels
echo
echo "=========================================="
echo "cleaning up old kernels..."
echo "=========================================="
echo
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
}
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)}'
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
done
}
if [ ! -f .kernel_remove_ready ]; then
echo "=========================================="
echo "basic information"
echo "=========================================="
echo
echo "machine name [ubuntu]:"
read machine_name
if [[ -z "${machine_name// }" ]]; then
machine_name=ubuntu
fi
sed -i s/ubuntu/$machine_name/ /etc/hosts
sed -i s/ubuntu/$machine_name/ /etc/hostname
hostname $machine_name
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"
deluser --remove-home user
echo "creating new user $new_account"
adduser --quiet --disabled-password --gecos "" $new_account
echo "setting password for $new_account"
echo "$new_account:$new_account_password" | chpasswd
adduser $new_account sudo
echo
echo "=========================================="
echo "upgrading to latest version before doing a release upgrade..."
echo "=========================================="
echo
# upgrade everything before release upgrade
upgrade_machine
touch .kernel_remove_ready
echo "press any key to reboot machine, rerun script after rebooting"
read confirm_key
reboot -h now
exit
fi
if [ ! -f .release_upgrade_done ]; then
cleanup_old_kernels
# manually select upgrade options (important)
echo
echo "=========================================="
echo "begin release upgrade..."
echo "=========================================="
echo
do-release-upgrade
touch .release_upgrade_done
echo "press any key to reboot machine, rerun script after rebooting"
read confirm_key
reboot -h now
exit
fi
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
rm .kernel_remove_ready
rm .release_upgrade_done
echo
echo "=========================================="
echo "Configuring SSH..."
echo "=========================================="
echo
echo "disabling root login..."
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 $ssh_port/" /etc/ssh/sshd_config
echo "creating new ssh server keys..."
regenerate_ssh_server_keys
echo "restarting ssh..."
service ssh restart
echo
echo "=========================================="
echo "System is Ready"
echo "=========================================="
echo
echo "press any key to reboot machine"
read confirm_key
reboot -h now