配置规则: — 命令
- 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操作
# 查看配置的规则
iptables --list | -L
# 清除iptables配置的规则
iptables --flush | -F
# 显示规则对相应的比那好
iptables -L -n --line-number
# 删除Chain:INPUT里面的第二条规则
iptables -D INPUT 2
2.端口和协议的规则
规则说明:
- 端口—必须更协议 成对出现
- -p tcp | icmp | udp | all
- —dport 目标端口、—dport 目标端口1 : 目标端口2 ; —sport 源端口、 —sport 源端口1 : 源端口2
- -i :入站网口; -o :出站网口
- 多端口匹配:
- 多端口配置:-m multiport —sport 源端口列表
- 多端口配置:-m multiport —dport 目的端口列表
- IP范围匹配:-m iprange —src-range IP范围
- MAC地址匹配:-m mac —macl-source MAC地址
- 状态匹配:-m state —state 连接状态
- 目标动作(target)
- ACCEPT:允许通过
- DROP:直接丢弃,不给出任何回应
- REJECT:拒绝通过,必要时会给出提示
- LOG:记录日志信息,然后传给下一条规则继续匹配
- SNAT:修改数据包源地址
- DNAT:修改数据包目的地址
- REDIRECT:重定向
## A追加,I前面插入,D删除,R替换,N新建一个自定义链 ##
## -i(interface)指定网络接口; -s(source)来源; -j(jump)执行 ##
# 添加限制指定IP来源的访问
iptables -I INPUT -s 192.168.30.195 -j DROP
# 添加限制指定IP段的访问
iptables -I INPUT -s 192.168.30.0/24 -j DROP
# 删除添加了的禁止访问IP
iptables -D INPUT -s 192.168.30.195 -j DROP
# 在第5条规则前,插入禁止除了192.168.30.0/24段的IP 访问80端口
iptables -I INPUT 5 -p tcp --dport 80 ! -s 192.168.30.0/24 -j DROP
# 新增一条规则,只允许除了192.168.0.3的主机外都能进行SSH连接
iptables -A INPUT -p tcp -s ! 192.168.0.3 --dport 22 -j ACCEPT
# 允许特定段端口段的访问
iptables -A INPUT -p udp --dport 1000:2000 -j ACCEPT
iptables -A INPUT -p udp --sport 1000:2000 -j ACCEPT
# 允许多个端口
iptablse -A Filter -p tcp -m multiport --destiination-port
# 查询DOCKER表并显示规则编号
iptables -L DOCKER -n --line-number
# 禁止全部访问DOCKER下的portainer的访问端口
iptables -I DOCKER -p tcp --dport 9000 -j DROP
# 开启指定源(192.168.30.191) 能够访问9000端口
iptables -I DOCKER -s 192.168.30.191 -p tcp --dport 9000 -j ACCEPT
# 替换对应编号的iptables 规则,这里添加了允许访问ip的限制
iptables -R DOCKER 5 -p tcp -m tcp -s 192.168.1.0/24 --dport 9000 -j ACCEPT
## 容器内访问宿主机的应用端口(23306) - 需要添加对172.18.0.0网段的开发;172.17似乎没起效果。 ##
iptablse -A INPUT -p tcp -s 172.17.0.0/16 -j ACCEPT
iptablse -A INPUT -p tcp -s 172.18.0.0/16 -j ACCEPT
## 限制Docker中的端口,不给非本机访问 ##
iptables -I DOCKER -p tcp --dport 9000 -j DROP
iptables -I DOCKER -p tcp --dport 6379 -j DROP
4. iptables的规则表
说明: · 语法构成 iptables [-I 链名] [-t 表名] [-p 条件] [-j 控制类型] · 参数详解 -A:在链的末尾追加一条规则 -I:在链的开头(或指定序号)插入一条规则 -L:列出所有的规则条目 -n:以数字形式显示规则信息(协议解释成数字) -v:以更详细的方式显示规则信息 —line-numbers:查看规则时,显示规则的序号 -D:删除链内指定序号(或内容)的一条规则 -F:清空所有的规则 -P:为指定的链设置默认规则(一条没有匹配上,按照默认规则走) · 注意事项 不指定表名时,默认指filter表 不指定链名时,默认指定表内的所有链 除非设置链的默认策略,否则必须指定匹配条件 选项、链名、控制类型使用大写字母,其余均为小写 · 数据包的常见控制类型 ACCEPT:允许通过 DROP:直接丢弃,不给出任何回应 REJECT:拒绝通过,必要时会给出提示 LOG:记录日志信息,然后传给下一跳规则继续匹配
# 1. 规则的管理: -A 在末尾添加, -I 在链的开头(指定序列号)插入一条规则
# 2. 不指定表明是,默认是filter表
## 去filter表中,第一行添加:允许全部的udp通行
iptables -t filter -I INPUT -p udp -j ACCEPT
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. 送检的配置
#### INPUT链路
iptablse -I INPUT 1 -p tcp -m multiport --dport 8888,12000,17000,18000 -j ACCEPT
iptablse -I INPUT 2 -p tcp -m multiport --dport 18022,23306,15000:15050 -j ACCEPT
iptables -I INPUT 3 -p tcp -m multiport --dport 6800,6801,10011,10012,10020 -j ACCEPT
iptables -I INPUT 4 -p all -s localhost -j ACCEPT
iptables -I INPUT 5 -p all -s 127.0.0.1 -j ACCEPT
iptables -I INPUT 6 -p all -s 192.168.30.87 -j ACCEPT
iptables -I INPUT 7 -p all -s 192.168.30.76 -j ACCEPT
iptables -A INPUT -p all -s 192.168.30.180 -j ACCEPT
iptables -A INPUT -p all -s 192.168.30.82 -j ACCEPT
iptables -A INPUT -p all -s 172.18.0.0/16 -j ACCEPT
iptables -A INPUT -p all -j DROP
### Docker这条链路
iptables -I DOCKER -p tcp -m multiport --dport 1883,6379,8083,8972,9000,9001,9081,18083,20016,17000,18000,18081 -j ACCEPT
iptables -I DOCKER 2 -p all -s localhost -j ACCEPT
iptables -I DOCKER 3 -p all -s 172.17.0.0/16 -j ACCEPT
iptables -A DOCKER -p all -j DROP
参考文档: