How to install LXD container hypervisor on Ubuntu 16.04 LTS server

Posted on in Categories , , last updated April 26, 2017

How do I install LXD container “hypervisor” to runs unmodified Debian/Ubuntu/CentOS Linux operating systems VM at incredible speed on a Ubuntu Linux 16.04 LTS server?

LXD is lxc on steroids with strong security on the mind. LXD is not a rewrite of LXC. Under the hood, LXD uses LXC through liblxc and its Go binding. In this tutorial, you will learn to set up LXD on a Ubuntu Linux server.
LXD on Ubuntu

Install LXD

Type the following apt-get command:
$ sudo apt install lxd
OR
$ sudo apt-get install lxd
Sample outputs:

Fig.01 Installing LXD on a Ubuntu Linux 16.04 LTS
Fig.01 Installing LXD on a Ubuntu Linux 16.04 LTS

Configure the LXD networking and storage for VM

I suggest you use ZFS and along with DHCP based IP networking for ease of setup for each VM. The ZFS offers quick option to make snapshots and other advanced features. See how to install ZFS on Ubuntu server for more information. The following command displays my ZFS setup for all VMs:
$ sudo zfs list
Sample outputs:

[sudo] password for vivek: 
NAME              USED  AVAIL  REFER  MOUNTPOINT
nixcraft          744K   461G   192K  /nixcraft
nixcraft/lxdvms   192K   461G   192K  /nixcraft/lxdvms

To setup both storage and networking option, enter:
$ sudo lxd init
Sample outputs:

Name of the storage backend to use (dir or zfs) [default=zfs]: 
Create a new ZFS pool (yes/no) [default=yes]? no
Name of the existing ZFS pool or dataset: nixcraft/lxdvms
Would you like LXD to be available over the network (yes/no) [default=no]? 
Do you want to configure the LXD bridge (yes/no) [default=yes]? 

You need to configure the LXD dhcp based bridge as prompted:

Say hello to lxc command

The lxc command used to talk with LXD server. The lxd package creates a new “lxd” group which contains all users allowed to talk to lxd over the local unix socket. In other words your username must be part of lxd group:
$ id username
$ id vivek
$ grep lxd /etc/group

Sample outputs:

lxd:x:110:vivek

If vivek (or any other user) user is not part of lxd group you will get an error that read as follows when you try to use lxc command:

Permission denied, are you in the lxd group?

Please note that group membership is only applied at login or ssh session, you then either need to close and re-open your user/ssh session or use the following command to avoid above error:
$ newgrp lxd
Verify that the lxc client is talking to the LXD daemon:
$ lxc list
Sample outputs:

+------+-------+------+------+------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+------+-------+------+------+------+-----------+

How do I list available images for various Linux distro?

$ lxc image list images:
Sample outputs:

Fig.02: Listing all images from default remote server
Fig.02: Listing all images from default remote server

Most of the images are known by several aliases. To see a list, enter:
$ lxc image alias list images:
$ lxc image alias list images: | grep -i centos
$ lxc image alias list images: | grep -i arch
$ lxc image alias list images: | grep -i debian

How create and use your first container

The syntax is:
lxc launch images:{distro}/{version}/{arch} {container-name-here}

Create a Alpine Linux 3.4 VM

$ lxc launch images:alpine/3.4/amd64 alpine-www
Sample outputs:

Creating alpine-www
Retrieving image: 100%
Starting alpine-www

Create a CentOS Linux 7 VM

$ lxc launch images:centos/7/amd64 cenots-db

Create a Ubuntu Linux 16.4 “xenial” LTS VM

$ lxc launch images:ubuntu/xenial/amd64 ubuntu-nginx

Create a Debian Linux 7.x “wheezy” VM

$ lxc launch images:debian/wheezy/amd64 file-server

Create a Debian Linux 8.x “jessie” VM

$ lxc launch images:debian/jessie/amd64 file-server

Create a Arch Linux VM

$ lxc launch images:archlinux/amd64 arch-c1

Create a Gentoo Linux VM

$ lxc launch images:gentoo/amd64 gentoo-c1

Create a Fedora Linux 24 VM

$ lxc launch images:fedora/24/amd64 fedora24-c1

Create a Fedora Linux 25 VM

$ lxc launch images:fedora/25/amd64 fedora25-c1

Create a Ppensuse Linux 13.2 VM

$ lxc launch images:opensuse/13.2/amd64 opensuse

Create a Opensuse Linux 42.2 VM

$ lxc launch images:opensuse/42.2/amd64 opensuse-42-2

Create a Oracle Linux 7.x VM

$ lxc launch images:oracle/7/amd64 oracle-proxy-c1

How do I list all my containers?

Type the following command:
$ lxc list --fast
$ lxc list | grep RUNNING
$ lxc list | grep STOPPED
$ lxc list | grep
$ lxc list "*c1*"
$ lxc list "*db*"
$ lxc list

Sample outputs:

Fig.03 Lists the available resource for the LXD
Fig.03 Lists the available resource for the LXD

How do I execute/run the specified command in a container?

The syntax is:
lxc exec containerName -- command
lxc exec containerName -- /path/to/script
lxc exec containerName --env EDITOR=/usr/bin/vim -- command
### run date, ip a, ip rm and other commands on various containers ###
$ lxc exec cenots-db -- date
$ lxc exec opensuse -- ifconfig
$ lxc exec ubuntu-nginx -- ip r
$ lxc exec fedora24-c1 -- dnf update
$ lxc exec file-server -- cat /etc/debian_version

Sample outputs:

Fig.03: Run commands on in a container
Fig.03: Run commands on in a container

How do I get the bash shell access in a container?

To gain login and gain shell access in a container named file-server , enter:
$ lxc exec file-server bash
Now you can run commands or install packages:
# cat /etc/*issue*
# apt-get update
# apt-get upgrade

To exit simply from container simply type exit:
# exit
Sample session:

Fig.04: Gain a  bash shell access in container
Fig.04: Gain a bash shell access in container

How do I start my containers?

The syntax is:
$ lxc start containerName
$ lxc start foo
$ lsc start centos-db opensuse

How do I stop my containers?

The syntax is:
$ lxc stop containerName
$ lxc stop foo
$ lsc stop centos-db opensuse

How do I restart my containers?

The syntax is:
$ lxc restart containerName
$ lxc restart foo
$ lsc restart centos-db opensuse

How do I delete my containers?

The syntax is (be careful as the LXD containers are deleted immediately without any confirmation prompt i.e. keep backups):
$ lxc delete containerName
$ lxc delete foo
$ lsc delete centos-db

You may get the following error while deleting the container:

The container is currently running, stop it first or pass –force.

To fix this:
$ lxc stop centos-db && lxc delete centos-db

How do I show information on LXD servers and containers?

Type the following command:
$ lxc info
$ lxc info containerName
$ lxc info cenots-db

Sample outputs:

Fig.05: How to see LXD containers memory, cpu, network usage info using lxc
Fig.05: How to see LXD containers memory, cpu, network usage info using lxc

Can I run the OpenSSH server in a container?

Yes. You can install and use the openssh-server just like a VPS or dedicated box. For example:
$ lxc exec centos-db -- yum install openssh-server
$ lxc exec cenots-db -- systemctl start sshd
$ ssh [email protected]
$ ssh [email protected]

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

1 comment

Leave a Comment