Linux:如何使用iptables在不同networking上的两台主机之间进行端口转发?

我几乎绝望…我已经读了大约2天的iptables转发的例子,我不能做一个简单的端口转发。 我有两台机器在不同的networking上。 server1(S1的ip为195.21.2.41)在我家,server2(s2的ip为10.234.141.126)在Amazon EC2。

我需要转发到s2的所有stream量到s1。 我试过这个:

冲洗所有的规则,激活内核参数转发,添加一个前后处理规则

iptables -F -t nat iptables -F echo 1 > /proc/sys/net/ipv4/ip_forward iptables -t nat -A POSTROUTING -d 195.21.2.41 -j MASQUERADE iptables -t nat -A PREROUTING -i eth0 -d 10.234.141.126 -p tcp --dport 80 -j DNAT --to 195.21.2.41 

可选我还补充说:

 iptables -A FORWARD -p tcp -i eth0 -d 195.21.2.41 --dport 80 -j ACCEPT 

然后我试着:

 telnet 10.234.141.126 80 

但没有工作。 为什么这不是在工作?

更新:看看一些testing:

 [root@ip-10-234-141-216 ~]# telnet 195.21.2.41 80 Trying 195.21.2.41... Connected to 195.21.2.41. Escape character is '^]'. [root@ip-10-234-141-216 ~]# iptables -F -t nat [root@ip-10-234-141-216 ~]# iptables -F [root@ip-10-234-141-216 ~]# echo 1 > /proc/sys/net/ipv4/ip_forward [root@ip-10-234-141-216 ~]# /sbin/iptables -t nat -A PREROUTING -i eth0 -p tcp -d 10.234.141.126 --dport 80 -j DNAT --to-destination 195.21.2.41 [root@ip-10-234-141-226 ~]# /sbin/iptables -t nat -A POSTROUTING -j MASQUERADE [root@ip-10-234-141-216 ~]# /sbin/iptables -A FORWARD -i eth0 -p tcp --dport 80 -j ACCEPT [root@ip-10-234-141-216 ~]# [root@ip-10-234-141-216 ~]# telnet 10.234.141.126 80 Trying 10.234.141.126... telnet: connect to address 10.234.141.126: Connection refused 

更新2路由输出:

 [root@ip-10-234-141-216 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.234.141.0 0.0.0.0 255.255.254.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 0.0.0.0 10.234.141.1 0.0.0.0 UG 0 0 0 eth0 

如果您只是需要将所有传入stream量redirect到指定的端口转发到另一台机器尝试rinetd而不是iptables。 这是一个stream量redirect服务器。

我目前有同样的问题。 我已经解决了

 echo 1 >| /proc/sys/net/ipv4/ip_forward iptables -t nat -A PREROUTING -p tcp -d 47.168.137.12 --dport 8081 -j DNAT --to 47.168.137.11:8086 iptables -t nat -A POSTROUTING -j MASQUERADE 

它不会工作,因为你从同一个路由器的机器telnet它应该从任何使用这台机器的机器作为下一个希望正常工作,或者如果你只需要做这些工作,你可以添加此规则

 /sbin/iptables -t nat -A OUTPUT -i eth0 -p tcp -d 10.234.141.126 --dport 80 -j DNAT --to-destination 195.21.2.41 

为什么不使用iptables?

 iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 3306 -j DNAT --to 192.168.7.101:3306 

一个工作岗位:

 iptables -t nat -A POSTROUTING -j MASQUERADE echo "1" > /proc/sys/net/ipv4/ip_forward sysctl net.ipv4.ip_forward=1 

你的规则看起来不错,除了你正在使用--to而不是--to-destination 。 可能是因为你正在使用不同版本的iptables但根据man 8 iptables (v.1.4.7):

  --to offset Set the offset from which it starts looking for any matching. If not passed, default is the packet size. 

然后在DNAT部分:

  --to-destination [ipaddr][-ipaddr][:port[-port]] which can specify a single new destination IP address, an inclusive range of IP addresses, and optionally, a port range (which is only valid if the rule also specifies -p tcp or -p udp). If no port range is specified, then the destination port will never be modified. If no IP address is specified then only the destination port will be modified. In Kernels up to 2.6.10 you can add several --to-destination options. For those kernels, if you specify more than one destination address, either via an address range or multiple --to-destination options, a simple round-robin (one after another in cycle) load balanc- ing takes place between these addresses. Later Kernels (>= 2.6.11-rc1) don't have the ability to NAT to multiple ranges anymore. 

这是我会尝试的:

 /sbin/iptables -t nat -A PREROUTING -i eth0 -p tcp -d 10.234.141.126 --dport 80 -j DNAT --to-destination 195.21.2.41 /sbin/iptables -t nat -A POSTROUTING -j MASQUERADE /sbin/iptables -A FORWARD -i eth0 -p tcp --dport 80 -j ACCEPT 

我也尝试从10.234.141.126远程login到目的地,以确保防火墙不会阻止连接。

 telnet 195.21.2.41 80