LXC on Ubuntu

This post talks about how to setup LXC (Linux Containers) on Ubuntu 12.04. LXC is an operating system-level virtualization technology, which allow you to run multiple virtual machines on one host.

There are quite a few limitations to this type of virtualization, when compared with the type of full, emulator style virtualization that VMware, VirtualBox etc… use. One of the main ones is that you’ll be unable to run different operating systems on the virtualization host. i.e. we can only run Linux VM’s on our host.

The big advantage is performance. Because the host doesn’t have to bother with all of the code which does virtual hardware emulation, the virtual machines run a lot faster in general.

So, to setup LXC, we first need to install it:

sudo apt-get install lxc lxctl uuid btrfs-tools

The ‘lxctl’, ‘uuid’ and ‘btrfs-tools’ packages aren’t really needed, but come recommended, and it doesn’t hurt to install them.

Now, at this point I did a reboot, which may not be necessary, and afterwards checked the LXC configuration using:


You should find that all of the different settings are set to “enabled”.

Now that we’ve got LXC installed, we can go ahead and start creating our first VM. Luckily, the ‘lxc’ package comes with a set of template scripts, which make setting up a VM easy. These scripts are located under ‘/usr/lib/lxc/templates’ and to create our first VM we run:

sudo /usr/lib/lxc/templates/lxc-ubuntu -p /lxc/vm0/

Where ‘/lxc/vm0’ is the path to the VM. Note that you will have to create this directory as it doesn’t exist by default.

Once you run that script, you’ll see a lot of output from the VM getting created and initialized. From the output and from looking at the template script, it looks like the template takes all of the currently installed packages, copies them over to the VM filesystem and configures them.

Once, this is done, we need to configure the networking, which I found to be the trickiest part of the setup. Because the guest VM is using the same hardware as the host, it has the ability to use the network interface attached to the host. Now, obviously you don’t want both the host and the guest using the same interface, as it will lead to IP address and MAC address conflicts. So, the guest should have a distinct MAC and/or IP address.

There are several ways to configure the networking for the guest VM’s and there are example configuration files of the many ways that they can be configured under file:///usr/share/doc/lxc/examples/ (note that you can enter this location into your web browser and it should load). For our purposes, we’re going to go with the lxc-veth.conf file, which sets up a virtual network interface connected to a network bridge which we have to create.

So, firstly, we need to create a network bridge, which is done by adding the following lines to the /etc/network/interfaces file:

auto br0

iface br0 inet dhcp

bridge_ports eth0

This will create the bridge that we’re going to connect the virtual network interface of the VM to. In order to enable it, restart the ‘networking’ service:

sudo service networking restart

Note that we have to create a bridge, even if we’ve only got one physical interface to connect to it.

Then add the relevant lines from the example config to the VM configuration file, under /lxc/vm0/config:

lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = br0
lxc.network.hwaddr = 4a:49:43:49:79:bf
lxc.network.ipv4 =

Note that the IP network should match that of your host interface, otherwise you might have some problems getting an IP through DHCP. Also, note that the MAC address is just taken from the example file an was probably randomly generated.

That should complete the configuration. We are now free to start up the VM using the ‘lxc-start’ command:

sudo lxc-start -n vm0 -f /lxc/vm0/config

This should start up the VM and bring up the console on the terminal screen. For the default Ubuntu template, you can log in using ubuntu/ubuntu username/password pair.

Once you’ve logged in, you can confirm that the VM has a different IP address to that of the host and start configuring it. 

Funnily enough, just as I’ve finished writing up this post, I stumbled upon a Launchpad blog post talking about how they’re using LXC to speed up their testing: http://blog.launchpad.net/general/parallelising-the-unparallelisable

One Reply to “LXC on Ubuntu”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.