iptables防火墙设置

一、 查看本机防火墙设置:

 iptables -L -n

如果设置过防火墙,则显示如下:


 

如果未设置防火墙,则显示如下:


 

二、清楚已设定的防火墙规则: 

 iptables -F      清除预设表filter中的所有规则链的规则

 iptables -X      清除预设表filter中使用者自定链中的规则

执行完成之后,再次查看防火墙:

 iptables -L -n

会发现已有的防火墙规则都被清空了。

但是这只是临时的,重启就会被还原,因此如果需要永久生效,则需要保存到系统配置里:

/etc/rc.d/init.d/iptables save

然后重启一下防火墙:

service iptables restart

三、添加防火墙规则: 

如果做了WEB服务器,开启80端口.
[root@tp ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT
如果做了邮件服务器,开启25,110端口.
[root@tp ~]# iptables -A INPUT -p tcp --dport 110 -j ACCEPT
[root@tp ~]# iptables -A INPUT -p tcp --dport 25 -j ACCEPT

如果做了FTP服务器,开启21端口
[root@tp ~]# iptables -A INPUT -p tcp --dport 21 -j ACCEPT
[root@tp ~]# iptables -A INPUT -p tcp --dport 20 -j ACCEPT
如果做了DNS服务器,开启53端口
[root@tp ~]# iptables -A INPUT -p tcp --dport 53 -j ACCEPT
减少不安全的端口连接
[root@tp ~]# iptables -A OUTPUT -p tcp --sport 31337 -j DROP
[root@tp ~]# iptables -A OUTPUT -p tcp --dport 31337 -j DROP
我们只允许192.168.0.3的机器进行SSH连接
[root@tp ~]# iptables -A INPUT -s 192.168.0.3 -p tcp --dport 22 -j ACCEPT
同样,我们如果需要永久生效,也需要执行:
/etc/rc.d/init.d/iptables save
然后重启防火墙才行。
 
关闭防火墙:
service iptables stop
 
参考:http://www.cnblogs.com/alimac/p/5848372.html
 
 

猜你喜欢

转载自guwq2014.iteye.com/blog/2390195