当前位置: 首页 > news >正文

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 su2. iptables-save > /etc/iptables.rules3. In /etc/network/if-pre-up.d/iptables,put:#!/bin/shiptables-restore < /etc/iptables.rulesexit 04. After, in /etc/network/if-post-down.d/iptables,put:#!/bin/shiptables-save -c > /etc/iptables.rulesif [ -f /etc/iptables.rules ]; theniptables-restore < /etc/iptables.rulesfiexit 05. After, give permission to the scripts:sudo chmod +x /etc/network/if-post-down.d/iptablessudo chmod +x /etc/network/if-pre-up.d/iptables
http://www.lryc.cn/news/70174.html

相关文章:

  • Liunx安装Android Studio
  • 8、Linux C/C++ 实现MySQL的图片插入以及图片的读取
  • 【搭建轻量级图床】本地搭建LightPicture开源图床管理系统 - 异地远程访问
  • 微信小程序全局路由拦截
  • 截图自动添加水印(macOS/windows)
  • 大学四年,我建议你这么学网络安全
  • Spring Boot整合Redis缓存并使用注解
  • 通知可以根据切入点表达式来进行增强,也可以根据自己的注解值来进行增强
  • <Python实际应用>做一个简单的签到投屏系统
  • 时序预测 | MATLAB实现BO-CNN-GRU贝叶斯优化卷积门控循环单元时间序列预测
  • Baumer工业相机堡盟工业相机使用BGAPISDK将工业相机设为Burst模式以及该模式的优势以及行业应用(C++)
  • BERT输入以及权重矩阵形状解析
  • 3 个令人惊艳的 ChatGPT 项目,开源了!
  • 一、12.C++内存管理
  • ensp实践dhcp服务
  • 【王道·计算机网络】第六章 应用层
  • 【论文解读】(如何微调BERT?) How to Fine-Tune BERT for Text Classification?
  • 工程师是怎样对待开源
  • Spring Boot日志系统大揭秘:从零开始学习Spring Boot日志:常见问题解答和最佳实践
  • 【06】Nginx之反向代理
  • TCP是面向字节流的协议
  • 读书笔记——《when breath becomes air》《超越自卑》
  • CMD与DOS脚本编程【第二章】
  • 面试字节,过关斩将直接干到 3 面,结果被吊打了?
  • OpenCV在iOS端的集成及Mat和UIImage互相转化(附源码)
  • 5月跳槽会有风险,不跳也会有?
  • 【小白版】最简单的 goland package 教程包括自定义包的使用
  • IMX6ULL的I2C驱动详细分析
  • 日志迁移到 logback
  • 开源字节 CRM 系统