Linux container virtualization (LXD) is similar to Virtual machines (e.g. KVM) but with more speed, efficiency and much lighter compared to virtual machines. LXD containers for OS like Ubuntu, Centos, Arch Linux, etc are easy to create. LXD uses the same Linux kernel which reduces the overhead of the addition of such kernels.
In this article, we will show the installation and configuration process of LXD on Ubuntu 20.04 server. Also, we will explain how to start an LXD container and how to run commands inside the container.
Install LXD on Ubuntu 20.04
To install LXD on Ubuntu 20.04 server, simply update the cache of the repository and install by using the apt package manager as shown below.
$ sudo apt update && sudo apt upgrade -y
Install the LXD with the command as shown below.
$ sudo apt install lxd -y
Or, install it with the use of snap commands.
$ sudo snap install lxd --channel=4.0/stable
Add user to the lxd group
It is recommended to add the user that you are currently logged in, in my case the user is named ‘ubuntu’ to the lxd group. This makes usage and maintenance of the LXC containers easier. Run the command as shown below for the purpose.
$ sudo adduser ubuntu lxd
To verify if the user is added to the lxd group, run the command as shown below.
To install the zfs for the storage backend
As a storage backend, zfs filesystem is mostly used for the LXD. To install zfs, run the command as shown below.
$ sudo apt update $ sudo apt install zfsutils-linux -y
To configure the LXD
To configure lxd on the Ubuntu 20.04 server, run the command as shown below with the use of the above zfs.
$ sudo lxd init
Get a list of available LXD container images
Simply, you are able to check the list of built-in LXD image, run the command as shown below
$ lxc image list images: $ lxc image list images: | grep -i centos
For example, let’s check the output of this command.
$ lxc image list images: | grep -i ubuntu
Create an LXD container
LXD containers are created with the use of lxc command. To do that, use lxc command with the image name or from the remote list. Check the remote list with the command as shown below.
$ lxc remote list
To create the lxd container by using remote as ubuntu, run the command as shown below.
$ lxc launch ubuntu:20.04 container_name
Here, we are creating a container with the container name “test” with the operating system “ubuntu 20.04” by using the command as shown below.
$ lxc launch ubuntu:20.04 test
The container image will be downloaded and the cache of the image will be stored so the same image is used if needed again. You will see the output as shown below after the successful execution of the above command.
To check the details of the container like name, state, ipv4 and ipv6 address, type and snapshots, run the command as shown below.
$ lxc ls
To run a command inside the container
After successfully creating the container, you are able to run commands inside it. It can be done either with a simple lxc command or by entering it bash. For further details, check the commands as shown below.
$ lxc exec container_name — command
$ lxc exec test apt update
For the bash shell, run the command as shown below.
$lxc exec container_name bash
$ lxc exec test bash
To start, Stop and delete the LXD container
To start the LXD container, run the command as shown below.
$ lxc start container_name
$ lxc start test
To stop the LXD container, run the command as shown below.
$ lxc stop container_name
$ lxc stop test
To delete the LXD container, run the command as shown below.
$ lxc delete container_name
$ lxc delete test
In this article, you have learned how to install and configure the LXD. I’ve also shown you the process of creating an lxd container and running commands through bash shell inside the container.