How to use LXC containers on Linux
LXC (aka Linux Containers) is a kernel-level virtualization tool. With it, users can create and run fully contained Linux operating systems. Here’s how to use LXC on your Linux system.
How to install LXD on Linux
LXC (Linux Containers) are managed on Linux via LXD, a program. If you wish to use LXC containers, you’ll have to install the LXD package on your computer. To install it, open up a terminal window.
Unsure about how to open up a terminal window? Press Ctrl + Alt + T on the keyboard. Or, search for “Terminal” in the app menu.
Ubuntu installation instructions
Ubuntu should already have LXD installed; however, if it isn’t, here’s how to get it working. Using the terminal, install the “lxd-installer” package using apt install.
sudo apt install lxd-installer
Debian installation instructions
LXD isn’t available in the Debian Linux software repositories. Thankfully, you can install the LXD Snap package to use it on your Debian system. Follow the Snap instructions below to get LXD working on Debian.
Arch Linux installation instructions
Arch Linux users can get the LXD package up and running using the pacman -S command. The “lxd” package is in the “Community” software repository. Ensure you have this repo enabled to install the package.
sudo pacman -S lxd lxc lxcfs
Fedora installation instructions
There doesn’t appear to be an “lxd” package in the official Fedora Linux software sources; however, following the official Snap package instructions, you can still use LXC containers on your Fedora system.
OpenSUSE installation instructions
OpenSUSE has the “lxd” package in the “OpenSUSE Oss” repo. To install LXD on your OpenSUSE system, use the following zypper install command below.
sudo zypper install lxd lxc lxcfs
Snap installation instructions
To get LXD working as a Snap, you need to install the “lxd” snap package. Next, ensure you have the Snap runtime configured on your computer. To configure the Snap runtime, follow our in-depth setup guide.
With the Snap runtime configured on your Linux system, you can install LXD with the following snap install command.
sudo snap install lxd
How to create a container
To create a container, open up a terminal window and log in as root using the su command. If you cannot log in, do the following to enable root.
sudo -s passwd exit su
Once logged in as root, use the lxc create command to create a new container. All available containers are located on the Canonical LXD page. In this example, we’ll be using OpenSUSE Linux.
lxc-create -n opensuse -t download -- --dist opensuse --release tumbleweed --arch amd64
Next to “-n,” name your container. In this case, we’ve called it “opensuse.” Then, next to “–dist,” specify the Linux distribution. For this example, it is “opensuse.”
After setting the container name and distribution, you’ll have to specify the release. For “–release,” specify the version of the Linux OS. For OpenSUSE, we’ll specify “tumbleweed.”
Lastly, you must specify the architecture of the container. Specify “amd64” unless you require a special container architecture.
lxc-create -n container-name -t download -- --dist os-name --release os-release --arch os-architecture
How to configure your container
After you’ve created your container, you need to start up the container. To start the container, use the lxc-start command.
lxc-start -n name-of-container
Once the container is started, you need access to create a user account and password. To access the container, enter the following command.
lxc-attach -n name-of-container --clear-env
Once the lxc-attach command is run, you will get a root console in which you can enter commands. To create a new user account, use the following command. Please note usernames must be all lowercase.
groupadd wheel
useradd -m -g users -G wheel -s /bin/bash new_username
After creating the new user account, use the passwd command to set the new user account password.
passwd new_username
Exit the container with exit.
exit
Setting up sudo in the container
You may want to set your new user up with sudo. To do it, first install the “Nano” text editor. You can find information on how to install it at pkgs.org. Then, run the following commands.
su EDITOR=nano visudo
In the Nano editor, locate the following:
# %wheel ALL=(ALL:ALL) ALL
Remove the # symbol, save with Ctrl + O, and exit with Ctrl + X. Then, add your user account to the “wheel” group.
usermod -a -G wheel username
How to access your container
To access your LXC container from the command line with your new user, you need to use the lxc-console command. This command differs from attaching, allowing you to log into the container directly with your user and password.
lxc-console -n name-of-container
To exit your container, press Ctrl + A followed by q on the keyboard. To shut off your container, run the following:
lxc-stop -n name-of-container