LXC, Linux Containers, is a fast, lightweight, and operating system-level virtualization technology that allows us to host multiple isolated Linux systems (Containers) in a single host. Unlike other virtualization methods such as VirtualBox, KVM, and Vmware, the guest systems uses the same kernel of the host system. All guests will efficiently shares the resources such as CPU, RAM, Hard disk, and network etc of your original host computer. The main goal of LXC is to create a Linux environment as close as a standard Linux installation, but without the need for a separate kernel.
In this tutorial, let me show how to install LXC, and how to create Virtual Machines using LXC. Since this article was tested in Ubuntu 15.04 64bit, the same steps will work on Ubuntu 14.10 and other older versions.
Install LXC On Ubuntu
LXC is available on Ubuntu default repositories. So, Run the following command to install it on Ubuntu, and its derivatives.
sudo apt-get install lxc lxctl lxc-templates
The above command will install LXC, and everything needed by LXC.
Now, enter the following command to check everything OK.
Kernel configuration not found at /proc/config.gz; searching... Kernel configuration found at /boot/config-3.19.0-15-generic --- 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 File capabilities: enabled Note : Before booting a new kernel, you can check its configuration usage : CONFIG=/path/to/config /usr/bin/lxc-checkconfig
Creating Linux Containers (Virtual Units)
LXC is usage very simple, and easy. By default, LXC has many ready made templates. To list all available templates, enter the following command:
sudo ls /usr/share/lxc/templates/
lxc-alpine lxc-archlinux lxc-centos lxc-debian lxc-fedora lxc-openmandriva lxc-oracle lxc-sshd lxc-ubuntu-cloud lxc-altlinux lxc-busybox lxc-cirros lxc-download lxc-gentoo lxc-opensuse lxc-plamo lxc-ubuntu
To create a new Container, the command should be as below.
sudo lxc-create -n <container-name> -t <template>
sudo lxc-create -n ubuntu01 -t ubuntu
The above command will create a ubuntu container with name “ubuntu01″.
Checking cache download in /var/cache/lxc/vivid/rootfs-amd64 ... Installing packages in template: ssh,vim,language-pack-en Downloading ubuntu vivid minimal ... I: Retrieving Release I: Retrieving Release.gpg I: Checking Release signature I: Valid Release signature (key id 790BC7277767219C42C86F933B4FE6ACC0B21F32) I: Retrieving Packages . . . Generation complete. Creating SSH2 RSA key; this may take some time ... 2048 ef:14:b4:98:fd:7a:3d:0c:cc:bd:69:17:87:03:a9:7c /etc/ssh/ssh_host_rsa_key.pub (RSA) Creating SSH2 DSA key; this may take some time ... 1024 b1:ab:2a:4b:c4:77:2e:3f:da:ab:df:24:74:9f:69:a9 /etc/ssh/ssh_host_dsa_key.pub (DSA) Creating SSH2 ECDSA key; this may take some time ... 256 1a:6a:93:88:05:c5:1c:10:ad:d5:f7:5a:aa:fb:04:57 /etc/ssh/ssh_host_ecdsa_key.pub (ECDSA) Creating SSH2 ED25519 key; this may take some time ... 256 80:a8:d4:8e:3e:d2:06:a3:96:6d:51:4a:6a:a2:23:47 /etc/ssh/ssh_host_ed25519_key.pub (ED25519) invoke-rc.d: policy-rc.d denied execution of start. Current default time zone: 'Asia/Kolkata' Local time is now: Fri Jun 12 15:03:30 IST 2015. Universal Time is now: Fri Jun 12 09:33:30 UTC 2015. ## # The default user is 'ubuntu' with password 'ubuntu'! # Use the 'sudo' command to run tasks as root in the container. ##
The new Ubuntu container has been created now. Note that the default username and its password of the Container at the last line of the above output. This will be needed while you logging into the virtual host.
After creating the containers, start it using the following command.
sudo lxc-start -n ubuntu01 -d
Now, log in to the container with command:
sudo lxc-console -n ubuntu01
Enter the username and password of the container. As you know, the default username and password will be generated while creating new containers. In my case, the default username is ubuntu, and password is ubuntu.
Now, start using the new virtual unit (container). You can now install, remove, configure packages in this new container as the way you do in a normal physical machine.
Similarly, you can create virtual hosts as many as you want to.
For example, to create a centos container, enter the following command. Be mindful that you should install “yum” package in order to create RPM based virtual units.
sudo apt-get install yum
sudo lxc-create -n centos01 -t centos
To create, opensuse container:
sudo lxc-create -n opensuse1 -t opensuse
To exit from the container’s console, and return back to your original host computer, press “ctrl+a” followed by the letter “q” from your keyboard. Now, you’ll be returned back to the original host computer’s Terminal.
Be mindful that, we didn’t power off the container. The Container is still running. We just returned back to host computer’s terminal.
To connect again to the Container’s console, run:
sudo lxc-console -n ubuntu01
Viewing the list of newly created containers
To view the list of newly generated containers, enter the following command from your original computer.
As you see above, I have only one virtual unit in my host computer.
Viewing the complete details of a Container
sudo lxc-info -n ubuntu01
Name: ubuntu01 State: RUNNING PID: 32681 IP: 10.0.3.20 CPU use: 0.53 seconds BlkIO use: 8.00 KiB Memory use: 17.52 MiB KMem use: 0 bytes Link: vethK0MFY8 TX bytes: 2.37 KiB RX bytes: 3.20 KiB Total bytes: 5.58 KiB
The above command shows the name, State, CPU usage, Memory usage of the container “ubuntu01″.
Viewing the IP address and state of a particular Container
To view the ip address, and state of a container, use the following command.
For example, here I am going to view my “ubuntu01″ container’s ip address and running state.
sudo lxc-ls --fancy ubuntu01
NAME STATE IPV4 IPV6 GROUPS AUTOSTART ----------------------------------------------------- ubuntu01 RUNNING 10.0.3.20 - - NO
As you see above, the “ubuntu01″ container is currently running, and its ip address is 10.0.3.20.
You can stop a running container using “lxc-stop” command. For example, to stop a ubuntu01 container, enter the following command:
sudo lxc-stop -n ubuntu01
Now, check the state of the ubuntu01 container.
sudo lxc-ls --fancy ubuntu01
NAME STATE IPV4 IPV6 GROUPS AUTOSTART ------------------------------------------------ ubuntu01 STOPPED - - - NO
As you see above, the ubuntu01 container has stopped.
It’s possible to clone a new container from an existing container. For example, to clone a new container called “ubuntu02″ from an existing container “ubuntu01″, use the following commands:
sudo lxc-stop -n ubuntu01
sudo lxc-clone ubuntu01 ubuntu02
Created container ubuntu02 as copy of ubuntu01
Now, you can start and use “ubuntu02″ container as usual.
sudo lxc-start -n ubuntu02
Then, connect to the ubuntu02 container’s console using command:
sudo lxc-console -n ubuntu02
Connected to tty 1 Type <Ctrl+a q> to exit the console, <Ctrl+a Ctrl+a> to enter Ctrl+a itself Ubuntu 15.04 ubuntu02 pts/0 ubuntu02 login: ubuntu Password: Last login: Fri Jun 12 15:20:45 IST 2015 on pts/0 [email protected]:~$
As you see in the above output, we just have logged in to the ubuntu02 container which is the clone of ubuntu01 container.
To power off this container, simply enter the following command:
Take snapshot of a container
It’s also possible to take snapshot of a container. To take snapshot of the container ubuntu01, enter the following commands:
sudo lxc-stop -n ubuntu01
sudo lxc-snapshot -n ubuntu01
lxc_container: lxccontainer.c: lxcapi_snapshot: 2953 Snapshot of directory-backed container requested. lxc_container: lxccontainer.c: lxcapi_snapshot: 2954 Making a copy-clone. If you do want snapshots, then lxc_container: lxccontainer.c: lxcapi_snapshot: 2955 please create an aufs or overlayfs clone first, snapshot that lxc_container: lxccontainer.c: lxcapi_snapshot: 2956 and keep the original container pristine.
In Ubuntu 15.04, the snapshots will be stored in /var/lib/lxc/ directory of your original host computer.
sudo ls /var/lib/lxc/
In Ubuntu 14.04 and older versions, the snapshots will be stored in /var/lib/lxcsnaps/ directory.
sudo ls /var/lib/lxcsnaps/
To restore a container from the snapshot, use the following command.
sudo lxc-snapshot -n ubuntu01 -r snap0
To destroy a container completely from you original host, enter the following command:
sudo lxc-destroy -n ubuntu01
Managing Containers using LXC web console
If you not happy with LXC command line console, you can use LXC web panel to manage containers easily through your web browser.
To install LXC web panel, run the following command:
wget http://lxc-webpanel.github.io/tools/install.sh -O - | sudo bash
--2015-06-12 15:37:41-- http://lxc-webpanel.github.io/tools/install.sh Resolving lxc-webpanel.github.io (lxc-webpanel.github.io)... 22.214.171.124 Connecting to lxc-webpanel.github.io (lxc-webpanel.github.io)|126.96.36.199|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 2678 (2.6K) [application/octet-stream] Saving to: ‘STDOUT’ - 0%[ ] 0 --.-KB/s _ __ _______ __ __ _ _____ _ | | \ \ / / ____| \ \ / / | | | __ \ | | | | \ V / | \ \ /\ / /__| |__ | |__) |_ _ _ __ ___| | | | > <| | \ \/ \/ / _ \ '_ \ | ___/ _` | '_ \ / _ \ | | |____ / . \ |____ \ /\ / __/ |_) | | | | (_| | | | | __/ | |______/_/ \_\_____| \/ \/ \___|_.__/ |_| \__,_|_| |_|\___|_| Automatic installer - 100%[=======================================================================================>] 2.62K --.-KB/s in 0.003s . . . . Cleaning up... Cloning LXC Web Panel... Cloning into '/srv/lwp'... remote: Counting objects: 167, done. remote: Total 167 (delta 0), reused 0 (delta 0), pack-reused 167 Receiving objects: 100% (167/167), 148.85 KiB | 132.00 KiB/s, done. Resolving deltas: 100% (63/63), done. Checking connectivity... done. Installation complete! Adding /etc/init.d/lwp... Done Starting server...done. Connect you on http://your-ip-address:5000/
Then, access the LXC web panel using URL: http://ip-address:5000. The default username/password is admin/admin.
LXC Web panel Dashboard:
From now on, you can create/clone/edit containers from the LXC web console. Also, you can view the list of existing containers, and edit network settings of a containers and so on.
This tutorial only shows the basic installation, and usage part of the LXC. If you want to know more about LXC, refer the LXC comprehensive guide from Ubuntu Community portal. This guide has everything you need to know about LXC, including installation, configuration, and troubleshooting of LXC.
That’s all for now. Enjoy!