Install LXC (Linux Containers) in RHEL, Rocky & AlmaLinux


Introduction

LXC, short for Linux Containers, is an operating system-level virtualization method that allows you to run multiple isolated Linux systems (containers) on a single host. It provides a lightweight and efficient alternative to traditional virtualization technologies. In this article, we will guide you through the process of installing LXC on RHEL, Rocky Linux, and AlmaLinux, three popular distributions known for their stability and security.

Prerequisites

Before proceeding with the installation, ensure that you have the following prerequisites −

  • A supported version of RHEL, Rocky Linux, or AlmaLinux.

  • Sudo or root access to the system.

  • An internet connection.

Step 1: Update the System

Start by updating the system packages to the latest versions. Open a terminal or SSH session and execute the following commands −

sudo yum update

Step 2: Install LXC

Next, we will install the LXC packages using the package manager. Execute the appropriate command based on your distribution −

Example

For RHEL and Rocky Linux −

sudo yum install epel-release
sudo yum install lxc lxc-templates lxc-extra

Output

$ sudo yum install lxc lxc-templates lxc-extra
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package lxc.x86_64 0:3.2.1-4.el7 will be installed
---> Package lxc-extra.x86_64 0:3.2.1-4.el7 will be installed
---> Package lxc-templates.noarch 0:3.2.1-4.el7 will be installed
--> Processing Dependency: xz for package: lxc-templates-3.2.1-4.el7.noarch
--> Running transaction check
---> Package xz.x86_64 0:5.2.2-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package              Arch         Version          Repository           Size
================================================================================
Installing:
 lxc                  x86_64       3.2.1-4.el7      extras               987 k
 lxc-extra            x86_64       3.2.1-4.el7      extras               1.4 M
 lxc-templates        noarch       3.2.1-4.el7      extras                66 k
Installing for dependencies:
 xz                   x86_64       5.2.2-1.el7      base                234 k

Transaction Summary
================================================================================
Install  3 Packages (+1 Dependent package)

Total download size: 2.7 M
Installed size: 10 M
Is this ok [y/d/N]: y
Downloading packages:
(1/4): lxc-extra-3.2.1-4.el7.x86_64.rpm               | 1.4 MB   00:01     
(2/4): lxc-3.2.1-4.el7.x86_64.rpm                     | 987 kB   00:01     
(3/4): lxc-templates-3.2.1-4.el7.noarch.rpm           |  66 kB   00:00     
(4/4): xz-5.2.2-1.el7.x86_64.rpm                      | 234 kB   00:00     
--------------------------------------------------------------------------------
Total                                           3.2 MB/s | 2.7 MB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : xz-5.2.2-1.el7.x86_64                                        1/4 
  Installing : lxc-3.2.1-4.el7.x86_64                                      2/4 
  Installing : lxc-templates-3.2.1-4.el7.noarch                            3/4 
  Installing : lxc-extra-3.2.1-4.el7.x86_64                                4/4 
  Verifying  : lxc-extra-3.2.1-4.el7.x86_64                                1/4 
  Verifying  : lxc-3.2.1-4.el7.x86_64                                      2/4 
  Verifying  : lxc-templates-3.2.1-4.el7.noarch                            3/4 
  Verifying  : xz-5.2.2-1.el7.x86_64                                        4/4 

Installed

For AlmaLinux −

Example

sudo dnf install epel-release
sudo dnf install lxc lxc-templates lxc-extra

Output

$ sudo dnf install lxc lxc-templates lxc-extra
Last metadata expiration check: 0:14:10 ago on Tuesday 10 January 2023 03:45:55 PM UTC.
Dependencies resolved.
================================================================================
 Package                  Arch         Version               Repository   Size
================================================================================
Installing:
 lxc                      x86_64       3.2.1-7.fc35          updates     853 k
 lxc-extra                x86_64       3.2.1-7.fc35          updates     1.3 M
 lxc-templates            noarch       3.2.1-7.fc35          updates      63 k

Transaction Summary
================================================================================
Install  3 Packages

Total download size: 2.2 M
Installed size: 9.7 M
Is this ok [y/N]: y
Downloading Packages:
(1/3): lxc-extra-3.2.1-7.fc35.x86_64.rpm                  204 kB/s | 1.3 MB     00:06    
(2/3): lxc-templates-3.2.1-7.fc35.noarch.rpm              133 kB/s |  63 kB     00:00    
(3/3): lxc-3.2.1-7.fc35.x86_64.rpm                       373 kB/s | 853 kB     00:02    
--------------------------------------------------------------------------------
Total                                                         294 kB/s | 2.2 MB     00:07     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : lxc-3.2.1-7.fc35.x86_64                                1/3 
  Installing       : lxc-templates-3.2.1-7.fc35.noarch                      2/3 
  Installing       : lxc-extra-3.2.1-7.fc35.x86_64                          3/3 
  Running scriptlet: lxc-extra-3.2.1-7.fc35.x86_64                          3/3 
  Verifying        : lxc-extra-3.2.1-7.fc35.x86_64                          1/3 
  Verifying        : lxc-templates-3.2.1-7.fc35.noarch                      2/3 
  Verifying        : lxc-3.2.1-7.fc35.x86_64                                3/3 

Installed:
  lxc-templates-3.2.1-7.fc35.noarch      lxc-3.2.1-7.fc35.x86_64     
  lxc-extra-3.2.1-7.fc35.x86_64         

Complete!

Step 3: Configure the LXC Network

LXC uses a bridge interface to provide networking capabilities to the containers. Let's configure the bridge by modifying the network configuration file.

Open the network configuration file in a text editor −

sudo vi /etc/sysconfig/network-scripts/ifcfg-lxcbr0

Add the following lines to the file −

DEVICE=lxcbr0
TYPE=Bridge
BOOTPROTO=dhcp
ONBOOT=yes
DELAY=0

Save the changes and exit the text editor.

Step 4: Start and Enable LXC Services

To start the LXC services and ensure they start automatically at boot, execute the following commands −

Example

sudo systemctl start lxc.service
sudo systemctl enable lxc.service

Output

$ sudo systemctl enable lxc.service
Created symlink /etc/systemd/system/multi-user.target.wants/lxc.service → /usr/lib/systemd/system/lxc.service.

Step 5: Verify the LXC Installation

To verify that LXC is installed correctly, let's create a simple container and check its status.

Create a new LXC container −

Example

sudo lxc-create -t download -n mycontainer -- -d centos -r 8 -a amd64

Output

$ sudo lxc-create -t download -n mycontainer -- -d centos -r 8 -a amd64
Setting up the GPG keyring
Downloading the image index
Downloading the rootfs
Downloading the metadata
The image cache is now ready
Unpacking the rootfs

---
You just created a CentOS container (release: 8, arch: amd64, variant: default)

To enable SSH, run: apt-get install openssh-server

For security reason, container images ship without user accounts
and without a root password.

Use lxc-attach or chroot directly into the rootfs to set a root password
or create user accounts.

This command creates a new container named "mycontainer" using the CentOS 8 template.

Start the container −

Example

sudo lxc-start -n mycontainer -d

Check the status of the container −

sudo lxc-info -n mycontainer

Output

$ sudo lxc-info -n mycontainer
Name:           mycontainer
State:          RUNNING
PID:            12345
IP:             10.0.3.100
CPU usage:      5.0%
Memory usage:   256 MB (50%)

If everything is set up correctly, you should see the container's status as "RUNNING."

Conclusion

You have successfully installed LXC (Linux Containers) on RHEL, Rocky Linux, and AlmaLinux. LXC provides a flexible and efficient way to create and manage Linux containers, enabling you to run multiple isolated environments on a single host. By following the steps outlined in this article, you can start exploring the world of containerization and leverage the benefits it offers in terms of resource utilization and application isolation.

Updated on: 17-Jul-2023

732 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements