Skip to the content.

[iptables]Series 1: Principles and Usage of Linux Firewall

发表于 2025-03-29

image

Linux 防火墙的原理与iptables用法

在使用 Linux 搭建服务器或网关的过程中,安全防护是重中之重。 其中,最常见的安全工具之一就是 —— iptables。

1. 什么是 iptables?

iptables 是 Linux 内核中 Netfilter 框架的用户空间控制工具,用于设置、维护和检查 Linux 内核中的 IP 数据包过滤规则表。 可以定义几个不同的表。每个表包含许多内置链,也可能包含用户定义的链。每个链都是一个可以匹配一组数据包的规则列表。 每个规则指定如何处理匹配的数据包。这称为“目标”,可能是跳转到同一表中的用户定义链。

通俗来说:它就是 Linux 系统自带的防火墙,负责控制哪些网络数据包可以进、可以出、该拦截还是放行,甚至可以改地址、端口做 NAT。

2. iptables的基本原理

iptables 的核心概念包括:

a. 基础术语

术语 含义
表(table) 分类不同用途的规则,如 filternatmangle
链(chain) 表中定义的规则链,如 INPUTOUTPUTFORWARD
规则(rule) 每条判断条件 + 动作组合,如“如果来自某IP就 DROP”
动作(target) 匹配后的操作,如 ACCEPTDROPSNATDNAT

b. 五个常用链

链名 作用
PREROUTING 控制刚进入本机,还未做路由决定的数据包
INPUT 管控入站数据包(发给本机)
FORWARD 控制经过本机但不终止的数据包(转发)
OUTPUT 控制本机发出的数据包
POSTROUTING 控制路由决定之后,即将离开本机的数据包

3. iptables 基础语法和用法

a. 查看现有规则

[root@xqjcool ~]# iptables -nvL    //默认查看的是filter表
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination 

[root@xqjcool ~]# iptables -t mangle -nvL     //查看mangle表的规则
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination 

b. 配置默认策略

iptables -P INPUT DROP  //将 INPUT 链的默认策略设置为丢弃。(白名单策略,默认丢弃,需要配置白名单)
iptables -P FORWARD ACCEPT  //将 FORWARD 链的默认策略设置为允许(黑名单策略,默认允许,需要配置黑名单)。

c. 新增规则

append附加到现有链最后

iptables -A INPUT -s 1.2.3.4 -j DROP    //append到filter表 INPUT链的最后。把来自 IP 为 1.2.3.4 的数据包丢弃。
iptables -A INPUT -i lo -j ACCEPT      //允许回环接口流量(本地通信)
iptables -A INPUT -p tcp --dport 22 -j ACCEPT  //允许访问 SSH
[root@xqjcool ~]# iptables -vnL
Chain INPUT (policy DROP 125K packets, 6402K bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 DROP       all  --  *      *       1.2.3.4              0.0.0.0/0
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
99917   16M ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination    

insert插入到指定位置。如果没有指定序号,则默认插入到最前。

[root@xqjcool ~]# iptables -I INPUT 2 -d 5.6.7.8 -p udp -j DROP    //插入到序号2,原来的序号2以及后面的规则整体后移
[root@xqjcool ~]# iptables -nvL
Chain INPUT (policy DROP 125K packets, 6403K bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 DROP       all  --  *      *       1.2.3.4              0.0.0.0/0
    0     0 DROP       udp  --  *      *       0.0.0.0/0            5.6.7.8                          //插入到序号2的位置
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
 100K   16M ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

d. 删除规则

按配置删除规则

iptables -D INPUT -s 1.2.3.4 -j DROP

按序号删除规则

[root@xqjcool ~]# iptables -nvL --line-numbers
Chain INPUT (policy DROP 125K packets, 6409K bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 DROP       udp  --  *      *       0.0.0.0/0            5.6.7.8
2        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
3     101K   16M ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
[root@xqjcool ~]# iptables -D INPUT 1

e. 清空规则

清空指定链

iptables -F INPUT
iptables -t mange -F INPUT

清空所有链规则

iptables -F
iptables -t nat -F

f. 新建用户自定义规则链

[root@xqjcool ~]# iptables -N my-chain
[root@xqjcool ~]# iptables -A my-chain -p tcp --dport 80 -j ACCEPT
[root@xqjcool ~]# iptables -A INPUT -j my-chain

查看配置的规则

[root@xqjcool ~]# iptables -nvL
Chain INPUT (policy DROP 125K packets, 6421K bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
 103K   16M ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22
    1    44 my-chain   all  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain my-chain (1 references)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80

g. 清空用户自定义规则链

[root@xqjcool ~]# iptables -F my-chain
[root@xqjcool ~]# iptables -nvL
Chain INPUT (policy DROP 125K packets, 6424K bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
 104K   16M ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22
   49  2631 my-chain   all  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain my-chain (1 references)
 pkts bytes target     prot opt in     out     source               destination

h. 删除用户自定义规则链

删除规则链之前,需要将对应的索引先删除

[root@xqjcool ~]# iptables -D INPUT 3
[root@xqjcool ~]# iptables -X my-chain

如果没有被索引,可以用下列命令删除所有自定义规则链

iptables -X

i. 保存iptables配置和恢复

iptables-save > iptables-backup.txt   //保存
iptables-restore < iptables-backup.txt   //恢复

j. iptables观察和调试

iptables -A INPUT -d 4.3.2.1              //用于追踪packet, 无target效果等同于 -j RETURN
iptables -A INPUT -d 4.3.2.1 -j RETURN
iptables -A INPUT -d 4.3.2.1 -j LOG       //匹配后会写入内核日志,用于调试

4. 常用场景配置

a. 拒绝外部访问,仅允许 SSH

iptables -P INPUT DROP
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

b. 开启NAT转发(源地址伪装)

echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

c. 限速限制连接频率

iptables -A INPUT -p tcp --dport 22 -m limit --limit 3/sec -j ACCEPT

5. 关于置顶示意图

这个示意图版本较老,新版本内核在INPUT链的fitler之后添加了nat处理。

本文访问次数:...