引用
iptables 命令,Linux iptables 命令详解:Linux上常用的防火墙软件 - Linux 命令搜索引擎
常用命令
列出规则
# 查看所有规则 ,-n 表示不解析主机名与服务名称
# 不用 -v 的话,有一些针对不同网卡的操作,区分不出来
# 使用 --line-numbers 列出序号,方便进行删除
sudo iptables -L -v -n --line-numbers
我解析的例子
sudo iptables -L -v
# INPUT 进,默认是 ACCEPT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
# 没有规则
# FORWARD 转发, 默认是 DROP
Chain FORWARD (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
# 所有先走 DOCKER-USER 规则, 具体规则下面会列出
0 0 DOCKER-USER all -- any any anywhere anywhere
# 然后再走 DOCKER-ISOLATION-STAGE-1 规则, 具体规则下面会列出
0 0 DOCKER-ISOLATION-STAGE-1 all -- any any anywhere anywhere
# 源是 docker0 发出已经建立连接的数据(ctstate RELATED,ESTABLISHED)接受
0 0 ACCEPT all -- any docker0 anywhere anywhere ctstate RELATED,ESTABLISHED
# 源是 docker0 发出的的数据再走一次 DOCKER 规则
0 0 DOCKER all -- any docker0 anywhere anywhere
# 源是 docker0 发出的的数据接收为非 docker0 的接受
0 0 ACCEPT all -- docker0 !docker0 anywhere anywhere
# 源是 docker0 发出的的数据接收为 docker0 的接受
0 0 ACCEPT all -- docker0 docker0 anywhere anywhere
# OUTPUT 出,默认是 ACCEPT
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
# DOCKER 规则为空,AI 说也是直接返回上一层
Chain DOCKER (1 references)
pkts bytes target prot opt in out source destination
# 下面的 DOCKER-ISOLATION-STAGE-* 为了隔离在不同的bridge网络之间的容器, 我没太看懂
# DOCKER-ISOLATION-STAGE-1 规则,docker0 给非 docker0 的交给 DOCKER-ISOLATION-STAGE-2 处理
# 其它的 RETURN ,交回给上级处理
Chain DOCKER-ISOLATION-STAGE-1 (1 references)
pkts bytes target prot opt in out source destination
0 0 DOCKER-ISOLATION-STAGE-2 all -- docker0 !docker0 anywhere anywhere
0 0 RETURN all -- any any anywhere anywhere
# DOCKER-ISOLATION-STAGE-2 docker0 发出的,都丢弃
# 其它的 RETURN ,交回给上级处理
Chain DOCKER-ISOLATION-STAGE-2 (1 references)
pkts bytes target prot opt in out source destination
0 0 DROP all -- any docker0 anywhere anywhere
0 0 RETURN all -- any any anywhere anywhere
# DOCKER-USER 所有的都交给上级处理
Chain DOCKER-USER (1 references)
pkts bytes target prot opt in out source destination
0 0 RETURN all -- any any anywhere anywhere
一次操作
sudo iptables -L -v -n --line-numbers
# 允许 22 进出-A 附加
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
# 允许 80 进出
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 80 -j ACCEPT
# 本地回环地址 127.0.0.1 允许 -I 在行首插入, 所有对 127.0.0.1 的访问都允许
iptables -I INPUT -i lo -j ACCEPT
iptables -I OUTPUT -o lo -j ACCEPT
# 默认进出 DROP, 注: RETURN 对方有响应,DROP 对方无响应 -P 默认
iptables -P INPUT DROP
iptables -P OUTPUT DROP