Install and Use LXC to Manage Containers in Ubuntu 20.04

Install LXC and LXC templates on Ubuntu 20.04: create, start, stop, snapshot, and manage Linux containers from the command line and LXC Web Panel.

LXC (Linux Containers) is an OS-level virtualization method for running multiple isolated Linux systems (containers) on a single host, using one Linux kernel. Unlike full virtual machines, LXC containers share the host kernel and are lightweight, making them useful for development, testing, and isolated workloads. This guide shows how to install and use LXC on Ubuntu 20.04, then optionally add the LXC Web Panel for browser-based management.

Prerequisites

  • Ubuntu 20.04 server or desktop with sudo access.
  • Enough disk space for container root filesystems (a minimal Ubuntu container is typically a few hundred MB).

Install LXC

Install the LXC userspace tools and templates:

1
2
sudo apt update
sudo apt install -y lxc lxc-templates

Check that the host kernel supports LXC:

1
lxc-checkconfig

You should see required and optional kernel features; all “required” should be enabled.

Create an LXC container

LXC ships with ready-made templates. List them:

1
ls /usr/share/lxc/templates/

Create a container interactively (you’ll be prompted for distribution, release, and architecture):

1
2
3
sudo lxc-create --template download --name u1
# or
sudo lxc-create -n new-container -t ubuntu

To create non-interactively, pass distribution, release, and architecture on the command line (replace DISTRO-SHORT-CODENAME with e.g. focal for Ubuntu 20.04):

1
2
3
sudo lxc-create -t download -n u1 -- --dist ubuntu --release focal --arch amd64
# or short form
sudo lxc-create -t download -n u1 -- -d ubuntu -r focal -a amd64

List existing containers:

1
lxc-ls

Start and use the container

Start the container in the background (-d daemonizes):

1
lxc-start -n new-container -d

Attach to the container console (log in as root; exit with Ctrl+A then Q to detach):

1
lxc-console -n new-container

Inspect the container state:

1
lxc-info -n new-container

Start and stop (foreground vs background is controlled by whether you use -d):

1
2
lxc-start -n new-container
lxc-stop -n new-container

Pause and unpause

Pause (freeze) and resume the container:

1
2
lxc-freeze -n new-container
lxc-unfreeze -n new-container

Snapshots

Take a snapshot (the container should be stopped for a consistent snapshot):

1
2
lxc-stop -n new-container
lxc-snapshot -n new-container

List snapshots:

1
lxc-snapshot -L -n new-container

Restore a snapshot (e.g. snap0):

1
lxc-snapshot -r snap0 -n new-container

Delete a container

Stop the container, then destroy it (this removes the root filesystem):

1
2
lxc-stop -n new-container
lxc-destroy -n new-container

LXC Web Panel (optional)

LXC Web Panel is a web UI to create, start, stop, clone, and delete LXC containers from a browser.

Install (run as root or with sudo; review the script before piping to shell):

1
wget https://lxc-webpanel.github.io/tools/install.sh -O - | sudo bash

When installation finishes, open http://<your-server-ip>:5000 in a browser. Log in with the default credentials (admin / admin) and change the password after first login.

Update the panel later with:

1
wget https://lxc-webpanel.github.io/tools/update.sh -O - | sudo bash

If you access it remotely, allow port 5000 in your firewall (e.g. sudo ufw allow 5000/tcp && sudo ufw reload).

Verifying the setup

  • LXC: lxc-checkconfig should show required kernel features enabled; lxc-ls should list your containers.
  • Container: After lxc-start -n new-container -d, lxc-info -n new-container should show state “RUNNING”; lxc-console -n new-container should give you a shell inside the container.
  • Web Panel: With the panel installed, http://localhost:5000 (or your server IP:5000) should show the login page.

Summary

You installed LXC and lxc-templates on Ubuntu 20.04, created containers with lxc-create (interactive or with --dist/--release/--arch), and used lxc-start, lxc-stop, lxc-console, lxc-info, lxc-freeze/lxc-unfreeze, lxc-snapshot, and lxc-destroy to manage them. Optionally, you added the LXC Web Panel for GUI management. For more automation and image-based workflows, consider LXD (the newer daemon and CLI built on LXC).

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy