日期:2014-05-16  浏览次数:20765 次

Linux防火墙iptables设置

iptables防火墙由Netfilter项目开发(http://www.netfilter.org),自2001年1月Linux 2.4内核发布以来,它就成为Linux的一部分。

多年来,iptables已发展成为一个功能强大的防火墙,它已具备通常只会在专有的商业防火墙中才能发现的大多数功能。例如,iptables提供了全面的协议状态跟踪、数据包的应用层检查、速率限制和一个功能强大的机制以指定过滤策略。所有主流的linux发行版都包含了iptables,而且许多发行版在系统安装过程中就提示用户部署iptables策略。

正因为iptables的功能过于强大,所以常常导致一些刚接触iptables的人在配置时出现错误。iptables的规则配置文件为/etc/sysconfig/iptables,如果你已非常了解iptables,那么可以直接在此文件中修改配置规则。对iptables不是很了解的话,可以按下面的步骤设置,这用出错的机会更小些。

以CentOS5.5为例:

1、增加规则指令
# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
# iptables -I INPUT -p udp -s 192.168.1.200 --dport 161 -j ACCEPT

2、保存新增指令
# /etc/rc.d/init.d/iptables save

3、使新增指令生效
# /etc/init.d/iptables restart
或者
# service iptables restart

4、查看iptables状态
# /etc/init.d/iptables status

5、关闭iptables
# /etc/init.d/iptables stop
或者
# service iptables stop

如果要永久的停用iptables,需要在启动服务中停止该服务。
# ntsysv中将iptables及ip6tables前的*号去掉
或者
# chkconfig iptables off

下面是我虚拟机中iptables的状态:

# /etc/init.d/iptables status

Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     udp  --  192.168.1.200        0.0.0.0/0           udp dpt:161 
2    RH-Firewall-1-INPUT  all  --  0.0.0.0/0            0.0.0.0/0           

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    RH-Firewall-1-INPUT  all  --  0.0.0.0/0            0.0.0.0/0           

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

Chain RH-Firewall-1-INPUT (2 references)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           icmp type 255 
3    ACCEPT     esp  --  0.0.0.0/0            0.0.0.0/0           
4    ACCEPT     ah   --  0.0.0.0/0            0.0.0.0/0           
5    ACCEPT     udp  --  0.0.0.0/0            224.0.0.251         udp dpt:5353 
6    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:631 
7    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:631 
8    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
9    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
10   REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited

?

?

?