我几乎绝望了…我已经阅读了大约2天iptables转发示例,我不能做一个简单的端口转发.我在不同的网络上有2台机器. server1(S1和ip 195.21.2.41)在我家,server2(s2与ip 10.234.141.126)在Amazon EC2.
我需要将转发到s2的所有流量转发到s1.我试过这个:
刷新所有规则,激活内核参数转发,添加postrouting和prerouting规则
- 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
但没有奏效.为什么这不起作用?
更新:看看一些测试:
- [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