rpm -q firewalld
rpm -e --nodeps firewalld
yum -y install iptables-services
systemctl start iptables
systemctl enable iptables
# 复制文件
[ -f /etc/sysconfig/iptables ]&& \cp /etc/sysconfig/iptables{,.bak-`date +%Y%m%d%H%M%S`}
# 命令导出
iptables-save > /tmp/iptables.bak-`date +%Y%m%d%H%M%S`
iptables --delete-chain
iptables --flush
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
iptables -A INPUT -i lo -j ACCEPT
- 允许已建立的或相关连接的通行,即允许我发出去的数据包入站
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -I INPUT -p tcp --dport 80 -m connlimit --connlimit-above 10 -j DROP
- 允许80(HTTP)/873(RSYNC)/443(HTTPS)/20,21(FTP)/25(SMTP)端口的连接
iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 873 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 20 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 25 -j ACCEPT
- 允许SSH端口的连接,脚本自动侦测目前的SSH端口,否则默认为22端口
if grep "^Port" /etc/ssh/sshd_config>/dev/null;then
sshdport=`grep "^Port" /etc/ssh/sshd_config | sed "s/Port\s//g" `
else
sshdport=22
fi
iptables -A INPUT -p tcp -m tcp --dport $sshdport -j ACCEPT
iptables -A INPUT -p icmp -m icmp --icmp-type 0 -j ACCEPT
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
iptables -A INPUT -p icmp -m icmp --icmp-type 11 -j ACCEPT
if [ -s /etc/resolv.conf ];then
nameserver1=`cat /etc/resolv.conf |grep nameserver |awk 'NR==1{print $2 }'`
nameserver2=`cat /etc/resolv.conf |grep nameserver |awk 'NR==2{print $2 }'`
fi
[ ! -z "$nameserver1" ] && iptables -A INPUT -p udp -m udp -s $nameserver1 --sport 53 -j ACCEPT
[ ! -z "$nameserver2" ] && iptables -A INPUT -p udp -m udp -s $nameserver2 --sport 53 -j ACCEPT
站内链接