iptables从外部接口重定向到loopback的端口?

inn*_*rld 7 linux iptables

我尝试将端口从我的lxc-container重定向到环回.

我的lxc-container配置了lxcbr1网桥11.0.3.1.

我尝试将netcat从主机连接到lxc,从lxc连接到主机.成功.

本地主机:

# nc -l 1088
Run Code Online (Sandbox Code Playgroud)

LXC:

# nc 11.0.3.1 1088
Hello!
Run Code Online (Sandbox Code Playgroud)

和localhost见消息:"你好!".成功!

当我以这种方式重定向端口时:

# iptables -t nat -A PREROUTING -i lxcbr1 -p tcp -d 11.0.3.1 --dport 1088  -j DNAT --to-destination 127.0.0.1:1088
# nc -l 127.0.0.1 1088
Run Code Online (Sandbox Code Playgroud)

此后,我尝试从lxc-container连接:

# nc 11.0.3.1 1088
Hello !
Run Code Online (Sandbox Code Playgroud)

但localhost没有看到此消息.

我哪里错了?

我找到了这个答案:https://serverfault.com/questions/211536/iptables-port-redirect-not-working-for-localhost

有一些声音,环回不使用PREROUTING.我该怎么办?

inn*_*rld 5

无法使用DNAT进行环回流量.

我发现了很多类似的问题.1,2,3,...等

根据RFC 5735,网络127.0.0.0/8不应在主机外部路由:

127.0.0.0/8 - 分配此块作为Internet主机环回地址.由更高级别协议发送到该块内任何地址的数据报循环回主机内部.这通常仅使用127.0.0.1/32来实现环回.如[RFC1122]第3.2.1.3节所述,整个127.0.0.0/8块中的地址在任何网络上都不合法地出现.

RFC 1700,第5页,«绝不应出现在主机之外».

有一个出口:使用inetd.

有许多服务器,xinetd等.

我的选择是rinetd.

我使用本手册http://www.howtoforge.com/port-forwarding-with-rinetd-on-debian-etch

我的配置如下所示:

$ cat /etc/rinetd.conf 
# bindadress    bindport  connectaddress  connectport
11.0.3.1        1081            127.0.0.1       1081
11.0.3.1        1088            127.0.0.1       1088
Run Code Online (Sandbox Code Playgroud)

我重启rinetd:

$ /etc/init.d/rinetd restart
Stopping internet redirection server: rinetd.
Starting internet redirection server: rinetd.
Run Code Online (Sandbox Code Playgroud)

重定向就像一个魅力.

我不会自己关闭这个问题,因为我仍然在为这样的任务寻找更优雅的解决方案.任何动物,netcat或inetd都不太可能这样做,这没关系.这是我的意见.


Irf*_*tif 5

仅供参考,如果有人在这里偶然发现,在新内核版本(可能 >= 3.6)上,您需要做的额外工作是:

~# echo 1 | sudo tee /proc/sys/net/ipv4/conf/all/route_localnet
Run Code Online (Sandbox Code Playgroud)

参考: ipv4:添加接口选项以启用 127.0.0.0/8 的路由