nftables 新一代防火墙配置教程

nftables 是 Linux 内核中 iptables 的正式继任者,从 Linux 3.13 开始引入,并在 Debian 10、Ubuntu 20.04、CentOS 8 及以后的版本中成为默认的防火墙框架。nftables 统一了 iptables、ip6tables、arptables 和 ebtables 四个工具,提供了更简洁的语法、更好的性能和更强大的功能。本文将详细介绍如何在搬瓦工 VPS 上使用 nftables 配置现代化的防火墙规则。

一、nftables 相比 iptables 的优势

  • 统一框架:一个工具管理 IPv4、IPv6、ARP 和桥接规则。
  • 语法更清晰:接近自然语言的规则描述,支持变量和集合。
  • 性能更好:内核态处理优化,减少规则匹配开销。
  • 原子操作:可以原子地加载整个规则集,避免规则加载期间的安全窗口。
  • 集合与映射:原生支持 IP 地址集合和端口映射,无需额外模块。

二、安装 nftables

apt update
apt install nftables -y
systemctl enable nftables
systemctl start nftables

验证安装:

nft --version
nft list ruleset

三、基本语法

nftables 的层级结构为:表(table)> 链(chain)> 规则(rule)。

3.1 创建表和链

# 创建 inet 类型的表(同时处理 IPv4 和 IPv6)
nft add table inet firewall

# 创建 input 链
nft add chain inet firewall input { type filter hook input priority 0\; policy drop\; }

# 创建 forward 链
nft add chain inet firewall forward { type filter hook forward priority 0\; policy drop\; }

# 创建 output 链
nft add chain inet firewall output { type filter hook output priority 0\; policy accept\; }

3.2 添加规则

# 允许环回
nft add rule inet firewall input iif lo accept

# 允许已建立的连接
nft add rule inet firewall input ct state established,related accept

# 允许 SSH
nft add rule inet firewall input tcp dport 22 accept

# 允许 HTTP/HTTPS
nft add rule inet firewall input tcp dport { 80, 443 } accept

# 允许 ICMP
nft add rule inet firewall input ip protocol icmp accept
nft add rule inet firewall input ip6 nexthdr icmpv6 accept

四、使用配置文件

推荐使用配置文件管理规则,这样可以原子加载整个规则集。创建 /etc/nftables.conf

cat > /etc/nftables.conf <<'EOF'
#!/usr/sbin/nft -f

flush ruleset

# 定义变量
define WAN_IF = eth0
define SSH_PORT = 22
define WEB_PORTS = { 80, 443 }
define TRUSTED_IPS = { 203.0.113.0/24, 198.51.100.0/24 }

table inet firewall {
    chain input {
        type filter hook input priority 0; policy drop;

        # 允许环回
        iif lo accept

        # 允许已建立的连接
        ct state established,related accept

        # 丢弃无效连接
        ct state invalid drop

        # 允许 ICMP/ICMPv6
        ip protocol icmp accept
        ip6 nexthdr icmpv6 accept

        # 允许 SSH(限速)
        tcp dport $SSH_PORT ct state new limit rate 5/minute accept

        # 允许 Web 端口
        tcp dport $WEB_PORTS accept

        # 允许信任 IP 访问所有端口
        ip saddr $TRUSTED_IPS accept

        # 记录被丢弃的包
        limit rate 5/minute log prefix "nftables-dropped: " counter drop
    }

    chain forward {
        type filter hook forward priority 0; policy drop;
        ct state established,related accept
    }

    chain output {
        type filter hook output priority 0; policy accept;
    }
}
EOF

加载配置文件:

nft -f /etc/nftables.conf

五、集合(Sets)的使用

nftables 原生支持集合,无需像 iptables 那样依赖 ipset:

# 创建 IP 黑名单集合
nft add set inet firewall blacklist { type ipv4_addr\; flags timeout\; }

# 添加 IP 到黑名单
nft add element inet firewall blacklist { 192.0.2.1, 192.0.2.2, 198.51.100.0/24 }

# 添加带超时的 IP
nft add element inet firewall blacklist { 203.0.113.5 timeout 1h }

# 在规则中引用集合
nft add rule inet firewall input ip saddr @blacklist drop

六、NAT 配置

table ip nat {
    chain prerouting {
        type nat hook prerouting priority -100;

        # 端口转发:外部 8080 转到内部 80
        tcp dport 8080 dnat to 192.168.1.10:80
    }

    chain postrouting {
        type nat hook postrouting priority 100;

        # MASQUERADE
        oifname "eth0" masquerade

        # 或使用 SNAT
        # oifname "eth0" snat to 你的VPS公网IP
    }
}

七、速率限制与防护

# 限制新 SSH 连接
tcp dport 22 ct state new limit rate 3/minute accept

# 限制每 IP 并发连接数
tcp dport 80 meter http-limit { ip saddr limit rate over 50/second } drop

# SYN Flood 防护
tcp flags syn limit rate 30/second burst 50 packets accept

八、从 iptables 迁移

nftables 提供了迁移工具,可以将现有 iptables 规则转换为 nftables 格式:

# 导出当前 iptables 规则为 nftables 格式
iptables-save > /tmp/iptables.rules
iptables-restore-translate -f /tmp/iptables.rules > /etc/nftables.conf

常见的语法对照:

# iptables
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

# nftables
nft add rule inet firewall input tcp dport 80 accept

九、常用管理命令

# 查看当前所有规则
nft list ruleset

# 查看特定表
nft list table inet firewall

# 查看规则(带句柄号)
nft -a list chain inet firewall input

# 删除指定规则(通过句柄号)
nft delete rule inet firewall input handle 15

# 清空所有规则
nft flush ruleset

# 导出规则
nft list ruleset > /etc/nftables.conf

十、规则持久化

# 保存当前规则
nft list ruleset > /etc/nftables.conf

# 确认 systemd 服务已启用
systemctl enable nftables

nftables 服务在启动时会自动加载 /etc/nftables.conf 中的规则。

十一、常见问题

nftables 和 iptables 冲突

两者可以共存但不建议同时使用。迁移到 nftables 后,建议完全移除 iptables 规则:

iptables -F
iptables -X

Docker 与 nftables 兼容性

Docker 默认使用 iptables 管理网络规则。如果使用 nftables,需要安装 iptables-nft 兼容层,或在 Docker 配置中禁用 iptables 管理并手动维护 NAT 规则。

总结

nftables 是 Linux 防火墙的未来方向,语法更清晰,功能更强大,性能更出色。在搬瓦工 VPS 上使用 nftables 可以构建现代化的网络安全策略。如果你还在使用 iptables,建议参考 iptables 高级规则教程 并逐步迁移到 nftables。购买搬瓦工 VPS 请查看 全部方案,使用优惠码 NODESEEK2026 可享 6.77% 折扣,购买链接:bwh81.net

关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工的方案、优惠和教程。我们不销售主机,不提供技术服务。

新手必读
搬瓦工优惠码

NODESEEK2026(优惠 6.77%)

购买时填入即可抵扣。