8.3.3. 使用 nftables 配置源 NAT


在路由器上,您可以使用 Source NAT (SNAT)将通过接口发送的数据包 IP 更改为特定的 IP 地址。然后,路由器会替换传出数据包的源 IP。

流程

  1. 创建一个表:

    # nft add table nat
  2. 向表中添加 preroutingpostrouting 链:

    # nft add chain nat postrouting { type nat hook postrouting priority 100 \; }
    重要

    即使您没有向 postrouting 链添加规则,nftables 框架也会要求此链与传出数据包回复相匹配。

    请注意,您必须将 -- 选项传递给 nft 命令,以防止 shell 将负优先级值解释为 nft 命令的选项。

  3. postrouting 链中添加一条规则,该规则将使用 192.0.2.1 替换通过ens3 的传出数据包的源 IP :

    # nft add rule nat postrouting oifname "ens3" snat to 192.0.2.1
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

关于红帽文档

Legal Notice

Theme

© 2026 Red Hat
返回顶部