背景

公司新买了十台服务器并且已安装了Linux操作系统

需求

1.设置时区并同步时间

2.禁用selinux

3.清空防火墙默认策略

4.历史命令显示时间

5.禁止 root远程登录

6.禁止定时任务发送邮件

7.设置最大打开文件数

8.减少Swap使用

9.系统内核参数优化

10.安装系统性能分析工具及其他常用工具

  1. #设置时间同步
  2. #!/bin/bash
  3. ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  4. if ! crontab -l|grep ntpdate &> /dev/null;then
  5. (echo "* 1 * * * ntpdate time.windows.com > /dev/null 2>&1";crontab -l)|crontab
  6. fi
  7. #设置软链接
  8. #如果 没有 设置时间同步定时命令 (将输入的内容移除)
  9. # 添加时间同步命令
  10. #关闭selinux
  11. sed -i '/SELINUX/{s/enforcing/disabled/}' /etc/selinux/config
  12. #直接编辑文件 将SELINUX这一行中的enforcing 替换为 disabled
  13. #关闭防火墙
  14. if egrep "7.[0-9]" /etc/redhat-release &> /dev/null;then
  15. systemctl stop firewalld
  16. systemctl disable firewalld
  17. elif egrep "7.[0-9]" /etc/redhat-release &> /dev/null;then
  18. service iptables stop
  19. chkconfig iptables off
  20. fi
  21. #如果是 7. 版本
  22. # 执行7版本的关闭防火墙
  23. #如果是 6. 版本
  24. # 执行6版本的防火墙
  25. #历史命令显示操作时间
  26. if ! grep HISTTIMEFORMAT /etc/bashrc;then
  27. echo 'export HISTTIMEFORMAT="%F%T `whoami`"' >> /etc/bashrc
  28. fi
  29. #/etc/bashrc为每一个运行bash shell的用户执行此文件,当bash shell被打开时,该文件被读取。
  30. #如果 /etc/bashrc中没有HISTTIMEFORMAT
  31. # 追加变量到该文件中,%F年月日,%T时分秒 和 执行查看当前用户命令
  32. #SSH超时时间
  33. if ! grep "TMOUT=600" /etc/profile &> /dev/null;then
  34. echo "export TMOUT=600" >> /etc/profile
  35. fi
  36. #如果配置文件中没有TMOUT变量
  37. # 追加TMOUT全局变量到环境变量配置文件中
  38. #禁止root远程登录
  39. sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
  40. #修改ssh配置文件,将注释的yes改为no
  41. #禁止定时任务向发送邮件
  42. sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab
  43. #将定时文件中的指定字符修改
  44. #设置最大打开文件数
  45. if ! grep "* soft nofile 65535" /etc/security/limits.conf &> /dev/null;then
  46. cat >> /etc/security/limits.conf << EOF
  47. * soft nofile 65535
  48. * hard nofile 65535
  49. EOF
  50. fi
  51. #如果 文件中没有指定字符
  52. # 在文件末尾添加以下内容
  53. #系统内核优化
  54. cat >> /etc/sysctl.conf << EOF
  55. net.ipv4.tcp_syncookies = 1
  56. net.ipv4.tcp_max_tw_buckets = 20480
  57. net.ipv4.tcp_max_syn_backlog= 20480
  58. net.core.netdev_max_backlog =262144
  59. net.ipv4.tcp_fin_timeout= 20
  60. EOF
  61. #直接添加优化参数到系统内核配置文件中
  62. #减少SWAP的使用
  63. echo "0" > /proc/sys/vm/swappiness
  64. #安装系统性能分析工具以及其他
  65. yum install -y gcc make autoconf vim systat net-tools htop tree mlocate ntpdate