摘要:本文介绍的这段Linux系统安全Shell脚本用于Linux系统的安全初始化脚本,可以在服务器系统安装完毕之后立即执行以快速建立起服务器的安全防护。该脚本由抚琴煮酒推荐至51CTO系统频道,在原有的脚本上又做了些小改动,修改了一些bug,已经在CentOS 5.5 x86_64下通过,目前在一些没有硬件防火墙的服务器上使用。
下面的这段Linux系统安全Shell脚本用于Linux系统的安全初始化脚本,可以在服务器系统安装完毕之后立即执行以快速建立起服务器的安全防护。最初的脚本由晓辉撰写,在数次修改之后已经大量应用在某大型媒体网站体系中。通过和原作者交流之后在原有的脚本上又做了些小改动,修改了一些bug,已经在CentOS 5.5 x86_64下通过,目前在一些没有硬件防火墙的服务器上使用。
使用方法:将其复制,保存为一个shell文件,比如security.sh。将其上传到linux服务器上,执行sh security.sh,就可以使用该脚本了。建议大家在系统初始化后立即执行,然后创建了用户帐号和密码后就不要再改动了,以免影响重要文件的初始md5值。
脚本内容(源脚本来自晓辉的博客。以下内容为了方便阅读,对注释进行了翻译):
#!/bin/sh # desc: setup linux system security # author:coralzd # powered by www.freebsdsystem.org # version 0.1.2 written by 2011.05.03 #设置账号 passwd -l xfs passwd -l news passwd -l nscd passwd -l dbus passwd -l vcsa passwd -l games passwd -l nobody passwd -l avahi passwd -l haldaemon passwd -l gopher passwd -l ftp passwd -l mailnull passwd -l pcap passwd -l mail passwd -l shutdown passwd -l halt passwd -l uucp passwd -l operator passwd -l sync passwd -l adm passwd -l lp # 用chattr给用户路径更改属性。chattr命令用法参考文末说明[1] chattr +i /etc/passwd chattr +i /etc/shadow chattr +i /etc/group chattr +i /etc/gshadow # 设置密码连续输错3次后锁定5分钟 sed -i 's#auth required pam_env.so#auth required pam_env.so\nauth required pam_tally.so onerr=fail deny=3 unlock_time=300\nauth required /lib/security/$ISA/pam_tally.so onerr=fail deny=3 unlock_time=300#' /etc/pam.d/system-auth # 5分钟后自动登出,原因参考文末说明[2] echo "TMOUT=300" >>/etc/profile # 历史命令记录数设定为10条 sed -i "s/HISTSIZE=1000/HISTSIZE=10/" /etc/profile # 让以上针对 /etc/profile 的改动立即生效 source /etc/profile # 在 /etc/sysctl.conf 中启用 syncookie echo "net.ipv4.tcp_syncookies=1" >> /etc/sysctl.conf sysctl -p # exec sysctl.conf enable # 优化 sshd_config sed -i "s/#MaxAuthTries 6/MaxAuthTries 6/" /etc/ssh/sshd_config sed -i "s/#UseDNS yes/UseDNS no/" /etc/ssh/sshd_config # 限制重要命令的权限 chmod 700 /bin/ping chmod 700 /usr/bin/finger chmod 700 /usr/bin/who chmod 700 /usr/bin/w chmod 700 /usr/bin/locate chmod 700 /usr/bin/whereis chmod 700 /sbin/ifconfig chmod 700 /usr/bin/pico chmod 700 /bin/vi chmod 700 /usr/bin/which chmod 700 /usr/bin/gcc chmod 700 /usr/bin/make chmod 700 /bin/rpm # 历史安全 chattr +a /root/.bash_history chattr +i /root/.bash_history # 给重要命令写 md5 cat > list << "EOF" && /bin/ping /usr/bin/finger /usr/bin/who /usr/bin/w /usr/bin/locate /usr/bin/whereis /sbin/ifconfig /bin/vi /usr/bin/vim /usr/bin/which /usr/bin/gcc /usr/bin/make /bin/rpm EOF for i in `cat list` do if [ ! -x $i ];then echo "$i not found,no md5sum!" else md5sum $i >> /var/log/`hostname`.log fi done rm -f list |