如何使一个Linux虚拟机作为一个路由器工作

我可以访问一个OpenStack帐户,在那里我可以创buildLinux 14.04虚拟机。 我创build了两个networking接口。

  1. 通过路由器连接到互联网的“公网”

  2. 没有暴露互联网的“私人networking”

现在,我创build了一个名为“GATEWAY”的虚拟机,它连接到两个networking接口,在eth0(10.70.0.6)和eth1(10.90.0.1)上有两个互联网地址。 eth0暴露于互联网,eth1是专用networking。 GATEWAY虚拟机在eth0上有一个公共IP地址。

现在我又创build了一个虚拟机,在私有networking界面上命名为“AGENT”。 IP地址是10.90.0.7,并使默认网关为10.90.0.1(GATEWAY虚拟机)

由于私有虚拟机没有暴露给任何路由器,所以我们无法访问虚拟机。 要启用互联网访问,我已经在GATEWAY虚拟机上添加了一个NAT规则,如下所示:

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE 

这将改变离开主机GATEWAY的所有因特网分组的源地址作为GATEWAY机器的地址。 另外,在GATEWAY机器上设置ipv4数据包转发= 1。

我可以从GATEWAY机器ping任何外部地址,但不能从内部代理机器ping。 更不用说这个私人AGENT机器也没有互联网接入。

任何人都可以请帮我设置网关虚拟机这样一个方式,使我可以使用它作为一个路由器和互联网访问到私人机器。

这就是我的路由表在AGENT机器中的样子:

 Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.90.0.1 0.0.0.0 UG 0 0 0 eth0 10.90.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.169.254 10.90.0.2 255.255.255.255 UGH 0 0 0 eth0 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0 

在这里我添加我的tcpdump icmp ping在这两个接口。

eth1:连接到专用networking的接口。

 18:43:39.309771 IP host-10-90-0-7.openstacklocal > 172.217.3.14: ICMP echo request, id 2395, seq 1, length 64 18:43:39.355430 IP 172.217.3.14 > host-10-90-0-7.openstacklocal: ICMP echo reply, id 2395, seq 1, length 64 18:43:40.318637 IP host-10-90-0-7.openstacklocal > 172.217.3.14: ICMP echo request, id 2395, seq 2, length 64 18:43:40.364178 IP 172.217.3.14 > host-10-90-0-7.openstacklocal: ICMP echo reply, id 2395, seq 2, length 64 

eth0:连接到互联网的接口。

 18:43:39.309796 IP host-10-70-0-6.openstacklocal > 172.217.3.14: ICMP echo request, id 2395, seq 1, length 64 18:43:39.355396 IP 172.217.3.14 > host-10-70-0-6.openstacklocal: ICMP echo reply, id 2395, seq 1, length 64 18:43:40.318679 IP host-10-70-0-6.openstacklocal > 172.217.3.14: ICMP echo request, id 2395, seq 2, length 64 18:43:40.364154 IP 172.217.3.14 > host-10-70-0-6.openstacklocal: ICMP echo reply, id 2395, seq 2, length 64 18:43:41.326618 IP host-10-70-0-6.openstacklocal > 172.217.3.14: ICMP echo request, id 2395, seq 3, length 64 

在这里我可以看到,ping响应来自外部地址和它的两个接口。 即使它被eth1接收到私有虚拟机,它的ping也会丢失100%的数据包。

 --------- ------------------- ------------- ------------ INTERNET |----| openstack-router| --10.70.0.1 --------10.70.0.6(NIC eth0) --| GATEWAY-VM |-- 10.90.0.1(NIC eth1) ---------10.90.0.7(NIC eth0) --| AGENT-VM | --------- ------------------- ------------- ------------ 

您的防火墙规则正处于正确的轨道上。 我以“老式”的方式来做事情 – 写一个脚本,把它放在/ etc中,从/etc/rc.local调用它。 不过,你喜欢这样做,这是什么对我有用。

操作系统是通过netinstall的Debian Jessie 64位,只有在tasksel时间select的“标准系统实用程序”,在Mint桌面上的VirtualBox上运行。 eth0通过桥接接口和DHCP连接到我的局域网。 eth1是我用于试验的虚拟机networking的局域网端。 将防火墙脚本复制/粘贴到/etc/rc.firewall,使其可执行,并在/etc/rc.local中调用它。

 #!/bin/bash # copyright me, licensed to you freely # a very simple set of iptables commands # to allow forwarding between ethernet # devices # make sure forwarding is enabled in the kernel echo 1 > /proc/sys/net/ipv4/ip_forward # where is iptables located? iptables=`which iptables` # flush all existing rules $iptables -F # this is for NAT # enable masquerading /sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE # don't forward packets from off-lan to lan if # they are a brand new connection being initiated $iptables -A FORWARD -i eth0 -o eth1 -m state --state NEW -j REJECT # if the packets come from off-lan but they are # related to a connection that was established from # within the lan, go ahead and forward them $iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT # whatever traffic comes from the lan to go to # the world allow thru $iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT 

当所有这一切都完成并运行,你应该能够看到类似的东西

 root@router:~# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 08:00:27:e6:43:df inet addr:192.168.1.126 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fee6:43df/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:729 errors:0 dropped:0 overruns:0 frame:0 TX packets:382 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:61777 (60.3 KiB) TX bytes:46468 (45.3 KiB) root@router:~# ifconfig eth1 eth1 Link encap:Ethernet HWaddr 08:00:27:af:50:e2 inet addr:10.99.99.1 Bcast:10.99.99.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:feaf:50e2/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:10 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:828 (828.0 B) root@router:~# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0 10.99.99.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 root@router:~# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination REJECT all -- anywhere anywhere state NEW reject-with icmp-port-unreachable ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT all -- anywhere anywhere Chain OUTPUT (policy ACCEPT) target prot opt source destination root@router:~# cat /proc/sys/net/ipv4/ip_forward 1 root@router:~#