From KVM
Revision as of 16:34, 16 May 2015 by Bsd (talk | contribs) (Bsd moved page AnthonyLiguori/Networking to UbuntuNetworking without leaving a redirect: Moved to more relevant heading)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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


Configuring a bridge

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

auto br0
iface br0 inet static
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 /etc/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