Linux nftables防火墙规则链配置与性能优化实战指南

Linux nftables作为iptables的继任者,从内核3.13版本开始引入并在近年的发行版中成为默认防火墙后端。nftables通过统一的nft命令行工具替代iptables/ip6tables/ebtables/arptables多套工具,提供更高效的规则处理引擎和更灵活的语法结构。服务器安全加固场景下,nftables的集合(set)、映射(map)和字典(dictionary)特性大幅简化规则维护工作。

nftables表与链的层次结构与基础配置

nftables采用table-chain-rule三级结构。table是顶层容器,按地址族(inet、ip、ip6、arp、bridge、netdev)划分。chain分为基链(base chain)和常规链(regular chain),基链挂载到内核网络栈的特定钩子点,常规链通过jump跳转调用。

# 创建inet族表和基链
nft add table inet firewall
nft 'add chain inet firewall input { type filter hook input priority 0; policy drop; }'
nft 'add chain inet firewall forward { type filter hook forward priority 0; policy drop; }'
nft 'add chain inet firewall output { type filter hook output priority 0; policy accept; }'

# 允许已建立连接和相关连接通过
nft add rule inet firewall input ct state established,related accept
# 允许回环接口通信
nft add rule inet firewall input iifname "lo" accept
# 允许SSH和HTTP/HTTPS
nft add rule inet firewall input tcp dport { 22, 80, 443 } accept
# 允许ICMP(ping)
nft add rule inet firewall input ip protocol icmp accept
# 记录被丢弃的数据包
nft add rule inet firewall input limit rate 10/minute log prefix "nft-drop: "

priority值决定链的执行顺序,filter表默认优先级为0。policy drop设置为默认拒绝策略,遵循最小权限原则。inet地址族同时处理IPv4和IPv6流量,避免重复编写两套规则。

集合与映射简化批量规则管理

nftables的set和map特性是相对iptables的显著优势。将IP地址列表或端口列表定义为集合后,单条规则即可匹配整个集合,内核内部使用哈希表或红黑树实现,查找效率远高于iptables的线性规则遍历。

# 定义IP地址集合
nft add set inet firewall allowed_ips { type ipv4_addr; flags interval; }
nft add element inet firewall allowed_ips { 10.0.0.0/8, 192.168.0.0/16 }

# 定义端口集合
nft add set inet firewall web_ports { type inet_service; }
nft add element inet firewall web_ports { 80, 443, 8080, 8443 }

# 使用集合匹配规则
nft add rule inet firewall input ip saddr @allowed_ips tcp dport @web_ports accept

# 使用映射(map)实现端口转发
nft add map inet firewall port_map { type inet_service: inet_service; }
nft add element inet firewall port_map { 8080: 80, 8443: 443 }
nft add rule inet firewall prerouting dnat tcp dport map @port_map

集合的flags interval参数支持CIDR网段表示法。集合操作是原子的,修改集合内容时无需重载整个规则集,配合nft -f批量加载配置文件可实现零中断规则更新。

连接跟踪与状态检测配置

ct(conntrack)模块提供基于连接状态的过滤能力,是防火墙规则的核心组件。ct state established匹配已建立连接的后续数据包,related匹配与已有连接关联的数据包(如FTP数据通道、ICMP错误消息),new匹配连接发起的首包,invalid匹配无法识别状态的数据包。

# 连接跟踪规则链
nft 'add chain inet firewall ct_chain { type filter hook input priority -10; }'

# 丢弃无效连接(优先于其他规则执行)
nft add rule inet firewall ct_chain ct state invalid drop
nft add rule inet firewall ct_chain ct state established,related accept

# 限制新连接速率,防止端口扫描和SYN Flood
nft add rule inet firewall input tcp flags semisyn / syn,rst,ack,fin     limit rate 50/second burst 100 packets accept

# per-source限速:单IP每分钟最多20个新SSH连接
nft add set inet firewall ssh_recent { type ipv4_addr; flags timeout; timeout 1m; }
nft add rule inet firewall input tcp dport 22 ct state new     add @ssh_recent { ip saddr limit rate 20/minute } accept

连接跟踪表大小通过net.netfilter.nf_conntrack_max内核参数调整,高并发服务器建议设为1048576以上。连接超时时间根据业务场景缩短,nf_conntrack_tcp_timeout_established默认5天过长,Web服务器设置为一小时可释放无效表项。

规则持久化与性能调优实践

规则持久化通过nft list ruleset导出为配置文件,systemd-nftables或iptables-persistent服务在开机时自动加载:

# 导出当前规则集到配置文件
nft list ruleset > /etc/nftables.conf

# 配置文件示例结构
cat > /etc/nftables.conf << 'EOF'
#!/usr/sbin/nft -f
flush ruleset

table inet firewall {
    set allowed_ips { type ipv4_addr; flags interval;
        elements = { 10.0.0.0/8, 192.168.0.0/16 }
    }
    chain input {
        type filter hook input priority 0; policy drop;
        ct state invalid drop
        ct state established,related accept
        iifname "lo" accept
        ip saddr @allowed_ips tcp dport { 22, 80, 443 } accept
        ip protocol icmp accept
        limit rate 10/minute log prefix "nft-drop: "
    }
}
EOF

# 启用开机自动加载
systemctl enable nftables.service

性能层面,nftables规则按优先级顺序执行,命中频率高的规则应排在前面以减少平均匹配次数。set和map查询复杂度为O(1)或O(log n),远优于线性规则匹配。对于上万条IP黑名单场景,使用集合配合interval标志可在微秒级完成查找,而iptables在此规模下会出现明显的CPU开销。

规则排查使用nft monitor命令实时监控规则变更和匹配计数,nft list chain inet firewall input可查看单条链的完整规则。生产环境修改规则前建议先用nft -c -f config.conf做语法检查,避免规则语法错误导致网络中断。

原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/linuxnftables-fang-huo-qiang-gui-ze-lian-pei-zhi-yu-xing/

(0)
小编小编
上一篇 2小时前
下一篇 2小时前

相关推荐