1. #!/bin/bash
    2. ##########################################################
    3. ## ##
    4. ## Script Name : security for Redhat7.0~7.6 ##
    5. ## By:Eren 2019-08-02 ##
    6. ## ##
    7. ##########################################################
    8. #创建系统运维用户chroot
    9. user=chroot
    10. if ! id $user;then
    11. groupadd -g 700 $user
    12. useradd -g 700 -u 700 -m -G $user $user
    13. echo "chroot:1qaz@WSX" |chpasswd
    14. echo "user $user is created"
    15. fi
    16. #设置系统最小化开机启动项目
    17. systemctl stop firewalld.service
    18. for serv in ` systemctl list-unit-files | grep enabled|awk '{print $1}'`;do systemctl disable $serv ;done
    19. for serv in autovt@.service crond.service getty@.service irqbalance.service kdump.service auditd.service microcode.service rsyslog.service sshd.service sysstat.service systemd-readahead-collect.service systemd-readahead-drop.service systemd-readahead-replay.service NetworkManager.service tuned.service lvm2-lvmetad.socket lvm2-lvmpolld.socket default.target multi-user.target runlevel2.target runlevel3.target runlevel4.target ;do systemctl enable $serv;done
    20. #设置单用户启动
    21. systemctl set-default multi-user.target
    22. #删除机器系统重启快捷键
    23. rm -f /usr/lib/systemd/system/ctrl-alt-del.target
    24. #设置NTP服务器
    25. cp /etc/chrony.conf /etc/chrony.conf.bak
    26. sed -i 's/^server/#server/g' /etc/chrony.conf
    27. sed -i '/#server 3/a\server 10.182.200.100' /etc/chrony.conf
    28. echo "leapsecmode slew" >>/etc/chrony.conf
    29. systemctl restart chronyd
    30. systemctl enable chronyd
    31. #设置文件系统打开文件数
    32. ##limits
    33. cat>>/etc/security/limits.conf<<EOF
    34. * soft nproc 20480
    35. * hard nproc 20480
    36. * hard nofile 20480
    37. * soft nofile 20480
    38. EOF
    39. sed -i "s/* soft nproc 4096/#* soft nproc 4096/g" /etc/security/limits.d/20-nproc.conf
    40. #设置系统目录权限
    41. chmod 400 /etc/crontab
    42. chmod 400 /etc/securetty
    43. #chmod 600 /boot/grub/grub.conf
    44. chmod 644 /etc/hosts.allow
    45. chmod 644 /etc/hosts.deny
    46. chmod 600 /etc/inittab
    47. chmod 644 /etc/login.defs
    48. chmod 644 /etc/profile
    49. chmod 644 /etc/bashrc
    50. chmod 744 /usr/bin/consolehelper
    51. #设置系统密码策略
    52. #cp /etc/pam.d/system-auth-ac /etc/pam.d/system-auth-ac.bak
    53. #sed -i '/password requisite pam_pwquality.so.*$/s//& minlen=8 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1/' /etc/pam.d/system-auth-ac
    54. #sed -i '/password sufficient pam_unix.so.*$/s//& remeber=3/' /etc/pam.d/system-auth-ac
    55. #cp /etc/pam.d/password-auth-ac /etc/pam.d/password-auth-ac.bak
    56. #sed -i '/password requisite pam_pwquality.so.*$/s//& minlen=8 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1/' /etc/pam.d/password-auth-ac
    57. #sed -i '/password sufficient pam_unix.so.*$/s//& remeber=3/' /etc/pam.d/password-auth-ac
    58. /sbin/authconfig --passminlen=8 --update
    59. /sbin/authconfig --enablereqdigit --update
    60. /sbin/authconfig --enablereqlower --update
    61. /sbin/authconfig --enablerequpper --update
    62. /sbin/authconfig --enablereqother --update
    63. #设置系统账户有效时间
    64. cp /etc/login.defs /etc/login.defs.bak
    65. sed -ie 's/PASS_MAX_DAYS\t99999/PASS_MAX_DAYS\t90/' /etc/login.defs
    66. sed -ie 's/PASS_MIN_LEN\t5/PASS_MIN_LEN\t8/' /etc/login.defs
    67. #避免记录不存在用户的登录信息,避免用户误输入导致密码泄露
    68. echo "LOG_UNKFAIL_ENAB yes" >> /etc/login.defs
    69. #配置用户密码尝试次数为6次,超过6次后登录会话自动登出,避免自动连续暴力破解
    70. echo "LOGIN_RETRIES 6" >> /etc/login.defs
    71. #记录用户上次登录时间,用户登录时给予提示
    72. echo "LASTLOG_ENAB yes" >> /etc/login.defs
    73. #ssh访问时超出密码失败次数后锁定用户5分钟
    74. cp /etc/pam.d/login /etc/pam.d/login.bak
    75. sed -i '/#%PAM/a\auth required pam_tally2.so deny=6 unlock_time=300' /etc/pam.d/login
    76. cp /etc/pam.d/sshd /etc/pam.d/sshd.bak
    77. sed -i '/#%PAM/a\auth required pam_tally2.so deny=6 unlock_time=300' /etc/pam.d/sshd
    78. ##sshd
    79. cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
    80. sed -i 's/^#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
    81. sed -i '/#Banner none/a\Banner /etc/issue.net' /etc/ssh/sshd_config
    82. sed -i 's/^#UseDNS no/UseDNS no/' /etc/ssh/sshd_config
    83. #sed -i 's/^UsePAM yes/UsePAM no/' /etc/ssh/sshd_config
    84. sed -i 's/^#Port 22/Port 22/' /etc/ssh/sshd_config
    85. echo 'Authorized users only! All activity may be monitored and reported!'> /etc/motd
    86. cp /etc/issue /etc/issue_`date +\%Y\%m\%d`
    87. cp /etc/issue.net /etc/issue.net_`date +\%Y\%m\%d`
    88. echo 'Be sure you are authorized to access this system!' > /etc/issue
    89. echo 'Be sure you are authorized to access this system!' > /etc/issue.net
    90. #profile
    91. echo "export TMOUT=600" >> /etc/profile
    92. echo 'export HISTTIMEFORMAT="%F %T `whoami` " ' >>/etc/profile
    93. sed -i "s/export HISTFILESIZE=.*/export HISTFILESIZE=10000/g" /etc/profile
    94. #关闭SELINUX
    95. cp /etc/sysconfig/selinux /etc/sysconfig/selinux.bak
    96. sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
    97. #kernel参数调优
    98. cat>>/etc/sysctl.conf<<EOF
    99. net.ipv4.conf.all.arp_ignore = 0
    100. net.ipv4.conf.default.arp_ignore = 0
    101. net.ipv4.conf.all.arp_filter = 0
    102. net.ipv4.conf.default.arp_filter = 0
    103. net.ipv4.conf.all.rp_filter = 1
    104. net.ipv4.conf.all.log_martians =0
    105. net.ipv4.conf.default.log_martians = 0
    106. net.ipv4.conf.all.promote_secondaries = 0
    107. net.ipv4.ip_no_pmtu_disc = 1
    108. net.ipv4.conf.all.forwarding = 0
    109. net.ipv4.conf.default.forwarding = 0
    110. Net.ipv4.icmp_echo_ignore_broadcasts = 1
    111. net.ipv4.conf.all.accept_source_route = 0
    112. net.ipv4.conf.default.accept_source_route = 0
    113. net.ipv4.conf.all.accept_redirects = 0
    114. net.ipv4.conf.default.accept_redirects = 0
    115. net.ipv4.tcp_timestamps = 0
    116. net.ipv4.icmp_ignore_bogus_error_responses = 1
    117. net.ipv4.conf.all.proxy_arp = 0
    118. net.ipv4.conf.default.proxy_arp = 0
    119. net.ipv4.tcp_max_syn_backlog = 4096
    120. net.ipv4.tcp_syncookies = 1
    121. net.ipv4.tcp_fin_timeout = 60
    122. net.ipv4.ip_forward = 0
    123. net.ipv4.tcp_keepalive_time = 150
    124. net.ipv4.tcp_keepalive_probes = 5
    125. net.ipv4.tcp_keepalive_intvl = 6
    126. EOF
    127. sysctl -p &>/dev/null
    128. #rsyslog
    129. echo "*.err;auth.info /var/adm/messages" >>/etc/rsyslog.conf
    130. systemctl restart sshd.service
    131. echo "------ Modify is OK ------"

    密码策略查看

    1. [root@db ~]# ll /etc/pam.d/password-auth-ac
    2. -rw-r--r--. 1 root root 1033 5 19 17:38 /etc/pam.d/password-auth-ac
    3. [root@db ~]# ll /etc/pam.d/system-auth-ac
    4. -rw-r--r--. 1 root root 1072 5 19 17:38 /etc/pam.d/system-auth-ac
    5. [root@db ~]# cat /etc/pam.d/password-auth-ac
    6. #%PAM-1.0
    7. # This file is auto-generated.
    8. # User changes will be destroyed the next time authconfig is run.
    9. auth required pam_env.so
    10. auth required pam_faildelay.so delay=2000000
    11. auth sufficient pam_unix.so nullok try_first_pass
    12. auth requisite pam_succeed_if.so uid >= 1000 quiet_success
    13. auth required pam_deny.so
    14. account required pam_unix.so
    15. account sufficient pam_localuser.so
    16. account sufficient pam_succeed_if.so uid < 1000 quiet
    17. account required pam_permit.so
    18. password requisite pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type=
    19. password sufficient pam_unix.so sha512 shadow nullok try_first_pass use_authtok
    20. password required pam_deny.so
    21. session optional pam_keyinit.so revoke
    22. session required pam_limits.so
    23. -session optional pam_systemd.so
    24. session [success=1 default=ignore] pam_succeed_if.so service in crond quiet use_uid
    25. session required pam_unix.so
    26. [root@db ~]# cat /etc/pam.d/system-auth-ac
    27. #%PAM-1.0
    28. # This file is auto-generated.
    29. # User changes will be destroyed the next time authconfig is run.
    30. auth required pam_env.so
    31. auth required pam_faildelay.so delay=2000000
    32. auth sufficient pam_fprintd.so
    33. auth sufficient pam_unix.so nullok try_first_pass
    34. auth requisite pam_succeed_if.so uid >= 1000 quiet_success
    35. auth required pam_deny.so
    36. account required pam_unix.so
    37. account sufficient pam_localuser.so
    38. account sufficient pam_succeed_if.so uid < 1000 quiet
    39. account required pam_permit.so
    40. password requisite pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type=
    41. password sufficient pam_unix.so sha512 shadow nullok try_first_pass use_authtok
    42. password required pam_deny.so
    43. session optional pam_keyinit.so revoke
    44. session required pam_limits.so
    45. -session optional pam_systemd.so
    46. session [success=1 default=ignore] pam_succeed_if.so service in crond quiet use_uid
    47. session required pam_unix.so
    48. [root@db ~]#

    修改示例:
    #

    1. cp /etc/pam.d/system-auth-ac /etc/pam.d/system-auth-ac.bak
    2. sed -i '/password requisite pam_pwquality.so.*$/s//& minlen=8 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1/' /etc/pam.d/system-auth-ac
    3. sed -i '/password sufficient pam_unix.so.*$/s//& remeber=3/' /etc/pam.d/system-auth-ac
    4. cp /etc/pam.d/password-auth-ac /etc/pam.d/password-auth-ac.bak
    5. sed -i '/password requisite pam_pwquality.so.*$/s//& minlen=8 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1/' /etc/pam.d/password-auth-ac
    6. sed -i '/password sufficient pam_unix.so.*$/s//& remeber=3/' /etc/pam.d/password-auth-ac


    #
    /sbin/authconfig —passminlen=8 —update
    /sbin/authconfig —enablereqdigit —update
    /sbin/authconfig —enablereqlower —update
    /sbin/authconfig —enablerequpper —update
    /sbin/authconfig —enablereqother —update