解决 Linux nftables 报错 No such file or directory 规则集语法修复
排查应用 nftables 规则时抛出 Error: Could not process rule: No such file or directory,核对表与链的父级依赖关系。
问题现象
执行 nft -f /etc/nftables.conf 加载防火墙配置时报错中止:
/etc/nftables.conf:15:1-35: Error: Could not process rule: No such file or directory
add rule inet my_filter input accept
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
触发根因
在 nftables 框架中,所有规则(Rule)必须严格依附于特定的链(Chain),而链必须严格依附于特定的表(Table)。如果配置文件在尝试 add rule 时,上方尚未显式声明创建 table inet my_filter 或 chain inet my_filter input,内核由于找不到对应的父级数据结构,直接返回 ENOENT (No such file or directory)。
解决步骤
1. 规范配置文件中 Table、Chain、Rule 的完整树状结构
编辑 /etc/nftables.conf,先建表、再建链、最后挂载规则:
#!/usr/sbin/nft -f
# 1. 清除旧规则
flush ruleset
# 2. 创建表
table inet my_filter {
# 3. 创建基础链并关联 hook
chain input {
type filter hook input priority 0; policy drop;
# 4. 写入具体规则
ct state established,related accept
iif "lo" accept
tcp dport { 22, 80, 443 } accept
}
chain forward {
type filter hook forward priority 0; policy drop;
}
chain output {
type filter hook output priority 0; policy accept;
}
}
2. 使用静态校验参数检查语法
在正式生效前使用 -c 进行无损测试:
sudo nft -c -f /etc/nftables.conf
验证确认
重载规则集并查看:
sudo nft -f /etc/nftables.conf
sudo nft list ruleset
控制台成功打印结构清晰的规则集,防火墙稳定接管。


