闲来无事,整理一个系统安全加固脚本,每个公司的要求不一样,所以仅供参考:
#!/bin/sh
echo "00 */1 * * * /usr/sbin/ntpdate 192.168.1.1 >>/var/log/ntpdate.log" > mycrontab
crontab mycrontab
rm -rf mycrontab
/usr/sbin/ntpdate 192.168.1.1
cp /etc/sysconfig/i18n /etc/sysconfig/i18n.bak
echo > /etc/sysconfig/i18n
echo LANG="zh_CN.GB18030" >> /etc/sysconfig/i18n
echo LANGUAGE="zh_CN.GB18030:zh_CN.GB2312:zh_CN" >> /etc/sysconfig/i18n
echo SUPPORTED="zh_CN.UTF-8:zh_CN:zh:en_US.UTF-8:en_US:en" >> /etc/sysconfig/i18n
echo SYSFONT="latarcyrheb-sun16" >> /etc/sysconfig/i18n
#echo "root:xbbwlcUoKjF7" | chpasswd
useradd weblogic
echo "weblogic:weblogic" | chpasswd
history -c
# 接收套接字缓冲区大小的缺省值
echo "net.core.rmem_default = 2569600" >>/etc/sysctl.conf
# 最大的TCP数据接收缓冲
echo "net.core.rmem_max = 2569600" >>/etc/sysctl.conf
# 发送套接字缓冲区大小的缺省值
echo "net.core.wmem_default = 2569600" >>/etc/sysctl.conf
# 最大的TCP数据发送缓冲
echo "net.core.wmem_max = 2569600" >>/etc/sysctl.conf
# 时间戳在(请参考RFC 1323)TCP的包头增加12个字节
echo "net.ipv4.tcp_timestamps = 0" >>/etc/sysctl.conf
# 有选择的应答
echo "net.ipv4.tcp_sack = 1" >>/etc/sysctl.conf
# 支持更大的TCP窗口. 如果TCP窗口最大超过65535(64K), 必须设置该数值为1
echo "net.ipv4.tcp_window_scaling = 1" >>/etc/sysctl.conf
# 开启keepalive的闲置时长
echo "net.ipv4.tcp_keepalive_time = 600" >>/etc/sysctl.conf
echo "kernel.sem = 500 64000 200 256" >>/etc/sysctl.conf
# 文件句柄
echo "fs.file-max = 65536" >>/etc/sysctl.conf
echo "net.ipv4.ip_local_port_range = 1024 65000" >>/etc/sysctl.conf
# 当本地系统向外发起tcp或udp连接请求时使用的端口范围
sysctl -p
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
sed -i "s/#PermitRootLogin yes/PermitRootLogin no/" /etc/ssh/sshd_config
#当普通用户登录时,密码输入错误三次,系统马上把该用户锁定,需要120s后重新登录
cp /etc/pam.d/system-auth /etc/pam.d/system-auth.bak
auth required pam_tally2.so deny=3 onerr=fail no_magic_root unlock_time=120
#由于远程是可以通过普通用户登录,所以给所有普通用户设置密码时,至少有一个特殊字符、大写字母、小写字母、最小长度为8位
password requisite /lib/security/$ISA/pam_cracklib.so retry=3 difok=5 ocredit=-1 dcredit=-1 ucredit=-1 minlen=8
#设置密码过期的时间最多天数,新建用户是,用户可以修改的天使,密码的最短长度(前面已经设置可以不设置),密码过期的警告天数。
cp /etc/login.defs /etc/login.defs.bak
sed -i 's/PASS_MAX_DAYS.* 99999/PASS_MAX_DAYS 90/' /etc/login.defs
sed -i 's/PASS_MIN_DAYS.* 0/PASS_MIN_DAYS 7/' /etc/login.defs
sed -i 's/PASS_MIN_LEN.* 5/PASS_MIN_LEN 8/' /etc/login.defs
#关闭DNS
cp /etc/resolv.conf /etc/resolv.conf.bak
echo 'nameserver 210.22.84.3' > /etc/resolv.conf
#备份环境变量文件
cp /etc/profile /etc/profile.bak
echo export TMOUT=600 >> /etc/profile #增加60S超时退出
echo export HISTTIMEFORMAT=\'%F %T \' >> /etc/profile #记录操作历史记录的时间
echo export HISTFILESIZE=10000 >> /etc/profile
echo export HISTSIZE=10000 >> /etc/profile
#修改系统文件最大打开数
echo -e "* soft nofile = 32768 \n* hard nofile = 65536" >> /etc/security/limits.conf
#计划任务
mkdir ~/Shell
echo -e " #!/bin/bash \n sync \n echo 3 > /proc/sys/vm/drop_caches" > ~/Shell/MemcacheClean.sh
echo "00 03 * * * /Shell/MemcacheClean.sh" > ~/Shell/mycrontab
crontab ~/Shell/mycrontab
#关闭selinux
sed '7s/enforcing/disabled/g' /etc/sysconfig/selinux -i
#关闭火墙
iptables -F
/etc/init.d/iptables save >> /dev/null
service sshd restart
history -c
本文转自青衫解衣 51CTO博客,原文链接:http://blog.51cto.com/215687833/1755103