iptables 添加,删除,查看,修改,及docker运行时修改端口

一,安装并启动防火墙

  1. [root@linux ~]# /etc/init.d/iptables start  

当我们用iptables添加规则,保存后,这些规则以文件的形势存在磁盘上的,以centos为例,文件地址是/etc/sysconfig/iptables,我们可以通过命令的方式去添加,修改,删除规则,也可以直接修改/etc/sysconfig/iptables这个文件就行了。

二,添加防火墙规则

1,添加filter表

  1. [root@linux ~]# iptables -A INPUT -p tcp -m tcp --dport 21 -j ACCEPT  //开放21端口  

出口我都是开放的iptables -P OUTPUT ACCEPT,所以出口就没必要在去开放端口了。

2,添加nat表

  1. [root@linux ~]# iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -j MASQUERADE  

将源地址是 192.168.10.0/24 的数据包进行地址伪装

3,-A默认是插入到尾部的,可以-I来插入到指定位置

  1. [root@linux ~]# iptables -I INPUT 3 -p tcp -m tcp --dport 20 -j ACCEPT  
  2.   
  3. [root@linux ~]# iptables -L -n --line-number  
  4. Chain INPUT (policy DROP)  
  5. num  target     prot opt source               destination  
  6. 1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0  
  7. 2    DROP       icmp --  0.0.0.0/0            0.0.0.0/0           icmp type 8  
  8. 3    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:20                //-I指定位置插的  
  9. 4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22  
  10. 5    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80  
  11. 6    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED  
  12. 7    DROP       all  --  0.0.0.0/0            0.0.0.0/0           state INVALID,NEW  
  13. 8    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:21                //-A默认插到最后  
  14.   
  15. Chain FORWARD (policy ACCEPT)  
  16. num  target     prot opt source               destination           
  17.   
  18. Chain OUTPUT (policy ACCEPT)  
  19. num  target     prot opt source               destination  

三,查下iptable规则

1,查看filter表

  1. [root@linux ~]# iptables -L -n --line-number |grep 21 //--line-number可以显示规则序号,在删除的时候比较方便  
  2. 5    ACCEPT     tcp  --  192.168.1.0/24       0.0.0.0/0           tcp dpt:21  

如果不加-t的话,默认就是filter表,查看,添加,删除都是的

2,查看nat表

  1. [root@linux ~]# iptables -t nat -vnL POSTROUTING --line-number  
  2. Chain POSTROUTING (policy ACCEPT 38 packets, 2297 bytes)  
  3. num   pkts bytes target     prot opt in     out     source               destination  
  4. 1        0     0 MASQUERADE  all  --  *      *       192.168.10.0/24      0.0.0.0/0  

四,修改规则

  1. [root@linux ~]# iptables -R INPUT 3 -j DROP    //将规则3改成DROP  

五,删除iptables规则

  1. [root@linux ~]# iptables -D INPUT 3  //删除input的第3条规则  
  2.   
  3. [root@linux ~]# iptables -t nat -D POSTROUTING 1  //删除nat表中postrouting的第一条规则  
  4.   
  5. [root@linux ~]# iptables -F INPUT   //清空 filter表INPUT所有规则  
  6.   
  7. [root@linux ~]# iptables -F    //清空所有规则  
  8.   
  9. [root@linux ~]# iptables -t nat -F POSTROUTING   //清空nat表POSTROUTING所有规则  

六,设置默认规则

  1. [root@linux ~]# iptables -P INPUT DROP  //设置filter表INPUT默认规则是 DROP  

所有添加,删除,修改后都要保存起来,/etc/init.d/iptables save.上面只是一些最基本的操作,要想灵活运用,还要一定时间的实际操作。

应用:

修改主机iptables端口映射docker的端口映射并不是在docker技术中实现的,而是通过宿主机的iptables来实现。通过控制网桥来做端口映射,类似路由器中设置路由端口映射。

如果我们有一个容器的8000端口映射到主机的9000端口,先查看iptabes设置了什么规则:

sudo iptables -t nat -vnL

结果中有一条:

Chain DOCKER (2 references)

pkts bytes target prot opt in out source destination

98 5872 RETURN all -- docker0 * 0.0.0.0/0 0.0.0.0/0

237 14316 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:9000 to:172.17.0.3:8000

我们可以看到docker创建了一个名为DOKCER的自定义的链条Chain。而我开放8000端口的容器的ip是172.17.0.3。

也可以通过inspect命令查看容器ip

docker inspect [containerId] |grep IPAddress

我们想再增加一个端口映射,比如8081->81,就在这个链条是再加一条规则:

sudo iptables -t nat -A DOCKER -p tcp --dport 8081 -j DNAT --to-destination 172.17.0.3:81

加错了或者想修改:先显示行号查看

sudo iptables -t nat -vnL DOCKER --line-number

删除规则3

sudo iptables -t nat -D DOCKER 3
 

修改iptables添加映射端口
在宿主机执行以下命令,即可达到目的

iptables -A DOCKER -d 172.17.0.2/32 ! -i docker0 -o docker0 -p tcp -m tcp --dport 1521 -j ACCEPT
1
iptables -t nat -A DOCKER ! -i br0 -p tcp -m tcp --dport 11522 -j DNAT --to-destination 172.17.0.2:1521
1
重要参数说明:

172.17.0.2: docker容器在docker中的ip,可通过"docker inspect `container_name` | grep IPAddress"获取
1521: 容器内部应用对外暴露的端口
11522: 容器内部应用端口映射到宿主机的端口
 

  保存持久化 iptables:

After of the write the commands iptables, do:

 1. sudo su
 2. iptables-save > /etc/iptables.rules
 3. In /etc/network/if-pre-up.d/iptables,put:

 #!/bin/sh
 iptables-restore < /etc/iptables.rules
 exit 0

 4. After, in /etc/network/if-post-down.d/iptables,put:
 #!/bin/sh
 iptables-save -c > /etc/iptables.rules
 if [ -f /etc/iptables.rules ]; then
 iptables-restore < /etc/iptables.rules
 fi
 exit 0
 5. After, give permission to the scripts:
 sudo chmod +x /etc/network/if-post-down.d/iptables
 sudo chmod +x /etc/network/if-pre-up.d/iptables

猜你喜欢

转载自blog.csdn.net/a1058926697/article/details/130833974