无法通过 iptables PREROUTING 链使 NAT 工作

And*_*ree 6 iptables nat

所以,我们不关心为什么,更关心如何,我想看看有没有人知道我在这里哪里出错了。

基本上,我想将所有前往端口 80 的数据包转发到我已别名为环回设备 (169.254.169.254) 的 IP 上,以便转发到另一个 IP 上的端口 8080,这恰好是该 IP 的公共 IP同一个框(我们将使用 1.1.1.1 来解决这个问题)。这样做,我应该[表面上]能够跑

telnet 169.254.169.254 80
Run Code Online (Sandbox Code Playgroud)

并达到 1.1.1.1:8080,但是,这并没有发生。

这是我在 iptables 中的 nat 表:

~# iptables -nvL -t nat
Chain PREROUTING (policy ACCEPT 66 packets, 3857 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DNAT       tcp  --  *      *       0.0.0.0/0            169.254.169.254      tcp dpt:80 to:1.1.1.1:8080

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?我已经按照 iptables 手册页和下面的链接中的大部分信息进行了操作,但是,在我的 telnet 尝试期间,我仍然收到“连接被拒绝”的消息。我曾尝试添加~#iptables -t nat -A POSTROUTING -j MASQUERADE到我的 iptables,但无济于事:/

如果有人能指出我正确的方向,那将是非凡的!

http://linux-ip.net/html/nat-dnat.html

https://www.frozentux.net/iptables-tutorial/chunkyhtml/x4033.html

编辑 我想补充一点,我确实启用了以下 sysctl 参数~# sysctl net.ipv4.ip_forward net.ipv4.ip_forward = 1

编辑号 2 我能够通过将规则添加到 nat 表中的 OUTPUT 链来解决这个问题,而不是我最初尝试的 PREROUTING 链。

And*_*ree 1

实际上,我通过确保内核模块“br_netfilter”已加载到主机上来实现​​此工作。事情就是这么简单,在被困了这么久之后,简单得令人恼火。

\n\n

引导我找到解决方案的文档/文章:

\n\n

1)

\n\n

https://github.com/omribahumi/libvirt_metadata_apihttps://thewiringcloset.wordpress.com/2013/03/27/linux-iptable-snat-dnat/

\n\n

请参阅“设置 iptables”部分 \xe2\x80\x93 我正在使用 \'DNAT\' 而不是 \'REDIRECT\' bc \'REDIRECT\' 只是将流量重定向到本地系统上的接口,而不是转发到已删除的地址(如目标 NAT 的情况)。

\n\n

2)

\n\n

https://serverfault.com/questions/179200/difference-beetween-dnat-and-redirect-in-iptableshttps://www.netfilter.org/documentation/HOWTO/NAT-HOWTO-6.html

\n\n

帮助我理解 REDIRECT 和 DNAT 之间的区别,如上所述。

\n\n

3)

\n\n

https://github.com/omribahumi/libvirt_metadata_api/pull/4/files

\n\n

这次提交使我找到了实际的解决方案(上述事情我之前已经做过,但无济于事)。

\n