1. #/bin/bash
    2. # 设置时区并同步时间
    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. # 禁用selinux
    8. sed -i '/SELINUX/{s/permissive/disabled/}' /etc/selinux/config
    9. # 关闭防火墙
    10. if egrep "7.[0-9]" /etc/redhat-release &>/dev/null; then
    11. systemctl stop firewalld
    12. systemctl disable firewalld
    13. elif egrep "6.[0-9]" /etc/redhat-release &>/dev/null; then
    14. service iptables stop
    15. chkconfig iptables off
    16. fi
    17. # 历史命令显示操作时间
    18. if ! grep HISTTIMEFORMAT /etc/bashrc; then
    19. echo 'export HISTTIMEFORMAT="%F %T `whoami` "' >> /etc/bashrc
    20. fi
    21. # SSH超时时间
    22. if ! grep "TMOUT=600" /etc/profile &>/dev/null; then
    23. echo "export TMOUT=600" >> /etc/profile
    24. fi
    25. # 禁止root远程登录
    26. sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
    27. # 禁止定时任务向发送邮件
    28. sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab
    29. # 设置最大打开文件数
    30. if ! grep "* soft nofile 65535" /etc/security/limits.conf &>/dev/null; then
    31. cat >> /etc/security/limits.conf << EOF
    32. * soft nofile 65535
    33. * hard nofile 65535
    34. EOF
    35. fi
    36. # 系统内核优化
    37. cat >> /etc/sysctl.conf << EOF
    38. net.ipv4.tcp_syncookies = 1
    39. net.ipv4.tcp_max_tw_buckets = 20480
    40. net.ipv4.tcp_max_syn_backlog = 20480
    41. net.core.netdev_max_backlog = 262144
    42. net.ipv4.tcp_fin_timeout = 20
    43. EOF
    44. # 减少SWAP使用
    45. echo "0" > /proc/sys/vm/swappiness
    46. # 安装系统性能分析工具及其他
    47. yum install gcc make autoconf vim sysstat net-tools iostat if