iptables防火墙
参考:
man iptables
语法
iptables(选项)(参数)
选项
-
t<表>:指定要操纵的表;
-
A:向规则链中添加条目;
-
D:从规则链中删除条目;
-
i:向规则链中插入条目;
-
R:替换规则链中的条目;
-
L:显示规则链中已有的条目;
-
F:清楚规则链中已有的条目;
-
Z:清空规则链中的数据包计算器和字节计数器;
-
N:创建新的用户自定义规则链;
-
P:定义规则链中的默认目标;
-
h:显示帮助信息;
-
p:指定要匹配的数据包协议类型;
-
s:指定要匹配的数据包源ip地址;
-
j<目标>:指定要跳转的目标;
-
i<网络接口>:指定数据包进入本机的网络接口;
-
o<网络接口>:指定数据包要离开本机所使用的网络接口。
iptables命令选项输入顺序:
iptables -t 表名
<-A/I/D/R> 规则链名 [规则号]
<-i/o 网卡名> -p 协议名
<-s 源IP/源子网> --sport 源端口
<-d 目标IP/目标子网> --dport 目标端口 -j 动作
开放指定的端口
iptables -A INPUT
-s 127.0.
0.1 -d 127.0.
0.1 -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -j ACCEPT
iptables -A INPUT -p tcp --dport
80 -j ACCEPT
iptables -A INPUT -p tcp --dport
21 -j ACCEPT
iptables -A INPUT -p tcp --dport
20 -j ACCEPT
iptables -A INPUT -j reject
iptables -A FORWARD -j REJECT
查看防火墙规则
iptables
-L -n -v
Chain INPUT (policy ACCEPT
0 packets,
0 bytes)
pkts
bytes target prot opt
in out source destination
0 0 ACCEPT
all -- * * 0.0.0.0/
0 0.0.0.0/
0 state RELATED,ESTABLISHED
0 0 ACCEPT icmp
-- * * 0.0.0.0/
0 0.0.0.0/
0
0 0 ACCEPT
all -- lo
* 0.0.0.0/
0 0.0.0.0/
0
0 0 ACCEPT tcp
-- * * 0.0.0.0/
0 0.0.0.0/
0 state
NEW tcp dpt:
22
0 0 REJECT
all -- * * 0.0.0.0/
0 0.0.0.0/
0 reject
-with icmp
-host-prohibited
Chain FORWARD (policy ACCEPT
0 packets,
0 bytes)
pkts
bytes target prot opt
in out source destination
0 0 REJECT
all -- * * 0.0.0.0/
0 0.0.0.0/
0 reject
-with icmp
-host-prohibited
Chain OUTPUT (policy ACCEPT
0 packets,
0 bytes)
pkts
bytes target prot opt
in out source destination
删除已添加的iptables规则
将所有iptables以序号标记显示,执行:
iptables -L -n --line-numbers
比如要删除INPUT里序号为8的规则,执行:
iptables -D INPUT 8