您的位置:首页 > 运维架构 > Shell

iptables模板

2016-05-06 12:01 411 查看
#!/bin/bash

#本机网卡名
network=eth0

#清空规则
/sbin/iptables -F

#允许外部主机建立22/80/443/10022端口连接
/sbin/iptables -A INPUT -i $network -p tcp -m multiport \
--dports 22,80,443,10022 -m state --state NEW,ESTABLISHED -j ACCEPT
/sbin/iptables -A OUTPUT -o $network -p tcp -m multiport \
--sports 22,80,443,10022 -m state --state ESTABLISHED -j ACCEPT

#允许指定主机访问3306端口
/sbin/iptables -A INPUT -i $network -p tcp -s 192.168.100.0/24 \
--dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPT
/sbin/iptables -A OUTPUT -o $network -p tcp --sport 3306 -m state \
--state ESTABLISHED -j ACCEPT

#允许lo访问
/sbin/iptables -A INPUT -i lo -j ACCEPT
/sbin/iptables -A OUTPUT -o lo -j ACCEPT

#允许外部主机ping内部主机
/sbin/iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
/sbin/iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
#允许内部主机ping外部主机
/sbin/iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
/sbin/iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT

#防止DOS攻击
/sbin/iptables -A INPUT -p tcp --dport 80 -m limit --limit 50/minute --limit-burst 100 -j ACCEPT
#–limit 25/minute: 每分钟最多50个连接数
#–limit-burst 100: 连接数达到100的时候启用生效

#端口转发,使用ssh连接10022会转发到22端口
/sbin/iptables -t nat -A PREROUTING -p tcp \
-d 192.168.100.254 --dport 10022 -j DNAT --to 192.168.100.254:22

#默认规则
/sbin/iptables -P INPUT DROP
/sbin/iptables -P OUTPUT ACCEPT
/sbin/iptables -P FORWARD DROP

#允许192.168.110.0/24网段NAT上网
/sbin/iptables -t nat -A POSTROUTING -s 192.168.110.0/24 -j SNAT --to 192.168.100.254




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux shell iptables