rpm -q firewalld
rpm -e --nodeps firewalld
yum -y install iptables-services
systemctl start iptables
systemctl enable iptables
# 复制文件
[ -f /etc/sysconfig/iptables ]&& /usr/bin/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 DROP
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
- 允许已建立的或相关连接的通行,即允许我发出去的数据包入站
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p udp -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -p udp -m state --state ESTABLISHED,RELATED -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/Ports//g" `
else
sshdport=22
fi
iptables -t filter -A INPUT -p tcp -m tcp --dport $sshdport -j ACCEPT
iptables -t filter -A INPUT 1 -p icmp --icmp-type 8 -m limit --limit 4/minute --limit-burst 6 -j ACCEPT
iptables -t filter -A OUTPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
iptables -t filter -A OUTPUT -p udp -m udp -j ACCEPT
站内链接