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/enforcing/disabled/}' /etc/selinux/config
    9. #安装命令补全包
    10. yum install -y epel-release &>/dev/null 2>&1
    11. yum install -y bash-completion &>/dev/null 2>&1
    12. #清空firewall防火墙原来所有的规则,只保留ssh
    13. cd /etc/firewalld/zones/
    14. echo "" > public.xml
    15. cat >> public.xml << EOF
    16. Public
    17. EOF
    18. firewall-cmd --reload >/dev/null 2>&1
    19. #历史命令显示操作时间
    20. if ! grep HISTTIMEFORMAT /etc/bashrc; then
    21. echo 'export HISTTIMEFORMAT="%F %T whoami "' >> /etc/bashrc
    22. fi
    23. #SSH超时时间
    24. if ! grep "TMOUT=600" /etc/profile &>/dev/null; then
    25. echo "export TMOUT=600" >> /etc/profile
    26. fi
    27. #添加一个同root一样的超级用户,并禁止root远程登录
    28. NEW_USER=admin
    29. useradd $NEW_USER
    30. echo 123456 |passwd --stdin $NEW_USER
    31. #NEW_USER_ID=`cat /etc/passwd | grep $NEW_USER | awk -F ":" '{print $3}'`
    32. #CHANGE=\$s/$NEW_USER_ID/0/
    33. #sed -i "$CHANGE" /etc/passwd
    34. sed -i "92a $NEW_USER ALL=(ALL) NOPASSWD:ALL" /etc/sudoers
    35. sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
    36. systemctl restart sshd
    37. #禁止定时任务向发送邮件
    38. sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab
    39. #设置最大打开文件数
    40. if ! grep "* soft nofile 65535" /etc/security/limits.conf &>/dev/null; then
    41. cat >> /etc/security/limits.conf << EOF
    42. * soft nofile 65535
    43. * hard nofile 65535
    44. EOF
    45. fi
    46. #系统内核优化
    47. cat >> /etc/sysctl.conf << EOF
    48. net.ipv4.tcp_syncookies = 1
    49. net.ipv4.tcp_max_tw_buckets = 20480
    50. net.ipv4.tcp_max_syn_backlog = 20480
    51. net.core.netdev_max_backlog = 262144
    52. net.ipv4.tcp_fin_timeout = 20
    53. EOF
    54. #减少SWAP使用
    55. echo "0" > /proc/sys/vm/swappiness
    56. #安装系统性能分析工具及其他
    57. yum -y install gcc make autoconf vim sysstat net-tools iostat iftop iotp lrzsz &>/dev/null 2>&1