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

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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Similar Posts