iptables用于本地连接

在“服务器A”上,我有一个服务在端口1445上运行。“服务器B”可以通过以下规则连接到端口445上的“服务器A”:

iptables -t nat -A PREROUTING -d www.kunde.de -p tcp -m tcp –dport 445 -j REDIRECT – 到端口1445

这不适用于在“服务器A”上启动的连接,即本地连接。 我能做什么?

iptables -t nat -A OUTPUT -d http://www.kunde.de -p tcp -m tcp –dport 445 -j REDIRECT – 到端口1445

因为本地产生的数据包通过OUTPUT,而不是通过PREROUTING。

您应该更好地阅读一些手册和文档。

无论如何简要的链条描述:

* “PREROUTING”: Packets will enter this chain before a routing decision is made. * “INPUT”: Packet is going to be locally delivered. (NB: It does not have anything to do with processes having a socket open. Local delivery is controlled by the “local-delivery” routing table: `ip route show table local`.) * “FORWARD”: All packets that have been routed and were not for local delivery will traverse this chain. * “OUTPUT”: Packets sent from the machine itself will be visiting this chain. * “POSTROUTING”: Routing decision has been made. Packets enter this chain just before handing them off to the hardware. 

看看这里:

Netfilter数据包stream

维基百科

如果使用lxc或其他虚拟化技术,则将来自端口445的所有stream量redirect到容器1455。

服务器A中的本地请求不会通过PREROUTING 。 原因之前已经解决了。 本地stream量将不会访问nat table ,但是如果您以前使用DNAT,它只适用于服务器A的外部stream量, REDIRECT只能用于端口转发,不能用作DNAT