iptables防火墙配置

  • 更改 Firewall 为 iptables
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
  • 限制80端口单个IP的最大连接数为10
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
  • 允许ping和被ping
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
  • 允许DNS
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

站内链接

转载于:https://www.cnblogs.com/outsrkem/p/11175533.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值