home | tech | misc | code | bookmarks (broken) | contact | README

QEMU notes

QEMU setup

Network setup of QEMU on NetBSD

After installing QEMU on a NetBSD host, some changes might be necessary. First, create a tap device with the follwing command:

# sysctl -w net.inet.ip.forwarding=1
# ifconfig tap0
# qemu-system-x86_64 -nographic -curses -boot c -net nic -net tap,ifname=tap0,script=no,downscript=no -m 326M emul.img

Also, you need to setup NAT for the host. I tried to configure this with NPF, but I couldn't. So I tried a configuration for PF, which worked. The contents of /etc/pf.conf is:

rdr on $ext_if proto {tcp udp} from any to any port 23 -> $emul port 22
nat pass on $ext_if from $int_if:network -> ($ext_if:0)
pass out
block in on $ext_if
pass in on $ext_if proto {tcp udp} from any to any port 22
pass in on $ext_if proto {tcp udp} from any to any port 23
pass in on $ext_if proto {tcp udp} from any to $emul port 22
pass in on $ext_if proto icmp all

Enable it on /etc/rc.conf and start it:

# echo 'pf=YES' >> /etc/rc.conf
# /etc/rc.d/pf start

Network setup of QEMU on Ubuntu Linux

To make this little guide, I basically followed this guide, which is a very good one.

Unlike it, though, I prefered to oversimplify it, so it not require nothing more than qemu (not kqemu) and uml-utilities. If you don't have them yet, fetch them:

# apt-get install qemu
# apt-get install uml-utilities

With the uml-utilities package, the tun kernel module and the tunctl user-level command become available. If the package manager didn't load the module itself, do that now:

# modprobe tun

Ok, now let's create a tap device (where <username> is the name of the user that will run QEMU, usually the user that you are logged on:

# tunctl -t tap0 -u <username>

We need to create (or edit) the script in /etc/qemu-ifup. This script is called whenever you run QEMU, to configure interfaces necessary to make networking work on QEMU:


sudo /sbin/ifconfig $1 netmask 0xffffff00 up

# sudo /sbin/iptables -t nat -A POSTROUTING -o $IFNAME_INTERNET -j MASQUERADE
# sudo /bin/sh -c "/bin/echo 1 > /proc/sys/net/ipv4/ip_forward"

Comment out the last three lines (and set them up accordingly to your environment) if you want NAT and it is not enabled by default.


I may had done something wrong, but I've had to make sure that the address I used on the script ( was not in the network of any of other interfaces (virtual or not) setup on the same machine.

At last, we just call qemu:

qemu -hda <your-image> -net nic -net tap,ifname=tap0 -localtime -m 256


QEMU will call the /etc/qemu-ifup script and pass tap0 as parameter, configuring it. I think it can be configured manually, without using the /etc/qemu-ifup script, but I didn't try that.

Finally, after booting your machine on QEMU, configure the network interface of the emulated machine with a IP on the same network of tap0:

# ifconfig eth0

And networking on QEMU is working.

Running qemu without X11 support

By default, qemu uses SDL to render output. If you want it to use the terminal you are running it, use:

$ qemu -nographic -curses ...other options

This link help me with that.