-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcentos.sh
186 lines (133 loc) · 4.02 KB
/
centos.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
#!/bin/bash
if [ $EUID != 0 ]; then
sudo "$0" "$@"
exit $?
fi
prepare_system() {
# 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
}
install_prerequisites() {
yum install yum-utils policycoreutils-python -y
}
upgrade_machine() {
yum update -y
}
cleanup_old_kernels() {
# clean up old kernels
echo
echo "=========================================="
echo "cleaning up old kernels..."
echo "=========================================="
echo
package-cleanup --oldkernels --count=1 -y
}
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)}'
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
done
}
if [ ! -f .kernel_remove_ready ]; then
prepare_system
echo "=========================================="
echo "basic information"
echo "=========================================="
echo
echo "machine name [centos]:"
read machine_name
if [[ -z "${machine_name// }" ]]; then
machine_name=centos
fi
hostnamectl set-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"
userdel -r user
echo "creating new user $new_account"
adduser $new_account
echo "setting password for $new_account"
echo "$new_account:$new_account_password" | chpasswd
gpasswd -a $new_account wheel
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
prepare_system
install_prerequisites
cleanup_old_kernels
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
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
semanage port -a -t ssh_port_t -p tcp $ssh_port
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