- Advertisement -

LXC, acronym for Linux Containers, is a lightweight Linux kernel based virtualization solution, which practically runs on top of the Operating System, allowing you to run multiple isolated distributions the same time.

The difference between LXC and KVM virtualization is that LXC doesn’t emulates hardware, but shares the same kernel namespace, similar to chroot applications.

This makes LXC a very fast virtualization solution compared to other virtualization solutions, such as KVM, XEN or VMware.

This article will guide you on how you can install, deploy and run LXC containers on a CentOS/RHEL and Fedora distributions.

Step 1: Installing LXC Virtualization in Linux

1. LXC virtualization is provided through Epel repositories. In order to use this repo, open a terminal and install Epel repositories in your system by issuing the following command:

[php]# yum install epel-release[/php]

2. Before continuing with LXC installation process, assure that Perl language interpreter, and debootstrap packages are installed by issuing the below commands.

- Advertisement -
Icedrive Ad

[php]# yum install debootstrap perl libvirt[/php]

3. Finally install LXC virtualization solution with the following command.

[php]# yum install lxc lxc-templates[/php]

4. After LXC service has been installed, verify if LXC and libvirt daemon is running.

[php]# systemctl status lxc.service
# systemctl start lxc.service
# systemctl start libvirtd
# systemctl status lxc.service[/php]

Sample Output

[php][root@tecmint ~]# systemctl status lxc.service
lxc.service – LXC Container Initialization and Autoboot Code
Loaded: loaded (/usr/lib/systemd/system/lxc.service; disabled)
Active: inactive (dead)

[root@tecmint ~]# systemctl start lxc.service
[root@tecmint ~]# systemctl status lxc.service
lxc.service – LXC Container Initialization and Autoboot Code
Loaded: loaded (/usr/lib/systemd/system/lxc.service; disabled)
Active: active (exited) since Fri 2016-04-01 02:33:36 EDT; 1min 37s ago
Process: 2250 ExecStart=/usr/libexec/lxc/lxc-autostart-helper start (code=exited, status=0/SUCCESS)
Process: 2244 ExecStartPre=/usr/libexec/lxc/lxc-devsetup (code=exited, status=0/SUCCESS)
Main PID: 2250 (code=exited, status=0/SUCCESS)

Apr 01 02:33:06 mail systemd[1]: Starting LXC Container Initialization and Autoboot Code…
Apr 01 02:33:06 mail lxc-devsetup[2244]: Creating /dev/.lxc
Apr 01 02:33:06 mail lxc-devsetup[2244]: /dev is devtmpfs
Apr 01 02:33:06 mail lxc-devsetup[2244]: Creating /dev/.lxc/user
Apr 01 02:33:36 mail lxc-autostart-helper[2250]: Starting LXC autoboot containers: [ OK ]
Apr 01 02:33:36 mail systemd[1]: Started LXC Container Initialization and Autoboot Code.[/php]

and check LXC kernel virtualization status by issuing the below command.

[php]# lxc-checkconfig[/php]

Sample Output

[php][root@tecmint ~]# lxc-checkconfig
Kernel configuration not found at /proc/config.gz; searching…
Kernel configuration found at /boot/config-3.10.0-229.el7.x86_64
— Namespaces —
Namespaces: enabled
Utsname namespace: enabled
Ipc namespace: enabled
Pid namespace: enabled
User namespace: enabled
Network namespace: enabled
Multiple /dev/pts instances: enabled

— Control groups —
Cgroup: enabled
Cgroup clone_children flag: enabled
Cgroup device: enabled
Cgroup sched: enabled
Cgroup cpu account: enabled
Cgroup memory controller: enabled
Cgroup cpuset: enabled

— Misc —
Veth pair device: enabled
Macvlan: enabled
Vlan: enabled
Bridges: enabled
Advanced netfilter: enabled

— Checkpoint/Restore —
checkpoint restore: missing
File capabilities: enabled

Note : Before booting a new kernel, you can check its configuration
usage : CONFIG=/path/to/config /usr/bin/lxc-checkconfig[/php]

Step 2: Create and Manage LXC Containers in Linux

5. To list available LXC templates containers already installed on your system issue the below command.

[php]# ls -alh /usr/share/lxc/templates/[/php]

[php]total 344K
drwxr-xr-x. 2 root root 4.0K Apr 1 02:32 .
drwxr-xr-x. 6 root root 100 Apr 1 02:32 ..
-rwxr-xr-x. 1 root root 11K Nov 15 10:19 lxc-alpine
-rwxr-xr-x. 1 root root 14K Nov 15 10:19 lxc-altlinux
-rwxr-xr-x. 1 root root 11K Nov 15 10:19 lxc-archlinux
-rwxr-xr-x. 1 root root 9.7K Nov 15 10:19 lxc-busybox
-rwxr-xr-x. 1 root root 29K Nov 15 10:19 lxc-centos
-rwxr-xr-x. 1 root root 11K Nov 15 10:19 lxc-cirros
-rwxr-xr-x. 1 root root 17K Nov 15 10:19 lxc-debian
-rwxr-xr-x. 1 root root 18K Nov 15 10:19 lxc-download
-rwxr-xr-x. 1 root root 49K Nov 15 10:19 lxc-fedora
-rwxr-xr-x. 1 root root 28K Nov 15 10:19 lxc-gentoo
-rwxr-xr-x. 1 root root 14K Nov 15 10:19 lxc-openmandriva
-rwxr-xr-x. 1 root root 14K Nov 15 10:19 lxc-opensuse
-rwxr-xr-x. 1 root root 35K Nov 15 10:19 lxc-oracle
-rwxr-xr-x. 1 root root 12K Nov 15 10:19 lxc-plamo
-rwxr-xr-x. 1 root root 6.7K Nov 15 10:19 lxc-sshd
-rwxr-xr-x. 1 root root 23K Nov 15 10:19 lxc-ubuntu
-rwxr-xr-x. 1 root root 12K Nov 15 10:19 lxc-ubuntu-cloud[/php]

6. The process of creating a LXC container is very simple. The command syntax to create a new container is explained below.

[php]# lxc-create -n container_name -t container_template[/php]

In the below excerpt we’ll create a new container named mydeb based on a debian template that will be pulled off from LXC repositories.

[php][root@tecmint ~]# lxc-create -n mydcb -t debian
debootstrap is /usr/sbin/debootstrap
Checking cache download in /var/cache/lxc/debian/rootfs-jessie-amd64 …
Downloading debian minimal …
W: Cannot check Release signature; keyring file not available /usr/share/keyrings/debian-archive-keyring.gpg
I: Retrieving Release
I: Retrieving Packages
I: Validating Packages
I: Resolving dependencies of required packages…
I: Resolving dependencies of base packages…


7. After a series of base dependencies and packages that will be downloaded and installed in your system the container will be created. When the process finishes a message will display your default root account password. Change this password once you start and login to the container console in order to be safe.

[php]are supported and installed on your system.
perl: warning: Falling back to the standard locale (“C”).
Failed to read /proc/cmdline. Ignoring: No such file or directory
invoke-rc.d: policy-rc.d denied execution of start.
Timezone in container is not configured. Adjust it manually.
Root password is ‘root’, please change !
Generating locales (this might take a while)…
en_IN.en_IN…character map file `en_IN’ not found: No such file or directory
/usr/share/i18n/locales/en_IN:55: LC_MONETARY: unknown character in field `currency_symbol’
Generation complete.[/php]

8. Now, you can use lxc-ls to list your containers and lxc-info to obtain information about a running/stopped container.

In order to start the newly created container in background (will run as a daemon by specifying the -d option) issue the following command:

[php]# lxc-start -n mydeb -d[/php]

9. After the container has been started you can list running containers using the lxc-ls --active command and get detailed information about the running container.

[php]# lxc-ls –active[/php]

10. In order to login to the container console issue the lxc-console command against a running container name. Login with the user root and the password generated by default by lxc supervisor.

Once logged in the container you can run several commands in order to verify the distribution by displaying the /etc/issue.net file content, change the root password by issuing passwd command or view details about network interfaces with ifconfig.

[php]# lxc-console -n mydeb
# cat /etc/issue.net
# ifconfig
# passwd[/php]

Sample Output

[php][root@tecmint~]# lxc-console -n mydcb

Connected to tty 1
Type &ltCtrl+a q> to exit the console, to enter Ctrl+a itself

Debian GNU/Linux 8 mydcb tty1

mydcb login: root
Last login: Fri Apr 1 07:39:08 UTC 2016 on console
Linux mydcb 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
root@mydcb:~# cat /etc/issue.net
Debian GNU/Linux 8
root@mydcb:~# ifconfig
eth0 Link encap:Ethernet HWaddr 00:16:3e:d9:21:d7
inet6 addr: fe80::216:3eff:fed9:21d7/64 Scope:Link
RX packets:107 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:5796 (5.6 KiB) TX bytes:648 (648.0 B)

lo Link encap:Local Loopback
inet addr: Mask:
inet6 addr: ::1/128 Scope:Host
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

root@mydcb:~# passwd root
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully[/php]

11. To detach from the container console and go back to your host console, leaving the container in active state, hit Ctrl+a then q on the keyboard.

To stop the a running container issue the following command.

[php]# lxc-stop -n mydcb[/php]

12. In order to create a LXC container based on an Ubuntu template, enter /usr/sbin/ directory and create the following debootstrap symlink.

[php]# cd /usr/sbin
# ln -s debootstrap qemu-debootstrap[/php]

13. Edit qemu-debootstrap file with Vi editor and replace the following two MIRROR lines as follows:


For reference, see the following content and place the above two lines as stated:



14. Finally create a new LXC container based on Ubuntu template issuing the same lxc-create command.

Once the process of generating the Ubuntu container finishes a message will display your container default login credentials as illustrated on the below screenshot.

[php]# lxc-create -n myubuntu -t ubuntu[/php]

Sample Output

[php]Checking cache download in /var/cache/lxc/precise/rootfs-amd64 …
Installing packages in template: ssh,vim,language-pack-en
Downloading ubuntu precise minimal …[/php]

15. In order to create a specific container based on local template use the following syntax:

[php]# lxc-create -n container_name -t container_template — -r distro_release -a distro_architercture[/php]

Here is an excerpt of creating a debian wheezy container with an amd64 system architecture.

[php]# lxc-create -n mywheezy -t debian — -r wheezy -a amd64[/php]

Sample Output

[php]debootstrap is /sbin/debootstrap
Checking cache download in /var/cache/lxc/debian/rootfs-wheezy-amd64 …
Downloading debian minimal …
W: Cannot check Release signature; keyring file not available /usr/share/keyrings/debian-archive-keyring.gpg
I: Retrieving Release
I: Retrieving Packages
I: Validating Packages
I: Resolving dependencies of required packages…
I: Resolving dependencies of base packages…
I: Found additional required dependencies: insserv libbz2-1.0 libdb5.1 libsemanage-common libsemanage1 libslang2 libustr-1.0-1
I: Found additional base dependencies: adduser debian-archive-keyring gnupg gpgv isc-dhcp-common libapt-pkg4.12 libbsd0 libclass-isa-perl libedit2 libgdbm3 libgssapi-krb5-2 libk5crypto3 libkeyutils1 libkrb5-3 libkrb5support0 libncursesw5 libprocps0 libreadline6 libssl1.0.0 libstdc++6 libswitch-perl libusb-0.1-4 libwrap0 openssh-client perl perl-modules procps readline-common
I: Checking component main on http://http.debian.net/debian…
I: Retrieving libacl1 2.2.51-8
I: Validating libacl1 2.2.51-8
I: Retrieving adduser 3.113+nmu3
I: Validating adduser 3.113+nmu3
I: Retrieving apt
I: Validating apt
I: Retrieving libapt-pkg4.12
I: Validating libapt-pkg4.12
I: Retrieving libattr1 1:2.4.46-8
I: Validating libattr1 1:2.4.46-8
I: Retrieving base-files 7.1wheezy9
I: Validating base-files 7.1wheezy9
I: Retrieving base-passwd 3.5.26

16. For instance, specific containers for different distro releases and architectures can be also created from a generic template which will be downloaded from LXC repositories as illustrated in the below example.

[php]# lxc-create -n mycentos6 -t download — -d centos -r 6 -a i386[/php]

Here is the list of lxc-create command line switches:

[php]-n = name
-t = template
-d = distibution
-a = arch
-r = release[/php]

17. Containers can be deleted from your host with the lxc-destroy command issued against a container name.

[php]# lxc-destroy -n mywheezy[/php]

18. A container can be cloned from an existing container by issuing lxc-clone command:

[php]# lxc-clone mydeb mydeb-clone[/php]

19. And finally, all created containers reside in /var/lib/lxc/ directory. If for some reason you need to manually adjust container settings you must edit the config file from each container directory.

[php]# ls /var/lib/lxc[/php]

This are just the basic things you need to know in order to work your way around LXC.

- Advertisement -
VPS House Ad