Linux 防火墙:nftables 与iptables 兼容层

端口「本机 curl 通、外网不通」、Docker 改表导致规则错乱,需要先分清 nftables 原生集合iptables-nft 兼容,再谈策略。

源码锚点

路径 / 手册 作用
man nft nftables 命令
man iptables 兼容前端
/etc/nftables.conf 常见落盘
man nftables 集合概念

放行本机 SSH 示例(示意):

bash 复制代码
nft add table inet filter
nft add chain inet filter input '{ type filter hook input priority 0; policy drop; }'
nft add rule inet filter input ct state established,related accept
nft add rule inet filter input iif lo accept
nft add rule inet filter input tcp dport 22 accept

调用链

#mermaid-svg-cgelA9lhcxHpPdBO{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-cgelA9lhcxHpPdBO .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-cgelA9lhcxHpPdBO .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-cgelA9lhcxHpPdBO .error-icon{fill:#552222;}#mermaid-svg-cgelA9lhcxHpPdBO .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-cgelA9lhcxHpPdBO .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-cgelA9lhcxHpPdBO .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-cgelA9lhcxHpPdBO .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-cgelA9lhcxHpPdBO .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-cgelA9lhcxHpPdBO .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-cgelA9lhcxHpPdBO .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-cgelA9lhcxHpPdBO .marker{fill:#333333;stroke:#333333;}#mermaid-svg-cgelA9lhcxHpPdBO .marker.cross{stroke:#333333;}#mermaid-svg-cgelA9lhcxHpPdBO svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-cgelA9lhcxHpPdBO p{margin:0;}#mermaid-svg-cgelA9lhcxHpPdBO .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-cgelA9lhcxHpPdBO .cluster-label text{fill:#333;}#mermaid-svg-cgelA9lhcxHpPdBO .cluster-label span{color:#333;}#mermaid-svg-cgelA9lhcxHpPdBO .cluster-label span p{background-color:transparent;}#mermaid-svg-cgelA9lhcxHpPdBO .label text,#mermaid-svg-cgelA9lhcxHpPdBO span{fill:#333;color:#333;}#mermaid-svg-cgelA9lhcxHpPdBO .node rect,#mermaid-svg-cgelA9lhcxHpPdBO .node circle,#mermaid-svg-cgelA9lhcxHpPdBO .node ellipse,#mermaid-svg-cgelA9lhcxHpPdBO .node polygon,#mermaid-svg-cgelA9lhcxHpPdBO .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-cgelA9lhcxHpPdBO .rough-node .label text,#mermaid-svg-cgelA9lhcxHpPdBO .node .label text,#mermaid-svg-cgelA9lhcxHpPdBO .image-shape .label,#mermaid-svg-cgelA9lhcxHpPdBO .icon-shape .label{text-anchor:middle;}#mermaid-svg-cgelA9lhcxHpPdBO .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-cgelA9lhcxHpPdBO .rough-node .label,#mermaid-svg-cgelA9lhcxHpPdBO .node .label,#mermaid-svg-cgelA9lhcxHpPdBO .image-shape .label,#mermaid-svg-cgelA9lhcxHpPdBO .icon-shape .label{text-align:center;}#mermaid-svg-cgelA9lhcxHpPdBO .node.clickable{cursor:pointer;}#mermaid-svg-cgelA9lhcxHpPdBO .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-cgelA9lhcxHpPdBO .arrowheadPath{fill:#333333;}#mermaid-svg-cgelA9lhcxHpPdBO .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-cgelA9lhcxHpPdBO .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-cgelA9lhcxHpPdBO .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-cgelA9lhcxHpPdBO .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-cgelA9lhcxHpPdBO .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-cgelA9lhcxHpPdBO .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-cgelA9lhcxHpPdBO .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-cgelA9lhcxHpPdBO .cluster text{fill:#333;}#mermaid-svg-cgelA9lhcxHpPdBO .cluster span{color:#333;}#mermaid-svg-cgelA9lhcxHpPdBO div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-cgelA9lhcxHpPdBO .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-cgelA9lhcxHpPdBO rect.text{fill:none;stroke-width:0;}#mermaid-svg-cgelA9lhcxHpPdBO .icon-shape,#mermaid-svg-cgelA9lhcxHpPdBO .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-cgelA9lhcxHpPdBO .icon-shape p,#mermaid-svg-cgelA9lhcxHpPdBO .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-cgelA9lhcxHpPdBO .icon-shape .label rect,#mermaid-svg-cgelA9lhcxHpPdBO .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-cgelA9lhcxHpPdBO .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-cgelA9lhcxHpPdBO .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-cgelA9lhcxHpPdBO :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} accept
drop/reject
网卡收包
Netfilter hooks
nftables 规则集
协议栈
丢弃/重置

重点知识

查清当前用哪套

bash 复制代码
nft list ruleset
iptables -L -n -v
# 有的系统 iptables 是 nft 后端
update-alternatives --display iptables 2>/dev/null

策略设计

  • 默认策略与显式 accept 成对出现。
  • established,related 放前面,避免回包被丢。
  • 管理口(SSH)先保证,再收紧。

容器与防火墙

Docker/Podman 会插入自己的链。排障时同时看 nft list ruleset 与容器网络,不要只改 filter 的 input。

持久化

bash 复制代码
nft list ruleset > /etc/nftables.conf
systemctl enable --now nftables

重启后规则消失 = 没落盘或服务未开。

相关推荐
xhbh6661 小时前
中小企业 Redis 运维,如何安全归档 RDB、AOF 备份文件?
运维·数据库·缓存·数据备份·文件备份·同步备份·号码备份
是逍遥子没错1 小时前
一个斜杠,击穿整座网关:API网关路径前缀绕过认证实战
运维·服务器·web安全·网络安全·渗透测试·系统安全
Fluxart.ai2 小时前
GPT Image 2.5 有使用次数限制吗?额度、并发、频率限制说明
服务器·前端·gpt
其实防守也摸鱼2 小时前
每天一个知识点——RCE漏洞
运维·服务器·数据库·windows·安全·github·漏洞
努力努力再努力wz2 小时前
【Docker 入门系列】:从 Namespace、CGroup 到 LXC:理解容器运行时、rootfs 与 LXC 实战
运维·docker·容器
ao-weilai3 小时前
Linux网络编程:网络知识基础
linux·网络·php
GeW3 小时前
RHCE10一次过!提高通过率+快速拿证的7个核心技巧
linux
JavaPub-rodert3 小时前
Docker 多阶段构建:为什么生产环境镜像可以小 90%
运维·docker·容器
浅念-3 小时前
C++ Protobuf 入门实战:基于通讯录项目吃透proto3语法与序列化
linux·服务器·网络·c++·protobuf·proto·proto3