此设置中只有一台物理机器,即具有两个networking适配器的虚拟机主机系统(VM)。
一个NIC(eth0)连接到内部networking(LAN子网,例如10.xxx/24),并用于内部通信。
另一个NIC(eth1)连接到公共互联网(它具有configuration的公共可路由IP)。 这个连接将被用于端口转发公共互联网stream量到虚拟机的内部IP(传入stream量),并允许虚拟机通过NAT访问公共互联网(传出stream量)。
虚拟机使用LAN-Subnet中的IP地址(10.xxx/24,与eth0相同)
我已经为虚拟机(vnet0,vnet1,…)和LAN-NIC(eth0)的虚拟networking接口configuration了桥接设备(br0)。 这意味着:
局域网内的通信工作正常。 此外,VM主机可通过公共IP访问,并具有互联网接入。
我的问题是允许虚拟机访问公共互联网的NATconfiguration。
我尝试使用一个简单的(S)NAT规则:
iptables -t nat -I POSTROUTING -s 10.xxx/24 ! -d 10.xxx/24 -j SNAT --to-source yyy102
而yyy102是第二个NIC(eth1)的公共可路由IP。
我发现我需要启用“ip_forward”和“bridge-nf-call-iptables”:
echo 1 > /proc/sys/net/ipv4/ip_forward echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
否则桥接的包不会被iptables处理。
现在来自虚拟机的数据包似乎经历了以下的iptables连锁:
但不是所有的数据包似乎到达PRE / POSTROUTING出于任何原因,我无法弄清楚到目前为止。
然而,更有趣的是, tcpdump -i eth0和tcpdump -i eth1之间的差别似乎是通过错误的接口eth0(= LAN-NIC)发送的(我尝试从虚拟机内ping外部IP)。 即使应用了NAT规则,也将源地址更改为另一个NIC(eth1)的IP地址。
我如何configuration系统输出带公网IP作为源地址的NAT数据包,通过正确的网卡(eth1)发送?
我是否需要添加eth1到桥(br0)? 如果是这样,我如何正确分配公共IP地址? 通常需要在网桥设备上configurationIP。 我需要分配一个别名地址到网桥(公共IP上br0:0)?
主机系统上的路由configuration:
# ip r default via yyy126 dev eth1 10.xx0/24 dev br0 proto kernel scope link src 10.xx11 yyy96/27 dev eth1 proto kernel scope link src yyy102
网卡configuration:
# ifconfig br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.xx11 netmask 255.255.255.0 broadcast 10.xx255 inet6 ####::###:####:####:#### prefixlen 64 scopeid 0x20<link> ether ##:##:##:##:##:## txqueuelen 0 (Ethernet) RX packets 2139490 bytes 243693436 (232.4 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 29085 bytes 2398024 (2.2 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 ####::###:####:####:#### prefixlen 64 scopeid 0x20<link> ether ##:##:##:##:##:## txqueuelen 1000 (Ethernet) RX packets 2521995 bytes 290600491 (277.1 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 383089 bytes 48876399 (46.6 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 device memory 0xdfa60000-dfa7ffff eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet yyy102 netmask 255.255.255.224 broadcast yyy127 inet6 ####::###:####:####:#### prefixlen 64 scopeid 0x20<link> ether ##:##:##:##:##:## txqueuelen 1000 (Ethernet) RX packets 2681476 bytes 597532550 (569.8 MiB) RX errors 0 dropped 130 overruns 0 frame 0 TX packets 187755 bytes 21894113 (20.8 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 device memory 0xdfa00000-dfa1ffff
桥接configuration:
# brctl show bridge name bridge id STP enabled interfaces br0 8000.002590eb1900 no eth0 vnet0
和iptables规则:
# iptables -vnL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 723 106K DROP udp -- * * yyy0/24 0.0.0.0/0 udp spt:5404 586 40052 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 5 420 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 2 458 LOG all -- * * 0.0.0.0/0 0.0.0.0/0 LOG flags 0 level 4 2 458 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 1343 173K ACCEPT tcp -- * * 10.xx2 0.0.0.0/0 tcp spt:3389 1648 127K ACCEPT tcp -- * * 0.0.0.0/0 10.xx2 tcp dpt:3389 18 1040 LOG all -- * * 0.0.0.0/0 0.0.0.0/0 LOG flags 0 level 4 18 1040 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 Chain OUTPUT (policy ACCEPT 525 packets, 84016 bytes) pkts bytes target prot opt in out source destination # iptables -vnL -t nat Chain PREROUTING (policy ACCEPT 13 packets, 1218 bytes) pkts bytes target prot opt in out source destination Chain INPUT (policy ACCEPT 5 packets, 420 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 13 packets, 880 bytes) pkts bytes target prot opt in out source destination Chain POSTROUTING (policy ACCEPT 14 packets, 920 bytes) pkts bytes target prot opt in out source destination 5 300 SNAT all -- * * 10.xx0/24 !10.xx0/24 to:yyy102
这里在LAN接口卡上捕获的NATed数据包(从VM ping):
# tcpdump -i eth0 12:53:55.243350 IP yyy102 > yyy110: ICMP echo request, id 2, seq 5, length 40
“ip规则”的输出:
# ip rule 0: from all lookup local 32766: from all lookup main 32767: from all lookup default
检查你的虚拟机的IP地址是10.xxx/24(networking掩码255.255.255.0)
设置10.xx11(br0 ip地址)作为虚拟机的默认网关
在物理主机上启用IP转发
启用SNAT:
iptables -t nat -A POSTROUTING -s 10.xxx/24 -o eth1 -j SNAT --to yyy102
iptables -t nat -I POSTROUTING -s 10.xxx/24! -d 10.xxx/24 -j SNAT – 来源yyy102
这个必须改成
iptables -t nat -I POSTROUTING --out-interface eth1 -j SNAT --to-source yyy102
根据你的第一条规则,只有目的地为10.xxx的包必须被处理。 那么,从外部到你的networking的stream量呢? (来源 – 来自世界各地,目的地是您的公共IP 🙂
我如何configuration系统输出带公网IP作为源地址的NAT数据包,通过正确的网卡(eth1)发送?
阅读以上部分。 只需更改NAT规则。
我是否需要添加eth1到桥(br0)? 如果是这样,我如何正确分配公共IP地址? 通常需要在网桥设备上configurationIP。 我需要分配一个别名地址到网桥(公共IP上br0:0)?
绝不是,除非你知道你在做什么和为什么这样做。 保持内部和外部接口分离。 只允许路由。
我已经描述了configuration现场(制作)超过5年。 为3个主机服务器和25个虚拟机(包括通过openvpn隧道的桥接链路)工作顺利。