我试图通过另一个Xen虚拟机的stream量透明地路由,如下所示:
------- 192.168.250.4 192.168.250.3 --------- | VM1 | <-----------------bridged----------------> | VM2 | <-----> Internet ------- | with | | squid | | proxy | ---------
不要问为什么,只是试验iptables。 我能够通过VM2的Squid代理(透明模式)成功路由HTTPstream量
iptables -t nat -A PREROUTING -p tcp --dport 80 –s ! 192.168.250.3 -j REDIRECT --to-port 3128
但是我怎样才能简单地通过所有其他stream量? 已经尝试过这个configuration,但它试图从VM1( 192.168.250.4 )访问互联网时,给我“连接被拒绝”错误:
vm2:~# iptables -t nat -L -n Chain PREROUTING (policy ACCEPT) target prot opt source destination # route outgoing udp traffic DNAT udp -- !192.168.250.3 0.0.0.0/0 udp dpt:!80 to:192.168.250.3 # route outgoing tcp traffic DNAT tcp -- !192.168.250.3 0.0.0.0/0 tcp dpt:!80 to:192.168.250.3 # this is the working squid rule REDIRECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 redir ports 3128 Chain POSTROUTING (policy ACCEPT) target prot opt source destination # route incoming traffic SNAT all -- 0.0.0.0/0 192.168.250.3 to:192.168.250.4 Chain OUTPUT (policy ACCEPT) target prot opt source destination
这里有什么问题? 我已经阅读了很多教程,但大多数不能正常工作…(顺便说一句: /proc/sys/net/ipv4/ip_forward是1)
而不是使用REDIRECT,试试DNAT和SNAT。 尝试这个:
iptables -t nat -I PREROUTING -d 192.168.250.3 -j DNAT -to-destination 192.168.250.4 iptables -t nat -I POSTROUTING -s 192.168.250.4 -j SNAT -to-source 192.168.250.3
我终于find了正确的方法来做到这一点:
NAT外出连接(VM1 – > Internet / Intranet)与源重写(SNAT)一起使用:
iptables -t nat -A POSTROUTING -s 192.168.250.4 -j SNAT --to-source 192.168.2.125
其中192.168.2.125是VM2的当前外部IP( 不能是内部地址)。 由于在我的情况下IP是由DHCP分配的,因此必须将规则更改为在dynamicIP上执行SNAT。 这是通过MASQUERADE命令完成的:
iptables -t nat -A POSTROUTING -s 192.168.250.4 -j MASQUERADE
最后的iptablesconfiguration现在看起来像这样(其他表/链是空的):
Chain POSTROUTING (policy ACCEPT) target prot opt source destination MASQUERADE all -- 192.168.250.4 0.0.0.0/0