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
#!/bin/sh
 
###open iptables service, allow this ports access 80, 3307, 21####
 
function  firewall() {
         service iptables start
 
         for  Port  in  21 80 3307
            do 
                 iptables -I INPUT 5  -m state --state NEW -m tcp -p tcp --dport $Port -j ACCEPT
           done
 
         /etc/init .d /iptables  save
}
 
###disable selinux service###
 
function  safety() {
         /usr/sbin/setenforce  0
         sed  -i  's/^SELINUX=enforcing/SELINUX=disabled/'  /etc/sysconfig/selinux
}
 
 
###edit os runlevel, 3 - Full multiuser mode###
 
function  runlevel() {
         sed  -i  's/^id:[0-9]:initdefault:/id:3:initdefault:/'  /etc/inittab
}
 
 
###thin systrv, initation system open this service: crond, iptables, network, sshd, rsyslog####
 
function  systrv() {
         Srv_List=`chkconfig --list| grep  3:on|  awk  '{print $1}' `
         for  in  $Srv_List
           do 
                 chkconfig --level 3 $i off
         done
 
         for  in  crond iptables network sshd rsyslog
           do
                 chkconfig --level 3 $j on
         done
}
 
###add common user zkyw as operation account###
 
function  adduser() {
         /usr/sbin/useradd  zkyw
         echo  "zkyw@123"  passwd  zkyw --stdin
}
 
 
###Optimization ssh service, alter default port 22, disable root login######
 
function  myssh() {
         sed  -i  's/^#Port 22/Port 16182/'  /etc/ssh/sshd_config   #alter  ssh  default port 16182
         sed  -i  's/^PermitRootLogin yes/PermitRootLogin no/'  /etc/ssh/sshd_config
         sed  -i  's/^#PermitEmptyPasswords no/PermitEmptyPasswords no/'  /etc/ssh/sshd_config
         sed  -i  's/^#MaxAuthTries 6/MaxAuthTries 3/'  /etc/ssh/sshd_config
         sed  -i  '$aAllowUsers zkyw'  /etc/ssh/sshd_config   #allow common user zkyw ssh login
         /etc/init .d /sshd  reload
}
 
 
###clock Synchronous with internet time###
 
function  ntpclock() {
         /usr/sbin/ntpdate  202.120.2.101
         echo  "30 22 * * * /usr/sbin/ntpdate 202.120.2.101"  >>  /var/spool/cron/root
         /etc/init .d /crond  reload
}
 
###lock the key files including: passwd、group、shadow、gshadow、inittab#####
 
function  lockfile() {
         for  file  in  passwd  group shadow gshadow inittab
           do
                 chattr +i  /etc/ $ file
         done
}
 
###alter max nofile and max user processes####
 
function  userlimit() {
         sed  -i  '$a*  soft    nofile  65536\n*  hard    nofile  65536'  /etc/security/limits .conf
         sed  -i  's/^/#/'  /etc/security/limits .d /90-nproc .conf
         sed  -i  '$a*     soft    nproc   51200\nroot     soft    nproc   unlimited'  /etc/security/limits .d /90-nproc .conf
}
 
###optimization system kernel parameters, including tcp/ip protocal, iptables and so on####
 
function  syskernel() {
         cp  /etc/sysctl .conf  /etc/sysctl .conf.eri
         modprobe bridge
         (
         cat  << EOF
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
 
net.nf_conntrack_max = 25000000
net.netfilter.nf_conntrack_max = 25000000
net.netfilter.nf_conntrack_tcp_timeout_established = 180
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
EOF
         ) >>  /etc/sysctl .conf
         /sbin/sysctl  -p  > /dev/null  2>&1
}
 
###delete some of no great importance users and groups####
 
function  cleanusers() {
         for  user  in  adm lp  sync  shutdown  halt uucp operator games gopher  ftp
           do
                 /usr/sbin/userdel  $user
         done
 
         for  gp  in  adm lp dip
           do
                 /usr/sbin/groupdel  $gp
         done
}
 
echo  "Iptables Optimization Starting..."
firewall
echo  "Selinux Disabled Starting..."
safety
echo  "Runlevel Optimization Starting..."
runlevel
echo  "System Init Service Optimization Starting..."
systrv
echo  "Add zkyw Common Account Starting..."
adduser
echo  "SSH Service Optimization Starting..."
myssh
echo  "Clock Synchronous Optimization Starting..."
ntpclock
echo  "Max nofile and user processes Optimization Starting..."
userlimit
echo  "System Kernel Parameters Optimization Starting..."
syskernel