QEMU and TUN/TAP networking
July 5th, 2006
Using TUN/TAP networking with QEMU grant guest machines access to some or all networks reachable by the host machine. This also allows accessing services offered by guests machines from any other host.
To be able to use TUN/TAP network, instead of directly configuring the physical Ethernet network device — my Realtek 8169 Gigabit Ethernet, which in my computer it’s named eth1 –, we need to reconfigure the network in order to get a bridge device, named br0, with the physical Ethernet eth1 device attached to it. We can achieve this by editing /etc/network/interfaces to look like this:
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
#auto eth1
#iface eth1 inet static
auto br0
iface br0 inet static
address a.b.c.d
netmask 255.255.255.128
gateway v.w.x.y
bridge_ports eth1
bridge_fd 1
bridge_hello 1
bridge_stp off
The helper script /etc/qemu-ifup, which QEMU uses to bring up a TAP network interface used by the guest and bind it to the existing bridge br0, should look like this:
#!/bin/sh echo "Executing /etc/qemu-ifup" echo "Bringing up $1 for bridged mode..." sudo /sbin/ifconfig $1 0.0.0.0 promisc up echo "Adding $1 to br0..." sudo /usr/sbin/brctl addif br0 $1 sleep 2
To allow running QEMU as an unprivileged user, we need to edit /etc/sudoers in order to grant access for running /sbin/ifconfig and /usr/sbin/brctl. It’s recommended to edit that file using visudo. The file should could look like this:
# Cmnd alias specification
Cmnd_Alias QEMU=/sbin/ifconfig, \\
/sbin/modprobe, \\
/usr/sbin/brctl
# Defaults
Defaults !lecture,tty_tickets,!fqdn,rootpw
# User privilege specification
root ALL=(ALL) ALL
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Grant access to Cmnd_Alias to user jdoe
jdoe ALL=NOPASSWD:QEMU
Now, we can launch qemu from the command line:
XLIB_SKIP_ARGB_VISUALS=1 \\ qemu -hda hda -cdrom *iso -boot d \\ -m 512 -localtime -net nic,vlan=0 \\ -net tap,ifname=tap0,script=/etc/qemu-ifup \\ -kernel-kqemu
June 30th, 2007 at 3:24 PM
Hi,
I see lot of site for Qemu network interface it is a good article but only thing is my host disconnet the Main network.
July 4th, 2007 at 9:07 PM
Hi,
I see lot of site for Qemu network interface it is a good article. and i really reffer this one for network interfacing for qemu.
November 24th, 2008 at 5:36 PM
[...] Felipe Alfaro Solana » Blog Archive » QEMU and TUN/TAP networking (tags: linux qemu) [...]
April 15th, 2010 at 12:09 AM
How could I do this tun/tap startup script in Windows ?? It will be some that ??
echo..
./qemu script.bat …
etc. ..
Thanks.
June 16th, 2010 at 12:16 PM
XLIB_SKIP_ARGB_VISUALS=1 \\
qemu -hda hda -cdrom *iso -boot d \\
-m 512 -localtime -net nic,vlan=0 \\
-net tap,ifname=tap0,script=/etc/qemu-ifup \\
-kernel-kqemu
open /dev/kvm: No such file or directory
Could not initialize KVM, will disable KVM support
qemu: could not open disk image hda: No such file or directory
Help?
June 19th, 2010 at 5:39 PM
It looks like you don’t have KVM support installed. What distribution are you using?
December 27th, 2011 at 1:26 PM
How about calling it “Better Late Than Never” if it was meant for the 2009 centennial.
January 9th, 2012 at 12:25 PM
Nice post. I understand something much harder on distinct blogs everyday. It will always be stimulating you just read content using their company writers and rehearse a little something from their website. I’d prefer to use some while using content on my own weblog whether or not you do not mind. Natually I’ll provide link on the web blog. Thank you for sharing.
January 20th, 2012 at 4:36 PM
“A late push by Louisville has put political pressure on the Big 12 and opened the possibility of Louisville’s being the university that is admitted instead of West Virginia. Two people with direct knowledge of the situation said that lobbying by the Senate minority leader, Mitch McConnell, Republican of Kentucky, including to David Boren, the president of the University of Oklahoma and a former senator, helped slow West Virginia’s admittance to the Big 12.”