Installing, Creating and Managing LXC in Ubuntu / Debian

Thank you for reading this post, don't forget to subscribe!
Over the past decade LXC, the open source community has seen a steady transition to container usage as the most popular way to deploy applications, thanks to its many benefits such as portability, flexibility, increased security, and easier application management. Popular container technologies include Docker, Podman, and LXD.
Go language written, LXD (pronounced Lexse) is described as the next generation of system storage and virtual machine management software that allows you to manage your repositories and virtual machines from the command line or by using REST API or other third party tools. LXD is an open source project and its extension LXC (Linux containers), which is an operating system-level virtualization technology.
LXC came into the picture around 2008, and LXD was launched 7 years later in 2015 with the same building blocks as LXC. LXD made containers more user-friendly and easier to manage.
As an extension LXC, LXD offers advanced features such as snapshots and real-time transfer. It also provides a daemon that allows you to easily manage repositories and virtual machines. It is not meant to be displaced LXC, but is intended to improve the availability and handling of LXC-based containers
In this guide, we will show you how to create and manage LXC using containers LXD on Debian / Ubuntu.
Step 1: Install LXD on Ubuntu
The first step is to install LXD. There are two ways to do this, you can install Ubuntu from the repository using the APT package manager, or you can use a snap.
Using APT, please upgrade your system first:
$ sudo apt update
Then install LXD system container hypervisor as follows.
$ sudo apt install lxd

Using snap, you can install the latest version LXD.
$ sudo snap install lxd
Additionally, you can install the latest LTS release that is LXD 4.0 as follows:
$ sudo snap install lxd --channel=4.0/stable
You can check the version LXD installed as shown:
$ lxd --version

If you used snap, you can make sure that LXD The snap package was installed as shown:
$ snap list

Step 2: Initialize the LXD service
To initialize or boot LXD container hypervisor, run the command:
$ sudo lxd init
The command asks you a series of questions about configuring LXD. The default settings work just fine, but feel free to customize your settings to suit your needs.
In this example, we have created a storage pool called tecmint_pool with ZFS file system and volume management. For other issues, we’ve chosen to choose the default options. An easy way to accept the default selection is to press the ENTER button on your keyboard.

Execute the command to verify the information you provided:
$ sudo lxc profile show default

You can further restrict it to the created storage pool. The commands below show information about the current storage reserves.
$ sudo lxc storage list $ sudo lxc storage show tecmint_pool

You can also display information about the network interface you are using LXD, in this case, lxdbr0, which is the default selection.
$ sudo lxc network show lxdbr0

Step 3: Create LXD containers in Ubuntu
Now let’s change gears and create Linux repositories. You can list all the pre-built containers that can be loaded with the command:
$ sudo lxc image list images:
This fills a huge list of all the repositories of different operating systems like Ubuntu, CentOS, Debian and AlmaLinux to name a few.
To limit it to a specific distribution, follow these steps:
$ sudo lxc image list images: | grep -i centos $ sudo lxc image list images: | grep -i debian
This example lists the available tanks.
$ sudo lxc image list images: | grep -i ubuntu

Now we are going to create our first container. The syntax for creating a container is as follows:
$ sudo lxc launch images:{distro}/{version}/{arch} {container-name}
Now we are going to create two containers Ubuntu 20 and Debian 10 respectively:
$ sudo lxc launch images:ubuntu/focal tecmint-con1 $ sudo lxc launch images:debian/10 tecmint-con2
In the examples above, we have created two containers: tecmint-con1 and tecmint-con2.
To list the created containers, run the command:
$ sudo lxc list
From the printout, we see two of our containers in the list.

To gain shell access a LXC container run command:
$ sudo lxc exec tecmint-con1 bash
Once you have obtained shell access, note that the prompt changes to indicate that you are using as an administrator.

Exit the cache by running the command:
$ exit
Step 4: Manage LXD Repositories in Ubuntu
Now let’s look at some of the commands you can use to manage LXD tanks.
To list all running containers, run the command:
$ sudo lxc list

To display detailed information LXC container, use syntax:
$ sudo lxc info container-name
This will give you information such as the name of the repository, architecture, creation date, network connections, bandwidth, CPU, memory and disk usage to name a few.

To stop an LXC container, use syntax:
$ sudo lxc stop container-name
For example, to stop a container tecmint-con1, run the command:
$ sudo lxc stop tecmint-con1
Re-list the containers to make sure the container is stopped.
$ sudo lxc list

Alternatively, you can list either running or stopped containers as follows:
$ sudo lxc list | grep -i STOPPED $ sudo lxc list | grep -i RUNNING

To start with an LXC container, use syntax:
$ sudo lxc start container-name
For example, to start a container tecmint-con1 run command:
$ sudo lxc start tecmint-con1

To start or stop containers, skip them with a single space-separated command using the following syntax:
$ sudo lxc stop container1 container2 $ sudo lxc start container1 container2
For example, to stop all containers, run:
$ sudo lxc stop tecmint-con1 tecmint-con2
To restart LXC container, use syntax:
$ sudo lxc restart container-name
For example, to restart the container tecmint-con1 run command:
$ sudo lxc restart tecmint-con1
Alternatively, you can pass multiple containers in a single command:
$ sudo lxc start container1 container2
For example, to restart all containers, run:
$ sudo lxc restart tecmint-con1 tecmint-con2
To delete an LXC tank, stop it first and then remove it. For example, to remove a container tecmint-con2, run the command:
$ sudo lxc stop tecmint-con2 $ sudo lxc delete tecmint-con2

This guide has given you a solid foundation LXD containers and how to launch, create and manage containers. We hope you can now comfortably launch and manage your containers without much difficulty.