Install LXD on Ubuntu 20.04 LTS using apt

ADVERTISEMENTS



Why use LXD on Ubuntu 20.04?

How can I install LXD pure-container hypervisor on Ubuntu 20.04 LTS Linux to runs unmodified Debian/Ubuntu/CentOS/Fedora/Arch Linux operating systems at incredible speed? How do I set up and use LXD on the Ubuntu Linux server? How do I install LXD on Ubuntu cloud server?: The LXD container system is supported and created by the Ubuntu team. It provides a 25-30% faster performance than Vmware ESX. 10 times the density with zero latency. One can move Linux VMs straight to containers using LXD without modifying the apps or administration processes. You can run CentOS, Arch Linux, Fedora Linux, OpenSUSE, Gentoo, Ubuntu, Debian and many other Linux distros in containers. This page shows how to install and configure LXD on Ubuntu 20.04 LTS.

LXD is a container experience providing a ReST API to manage LXC containers. One can do the following things with LXD:

Unprivileged containers (secure by design) Scalable Live migration Advanced resource control for CPU, memory, disk/network I/O, kernel modules and more) Hardware passthrough support for GPU, USB, NIC, disks and more Run containers Update containers Clustering support Ease of management Install different Linux distro inside containers Manage container resources, like storage volumes, map directories, memory/disk I/O restrictions, networking and more

Install and setting up LXD on Ubuntu 20.04 LTS

Let us see all step-by-step instructions to set up LXD:

Step 1 – Install LXD on Ubuntu 20.04 LTS Linux server

Make sure all security updates installed on Ubuntu either using the apt or apt-get command, enter:

sudo apt update

sudo apt upgrade

To install the stable branch of LXD, run the following apt command or snap command:

sudo apt install lxd



sudo snap install lxd --channel=4.0/stable

Step 2 – Add user to the LXD group for management purpose

Alternatively, we can use the snap command directly as follows:

No need to be the root user to manage LXD daemon. To manage LXD server add your username to lxd group using the adduser command on Ubuntu Linux:

sudo adduser {USERNameHere} lxd

sudo adduser vivek lxd

sudo adduser $USER lxd

The group membership is only applied at the next login, or a new ssh session started. Hence, either you need to close and re-open your user/ssh session or use the following newgrp command to apply for group memberships immediately:

newgrp lxd

Verify membership details with the id command:

id

Test it with the lxc command:

lxc list



Step 3 – Set up and configure LXD networking and storage options

Now that we installed LXD, it is time to set up the LXD server. You must configure networking and storage option such as directory, ZFS, Btrfs, and more, enter:

sudo lxd init

Next, you must answer a series of questions on how to configure the LXD server, including storage and networking option. Here is a sample session I set up for ZFS and 10.105.28.1/24 NATed subnet. Feel free to choose options as per your needs:



lxc profile list

lxc profile show default

lxc network list

lxc network show lxdbr0

lxc storage list

lxc storage show nixcraftzfs

Step 4 – Listing built-in LXD image for various Linux distros

We can verify information by typing the following commands:

To list all LXD images run:

lxc image list images:

lxc image list images: | grep -i centos

lxc image list images: | grep -i ubuntu

lxc image list images: | grep -i debian



Step 5 – Creating your first Linux container

Creating your first container is straightforward. The syntax is:

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

Alpine Linux 3.11 container

Your container here is called alpine-c1:

lxc launch images:alpine/3.11/amd64 alpine-c1



CentOS Linux 8 container

lxc launch images:centos/8/amd64 cenots-8-c2

Ubuntu Linux 16.04 or 18.04 or 20.04 LTS container

lxc launch images:ubuntu/xenial/amd64 ubuntu-xenial-c3

lxc launch images:ubuntu/bionic/amd64 ubuntu-bionic-c4

lxc launch images:ubuntu/focal/amd64 ubuntu-focal-c5

Debian Linux 9.x “stretch” or 10.x container

lxc launch images:debian/stretch/amd64 debian-9-c6

lxc launch images:debian/10/amd64 debian-10-www

Arch Linux container

lxc launch images:archlinux/amd64 arch-c7

Gentoo Linux container

lxc launch images:gentoo/amd64 gentoo-c8

Fedora Linux 31 container

lxc launch images:fedora/31/amd64 fedora-31-c9

OpenSUSE Linux 15.0 container

lxc launch images:opensuse/15.1/amd64 opensuse15-1-c10

Oracle Linux 8.x VM

lxc launch images:oracle/8/amd64 oracle-8-c11

Step 6 – Managing LXD based containers using the lxc CLI

Let us dive into essential commands to manage containers

List Linux container instances

We can list the existing containers using the following syntax and grep command/egrep command as pipe:

lxc list --fast

lxc list | grep RUNNING

lxc list | grep STOPPED

lxc list | grep -i opensuse

lxc list "*c1*"

lxc list "*c2*"

lxc list



How to execute/run the specified command in a container

We run or execute commands in containers using the exec command as follows:

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-8-c2 -- date

lxc exec cenots-8-c2 -- ip a

lxc exec ubuntu-focal-c5 -- ip r

lxc exec fedora-31-c9 -- dnf -y update

lxc exec debian-10-www -- cat /etc/debian_version



How to get the bash shell access in a container

Want to gain login/shell access in a container named debian-10-www, enter:

lxc exec {container-name} {shell-name}

lxc exec debian-10-www bash

lxc exec alpine-c1 sh

Now you can run commands or install packages. For instance, use the apt command/apt-get command inside container:

# cat /etc/*issue*

# apt update

# apt upgrade

Simply type exit to get back to host:

# exit

Start my containers command

lxc start {container-name}

lxc start oracle-8-c11

Stop my containers command

lxc stop {container-name}

lsc stop alpine-c1

Restart my containers

$ lxc restart {container-name}

lxc restart gentoo-c8

Delete LXD containers

The command is as follows. Be careful as the LXD containers are deleted immediately without any confirmation prompt. In other words, always keep backups:

lxc delete {container-name}

lxc delete ubuntu-xenial-c3

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 ubuntu-xenial-c3 && lxc delete ubuntu-xenial-c3

How do I show information on LXD servers and containers?

Type the following command:

lxc info

lxc info {container-name}

lxc info opensuse15-1-c10

Pull a file from the container

lxc file pull {continer-nane}/{path/to/file} {/path/to/local/dest}

lxc file pull ubuntu-xenial-c3/var/www/nginx/app/config.php .

Push a file to the container

lxc file push {/path/to/file} {continer-nane}/path/to/dest/dir/

lxc file push config.php ubuntu-xenial-c3/var/www/nginx/app/

Step 7 – Getting help about lxc commands

Getting help is easy:

lxc --help

lxc {command} --help

lxc list --help

Conclusion

In this in-depth tutorial, you learned how to install and set up LXD on Ubuntu 20.04 LTS server using the apt command to run various Linux distros. For further information see lxc commands and docs here.

