I just installed KVM on my Ubuntu Server according to this guide : https://help.ubuntu.com/community/KVM/Installation
Then prepared a bridged network as shown in here : https://help.ubuntu.com/community/KVM/Networking
Then, I created a virtual machine with virt-manager. I tried several times but the guest fails to connect to the network! Any help?
ifconfig :
br0 Link encap:Ethernet HWaddr d0:27:88:b0:e4:38
inet addr:192.168.20.100 Bcast:192.168.20.255 Mask:255.255.255.0
inet6 addr: fe80::d227:88ff:feb0:e438/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:62 errors:0 dropped:0 overruns:0 frame:0
TX packets:62 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:10493 (10.4 KB) TX bytes:8433 (8.4 KB)
eth0 Link encap:Ethernet HWaddr d0:27:88:b0:e4:38
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:62 errors:0 dropped:0 overruns:0 frame:0
TX packets:63 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:11361 (11.3 KB) TX bytes:8479 (8.4 KB)
Interrupt:41
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
virbr0 Link encap:Ethernet HWaddr 5a:8c:57:95:af:3b
inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
brctl show :
bridge name bridge id STP enabled interfaces
br0 8000.d02788b0e438 no eth0
virbr0 8000.000000000000 yes
brctl showmacs br0 :
port no mac addr is local? ageing timer
1 5c:d9:98:67:b6:28 no 48.33
1 d0:27:88:b0:e4:38 yes 0.00
1 e0:2a:82:f9:6c:09 no 0.00
ip route :
default via 192.168.20.1 dev br0 metric 100
192.168.20.0/24 dev br0 proto kernel scope link src 192.168.20.100
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
*In the guest * I was unable to copy paste the info from the guest because can't ssh to it. It didn't get any ip from DHCP. Won't work even after setting it up manually.
Preliminaries
This following worked for me for Ubuntu 12.04. You should disable your computer's firewall as you test this so it won't interfere.
The /etc/default/qemu-kvm file should be as originally installed.
You will need to have bridge-utils qemu-kvm and libvirt-bin installed. Any users using virtual machines should be added to the libvirtd group.
There no longer seems to be any need to add CAP_NET_ADMIN capability.
Network Setup
The default network mode is the User mode, also called SLIRP. It uses a predefined virbr0 bridge which is NAT routed to the guest computer. The NAT routing uses the kernel's ip_forwarding feature and iptables. Bridge mode uses a virtual bridge in the guest to which the (unnumbered) Ethernet interface connects, and on which both the host and the guest have their network interfaces.
The following diagrams may make the differences clearer:
You can see how the default User network is defined with:
I can set up the bridged mode with the following approaches:
In /etc/network/interfaces (from the bridging part of the post you mention in your question):
Reboot; and make sure that wireless networking isn't active. Check the default IP route with
ip route
. It must be using the br0 interface.N.B. If your Ethernet isn't hooked up when this change is made you need to have your Ethernet cable plugged in and getting a carrier or the boot will hang for two minutes and you won't have network capability That's because the eth0 interface, by being in this file, must come up before the boot can proceed normally.
N.B. Generally you can't use a wireless network instead of eth0 because of their inability to use multiple MAC addresses (I infer that they need a second one for the bridge).
As an alternative you can disable the use of Ethernet and make sure that it does not have an IP address, and that there isn't a default route set up with
ip route
. Then:You could also supply a static IP address here, as well as defining the default route and DNS address. For this example
dhclient
does this.Here's my route table:
Using kvm
I can then boot a bridged kvm machine with:
The
-netdev tap
parameter makes sudo a requirement. As the VM is started qemu-kvm runs the following commands:This is done by /etc/qemu-ifup
The VM's vnet0 interface is added to the br0 bridge because the default route above uses that bridge interface. If it weren't there the tap interface instead would be added to the virbr0 interface. Since that's not connected to the Internet, NAT would be used to connect the guest to the host and the Internet, in my experiments. You can direct the vnet0 to a particular bridge in /etc/default/qemu-kvm. Using virt-manager below you can explicitly direct which bridge to connect to.
Because of the above commands issued by qemu-kvm, and the
-netdev tap,id=tunnel,ifname=vnet0
parameter, the vm virtual machine is connected to the vnet0 tunnel, and the tunnel is connected to the br0 bridge.I can now directly ssh into this guest VM from another computer on my network.
My host
ifconfig
(note the vnet0 interface that appears on my network when the VM is running):My bridge configuration while running the VM:
Note that both the virtual machine's vnet0 interface and the eth0 interface are connected to the br0 bridge.
And the MAC's on the br0 interface:
Note that the br0 interface connects my host computer to the same bridge being used by the guest.
You can check that you are bridged rather than NAT routed to your own network by using
traceroute 8.8.8.8
. If the first node is your network's router rather than the guest's ip address your network should be working correctly.See this documentation.
virt-manager
Be sure that you have installed
virt-manager
andhal
. Thehal
package is a suggested dependency forvirt-manager
and is used to determine the network configuration of your system when creating or editing guests.While having the br0 bridge defined as above I created a virtual machine with virt-manager as follows:
I was able to go directly to the rest of my home network and to the Internet from this guest. I was also able to ssh into it from the other (non-host, non-guest) Ubuntu computer on my home network.
Here's the very long
kvm
command run by virt-manager (for comparison with EApubs or anyone else having trouble with this):Here's the network portion of the virtual machine description in /etc/libvirt/qemu/quantal.xml
According to this link, for performance and reliability, it may be best to set the network device model to
virtio
, you can do this by in the virt-viewer by pressing the i button, going to the NIC setting, and setting the "Device model" tovirtio
. You could also add this to the XML above by adding the line:In Summary
All this took on 12.04 was:
-device e1000,netdev=tunnel -netdev tap,id=tunnel,ifname=vnet0
, or create a virtual machine with virt-manager, specifying network Bridge br0 under the Step 4->Advanced Options panel.No further changes were needed to networking, capabilities, templates, or configurations.
To expose a service in your new guest to the Internet you should:
Remember to test and re-enable the firewall service for your host computer. It may need any entry to forward traffic to the guest.
See https://help.ubuntu.com/community/KVM/Installation, https://help.ubuntu.com/community/KVM/Networking, and https://help.ubuntu.com/12.04/serverguide/libvirt.html.
If the behavior you are seeing is host can access the guest, and guest can access the host, but the guest can't access other machines on the network or visa versa... probably the host's firewall is blocking access.
See: https://bugs.launchpad.net/ubuntu/+source/ufw/+bug/573461
Specifically, this section: "The final step is to disable netfilter on the bridge:
These are the two scripts I use to create a bridge for
qemu-kvm
.First, let the host become a IP router.
Script
ip-router.sh
:Then, create the
tun-tap
interface andbridge
it with your default interface (usually the one with an Internet connection).Script
create-qemu-bridged-tuntap.sh
:I use these scripts daily, so they should work well also for you. You'll have to install some package in order to have all of this working. Using:
you can see which package is required to have
COMMAND
. For example to see which package is required to havebrctl
, simply run:and you'll have:
Using the same approach for all the commands in these scripts, you should (at least) run this
aptitude
command line:Finally, you can launch the main script (as a normal user):
Running
ip addr
you should see abr0
interface with IP address192.168.1.1
, as specified inside thecreate-qemu-bridged-tuntap.sh
script:This is the
host address
as seen by theguest
. Conversely, the guest will have as IP address192.168.1.95
(again, this can be easily changed inside the main script).Now, using
virt-manager
, you just have to setup your guest nic to usebr0
as physical interface.Inside the
guest
, you just have to give toeth0
an IP address of192.168.1.95
and everything should run fine.