配置规则: — 命令

  • iptables -A 将一个规则添加到链末尾
  • iptables -D 将指定的链中删除规则
  • iptables -F 将指定的链中删除所有规则
  • iptables -I 将在指定链的指定编号位置插入一个规则
  • iptables -L 列出指定链中所有规则
  • iptables -t nat -L 列出所有NAT链中所有规则
  • iptables -N 建立用户定义链
  • iptables -X 删除用户定义链
  • iptables -P 修改链的默认设置,如将iptables -P INPUT DROP (将INPUT链设置为DROP)

—规则

  • —dport 指定目标TCP/IP端口 如 –dport 80
  • —sport 指定源TCP/IP端口 如 –sport 80
  • -p tcp 指定协议为tcp
  • -p icmp 指定协议为ICMP
  • -p udp 指定协议为UDP
  • -j DROP 拒绝
  • -j ACCEPT 允许
  • -j REJECT 拒绝并向发出消息的计算机发一个消息
  • -j LOG 在/var/log/messages中登记分组匹配的记录
  • -m mac –mac 绑定MAC地址
  • -m limit –limit 1/s 1/m 设置时间策列
  • -s 10.10.0.0或10.10.0.0/16 指定源地址或地址段
  • -d 10.10.0.0或10.10.0.0/16 指定目标地址或地址段
  • -s ! 10.10.0.0 指定源地址以外的

1. 基础的iptables操作

  1. # 查看配置的规则
  2. iptables --list | -L
  3. # 清除iptables配置的规则
  4. iptables --flush | -F
  5. # 显示规则对相应的比那好
  6. iptables -L -n --line-number
  7. # 删除Chain:INPUT里面的第二条规则
  8. iptables -D INPUT 2

2.端口和协议的规则

规则说明:

  1. 端口—必须更协议 成对出现
  • -p tcp | icmp | udp | all
  • —dport 目标端口、—dport 目标端口1 : 目标端口2 ; —sport 源端口、 —sport 源端口1 : 源端口2
  • -i :入站网口; -o :出站网口
  1. 多端口匹配:
  • 多端口配置:-m multiport —sport 源端口列表
  • 多端口配置:-m multiport —dport 目的端口列表
  • IP范围匹配:-m iprange —src-range IP范围
  • MAC地址匹配:-m mac —macl-source MAC地址
  • 状态匹配:-m state —state 连接状态
  1. 目标动作(target)
  • ACCEPT:允许通过
  • DROP:直接丢弃,不给出任何回应
  • REJECT:拒绝通过,必要时会给出提示
  • LOG:记录日志信息,然后传给下一条规则继续匹配
  • SNAT:修改数据包源地址
  • DNAT:修改数据包目的地址
  • REDIRECT:重定向
  1. ## A追加,I前面插入,D删除,R替换,N新建一个自定义链 ##
  2. ## -i(interface)指定网络接口; -s(source)来源; -j(jump)执行 ##
  3. # 添加限制指定IP来源的访问
  4. iptables -I INPUT -s 192.168.30.195 -j DROP
  5. # 添加限制指定IP段的访问
  6. iptables -I INPUT -s 192.168.30.0/24 -j DROP
  7. # 删除添加了的禁止访问IP
  8. iptables -D INPUT -s 192.168.30.195 -j DROP
  9. # 在第5条规则前,插入禁止除了192.168.30.0/24段的IP 访问80端口
  10. iptables -I INPUT 5 -p tcp --dport 80 ! -s 192.168.30.0/24 -j DROP
  11. # 新增一条规则,只允许除了192.168.0.3的主机外都能进行SSH连接
  12. iptables -A INPUT -p tcp -s ! 192.168.0.3 --dport 22 -j ACCEPT
  13. # 允许特定段端口段的访问
  14. iptables -A INPUT -p udp --dport 1000:2000 -j ACCEPT
  15. iptables -A INPUT -p udp --sport 1000:2000 -j ACCEPT
  16. # 允许多个端口
  17. iptablse -A Filter -p tcp -m multiport --destiination-port
  1. # 查询DOCKER表并显示规则编号
  2. iptables -L DOCKER -n --line-number
  3. # 禁止全部访问DOCKER下的portainer的访问端口
  4. iptables -I DOCKER -p tcp --dport 9000 -j DROP
  5. # 开启指定源(192.168.30.191) 能够访问9000端口
  6. iptables -I DOCKER -s 192.168.30.191 -p tcp --dport 9000 -j ACCEPT
  7. # 替换对应编号的iptables 规则,这里添加了允许访问ip的限制
  8. iptables -R DOCKER 5 -p tcp -m tcp -s 192.168.1.0/24 --dport 9000 -j ACCEPT
  9. ## 容器内访问宿主机的应用端口(23306) - 需要添加对172.18.0.0网段的开发;172.17似乎没起效果。 ##
  10. iptablse -A INPUT -p tcp -s 172.17.0.0/16 -j ACCEPT
  11. iptablse -A INPUT -p tcp -s 172.18.0.0/16 -j ACCEPT
  12. ## 限制Docker中的端口,不给非本机访问 ##
  13. iptables -I DOCKER -p tcp --dport 9000 -j DROP
  14. iptables -I DOCKER -p tcp --dport 6379 -j DROP

4. iptables的规则表

image.png
image.png

说明: · 语法构成 iptables [-I 链名] [-t 表名] [-p 条件] [-j 控制类型] · 参数详解 -A:在链的末尾追加一条规则 -I:在链的开头(或指定序号)插入一条规则 -L:列出所有的规则条目 -n:以数字形式显示规则信息(协议解释成数字) -v:以更详细的方式显示规则信息 —line-numbers:查看规则时,显示规则的序号 -D:删除链内指定序号(或内容)的一条规则 -F:清空所有的规则 -P:为指定的链设置默认规则(一条没有匹配上,按照默认规则走) · 注意事项 不指定表名时,默认指filter表 不指定链名时,默认指定表内的所有链 除非设置链的默认策略,否则必须指定匹配条件 选项、链名、控制类型使用大写字母,其余均为小写 · 数据包的常见控制类型 ACCEPT:允许通过 DROP:直接丢弃,不给出任何回应 REJECT:拒绝通过,必要时会给出提示 LOG:记录日志信息,然后传给下一跳规则继续匹配

  1. # 1. 规则的管理: -A 在末尾添加, -I 在链的开头(指定序列号)插入一条规则
  2. # 2. 不指定表明是,默认是filter表
  3. ## 去filter表中,第一行添加:允许全部的udp通行
  4. iptables -t filter -I INPUT -p udp -j ACCEPT
  5. iptables -I INPUT -p tcp -j ACCEPT

5. 多端口匹配

-m state —state {NEW,ESTATBLISHED,INVALID,RELATED} 指定检测那种状态

iptables -A INPUT -p tcp -m state —state NEW,ESTABLISHED -j ACCEPT iptables -A OUTPUT -p tcp -m state —state NEW,ESTABLISHED -j ACCEPT

-m multiport 指定多端口号 —sport —dport —ports

iptables -A INPUT -p tcp -m multiport —ports 22,80,8080 -j ACCEPT iptables -A OUTPUT -p tcp -m multiport —ports 22,80,8080 -j ACCEPT

-m iprange 指定IP段 —src-range ip-ip —dst-range ip-ip

iptables -A INPUT -p tcp -m iprange —src-range 100.0.0.0/24 —dport 80 -j ACCEPT iptables -A OUTPUT

-m connlimit 连接限定 —comlimit-above # 限定大连接个数 -m limit 现在连接速率,也就是限定匹配数据包的个数 —limit 指定速率 —limit-burst # 峰值速率,最大限定 -m string 按字符串限定 —algo bm|kmp 指定算法bm或kmp —string “STRING” 指定字符串本身

  1. ## 1. 送检的配置
  2. #### INPUT链路
  3. iptablse -I INPUT 1 -p tcp -m multiport --dport 8888,12000,17000,18000 -j ACCEPT
  4. iptablse -I INPUT 2 -p tcp -m multiport --dport 18022,23306,15000:15050 -j ACCEPT
  5. iptables -I INPUT 3 -p tcp -m multiport --dport 6800,6801,10011,10012,10020 -j ACCEPT
  6. iptables -I INPUT 4 -p all -s localhost -j ACCEPT
  7. iptables -I INPUT 5 -p all -s 127.0.0.1 -j ACCEPT
  8. iptables -I INPUT 6 -p all -s 192.168.30.87 -j ACCEPT
  9. iptables -I INPUT 7 -p all -s 192.168.30.76 -j ACCEPT
  10. iptables -A INPUT -p all -s 192.168.30.180 -j ACCEPT
  11. iptables -A INPUT -p all -s 192.168.30.82 -j ACCEPT
  12. iptables -A INPUT -p all -s 172.18.0.0/16 -j ACCEPT
  13. iptables -A INPUT -p all -j DROP
  14. ### Docker这条链路
  15. iptables -I DOCKER -p tcp -m multiport --dport 1883,6379,8083,8972,9000,9001,9081,18083,20016,17000,18000,18081 -j ACCEPT
  16. iptables -I DOCKER 2 -p all -s localhost -j ACCEPT
  17. iptables -I DOCKER 3 -p all -s 172.17.0.0/16 -j ACCEPT
  18. iptables -A DOCKER -p all -j DROP

参考文档:

  1. docker 内的端口配置: http://t.zoukankan.com/caidingyu-p-11077320.html
  2. iptables规则: https://blog.csdn.net/m0_49946916/article/details/109277325
  3. https://www.freeaihub.com/post/107770.html
  4. https://blog.51cto.com/oslibo/1856544