iptables的结构:iptables -> Tables -> Chains -> Rules. 简单地讲,tables由chains组成,而chains又由rules组成。
表.png
防火墙的4种表:
nat 表
filter表
mangle表
raw表
iptables -nL 默认列出filter表的链及规则, -t指定要列出表的信息:
iptables -nL
iptables -nL -t nat ---------(列出nat表的信息)
iptables-nL -t nat --line-numbers -----------(列出带规则号的信息)
iptables -nL eth3.local ----------(列出eth3.local链的信息,也可以接INPUT, OUTPUT,)
root@vyos:/home/vyos# iptables -nL eth3.local
Chain eth3.local (1 references)
target prot opt source destination
RETURN all -- 0.0.0.0/0 10.1.35.1 /* eth3.local-1 */ state RELATED,ESTABLISHED
RETURN icmp -- 0.0.0.0/0 10.1.35.1 /* eth3.local-2 */
REJECT tcp -- 0.0.0.0/0 10.1.35.1 /* eth3.local-3 */ tcp dpt:22 reject-with icmp-port-unreachable
RETURN tcp -- 0.0.0.0/0 0.0.0.0/0 /* eth3.local-4 */ tcp dpt:53
RETURN udp -- 0.0.0.0/0 0.0.0.0/0 /* eth3.local-4 */ udp dpt:53
RETURN tcp -- 0.0.0.0/0 172.24.3.2 /* eth3.local-5 */ tcp dpt:3333
REJECT all -- 0.0.0.0/0 0.0.0.0/0 /* eth3.local-10000 default-action reject */ reject-with icmp-port-unreachable
root@vyos:/home/vyos# iptables -t filter -nL INPUT
Chain INPUT (policy ACCEPT)
target prot opt source destination
VYATTA_PRE_FW_IN_HOOK all -- 0.0.0.0/0 0.0.0.0/0
VYATTA_FW_LOCAL_HOOK all -- 0.0.0.0/0 0.0.0.0/0
VYATTA_POST_FW_IN_HOOK all -- 0.0.0.0/0 0.0.0.0/0
增删改查规则 -A追加规则,-I插入规则, -D 删除规则:
#示例如下
iptables -t filter -I OUTPUT -d 192.168.1.146 -p udp -m multiport --sports 137,138 -j REJECT
iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport --dports 22,80 -j REJECT
iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport ! --dports 22,80 -j REJECT
iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport --dports 80:88 -j REJECT
iptables -t filter -I INPUT -s 192.168.1.146 -p tcp -m multiport --dports 22,80:88 -j REJECT
#示例如下
iptables -t filter -I INPUT -s 192.168.1.111,192.168.1.118 -j DROP
iptables -t filter -I INPUT -s 192.168.1.0/24 -j ACCEPT
iptables -t filter -I INPUT ! -s 192.168.1.0/24 -j ACCEPT
#示例如下
iptables -t filter -I OUTPUT -d 192.168.1.111,192.168.1.118 -j DROP
iptables -t filter -I INPUT -d 192.168.1.0/24 -j ACCEPT
iptables -t filter -I INPUT ! -d 192.168.1.0/24 -j ACCEPT
命令语法:iptables -t 表名 -A 链名 匹配条件 -j 动作
示例:iptables -t filter -A INPUT -s 192.168.1.146 -j DROP
命令语法:iptables -t 表名 -I 链名 匹配条件 -j 动作
示例:iptables -t filter -I INPUT -s 192.168.1.146 -j ACCEPT
命令语法:iptables -t 表名 -I 链名 规则序号 匹配条件 -j 动作
示例:iptables -t filter -I INPUT 5 -s 192.168.1.146 -j REJECT
命令语法:iptables -t 表名 -P 链名 动作
示例:iptables -t filter -P FORWARD ACCEPT
命令语法:iptables -t 表名 -D 链名 规则序号
示例:iptables -t filter -D INPUT 3