QEMU networking in NAT mode on Linux host

tags: qemu, linux, openbsd, netbsd

NAT mode

To configure networking on QEMU in NAT mode, bring up the tap interface tap0 and assign IP address to it:

ip tuntap add tap0 mode tap
ip addr add dev tap0
ip link set tap0 up

Enable kernel IP forwarding and masquerading on internet connected interface:

echo 1 > /proc/sys/net/ipv4/ip_forward

iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE

In my case, it’s wlan0 wireless interface.

If you have static IP addresses, then you can set it as a source IP implicitly for SNAT rule(instead of masquerading):

iptables -t nat -A POSTROUTING -o wlan0 -j SNAT --to

Run your VM as following:

qemu-system-x86_64 -m 512 \
    -enable-kvm \
    -net nic,model=virtio,macaddr=00:00:00:00:00:01 \
    -net tap,ifname=tap0,script=no,downscript=no \

Bridge mode

Create bringe

brctl addbr br0
ip link set dev br0 up
ip addr add dev br0

Prepare the network 2 – the tun/tap for the virtual machine

ip tuntap add tap0 mode tap
brctl addif br0 tap0

Inside your VM, configure network interface and use as a default route.

OpenBSD guest

ifconfig em0 up
route add default

To permanently save network configuration run:

echo "inet" > /etc/hostname.em0
echo "" > /etc/mygate
sh /etc/netstart

NetBSD guest

ifconfig wm0 up
route add default

Save network configuration:

cat > /etc/ifconfig.wm0 << EOF
up netmask

echo 'defaultroute=""' >> /etc/rc.conf
sh /etc/rc.d/network restart
  1. QEMU/Networking
  3. OpenBSD Network Configuration
  4. QEMU Documentation/Networking/NAT