1. #!/bin/sh
    2. . /etc/rc.d/init.d/functions
    3. export LANG=zh_CN.UTF-8
    4. #一级菜单
    5. menu1()
    6. {
    7. clear
    8. cat <<EOF
    9. ----------------------------------------
    10. |**** 欢迎使用cetnos7.9优化脚本 ****|
    11. |**** ****|
    12. ----------------------------------------
    13. 1. 一键优化
    14. 2. 自定义优化
    15. 3. 退出
    16. EOF
    17. read -p "please enter your choice[1-3]:" num1
    18. }
    19. #二级菜单
    20. menu2()
    21. {
    22. clear
    23. cat <<EOF
    24. ----------------------------------------
    25. |****Please Enter Your Choice:[0-13]****|
    26. ----------------------------------------
    27. 1. 修改字符集
    28. 2. 关闭selinux
    29. 3. 关闭firewalld
    30. 4. 精简开机启动
    31. 5. 修改文件描述符
    32. 6. 安装常用工具及修改yum
    33. 7. 优化系统内核
    34. 8. 加快ssh登录速度
    35. 9. 禁用ctrl+alt+del重启
    36. 10.设置时间同步
    37. 11.history优化
    38. 12.返回上级菜单
    39. 13.退出
    40. EOF
    41. read -p "please enter your choice[1-13]:" num2
    42. }
    43. #1.修改字符集
    44. localeset()
    45. {
    46. echo "========================修改字符集========================="
    47. cat > /etc/locale.conf <<EOF
    48. LANG="zh_CN.UTF-8"
    49. #LANG="en_US.UTF-8"
    50. SYSFONT="latarcyrheb-sun16"
    51. EOF
    52. source /etc/locale.conf
    53. echo "#cat /etc/locale.conf"
    54. cat /etc/locale.conf
    55. action "完成修改字符集" /bin/true
    56. echo "==========================================================="
    57. sleep 2
    58. }
    59. #2.关闭selinux
    60. selinuxset()
    61. {
    62. selinux_status=`grep "SELINUX=disabled" /etc/sysconfig/selinux | wc -l`
    63. echo "========================禁用SELINUX========================"
    64. if [ $selinux_status -eq 0 ];then
    65. sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/sysconfig/selinux
    66. setenforce 0
    67. echo '#grep SELINUX=disabled /etc/sysconfig/selinux'
    68. grep SELINUX=disabled /etc/sysconfig/selinux
    69. echo '#getenforce'
    70. getenforce
    71. else
    72. echo 'SELINUX已处于关闭状态'
    73. echo '#grep SELINUX=disabled /etc/sysconfig/selinux'
    74. grep SELINUX=disabled /etc/sysconfig/selinux
    75. echo '#getenforce'
    76. getenforce
    77. fi
    78. action "完成禁用SELINUX" /bin/true
    79. echo "==========================================================="
    80. sleep 2
    81. }
    82. #3.关闭firewalld
    83. firewalldset()
    84. {
    85. echo "=======================禁用firewalld========================"
    86. systemctl stop firewalld.service &> /dev/null
    87. echo '#firewall-cmd --state'
    88. firewall-cmd --state
    89. systemctl disable firewalld.service &> /dev/null
    90. echo '#systemctl list-unit-files | grep firewalld'
    91. systemctl list-unit-files | grep firewalld
    92. action "完成禁用firewalld,生产环境下建议启用!" /bin/true
    93. echo "==========================================================="
    94. sleep 5
    95. }
    96. #4.精简开机启动
    97. chkset()
    98. {
    99. echo "=======================精简开机启动========================"
    100. systemctl disable auditd.service
    101. systemctl disable postfix.service
    102. systemctl disable dbus-org.freedesktop.NetworkManager.service
    103. echo '#systemctl list-unit-files | grep -E "auditd|postfix|dbus-org\.freedesktop\.NetworkManager"'
    104. systemctl list-unit-files | grep -E "auditd|postfix|dbus-org\.freedesktop\.NetworkManager"
    105. action "完成精简开机启动" /bin/true
    106. echo "==========================================================="
    107. sleep 2
    108. }
    109. #5.修改文件描述符
    110. limitset()
    111. {
    112. echo "======================修改文件描述符======================="
    113. echo '* - nofile 65535'>/etc/security/limits.conf
    114. ulimit -SHn 65535
    115. echo "#cat /etc/security/limits.conf"
    116. cat /etc/security/limits.conf
    117. echo "#ulimit -Sn ; ulimit -Hn"
    118. ulimit -Sn ; ulimit -Hn
    119. action "完成修改文件描述符" /bin/true
    120. echo "==========================================================="
    121. sleep 2
    122. }
    123. #6.安装常用工具及修改yum源
    124. yumset()
    125. {
    126. echo "=================安装常用工具及修改yum源==================="
    127. yum install wget -y &> /dev/null
    128. if [ $? -eq 0 ];then
    129. cd /etc/yum.repos.d/
    130. \cp CentOS-Base.repo CentOS-Base.repo.$(date +%F)
    131. ping -c 1 mirrors.aliyun.com &> /dev/null
    132. if [ $? -eq 0 ];then
    133. wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
    134. yum clean all &> /dev/null
    135. yum makecache &> /dev/null
    136. else
    137. echo "无法连接网络"
    138. exit $?
    139. fi
    140. else
    141. echo "wget安装失败"
    142. exit $?
    143. fi
    144. yum -y install ntpdate lsof net-tools telnet vim lrzsz tree nmap nc sysstat &> /dev/null
    145. action "完成安装常用工具及修改yum源" /bin/true
    146. echo "==========================================================="
    147. sleep 2
    148. }
    149. #7. 优化系统内核
    150. kernelset()
    151. {
    152. echo "======================优化系统内核========================="
    153. chk_nf=`cat /etc/sysctl.conf | grep conntrack |wc -l`
    154. if [ $chk_nf -eq 0 ];then
    155. cat >>/etc/sysctl.conf<<EOF
    156. net.ipv4.tcp_fin_timeout = 2
    157. net.ipv4.tcp_tw_reuse = 1
    158. net.ipv4.tcp_tw_recycle = 1
    159. net.ipv4.tcp_syncookies = 1
    160. net.ipv4.tcp_keepalive_time = 600
    161. net.ipv4.ip_local_port_range = 4000 65000
    162. net.ipv4.tcp_max_syn_backlog = 16384
    163. net.ipv4.tcp_max_tw_buckets = 36000
    164. net.ipv4.route.gc_timeout = 100
    165. net.ipv4.tcp_syn_retries = 1
    166. net.ipv4.tcp_synack_retries = 0
    167. net.core.somaxconn = 16384
    168. net.core.netdev_max_backlog = 16384
    169. net.ipv4.tcp_max_orphans = 16384
    170. net.netfilter.nf_conntrack_max = 25000000
    171. net.netfilter.nf_conntrack_tcp_timeout_established = 180
    172. net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
    173. net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
    174. net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
    175. EOF
    176. sysctl -p
    177. else
    178. echo "优化项已存在。"
    179. fi
    180. action "内核调优完成" /bin/true
    181. echo "==========================================================="
    182. sleep 2
    183. }
    184. #8.加快ssh登录速度
    185. sshset()
    186. {
    187. echo "======================加快ssh登录速度======================"
    188. sed -i 's#^GSSAPIAuthentication yes$#GSSAPIAuthentication no#g' /etc/ssh/sshd_config
    189. sed -i 's/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
    190. systemctl restart sshd.service
    191. echo "#grep GSSAPIAuthentication /etc/ssh/sshd_config"
    192. grep GSSAPIAuthentication /etc/ssh/sshd_config
    193. echo "#grep UseDNS /etc/ssh/sshd_config"
    194. grep UseDNS /etc/ssh/sshd_config
    195. action "完成加快ssh登录速度" /bin/true
    196. echo "==========================================================="
    197. sleep 2
    198. }
    199. #9. 禁用ctrl+alt+del重启
    200. restartset()
    201. {
    202. echo "===================禁用ctrl+alt+del重启===================="
    203. rm -rf /usr/lib/systemd/system/ctrl-alt-del.target
    204. action "完成禁用ctrl+alt+del重启" /bin/true
    205. echo "==========================================================="
    206. sleep 2
    207. }
    208. #10. 设置时间同步
    209. ntpdateset()
    210. {
    211. echo "=======================设置时间同步========================"
    212. yum -y install ntpdate &> /dev/null
    213. if [ $? -eq 0 ];then
    214. /usr/sbin/ntpdate time.windows.com
    215. echo "*/5 * * * * /usr/sbin/ntpdate ntp.aliyun.com &>/dev/null" >> /var/spool/cron/root
    216. else
    217. echo "ntpdate安装失败"
    218. exit $?
    219. fi
    220. action "完成设置时间同步" /bin/true
    221. echo "==========================================================="
    222. sleep 2
    223. }
    224. #11. history优化
    225. historyset()
    226. {
    227. echo "========================history优化========================"
    228. chk_his=`cat /etc/profile | grep HISTTIMEFORMAT |wc -l`
    229. if [ $chk_his -eq 0 ];then
    230. cat >> /etc/profile <<'EOF'
    231. #设置history格式
    232. export HISTTIMEFORMAT="[%Y-%m-%d %H:%M:%S] [`whoami`] [`who am i|awk '{print $NF}'|sed -r 's#[()]##g'`]: "
    233. #记录shell执行的每一条命令
    234. export PROMPT_COMMAND='\
    235. if [ -z "$OLD_PWD" ];then
    236. export OLD_PWD=$PWD;
    237. fi;
    238. if [ ! -z "$LAST_CMD" ] && [ "$(history 1)" != "$LAST_CMD" ]; then
    239. logger -t `whoami`_shell_dir "[$OLD_PWD]$(history 1)";
    240. fi;
    241. export LAST_CMD="$(history 1)";
    242. export OLD_PWD=$PWD;'
    243. EOF
    244. source /etc/profile
    245. else
    246. echo "优化项已存在。"
    247. fi
    248. action "完成history优化" /bin/true
    249. echo "==========================================================="
    250. sleep 2
    251. }
    252. #控制函数
    253. main()
    254. {
    255. menu1
    256. case $num1 in
    257. 1)
    258. localeset
    259. selinuxset
    260. firewalldset
    261. chkset
    262. limitset
    263. yumset
    264. kernelset
    265. sshset
    266. restartset
    267. ntpdateset
    268. historyset
    269. ;;
    270. 2)
    271. menu2
    272. case $num2 in
    273. 1)
    274. localeset
    275. ;;
    276. 2)
    277. selinuxset
    278. ;;
    279. 3)
    280. firewalldset
    281. ;;
    282. 4)
    283. chkset
    284. ;;
    285. 5)
    286. limitset
    287. ;;
    288. 6)
    289. yumset
    290. ;;
    291. 7)
    292. kernelset
    293. ;;
    294. 8)
    295. sshset
    296. ;;
    297. 9)
    298. restartset
    299. ;;
    300. 10)
    301. ntpdateset
    302. ;;
    303. 11)
    304. historyset
    305. ;;
    306. 12)
    307. main
    308. ;;
    309. 13)
    310. exit
    311. ;;
    312. *)
    313. echo 'Please select a number from [1-13].'
    314. ;;
    315. esac
    316. ;;
    317. 3)
    318. exit
    319. ;;
    320. *)
    321. echo 'Err:Please select a number from [1-3].'
    322. sleep 3
    323. main
    324. ;;
    325. esac
    326. }
    327. main $*