如何在Debian / Ubuntu上将一系列公有IP连接到KVM虚拟机

我已经尝试了几个星期来弄清楚正确的networkingconfiguration,以便与我的服务器上运行的KVM虚拟机共享一系列公有IP, 但到目前为止运气不多 并在友好的ServerFault社区的帮助下,我设法使其工作。 你可以在下面find我的工作设置:

我的ISP将所有的stream量路由到192.168.8.118 (因此需要成为eth0的主要IP),但我的configuration是192.168.239.160/28

这里是主机上的/etc/network/interfaces

 # Loopback device: auto lo iface lo inet loopback # device: eth0 auto eth0 iface eth0 inet static address 192.168.8.118 broadcast 192.168.8.127 netmask 255.255.255.224 gateway 192.168.8.97 pointopoint 192.168.8.97 # This device acts as gateway for the bridge, so provide a route. up ip route add 192.168.8.118/32 dev eth0 scope host # device: br0 auto br0 iface br0 inet static bridge_stp off bridge_maxwait 0 bridge_fd 0 address 192.168.239.174 broadcast 192.168.239.175 netmask 255.255.255.240 gateway 192.168.8.118 # Create and destroy the bridge automatically. pre-up brctl addbr br0 post-down brctl delbr br0 # Our additional IPs are allocated on the bridge. up ip route add to 192.168.239.160/28 dev br0 scope host 

我已经configuration了这样一个虚拟机:

 sudo ubuntu-vm-builder kvm precise \ --domain pippin \ --dest pippin \ --hostname pippin.hobbiton.arnor \ --flavour virtual \ --mem 8196 \ --user mikl \ --pass hest \ --bridge=br0 \ --ip 192.168.239.162 \ --mask 255.255.255.240 \ --net 192.168.239.160 \ --bcast 192.168.239.175 \ --gw 192.168.239.174 \ --dns 8.8.8.8 \ --components main,universe \ --addpkg git \ --addpkg openssh-server \ --addpkg vim-nox \ --addpkg zsh \ --libvirt qemu:///system ; 

如果我检查虚拟机的XML定义,其networking接口是这样定义的:

 <interface type='bridge'> <mac address='52:54:00:b1:e9:52'/> <source bridge='br0'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> 

当我(重新)启动虚拟机时, /var/log/syslog收到这些行:

 Jul 20 03:13:02 olin kernel: [ 4084.652906] device vnet0 entered promiscuous mode Jul 20 03:13:02 olin kernel: [ 4084.686388] br0: port 2(vnet0) entering forwarding state Jul 20 03:13:02 olin kernel: [ 4084.686394] br0: port 2(vnet0) entering forwarding state 

我的服务器运行的Ubuntu 12.04 64位与内核3.2.0-26-generic(从Ubuntu)。 我正在运行libvirt-bin 0.9.8-2ubuntu1qemu-kvm 1.0+noroms-0ubuntu13

主机上的iptables目前设置为允许所有stream量(消除这个问题来源),并且启用了ipv4和ipv6stream量的转发。

当我通过SSH从主机login到客户机时,在客户机操作系统内部没有互联网连接。 guest虚拟机的/etc/network/interfaces如下所示:

 auto lo iface lo inet loopback auto eth0 iface eth0 inet static address 192.168.239.162 netmask 255.255.255.240 network 192.168.239.160 broadcast 192.168.239.175 gateway 192.168.239.174 # dns-* options are implemented by the resolvconf package, if installed dns-nameservers 8.8.8.8 dns-search pippin 

现在起作用了

上面的configuration大纲实际上是我想要的。 如果您想查看我以前的尝试,请参阅编辑历史logging。

如果您正在桥接,则不需要在主机上configuration与虚拟机IP地址相关的任何内容。 只需configuration它们即可连接到网桥,并以通常的方式configuration每个虚拟机内的IP。 桥接将networking连接在IP层不重要的以太网层, 从您的ISP的angular度来看,它会看起来像你有几台计算机插入到直接连接到ISP的交换机。

但是,如果您的ISP stream量路由到主机的.118地址,则需要将VM主机configuration为充当路由器并转发虚拟机的stream量。 为此,从interfaces文件中删除bridge_ports eth0行, ip route add to 192.168.239.160/28 dev br0 ,并echo 1 > /proc/sys/net/ipv4/ip_forward 。 在虚拟机中,您需要将192.168.8.118configuration为默认网关,并添加一条说明192.168.8.118可通过eth0直接访问的路由。 (这是VM的eth0 ,连接到主机的br0 。)

在任何情况下,您都不应该直接将虚拟机的地址添加到br0接口。 在桥接情况下,您希望虚拟机而不是主机回答这些地址的ARP请求,并且在路由情况下,您希望主机了解当它收到其中一个地址的数据包时,需要路由在别的地方,没有在当地交付。

你不想把你的虚拟机的IP地址分配给主机的br0接口 – 这只会使该地址属于主机,而不是虚拟机。

但是,您的虚拟机将需要一个网关地址来路由所有出站数据包。 我build议将/ 28中的一个IP分配给您的主机,并configuration您的虚拟机使用该IP作为其默认路由。 子网中第一个或最后一个可用的IP是网关地址的合理select….

 ip addr add 192.168.239.161/28 dev br0 

你有没有启用IP转发? 例如在/etc/sysctl.conf(ipv4和/或ipv6中的一个或两个)中取消以下注释:

 #net.ipv4.ip_forward=1 #net.ipv6.conf.all.forwarding=1 

最后,你是否尝试过login虚拟机的控制台(例如使用virt-manager或VNC查看器如vinagre或xvnc4viewer)? 如果是这样,它有什么IP地址(如果有的话)? VM是否configuration为静态IP或DHCP? 如果是后者,你是否configuration了你的dhcp服务器,给虚拟机的MAC地址提供相应的IP地址?