How to install LXD container hypervisor on Ubuntu 16.04 LTS

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? How do I setup and use LXD on Ubuntu Linux 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 setup and use LXD on Ubuntu Linux 16.04 server.

Install LXD on Ubuntu

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

Fig.01 How to install LXD container hypervisor  on Ubuntu

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: 
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:


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:


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

To create and start containers from images use the launch command:
lxc launch images:{distro}/{version}/{arch} {container-name-here}
Let us see some examples to create and start containers from various Linux distro images as per your needs.

Alpine Linux 3.7 VM

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

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

CentOS Linux 7 VM

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

Ubuntu Linux 16.4 “xenial” LTS VM

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

Debian Linux 9.x “stretch” VM

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

Arch Linux VM

$ lxc launch images:archlinux/amd64 arch-c1

Gentoo Linux VM

$ lxc launch images:gentoo/amd64 gentoo-c1

Fedora Linux 26 and 27 VM

$ lxc launch images:fedora/26/amd64 fedora26-c1
$ lxc launch images:fedora/27/amd64 fedora27-c1

OpenSUSE Linux 42.3 VM

$ lxc launch images:opensuse/42.3/amd64 opensuse

Oracle Linux 7.x VM

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

How do I list all my containers?

List the existing containers:
$ lxc list --fast
$ lxc list | grep RUNNING
$ lxc list | grep STOPPED
$ lxc list | grep
$ lxc list "*c1*"
$ lxc list "*db*"
$ lxc list

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?

To run or execute commands in containers use exec command:
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?

Start containers using the following cli:
$ lxc start containerName
$ lxc start foo
$ lsc start centos-db opensuse

How do I stop my containers?

Stop containers using the following syntax:
$ lxc stop containerName
$ lxc stop foo
$ lsc stop centos-db opensuse

How do I restart my containers?

Want to restart your containers for any reasons? Try:
$ lxc restart containerName
$ lxc restart foo
$ lsc restart centos-db opensuse

How do I delete my containers?

The command 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 user@centos-db
$ ssh user@centos-db-ip-address-here

And, there you have install lxd on Ubuntu 16.04, up and running with distr of your choice. See the following urls for more info:

This entry is 1 of 20 in the LXD Tutorial series. Keep reading the rest of the series:
  1. Install LXD container hypervisor on Ubuntu 16.04 LTS
  2. How to install and setup LXC (Linux Container) on Fedora Linux 26
  3. Set up LXD container under KVM or Xen virtual machine
  4. List VM images in LXD (Linux Containers)
  5. Upgrade LXD containers powered by Ubuntu/Debian or CentOS Linux
  6. Auto start LXD containers at boot time in Linux
  7. Command to rename LXD / LXC container
  8. Run commands on Linux Container (LXD) instance at provision launch time
  9. Use LXD (Linux containers) in a shell script to create VM when the cloud instance launches
  10. Move/migrate LXD VM to another host on Linux
  11. Fedora install and set up LXD
  12. CentOS 7.x install and set up LXD server
  13. Install LXD pure-container hypervisor on Ubuntu 18.04 LTS
  14. Create snapshots with lxc command for LXD
  15. Set up and install LXD on CentOS/RHEL 8
  16. Ubuntu 20.04 LTS install and set up LXD
  17. Full backup and restore LXD containers
  18. Disable firewall and NAT rules on the LXD bridge
  19. Delete or remove LXD container using the lxc
  20. Linux Filesystem Error: Transaction failed solution

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • justin Nov 21, 2016 @ 20:28

    Do you have a moment to go over how I can back up lxd containers? I can’t really find a good answer to this…

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum