配置文件目录
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:8088iptables -t nat -A POSTROUTING -p tcp -m tcp --dport 8088 -j SNAT --to-source 192.168.1.12echo 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 saveiptables: Nothing to save.bash> service iptables startiptables: No config file.执行:bash> iptables -P OUTPUT ACCEPT然后重新保存配置即可bash> service iptables saveiptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]启动防火墙bash> service iptables start iptables: Applying firewall rules: [ OK ]
原文地址:http://13314012.blog.51cto.com/13304012/1977579