1. #!/bin/bash
    2. # init centos7 ./centos7-init.sh 主机名
    3. # 检查是否为root用户,脚本必须在root权限下运行
    4. if [[ "$(whoami)" != "root" ]]; then
    5. echo "please run this script as root !" >&2
    6. exit 1
    7. fi
    8. echo -e "\033[31m the script only Support CentOS_7 x86_64 \033[0m"
    9. echo -e "\033[31m system initialization script, Please Seriously. press ctrl+C to cancel \033[0m"
    10. # 检查是否为64位系统,这个脚本只支持64位脚本
    11. platform=`uname -i`
    12. if [ $platform != "x86_64" ];then
    13. echo "this script is only for 64bit Operating System !"
    14. exit 1
    15. fi
    16. if [ "$1" == "" ];then
    17. echo "The host name is empty."
    18. exit 1
    19. else
    20. hostnamectl --static set-hostname $1
    21. hostnamectl set-hostname $1
    22. fi
    23. cat << EOF
    24. +---------------------------------------+
    25. | your system is CentOS 7 x86_64 |
    26. | start optimizing |
    27. +---------------------------------------+
    28. EOF
    29. sleep 1
    30. # 安装必要支持工具及软件工具
    31. yum_update(){
    32. yum update -y
    33. yum install -y nmap unzip wget vim lsof xz net-tools iptables-services ntpdate ntp-doc psmisc
    34. }
    35. # 设置时间同步 set time
    36. zone_time(){
    37. timedatectl set-timezone Asia/Shanghai
    38. /usr/sbin/ntpdate 0.cn.pool.ntp.org > /dev/null 2>&1
    39. /usr/sbin/hwclock --systohc
    40. /usr/sbin/hwclock -w
    41. cat > /var/spool/cron/root << EOF
    42. 10 0 * * * /usr/sbin/ntpdate 0.cn.pool.ntp.org > /dev/null 2>&1
    43. * * * * */1 /usr/sbin/hwclock -w > /dev/null 2>&1
    44. EOF
    45. chmod 600 /var/spool/cron/root
    46. /sbin/service crond restart
    47. sleep 1
    48. }
    49. # 修改文件打开数 set the file limit
    50. limits_config(){
    51. cat > /etc/rc.d/rc.local << EOF
    52. #!/bin/bash
    53. touch /var/lock/subsys/local
    54. ulimit -SHn 1024000
    55. EOF
    56. sed -i "/^ulimit -SHn.*/d" /etc/rc.d/rc.local
    57. echo "ulimit -SHn 1024000" >> /etc/rc.d/rc.local
    58. sed -i "/^ulimit -s.*/d" /etc/profile
    59. sed -i "/^ulimit -c.*/d" /etc/profile
    60. sed -i "/^ulimit -SHn.*/d" /etc/profile
    61. cat >> /etc/profile << EOF
    62. ulimit -c unlimited
    63. ulimit -s unlimited
    64. ulimit -SHn 1024000
    65. EOF
    66. source /etc/profile
    67. ulimit -a
    68. cat /etc/profile | grep ulimit
    69. if [ ! -f "/etc/security/limits.conf.bak" ]; then
    70. cp /etc/security/limits.conf /etc/security/limits.conf.bak
    71. fi
    72. cat > /etc/security/limits.conf << EOF
    73. * soft nofile 1024000
    74. * hard nofile 1024000
    75. * soft nproc 1024000
    76. * hard nproc 1024000
    77. hive - nofile 1024000
    78. hive - nproc 1024000
    79. EOF
    80. if [ ! -f "/etc/security/limits.d/20-nproc.conf.bak" ]; then
    81. cp /etc/security/limits.d/20-nproc.conf /etc/security/limits.d/20-nproc.conf.bak
    82. fi
    83. cat > /etc/security/limits.d/20-nproc.conf << EOF
    84. * soft nproc 409600
    85. root soft nproc unlimited
    86. EOF
    87. sleep 1
    88. }
    89. # 优化内核参数 tune kernel parametres
    90. sysctl_config(){
    91. if [ ! -f "/etc/sysctl.conf.bak" ]; then
    92. cp /etc/sysctl.conf /etc/sysctl.conf.bak
    93. fi
    94. #add
    95. cat > /etc/sysctl.conf << EOF
    96. net.ipv6.conf.all.disable_ipv6 = 1
    97. net.ipv6.conf.default.disable_ipv6 = 1
    98. net.ipv4.tcp_syn_retries = 1
    99. net.ipv4.tcp_synack_retries = 1
    100. net.ipv4.tcp_keepalive_time = 600
    101. net.ipv4.tcp_keepalive_probes = 3
    102. net.ipv4.tcp_keepalive_intvl =15
    103. net.ipv4.tcp_retries1 = 3
    104. net.ipv4.tcp_retries2 = 5
    105. net.ipv4.tcp_fin_timeout = 10
    106. net.ipv4.tcp_tw_recycle = 1
    107. net.ipv4.tcp_tw_reuse = 1
    108. net.ipv4.tcp_syncookies = 1
    109. net.ipv4.tcp_window_scaling = 1
    110. net.ipv4.tcp_max_tw_buckets = 60000
    111. net.ipv4.tcp_max_orphans = 32768
    112. net.ipv4.tcp_max_syn_backlog = 16384
    113. net.ipv4.tcp_mem = 94500000 915000000 927000000
    114. net.ipv4.tcp_wmem = 4096 16384 13107200
    115. net.ipv4.tcp_rmem = 4096 87380 17476000
    116. net.ipv4.ip_local_port_range = 1024 65000
    117. net.ipv4.ip_forward = 1
    118. net.ipv4.route.gc_timeout = 100
    119. net.core.somaxconn = 32768
    120. net.core.netdev_max_backlog = 32768
    121. net.nf_conntrack_max = 6553500
    122. net.netfilter.nf_conntrack_max = 6553500
    123. net.netfilter.nf_conntrack_tcp_timeout_established = 180
    124. vm.overcommit_memory = 1
    125. vm.swappiness = 1
    126. fs.file-max = 1024000
    127. EOF
    128. #reload sysctl
    129. /sbin/sysctl -p
    130. sleep 1
    131. }
    132. # 设置UTF-8 LANG="zh_CN.UTF-8"
    133. LANG_config(){
    134. echo "LANG=\"en_US.UTF-8\"">/etc/locale.conf
    135. source /etc/locale.conf
    136. }
    137. #关闭SELINUX disable selinux
    138. selinux_config(){
    139. sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    140. setenforce 0
    141. sleep 1
    142. }
    143. #日志处理
    144. log_config(){
    145. setenforce 0
    146. systemctl start systemd-journald
    147. systemctl status systemd-journald
    148. }
    149. # 关闭防火墙
    150. firewalld_config(){
    151. /usr/bin/systemctl stop firewalld.service
    152. /usr/bin/systemctl disable firewalld.service
    153. }
    154. # SSH配置优化 set sshd_config
    155. sshd_config(){
    156. if [ ! -f "/etc/ssh/sshd_config.bak" ]; then
    157. cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
    158. fi
    159. cat >/etc/ssh/sshd_config<<EOF
    160. Port 22
    161. AddressFamily inet
    162. ListenAddress 0.0.0.0
    163. Protocol 2
    164. HostKey /etc/ssh/ssh_host_rsa_key
    165. HostKey /etc/ssh/ssh_host_ecdsa_key
    166. HostKey /etc/ssh/ssh_host_ed25519_key
    167. SyslogFacility AUTHPRIV
    168. PermitRootLogin yes
    169. MaxAuthTries 6
    170. RSAAuthentication yes
    171. PubkeyAuthentication yes
    172. AuthorizedKeysFile .ssh/authorized_keys
    173. PasswordAuthentication yes
    174. ChallengeResponseAuthentication no
    175. UsePAM yes
    176. UseDNS no
    177. X11Forwarding yes
    178. UsePrivilegeSeparation sandbox
    179. AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
    180. AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
    181. AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
    182. AcceptEnv XMODIFIERS
    183. Subsystem sftp /usr/libexec/openssh/sftp-server
    184. EOF
    185. /sbin/service sshd restart
    186. }
    187. # 关闭ipv6 disable the ipv6
    188. ipv6_config(){
    189. echo "NETWORKING_IPV6=no">/etc/sysconfig/network
    190. echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6
    191. echo 1 > /proc/sys/net/ipv6/conf/default/disable_ipv6
    192. echo "127.0.0.1 localhost localhost.localdomain">/etc/hosts
    193. #sed -i 's/IPV6INIT=yes/IPV6INIT=no/g' /etc/sysconfig/network-scripts/ifcfg-enp0s8
    194. for line in $(ls -lh /etc/sysconfig/network-scripts/ifcfg-* | awk -F '[ ]+' '{print $9}')
    195. do
    196. if [ -f $line ]
    197. then
    198. sed -i 's/IPV6INIT=yes/IPV6INIT=no/g' $line
    199. echo $i
    200. fi
    201. done
    202. }
    203. # 设置历史命令记录格式 history
    204. history_config(){
    205. export HISTFILESIZE=10000000
    206. export HISTSIZE=1000000
    207. export PROMPT_COMMAND="history -a"
    208. export HISTTIMEFORMAT="%Y-%m-%d_%H:%M:%S "
    209. ##export HISTTIMEFORMAT="{\"TIME\":\"%F %T\",\"HOSTNAME\":\"\$HOSTNAME\",\"LI\":\"\$(who -u am i 2>/dev/null| awk '{print \$NF}'|sed -e 's/[()]//g')\",\"LU\":\"\$(who am i|awk '{print \$1}')\",\"NU\":\"\${USER}\",\"CMD\":\""
    210. cat >>/etc/bashrc<<EOF
    211. alias vi='vim'
    212. HISTDIR='/var/log/command.log'
    213. if [ ! -f \$HISTDIR ];then
    214. touch \$HISTDIR
    215. chmod 666 \$HISTDIR
    216. fi
    217. export HISTTIMEFORMAT="{\"TIME\":\"%F %T\",\"IP\":\"\$(ip a | grep -E '192.168|172' | head -1 | awk '{print \$2}' | cut -d/ -f1)\",\"LI\":\"\$(who -u am i 2>/dev/null| awk '{print \$NF}'|sed -e 's/[()]//g')\",\"LU\":\"\$(who am i|awk '{print \$1}')\",\"NU\":\"\${USER}\",\"CMD\":\""
    218. export PROMPT_COMMAND='history 1|tail -1|sed "s/^[ ]\+[0-9]\+ //"|sed "s/$/\"}/">> /var/log/command.log'
    219. EOF
    220. source /etc/bashrc
    221. }
    222. # 服务优化设置
    223. service_config(){
    224. /usr/bin/systemctl enable NetworkManager-wait-online.service
    225. /usr/bin/systemctl start NetworkManager-wait-online.service
    226. /usr/bin/systemctl stop postfix.service
    227. /usr/bin/systemctl disable postfix.service
    228. chmod +x /etc/rc.local
    229. chmod +x /etc/rc.d/rc.local
    230. #ls -l /etc/rc.d/rc.local
    231. }
    232. # VIM设置
    233. vim_config(){
    234. cat > /root/.vimrc << EOF
    235. set history=1000
    236. EOF
    237. #autocmd InsertLeave * se cul
    238. #autocmd InsertLeave * se nocul
    239. #set nu
    240. #set bs=2
    241. #syntax on
    242. #set laststatus=2
    243. #set tabstop=4
    244. #set go=
    245. #set ruler
    246. #set showcmd
    247. #set cmdheight=1
    248. #hi CursorLine cterm=NONE ctermbg=blue ctermfg=white guibg=blue guifg=white
    249. #set hls
    250. #set cursorline
    251. #set ignorecase
    252. #set hlsearch
    253. #set incsearch
    254. #set helplang=cn
    255. }
    256. # done
    257. done_ok(){
    258. touch /var/log/init-ok
    259. cat << EOF
    260. +-------------------------------------------------+
    261. | optimizer is done |
    262. | it's recommond to restart this server ! |
    263. | Please Reboot system |
    264. +-------------------------------------------------+
    265. EOF
    266. }
    267. # main
    268. main(){
    269. yum_update
    270. zone_time
    271. limits_config
    272. sysctl_config
    273. LANG_config
    274. selinux_config
    275. log_config
    276. firewalld_config
    277. sshd_config
    278. ipv6_config
    279. history_config
    280. service_config
    281. vim_config
    282. done_ok
    283. }
    284. main