关于linux系统安全配置脚本

时间:2024-07-26 18:35:14

本脚本是第二次更新,已经大量应用在某大型媒体网站体系中,加入了之前没有想到的一些安全设置。使用方法将其复制,保存为一个shell文件,比如security.sh.将其上传到Linux服务器上,执行sh security.sh,就可以使用该脚本了!
建议根据自己的实际需要来修改此脚本,不要盲目使用!

#!/bin/sh
# desc: setup linux system security
# author:coralzd
# powered by www.freebsdsystem.org
# version 0.1. written by 2011.05.
#account setup 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 /etc/passwd /etc/shadow
chattr +i /etc/passwd
chattr +i /etc/shadow
chattr +i /etc/group
chattr +i /etc/gshadow # add continue input failure ,passwd unlock time minite
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
# system timeout minite auto logout
echo "TMOUT=300" >>/etc/profile # will system save history command list to
sed -i "s/HISTSIZE=1000/HISTSIZE=10/" /etc/profile # enable /etc/profile go!
source /etc/profile # add syncookie enable /etc/sysctl.conf
echo "net.ipv4.tcp_syncookies=1" >> /etc/sysctl.conf sysctl -p # exec sysctl.conf enable
# optimizer sshd_config sed -i "s/#MaxAuthTries 6/MaxAuthTries 6/" /etc/ssh/sshd_config
sed -i "s/#UseDNS yes/UseDNS no/" /etc/ssh/sshd_config # limit chmod important commands
chmod /bin/ping
chmod /usr/bin/finger
chmod /usr/bin/who
chmod /usr/bin/w
chmod /usr/bin/locate
chmod /usr/bin/whereis
chmod /sbin/ifconfig
chmod /usr/bin/pico
chmod /bin/vi
chmod /usr/bin/which
chmod /usr/bin/gcc
chmod /usr/bin/make
chmod /bin/rpm # history security chattr +a /root/.bash_history
chattr +i /root/.bash_history # write important command md5
cat > list << "EOF" &&
/bin/ping
/bin/finger
/usr/bin/who
/usr/bin/w
/usr/bin/locate
/usr/bin/whereis
/sbin/ifconfig
/bin/pico
/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