LXD in 4 Easy Steps

joemcmanus

on 29 August 2019

Tags: LXD

This article is more than 5 years old.



I needed to install a clean instance of Bionic to test some code, but I did not want to use a full virtual machine as I was in a hurry.  To do this, I used LXD to quickly deploy new Bionic and Xenial instances in minutes. 

If you are not familiar with LXD, it is a next generation container management system that is more like a VM than a traditional container. If you find yourself in a similar spot, here are the 3 commands to get an instance of Linux running and fourth to get you logged in. 

TL;DR 

snap install lxd

lxd init

(accept the default/yes to everything)

lxc launch ubuntu:18.04 bionic

lxc exec bionic -- /bin/bash

For the more inquisitive reader, first you install LXD as a snap.

snap install lxd

Then you set up LXD using lxd init and select ‘yes’ to the defaults. 

lxd init

Next, install your system using lxc launch <the image> <nickname> . Below we install Ubuntu 18.04 and give it the nickname ‘bionic’. 

lxc launch ubuntu:18.04 bionic

To connect to the system, use lxc exec. 

lxc exec bionic -- /bin/bash

Now verify your guest OS. 

I specifically wanted to compare something on Xenial and Bionic, so I ran the same commands except I substituted 16.04 for 18.04. 

Create another LXD image of Xenial and execute bash to connect. 

That is your first step in to a larger world. 

What’s Next ? 

One task I do fairly often as a security engineer is set up a web service and attac^H^H to test it for vulnerabilities. With the LXD instances you have created so far they are only available on the host they are running on.  I usually test from a remote host – to do that with LXD you configure a LXD proxy. In the following example, we’ll set up a proxy to allow web traffic into our LXD container. 

lxc exec bionic bash

apt install lighttpd

You are going to want to make sure you are connecting to the right VM, create a page unique to your instance by editing /var/www/html/index.html 

On the host, not the container, you will run lxc config to add a proxy. 


lxc config device add bionic web proxy listen=tcp:0.0.0.0:80 connect=tcp:127.0.0.1:80

Now in your container, start your lighttpd service. 

lxc exec bionic bash`

service lighttpd start

Browse from your workstation to the IP of your lxd host. 

With that you know have a very lightweight virtual container environment capable of performing any number of tasks. 

Note For Ubuntu Server Users

These examples were all performed on an Ubuntu 19.04 Desktop, on Ubuntu Server LXD setup is even simpler. For those on Ubuntu 16.04 and 18.04 LXD is often pre-installed as a deb, if you want to switch to the snap run “snap install lxd” followed by “lxd.migrate”. On Ubuntu Server 18.10 and newer LXD is installed as a snap, simply run “lxd init” to begin using LXD.

Ubuntu cloud

Ubuntu offers all the training, software infrastructure, tools, services and support you need for your public and private clouds.

Newsletter signup

Get the latest Ubuntu news and updates in your inbox.

By submitting this form, I confirm that I have read and agree to Canonical's Privacy Policy.

Related posts

Join Canonical in London at Dell Technologies Forum

Canonical is excited to be partnering with Dell Technologies at the upcoming Dell Technologies Forum – London, taking place on 26th November. This prestigious...

Canonical announces the first MicroCloud LTS release 

Canonical announces the first MicroCloud LTS release. MicroCloud 2.1.0 LTS features support for single-node deployments, improved security posture, and more...

Join Canonical in Paris at Dell Technologies Forum

Canonical is thrilled to be joining forces with Dell Technologies at the upcoming Dell Technologies Forum – Paris, taking place on 19 November. This premier...