适用场景
需要为 Linux 服务器配置 nftables 防火墙的系统管理员与运维人员。RHEL/CentOS 8+、Debian 11+、Ubuntu 22.04+ 默认或推荐使用 nftables 作为防火墙内核框架,传统 iptables 命令已进入维护模式。本文面向 Web 服务器、数据库服务器等常见业务场景,给出从零到可用的完整规则集。
前置条件
- 操作系统内核 3.13+(nf_tables 模块)
- 已安装 nftables 包:
apt install nftables或yum install nftables - root 权限,且本机有备用登录通道(如带外管理、云控制台 VNC),避免误封 SSH
- 了解本机监听端口:
ss -tlnp
原理说明
nftables 基于内核 nf_tables 框架,用「表(table)-链(chain)-规则(rule)」三级结构组织规则,一条命令即可批量管理,语法比 iptables 更直观。它按协议栈位置分为三类链:prerouting(路由前)、input/output(本机进出)、forward(转发)。nftables 同时支持 IPv4/IPv6 双栈,规则可携带计数器(counter)便于排障,配合 limit 与 ct state 可实现防端口扫描与状态跟踪,替代 iptables 的 state 模块。
操作步骤
1. 创建表与基础链
# 创建表(family 用 inet 同时覆盖 IPv4/IPv6)
nft add table inet filter
# 创建基础链:默认策略 drop,先建链避免误封
nft add chain inet filter input { type filter hook input priority 0; policy accept; }
nft add chain inet filter output { type filter hook output priority 0; policy accept; }
nft add chain inet filter forward { type filter hook forward priority 0; policy drop; }
关键点:先把默认策略设为 accept,待规则全部就位、验证无误后再改为 drop,防止中途失联。
2. 放行回环与已建立连接
nft add rule inet filter input iif lo accept
nft add rule inet filter input ct state established,related accept
3. 放行常用服务端口
nft add rule inet filter input tcp dport 22 accept # SSH
nft add rule inet filter input tcp dport 80 accept # HTTP
nft add rule inet filter input tcp dport 443 accept # HTTPS
nft add rule inet filter input icmp type echo-request limit rate 5/second accept
4. 限流防端口扫描
# 限制单 IP 新建连接速率
nft add rule inet filter input ct state new tcp dport 22 limit rate 5/minute accept
nft add rule inet filter input ct state new tcp dport 80 limit rate 100/minute accept
# 每 IP 每分钟最多 20 次 SYN(防扫描)
nft add rule inet filter input tcp flags syn tcp dport 1-65535 ct state new limit rate 20/minute burst 40 drop
5. 收紧默认策略并保存
# 全部规则就绪后,将 input 默认策略改为 drop
nft chain inet filter input { policy drop; }
# 查看当前完整规则集
nft list ruleset
# 持久化:写入配置文件(Debian/Ubuntu)
nft list ruleset > /etc/nftables.conf
systemctl enable nftables --now
# RHEL 系:将规则写入 /etc/sysconfig/nftables.conf 后同样启用服务
6. 使用脚本化配置便于维护
# /etc/nftables.conf 示例(Debian 风格)
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
iif lo accept
ct state established,related accept
tcp dport 22 accept
tcp dport 80 accept
tcp dport 443 accept
icmp type echo-request limit rate 5/second accept
}
chain output {
type filter hook output priority 0; policy accept;
}
chain forward {
type filter hook forward priority 0; policy drop;
}
}
修改后执行 nft -f /etc/nftables.conf 原子加载,语法错误会整体回滚,不会留下半套规则。
配置验证
# 1. 查看规则与计数器(命中次数一目了然)
nft list ruleset
# 2. 本机放行端口可访问
curl -sI http://127.0.0.1/ | head -3
# 3. 从外部机器验证被拦截端口无响应
nc -zv <服务器IP> 3306
# 4. 验证配置语法(不实际生效)
nft -c -f /etc/nftables.conf
# 5. 重启后规则自动加载
systemctl restart nftables && nft list ruleset | head -5
常见问题
Q1:保存规则后重启服务器规则丢失?
大概率是 nftables 服务未开机自启或配置文件路径不对。Debian 系需执行 systemctl enable nftables,并确认规则写入了 /etc/nftables.conf;RHEL 系写入 /etc/sysconfig/nftables.conf。可用 systemctl status nftables 确认服务状态。
Q2:nft 命令报「Operation not permitted」?
说明当前用户无 CAP_NET_ADMIN 权限,需以 root 或 sudo 执行;若在容器中运行,需确认容器未限制 NET_ADMIN 能力。另外某些云主机安全组规则独立于操作系统防火墙,记得同步检查云控制台的安全组放行策略。
总结
nftables 防火墙配置是 Linux 运维的必备技能。掌握「表-链-规则」三级结构与 limit、ct state 等关键特性后,即可写出既安全又可维护的规则集。核心心法是:先建链放行、再限流、最后收紧默认策略,并用 nft -f 原子加载 + 开机自启保证规则可靠落地。