Install LXC and LXC UI on Ubuntu 22.04|20.04|18.04|16.04

Posted on 139 views

LXC is a lightweight Virtualization technology that is used to run multiple isolated virtual units often referred to as containers in a chroot environment on a single host using a single Linux kernel.

Some common terminologies used with LXC include:

  • chroot – Chroot also referred to as change root or change root jail is a section in the file system which is isolated from the rest of the file system. A program executed in this environment cannot access files outside the designated directory tree.
  • cgroups – This is a Kernel feature that allows aggregating or partitioning tasks (processes) and all their children into hierarchically organized groups to isolate resources.

Installing LXC on Ubuntu 22.04|20.04|18.04|16.04

LXC can easily be installed on Ubuntu from upstream repositories using the following commands:

sudo apt update
sudo apt install lxc

The above command will install lxc package and all dependencies required then configure a default container network. The name of the bridge is lxcbr0:

$ ip ad | grep lxc
3: lxcbr0:  mtu 1500 qdisc noqueue state DOWN group default qlen 1000
inet 10.0.3.1/24 scope global lxcbr0

After the installation, define uid mappings to run containers as non root user. See LXC man page docs for more details.

# Create directory if it doesn't exist
mkdir -p ~/.config/lxc

# Add configs required
echo "lxc.include = /etc/lxc/default.conf" > ~/.config/lxc/default.conf
echo "lxc.idmap = u 0 100000 65536" >> ~/.config/lxc/default.conf
echo "lxc.idmap = g 0 100000 65536" >> ~/.config/lxc/default.conf
echo "lxc.net.0.type = veth" >> ~/.config/lxc/default.conf
echo "lxc.net.0.link = lxcbr0" >> ~/.config/lxc/default.conf
echo "lxc.net.0.link = lxcbr0" >> ~/.config/lxc/default.conf
echo "$USER veth lxcbr0 2" | sudo tee -a /etc/lxc/lxc-usernet

List current LXC configs:

$ lxc-config -l
lxc.default_config
lxc.lxcpath
lxc.bdev.lvm.vg
lxc.bdev.lvm.thin_pool
lxc.bdev.zfs.root
lxc.cgroup.use
lxc.cgroup.pattern

Using LXC onUbuntu 22.04|20.04|18.04|16.04

You can use LXC in two modes:

  • Privileged – This is when you run lxc commands as root user.
  • Unprivileged – This is when you run commands as a non-root user.

Below is an example of essential commands to create an LXC container.

$ lxc-create -t download \
  -n mylxc-ubuntu -- \
  --dist ubuntu \
  --release focal \
  --arch amd64

Where:

  • -n for the name of the container
  • -t for a template.

If you get an error message “ERROR: Unable to fetch GPG key from keyserver“, you can use --no-validate option:

$ lxc-create -t download \
  -n mylxc-ubuntu -- \
  --dist ubuntu \
  --release focal \
  --arch amd64 \
  --no-validate

Sample output:

Downloading the image index
WARNING: Running without gpg validation!
Downloading the rootfs
Downloading the metadata
The image cache is now ready
Unpacking the rootfs

---
You just created an Ubuntu focal amd64 (20211228_07:42) container.

To enable SSH, run: apt install openssh-server
No default root or user password are set by LXC.

Once the container has been created, it is nor started by default. You can list all lxc containers using:

$ lxc-ls
mylxc-ubuntu

To start the container, run:

$ lxc-start -n 

# Example
$ lxc-start -n mylxc-ubuntu

Install LXC Web UI on Ubuntu

There are a number of tools that you can use to manage your LXC containers. On this article, we’ll install and use LXC Web Panel. Run this command to install it:

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

This will automatically install and configure LXC Web UI for you. You’ll get an output similar to below after installation.

022-01-07 08:56:55 (65.4 MB/s) - written to stdout [2679/2679]

 _     __   _______  __          __  _       _____                 _
| |    \ \ / / ____| \ \        / / | |     |  __ \               | |
| |     \ V / |       \ \  /\  / /__| |__   | |__) |_ _ _ __   ___| |
| |      > <| |        \ \/  \/ / _ \ '_ \  |  ___/ _` | '_ \ / _ \ |
| |____ / . \ |____     \  /\  /  __/ |_) | | |  | (_| | | | |  __/ |
|______/_/ \_\_____|     \/  \/ \___|_.__/  |_|   \__,_|_| |_|\___|_|


Automatic installer

Installing requirement...
+ Installing Python
+ Installing Python pip
E: Package 'python-pip' has no installation candidate
| + Flask Python...
Cloning LXC Web Panel...
Cloning into '/srv/lwp'...
remote: Enumerating objects: 243, done.
remote: Total 243 (delta 0), reused 0 (delta 0), pack-reused 243
Receiving objects: 100% (243/243), 198.33 KiB | 9.92 MiB/s, done.
Resolving deltas: 100% (108/108), done.

Installation complete!


Adding /etc/init.d/lwp...
Done
Starting server...done.
Connect you on http://your-ip-address:5000/

As you can see, the service is listening on port 5000. If you have a firewall, open the port so that you can access it from a remote device.

sudo ufw allow 5000

You can now open the URL http://your-ip-address:5000/ on your browser to access the dashboard.

lxc_web_ui

Login with user admin and password admin. Don’t forget to change the password after logging in.

Updating LXC Web Panel

To perform an automatic update run the commands:

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

You now have LXC and LXC Web Panel installed on your Ubuntu system. Enjoy using the tools to run containers in your system.

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.