Install LXD on Ubuntu 20.04 LTS using apt

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?

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

ADVERTISEMENTS

Why use LXD on Ubuntu 20.04?

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

  1. Unprivileged containers (secure by design)
  2. Scalable
  3. Live migration
  4. Advanced resource control for CPU, memory, disk/network I/O, kernel modules and more)
  5. Hardware passthrough support for GPU, USB, NIC, disks and more
  6. Run containers
  7. Update containers
  8. Clustering support
  9. Ease of management
  10. Install different Linux distro inside containers
  11. 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

Install LXD on Ubuntu 20.04 Linux LTS using apt

For testing, the purpose selects the latest version else picks 4.0 for stable servers on next screen

LXD Package Installation

I choose version 4.0 snap track.

Alternatively, we can use the snap command directly as follows:
sudo snap install lxd --channel=4.0/stable
Setting up LXD on Ubuntu 20.04 LTS

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

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

Add user to LXD to run lxc commands on Ubuntu

Look ma no more permission errors and unprivileged user managing LXD

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:
Set up and configure LXD networking and storage options on Ubuntu 20.04 LTS
We can verify information by typing the following commands:
lxc profile list
lxc profile show default
lxc network list
lxc network show lxdbr0
lxc storage list
lxc storage show nixcraftzfs

lxc storage and networking profile

Gif.01: Exploring our LXD server storage and networking profile

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

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

LXD list images

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
Creating and using your first Linux container on Ubuntu Linux 20.04 LTS

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

lxc list containers command running on Ubuntu 20.04

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

lxc exec commands

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.

This entry is 16 of 19 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

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig 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

ADVERTISEMENTS
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.