QEMU and TUN/TAP networking

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

11 thoughts on “QEMU and TUN/TAP networking

  1. 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.

  2. 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.

  3. Pingback: Leader’s Blog » Blog Archive » links for 2008-11-24

  4. 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?

  5. 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.

  6. “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.”

  7. I do believe all of the concepts you’ve presented on your post. They’re really convincing and will definitely work. Still, the posts are very quick for beginners. Could you please lengthen them a little from next time? Thanks for the post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s