How To Install And Use Linux Containers (LXC) On Ubuntu?


LXC (Linux containers) is the operating system-level virtualization which is used for running multiple isolated Linux systems called containers on a host machine. Unlike a virtual machine that requires dedicated system resources, Linux containers shares kernel and other resources of the host system.

These isolated Linux systems have their own namespace for filesystem, network, PID, CPU, memory allocation, etc. The feature such as cgroups and namespaces are used to control the resources and spaces.

Linux containers are similar to FreeBSD Jails or Solaris zones. Since LXC shares the host kernel and resources it is lightweight and faster than other full-fledged virtualization solutions such as  KVM or VMware, etc.

How to install LXC on Ubuntu

To install the LXC on Ubuntu or other distributions based on it, you can use the following command –

First, update your system with the latest available packages by using –

sudo apt update

and then use –

sudo apt install lxc lxc-templates -y

after installing you can check the LXC configuration by using –

lxc-checkconfig

You will see the output like this –

Display the list of templates

LXC templates are the scripts that are called by the lxc-create command to create Linux containers. There are many ready-made templates are available that can be used to create Linux containers.

Now to display the list of available templates use –

ls /usr/share/lxc/templates/

Create an LXC container

To create your first container in LXC, use the following command in your terminal –

sudo lxc-create -t download -n my-container

This command will display the list of distributions with version and architecture. Choose one of them and enter the details as it is asking at the end of the output.

For example, you will be asked for distribution name, release, and architecture once you enter these details it will start downloading image –

You can check the list of created containers by using –

sudo lxc-ls

Working with LXC containers

Starting a container

To start created container use the following command –

lxc-start -n my-container

You can then confirm its status with –

lxc-info -n my-container

Next, get the console of the running container by using –

lxc-attach -n my-container

You can exit from the container anytime by executing the exit command.

Stop a running container

You can use the following command to stop a running LXC container by using the following command –

lxc-stop -n my-container

Deleting a container

If you want to delete an LXC container then first you need to stop the container if it is running –

Use the given command to stop the container –

lxc-stop -n my-container

And then use the following command to delete the container –

lxc-destroy -n my-container

To delete container along with all snapshots related to that container use –

lxc-destroy -n my-container -s

Taking a snapshot of the running container

You can take the snapshot of the running container for that first you need to stop the running container. Use the following command to stop the running container –

lxc-stop -n my-container

and then use the given command to take the snapshot –

lxc-snapshot -n my-container

By using the above LXC commands you can create, manage, and delete a Linux container.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.