service iptables status #查看状态 service iptables save #保存规则 service iptables stop #关闭 service iptables start #开启 service iptables restart #重启
配置文件目录
bash> vim /etc/sysconfig/iptables
WEB服务器,开启80端口.
bash> iptables -A INPUT -p tcp --dport 80 -j ACCEPT
邮件服务器,开启一组端口.
bash> iptables -A INPUT -m state --state NEW -m multiport -p tcp --dports 110,25 -j ACCEPT
FTP服务器,开启21端口
bash> iptables -A INPUT -m state --state NEW -m multiport -p tcp --dports 21,20 -j ACCEPT
DNS服务器,开启53端口
bash> iptables -A INPUT -p tcp --dport 53 -j ACCEPT
允许icmp包通过,也就是允许ping,
-A OUTPUT -p icmp -j ACCEPT (OUTPUT设置成DROP的话 -A INPUT -p icmp -j ACCEPT (INPUT设置成DROP的话)
将本机的8080端口转发至其他主机,主机IP:192.168.1.12,目标主机IP和端口:192.168.1.13:8088,规则如下;
iptables -t nat -A PREROUTING -p tcp -m tcp --dport 8080 -j DNAT --to-destination 192.168.1.13:8088 iptables -t nat -A POSTROUTING -p tcp -m tcp --dport 8088 -j SNAT --to-source 192.168.1.12 echo 1 > /proc/sys/net/ipv4/ip_forward
同时开启iptables forward转发功能。 常用配置
*filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT-A INPUT -p icmp -j ACCEPT-A INPUT -i lo -j ACCEPT-A INPUT -s 192.168.10.0/24 -j ACCEPT-A INPUT -s 192.168.10.[100-200] -j ACCEPT-A INPUT -m state --state NEW -m multiport -p tcp --dports 22,80,8080,10050 -j ACCEPT-A INPUT -j REJECT --reject-with icmp-host-prohibited-A FORWARD -j REJECT --reject-with icmp-host-prohibited
常见故障 启动或保存配置规则报错,配置文件不存在
故障: bash> service iptables save iptables: Nothing to save. bash> service iptables start iptables: No config file. 执行: bash> iptables -P OUTPUT ACCEPT 然后重新保存配置即可 bash> service iptables save iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ] 启动防火墙 bash> service iptables start iptables: Applying firewall rules: [ OK ]