12.2. 使用 nftables 配置伪装
伪装使路由器动态地更改通过接口到接口 IP 地址发送的数据包的源 IP。这意味着,如果接口被分配了一个新 IP,nftables 会在替换源 IP 时自动使用新的 IP。
将通过 ens3 接口离开主机的数据包源 IP 替换为 ens3 上设置的 IP。
流程
创建一个表:
nft add table nat
# nft add table natCopy to Clipboard Copied! Toggle word wrap Toggle overflow 将
postrouting链添加到表中:nft add chain nat postrouting { type nat hook postrouting priority 100 \; }# nft add chain nat postrouting { type nat hook postrouting priority 100 \; }Copy to Clipboard Copied! Toggle word wrap Toggle overflow 向
postrouting链中添加一条规则,来匹配ens3接口上传出的数据包:nft add rule nat postrouting oifname "ens3" masquerade
# nft add rule nat postrouting oifname "ens3" masqueradeCopy to Clipboard Copied! Toggle word wrap Toggle overflow
重要
您只能在 iifname 和 oifname 参数中使用实际的接口名称,不支持替代名称(altname)。