Linux防火墙(Iptables)配置详解

        最近的一个项目需要部署到腾讯云Linux Centos 6.5,腾讯云服务器默认是不开启防火墙的,由于项目中使用nginx+tomcat做负载均衡与动静分离,需要外网访问tomcat占用的8080,8081。之前没使用过Linux 防火墙,经过一番查阅资料以及实验终于配置成功了,这里记录一下,方便以后查阅,也希望对有同样需求的人提供一个参考。

        1. iptables介绍

        iptables是Linux内核集成的IP信息包过滤系统,如果 Linux 系统连接到因特网或LAN、服务器或连接LAN和因特网的代理服务器,则该系统有利于在Linux上更好地控制IP信息包过滤和防火墙配置

        2. 开启防火墙

        先用命令iptables -L -n查看本机防火墙的设置情况,由于安装linux时选择不开启防火墙,返回的结果为:

[root@VM_13_29_centos ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       tcp  --  218.5.1.142          0.0.0.0/0           

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 
  这里在介绍一下其他几个常用的iptables命令:
service  iptables  status        查看防火墙状态
service  iptables  start           开启防火墙
service  iptables  stop           关闭防火墙
service  iptables  restart        重启防火墙
iptables -F                       清除预设表filter中的所有规则链的规则
iptables -X                       清除预设表filter中使用者自定链中的规则
          3.  设置预设规则          在还未设置预设规则之前,需要先设置ssh远程连接,因为是通过ssh远程操作系统,如果先开启防火墙,而ssh连接的22端口又设置允许通过,ssh远程连接就会断开(那就悲剧了,什么操作都做不了了)。       
[root@VM_13_29_centos ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
[root@VM_13_29_centos ~]# iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
[root@VM_13_29_centos ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[root@VM_13_29_centos ~]# service iptables restart
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                                 [  OK  ]
iptables: Unloading modules:                                     [  OK  ]
iptables: Applying firewall rules:                                 [  OK  ]
    接下来就可以设置预设规则了,输入如下命令:
[root@VM_13_29_centos ~]# iptables -p INPUT DROP
[root@VM_13_29_centos ~]# iptables -p OUTPUT DROP
[root@VM_13_29_centos ~]# iptables -p FORWARD DROP
 
   现在INPUT、OUTPUT、FORWRD链都被设置成DROP,这样所有网络的进出都要配置,就像配置web服务器80端口一样:

         

[root@VM_13_29_centos ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT
[root@VM_13_29_centos ~]# iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT

   到此为止,处理ssh远程连接、web服务80端口,所有其他网络请求都无法进出。

        4. iptables其他操作

       

[root@VM_13_29_centos ~]# iptables -nL --line-number    ///查看过滤链,并显示链序号
Chain INPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22 
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80 

Chain FORWARD (policy DROP)
num  target     prot opt source               destination         

Chain OUTPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp spt:22 
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp spt:80 
[root@VM_13_29_centos ~]# iptables -D OUTPUT 2 //删除OUTPUT链,序号为2的链

猜你喜欢

转载自shiguangqi11.iteye.com/blog/2185129