UbuntuNetworking

From KVM

Setting up NAT with KVM in Ubuntu

Normally, the best way to do guest networking is by setting up a bridge. However, if you use a wireless card, are frequently switching network interfaces or using VPN, or for other reason want to do NAT, then this outlines the easiest way to set it up with Ubuntu. The setup here is very similar to what libvirt uses for QEMU networking.

Install Dependencies

sudo apt-get install bridge-utils dnsmasq

Check Kernel Modules

Make sure that all of the following modules are around: either built in to the kernel or loadable. If you are running a plain distribution kernel, you should have all of these around without doing anything extra.

# lsmod
Module                  Size  Used by
tun                    15616  0      
bridge                 64104  0      
llc                    11504  1 bridge
xt_state                5248  1       
ipt_MASQUERADE          6720  1       
iptable_nat            10512  1       
nf_nat                 24344  2 ipt_MASQUERADE,iptable_nat
nf_conntrack_ipv4      21776  3 iptable_nat               
nf_conntrack           76688  5 xt_state,ipt_MASQUERADE,iptable_nat,nf_nat,nf_conntrack_ipv4

Enabling ip forwarding

Add the following line to the bottom of /etc/sysctl.conf

net.ipv4.ip_forward=1

Configuring a bridge

Add the following to the bottom of /etc/network/interfaces

auto br0
iface br0 inet static
address 10.0.1.1
netmask 255.255.255.0
network 10.0.1.0
bridge_stp off
bridge_maxwait 5
bridge_ports none
pre-up iptables-restore < /etc/iptables.up.rules

Installing config files

  1. Copy dnsmasq.conf to /ec/dnsmasq.conf
  2. Copy iptables.up.rules to /etc/iptables.up.rules
  3. Copy qemu-ifup to /etc/qemu-ifup and change permissions to 755