Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Install LXC (Linux Containers) in RHEL, Rocky & AlmaLinux
LXC (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 like VMware or VirtualBox. In this guide, we will walk through installing LXC on RHEL, Rocky Linux, and AlmaLinux.
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
At least 2GB RAM and 10GB free disk space
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 -y
Step 2 Install EPEL Repository
LXC packages are available in the EPEL (Extra Packages for Enterprise Linux) repository. Install EPEL first
sudo yum install epel-release -y
Step 3 Install LXC Packages
Install the LXC packages using the appropriate package manager based on your distribution
For RHEL and Rocky Linux
sudo yum install lxc lxc-templates lxc-extra -y
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 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 Installed: lxc.x86_64 0:3.2.1-4.el7 lxc-extra.x86_64 0:3.2.1-4.el7 lxc-templates.noarch 0:3.2.1-4.el7 Complete!
For AlmaLinux
sudo dnf install lxc lxc-templates lxc-extra -y
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 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 4 Configure LXC Network Bridge
LXC uses a bridge interface to provide networking capabilities to containers. Configure the default bridge by creating a configuration file
sudo vi /etc/lxc/default.conf
Add the following network configuration
lxc.net.0.type = veth lxc.net.0.link = lxcbr0 lxc.net.0.flags = up lxc.net.0.hwaddr = 00:16:3e:xx:xx:xx
Step 5 Start and Enable LXC Services
Start the LXC services and enable them to start automatically at boot
sudo systemctl start lxc.service sudo systemctl enable lxc.service
Created symlink /etc/systemd/system/multi-user.target.wants/lxc.service ? /usr/lib/systemd/system/lxc.service.
Step 6 Create and Test a Container
Verify the LXC installation by creating a test container
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) 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.
Start the container and check its status
sudo lxc-start -n mycontainer -d 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%)
Common LXC Commands
| Command | Description |
|---|---|
lxc-ls |
List all containers |
lxc-start -n <name> |
Start a container |
lxc-stop -n <name> |
Stop a container |
lxc-attach -n <name> |
Attach to running container |
lxc-destroy -n <name> |
Delete a container |
Conclusion
You have successfully installed LXC on RHEL, Rocky Linux, and AlmaLinux. LXC provides an efficient containerization solution with lower overhead than traditional virtualization. With these containers, you can now isolate applications, test different configurations, and optimize resource utilization on your system.
