12.3. 使用 nftables 配置源 NAT
在路由器中,源 NAT(SNAT)可让您将通过接口发送的数据包 IP 改为专门的 IP 地址。然后,路由器会替换传出数据包的源 IP。
流程
创建一个表:
nft add table nat
# nft add table nat
Copy 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
链中添加一条规则,该规则将使用192.0.2.1
替换通过ens3
的传出数据包的源 IP :nft add rule nat postrouting oifname "ens3" snat to 192.0.2.1
# nft add rule nat postrouting oifname "ens3" snat to 192.0.2.1
Copy to Clipboard Copied! Toggle word wrap Toggle overflow