home | whoami

QEMU networking in 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

Run your VM as following:

qemu-system-x86_64 -m 512 \
    -enable-kvm \
    -net nic \
    -net tap,ifname=tap0,script=no,downscript=no \

Inside your VM, configure network using IP from network and use as a default route.

OpenBSD guest

ifconfig em0 up
route add default

It’s better to permanently save network configuration:

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


Masquerading should only be used for dynamically-assigned IP addresses.

If you have static IP then set it as a source IP implicitly for SNAT rule:

iptables -t nat -A POSTROUTING -o wlan0 -j SNAT --to
  1. QEMU/Networking
  3. OpenBSD Network Configuration