-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcentos_optimization.sh
More file actions
392 lines (344 loc) · 10.8 KB
/
centos_optimization.sh
File metadata and controls
392 lines (344 loc) · 10.8 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/bin/bash
#参考自网络,仅学习使用!
[ -f /etc/profile ] && source /etc/profile
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
function Inspect(){
# NO_1:检查是否root用户
if [[ "$(whoami)" != "root" ]]; then
echo "please run this script as root !" >&2
exit 1
fi
# No_2: 检查系统是否为64位系统,不是就退出
platform=`uname -i`
if [ $platform != "x86_64" ];then
echo "this script is only for 64bit Operating System !"
exit 1
fi
# NO_3: 检查系统是否是CentOS7
##Explain: Custom version '6' or '7'
distributor=`cat /etc/redhat-release|awk '{print $1}'`
VERSION=`cat /etc/redhat-release`
if [ $distributor != 'CentOS' ]; then
echo "this script is only for ${distributor} series"
exit 1
fi
cat << EOF
your system Version information: ${VERSION}
start optimizing...
EOF
}
function Choice_6_7(){
# No.4:确认输入Y或者N
yn="1"
cat <<EOF
`echo -e "\033[32m Please select CentOS6 or CentOS7 ? \033[0m"`
`echo -e "\033[32m 1: CentOS.6.x \033[0m"`
`echo -e "\033[32m 2: CentOS.7.x \033[0m"`
EOF
echo -n "please input [1\2]: "
read yn
if [ "$yn" != "1" -a "$yn" != "2" ]; then
echo "bye-bye!"
exit 0
else
if [ $yn -eq 1 ];then
main_6
else
main_7
fi
fi
}
function Count_down(){
# No.5:倒计时
for i in `seq -w 3 -1 1`
do
echo -ne "\b___${i}";
sleep 1;
done
echo -e "\b Good Luck ^_^!"
echo
}
#=============优化开始==================
# 1: instll repo
function CentOS6_repo(){
yum -y install wget &>/dev/null
if [ ! -e "/etc/yum.repos.d/bak" ]; then
mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/bak/CentOS-Base.repo.backup
fi
wget -q -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
wget -q -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum clean all >/dev/null 2>&1
yum makecache >/dev/null 2>&1
echo -e "\033[31m install CentOS6.x repo OK! \033[0m"
}
function CentOS7_repo(){
yum -y install wget &>/dev/null
if [ ! -e "/etc/yum.repos.d/bak" ]; then
mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/bak/CentOS-Base.repo.backup
fi
wget -q -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -q -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all >/dev/null 2>&1
yum makecache >/dev/null 2>&1
echo -e "\033[31m install CentOS7.x repo OK! \033[0m"
}
# 2: 安装必要的软件包
function Install_Soft(){
yum -y install tree nmap sysstat lrzsz dos2unix telnet &>/dev/null
yum -y install ntpdate &>/dev/null
yum -y install openssl openssh bash &>/dev/null
yum -y install bash-comp* &>/dev/null
echo -e "\033[31m Soft install Ok! \033[0m"
}
# 3: 关闭SELinux
function SELinux(){
sed -i 's#SELINUX=enforcing#SELINUX=disabled#' /etc/selinux/config
setenforce 0 &>/dev/null
echo -e "\033[31m SELinux stop ok \033[0m"
}
# 4: 设置系统运行级别
function CentOS6_init(){
INIT=`grep '^id:3:initdefault:$' /etc/inittab|wc -l`
if [ $INIT -ne 1 ];then
echo "id:3:initdefault:" >>/etc/inittab
fi
echo -e "\033[31m Runlevel 3 Ok! \033[0m"
}
# 5: 精简开机启动项
function C6_start_item(){
chkconfig --list|grep "3:on"|grep -vE "sshd|crond|network|rsyslog|sysstat"|awk '{print $1}'|sed -r 's#^(.*)#chkconfig \1 off#g'|bash
echo -e "\033[31m Stop CentOS_6 Useless service Ok! \033[0m"
}
function C7_start_item(){
#stop firewalld
systemctl stop firewalld
systemctl disable firewalld &>/dev/null
#systemctl stop postfix
#systemctl disable postfix
echo -e "\033[31m Stop CentOS_7 Useless service Ok! \033[0m"
}
# 6: 更改SSH配置文件
function C6_ssh(){
if [ -f /etc/ssh/sshd_config ];then
cat >>/etc/ssh/sshd_config <<EOF
Port 22
PermitEmptyPasswords no
UseDNS no
GSSAPIAuthentication no
LoginGraceTime 3m
#PermitRootLogin no
#ListenAddress 172.16.1.41
EOF
fi
/etc/init.d/sshd restart >/dev/null 2>&1
echo -e "\033[31m Configure sshd Ok! \033[0m"
}
function C7_ssh(){
if [ -f /etc/ssh/sshd_config ];then
cat >>/etc/ssh/sshd_config <<EOF
Port 22
PermitEmptyPasswords no
UseDNS no
GSSAPIAuthentication no
LoginGraceTime 3m
#PermitRootLogin no
#ListenAddress 172.16.1.41
EOF
fi
systemctl restart sshd
echo -e "\033[31m Configure sshd Ok! \033[0m"
}
# 7: 字符集设置
function C6_UTF(){
if [ -f /etc/sysconfig/i18n ];then
LANG="en_US.UTF-8"
echo 'LANG="en_US.UTF-8"'>/etc/sysconfig/i18n
echo -e "\033[31m Character set to UTF-8 \033[0m"
else
echo "/etc/sysconfig/i18n No file! false"
fi
}
function C7_UTF(){
if [ -f /etc/locale.conf ];then
LANG="en_US.UTF-8"
echo 'LANG="en_US.UTF-8"' >/etc/locale.conf
fi
echo -e "\033[31m Character set to UTF-8 \033[0m"
}
# 8: 设置时间同步
function NTPdate(){
/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
echo "#Time synchronization system-`date +%F`" >/var/spool/cron/root
echo '*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' >>/var/spool/cron/root
date
echo -e "\033[31m ntpdate Ok! \033[0m"
}
# 9: 调整Linux系统文件描述符数量
function Ulimit(){
sed -i "/^ulimit -SHn.*/d" /etc/rc.local
sed -i "/^ulimit -s.*/d" /etc/profile
sed -i "/^ulimit -c.*/d" /etc/profile
sed -i "/^ulimit -SHn.*/d" /etc/profile
if [ -f /etc/security/limits.conf ];then
cp -a /etc/security/limits.conf /etc/security/limits.conf.`date +%F`.bak
cat >> /etc/security/limits.conf <<EOF
#-- File descriptor number adjustment --------
* - nofile 65535
EOF
fi
echo -e "\033[31m limits Ok! \033[0m"
}
# 10: Linux服务器内核参数优化
function Sysctl(){
if [ ! -f /etc/sysctl.conf.`date +%F`.bak ];then
cp -a /etc/sysctl.conf /etc/sysctl.conf.`date +%F`.bak
fi
sed -i "/^net.ipv4.tcp_fin_timeout/d" /etc/sysctl.conf
sed -i "/^net.ipv4.tcp_tw_reuse/d" /etc/sysctl.conf
sed -i "/^net.ipv4.tcp_tw_recycle/d" /etc/sysctl.conf
sed -i "/^net.ipv4.tcp_syncookies/d" /etc/sysctl.conf
sed -i "/^net.ipv4.tcp_keepalive_time/d" /etc/sysctl.conf
sed -i "/^net.ipv4.ip_local_port_range/d" /etc/sysctl.conf
sed -i "/^net.ipv4.tcp_max_syn_backlog/d" /etc/sysctl.conf
sed -i "/^net.ipv4.tcp_max_tw_buckets/d" /etc/sysctl.conf
sed -i "/^net.ipv4.route.gc_timeout/d" /etc/sysctl.conf
sed -i "/^net.ipv4.tcp_syn_retries/d" /etc/sysctl.conf
sed -i "/^net.ipv4.tcp_synack_retries/d" /etc/sysctl.conf
sed -i "/^net.core.somaxconn/d" /etc/sysctl.conf
sed -i "/^net.core.netdev_max_backlog/d" /etc/sysctl.conf
sed -i "/^net.ipv4.tcp_max_orphans/d" /etc/sysctl.conf
#add
cat >> /etc/sysctl.conf <<EOF
#-----------Kernel optimization--------
net.ipv4.tcp_fin_timeout = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.ip_local_port_range = 4000 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.core.somaxconn = 16384
net.core.netdev_max_backlog = 16384
net.ipv4.tcp_max_orphans = 16384
EOF
#delete
sed -i "/^kernel.shmmax/d" /etc/sysctl.conf
sed -i "/^kernel.shmall/d" /etc/sysctl.conf
#add
shmmax=`free -l |grep Mem |awk '{printf("%d\n",$2*1024*0.9)}'`
shmall=$[$shmmax/4]
echo "kernel.shmmax = "$shmmax >> /etc/sysctl.conf
echo "kernel.shmall = "$shmall >> /etc/sysctl.conf
sysctl -p >/dev/null 2>&1
echo -e "\033[31m Kernel optimization Ok! \033[0m"
}
# 11: 定时清理邮件目录的垃圾文件
function Rm_mail(){
if [ -d /var/spool/clientmqueue/ ];then
find /var/spool/clientmqueue/ -type f|xargs rm -f
mkdir -p /var/spool/scripts/
cat >/var/spool/scripts/Dtm.sh <<EOF
#!/bin/bash
#Explain: Delete temporary mail
find /var/spool/clientmqueue/ -type f|xargs rm -f
EOF
echo ' ' >>/var/spool/cron/root
echo '#Delete temporary mail' >>/var/spool/cron/root
echo '/bin/sh /var/spool/scripts/Dtm.sh >/dev/null 2>&1' >>/var/spool/cron/root
fi
if [ -d /var/spool/postfix/maildrop/ ];then
find /var/spool/postfix/maildrop/ -type f|xargs rm -f
mkdir -p /var/spool/scripts/
cat >/var/spool/scripts/Dtm02.sh <<EOF
#!/bin/bash
#Explain: Delete temporary mail
find /var/spool/postfix/maildrop/ -type f|xargs rm -f
EOF
echo ' ' >>/var/spool/cron/root
echo "#Delete temporary mail" >>/var/spool/cron/root
echo '/bin/sh /var/spool/scripts/Dtm02.sh >/dev/null 2>&1' >>/var/spool/cron/root
fi
echo -e "\033[31m Spam mail clearance Ok! \033[0m"
}
# 12: 隐藏Linux版本信息显示
function RM_Versioninfo(){
if [ -f /etc/issue ];then
>/etc/issue
fi
if [ -f /etc/issue.net ];then
>/etc/issue.net
fi
echo -e "\033[31m Version information delete Ok! \033[0m"
}
# 13: hosts config
function Hosts(){
cat >>/etc/hosts <<EOF
`hostname -I` `hostname`
EOF
}
function done_ok(){
cat << EOF
+------------------------------------------------------------------------+
optimizer is done
Congratulations on your system optimization complete
System reboot ount down 10s
+------------------------------------------------------------------------+
EOF
}
function REBOOT(){
for i in `seq -w 9 -1 0`
do
echo -ne "\b__$i";
sleep 1;
done
echo -e "\b system reboot..."
sleep 1
reboot &>/dev/nill
}
function main_6(){
Count_down
CentOS6_repo
Install_Soft
SELinux
CentOS6_init
C6_start_item
C6_ssh
C6_UTF
NTPdate
Ulimit
Sysctl
Rm_mail
RM_Versioninfo
Hosts
done_ok
REBOOT
}
function main_7(){
Count_down
CentOS7_repo
Install_Soft
SELinux
C7_start_item
C7_ssh
C7_UTF
NTPdate
Ulimit
Sysctl
Rm_mail
RM_Versioninfo
Hosts
done_ok
REBOOT
}
function main(){
Inspect
Choice_6_7
}
main
exit 0