≡ Menu

How to Create and Launch LXC Linux Containers with LXC Commands

LXC Containers are a very quick way to create a psuedo virtual environment.

In the previous LXC article, we explained how to install and configure LXC linux containers.

In this tutorial, we’ll explain how to create a new Linux container, start the container, and login to the LXC virtual console to use the new container.

For demonstration, we’ll be creating a CentOS LXC virtual container in this tutorial, but you can create virtual container for pretty much any Linux distro that you want.

1. LXC Container Templates

Linux Containers LXC by default provides container templates for several popular linux distros. The following are some of the LXC templates that you can use immediately.

  • CentOS
  • Ubuntu
  • Fedora
  • OpenSUSE
  • Gentoo
  • Debian
  • Oracle Linux
  • ArchLinux

All available LXC templates are located under /usr/local/share/lxc/templates directory.

# ls -1 /usr/local/share/lxc/templates
lxc-alpine
lxc-altlinux
lxc-archlinux
lxc-busybox
lxc-centos
lxc-cirros
lxc-debian
lxc-download
lxc-fedora
lxc-gentoo
lxc-openmandriva
lxc-opensuse
lxc-oracle
lxc-plamo
lxc-sshd
lxc-ubuntu
lxc-ubuntu-cloud

2. Create a Container using lxc-create

To create the container, use lxc-create command as shown below.

In the following command:

  • –n option indicates the container name
  • –t option indicates the template that is used to create the container. In this example, we are using lxc-centos template to create a CentOS container.

In this example, this will create the CentOS container with centos minimal install.

This will download all the packages that are required to run the CentOS minimal (for example, approximately around 140 packages), and install them as part of the MyCentOSContainer1 container

# lxc-create -n MyCentOSContainer1 -t /usr/local/share/lxc/templates/lxc-centos
/usr/local/share/lxc/templates/lxc-centos
Host CPE ID from /etc/system-release-cpe: cpe:/o:centos:linux:6:GA
dnsdomainname: Unknown host
Checking cache download in /usr/local/var/cache/lxc/centos/x86_64/6/rootfs ... 
Downloading centos minimal ...
...
...
  Installing : libgcc-4.4.7-16.el6.x86_64       1/142 
  Installing : setup-2.8.14-20.el6_4.1.noarch   2/142 
  Installing : filesystem-2.4.30-3.el6.x86_64   3/142 
...
  Verifying  : gzip-1.3.12-22.el6.x86_64        139/142 
  Verifying  : mingetty-1.08-5.el6.x86_64       140/142 
  Verifying  : libxml2-2.7.6-20.el6_7.1.x86_64  141/142 
  Verifying  : 1:findutils-4.4.2-6.el6.x86_64   142/142 
...
Download complete.
Copy /usr/local/var/cache/lxc/centos/x86_64/6/rootfs to /usr/local/var/lib/lxc/MyCentOSContainer1/rootfs ... 
Copying rootfs to /usr/local/var/lib/lxc/MyCentOSContainer1/rootfs ...

3. Root Password for the LXC Container

The root password for the LXC container is stored under: /usr/local/var/lib/lxc/{container-name}/tmp_root_pass

So, in the above example, for the LXC container that we just created, temporary root password is under the following:

# cat /usr/local/var/lib/lxc/MyCentOSContainer1/tmp_root_pass
Root-MyCentOSContainer1-pf7qMB

You can change the root password of your container at any time, even if your container is turned off by using chroot from the control host as shown below.

# chroot /usr/local/var/lib/lxc/MyCentOSContainer1/rootfs passwd
Changing password for user root.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

4. Start the LXC Linux Container using lxc-start

Once the container is created, use lxc-start as shown below to start your container.

# lxc-start -n MyCentOSContainer1
lxc-start: cgfs.c: handle_cgroup_settings: 2077 Device or resource busy - failed to set memory.use_hierarchy to 1; continuing

CentOS release 6.6 (Final)
Kernel 2.6.32-431.el6.x86_64 on an x86_64

MyCentOSContainer1 login: init: rcS main process (8) killed by TERM signal
Entering non-interactive startup
Bringing up loopback interface:               [  OK  ]
Bringing up interface eth0:
Determining IP information for eth0... done.  [  OK  ]
Starting system logger:                       [  OK  ]
Mounting filesystems:                         [  OK  ]
Generating SSH2 RSA host key:                 [  OK  ]
Generating SSH1 RSA host key:                 [  OK  ]
Generating SSH2 DSA host key:                 [  OK  ]
Starting sshd:               				  [  OK  ]

CentOS release 6.6 (Final)
Kernel 2.6.32-431.el6.x86_64 on an x86_64

MyCentOSContainer1 login:

If you get the following “container failed to start” error message, then something is wrong in your configuration.

# lxc-start -n MyCentOSContainer1
lxc-start: lxc_start.c: main: 344 The container failed to start.
lxc-start: lxc_start.c: main: 346 To get more details, run the container in foreground mode.
lxc-start: lxc_start.c: main: 348 Additional information can be obtained by setting the --logfile and --logpriority options.

To find-out exactly what is wrong with your configuration, run the LXC container in the foreground using the -F option as shown below.

If you get the following, “failed to attach to bridge”, “failed to create netdev”, “failed to create the network” error message, then bridge adapter is not configured properly on your system.

Check the ifcfg-br0 settings that we mentioned in our earlier LXC Linux Container setup article, to make sure you have the bridge configuration setup properly and working.

# lxc-start -F -n MyCentOSContainer1
lxc-start: conf.c: instantiate_veth: 2796 failed to attach 'veth8VPW29' to the bridge 'lxcbr0': Operation not permitted
lxc-start: conf.c: lxc_create_network: 3079 failed to create netdev
lxc-start: start.c: lxc_spawn: 935 failed to create the network
lxc-start: start.c: __lxc_start: 1192 failed to spawn 'MyCentOSContainer1'
lxc-start: lxc_start.c: main: 344 The container failed to start.
lxc-start: lxc_start.c: main: 348 Additional information can be obtained by setting the --logfile and --logpriority options.

5. View Container Info using lxc-info

To display detailed information about your container, use lxc-info command as shown below.

# lxc-info -n MyCentOSContainer1
Name:           MyCentOSContainer1
State:          RUNNING
PID:            7696
IP:             192.168.100.101
CPU use:        1.61 seconds
BlkIO use:      32.00 KiB
Memory use:     2.63 MiB
Link:           vethLNI1UH
 TX bytes:      1.98 KiB
 RX bytes:      46.08 KiB
 Total bytes:   48.06 KiB

6. Stop LXC Linux Container using lxc-stop

To stop your LXC linux container use lxc-stop as shown below, and specify the container name that you like to stop.

# lxc-stop -n MyCentOSContainer1

7. Clone an Existing Container using lxc-clone

Cloning is useful when you want to take a snapshot of the old container before you make any changes to the configuration on the container. You can use this as a simple backup method for container configuration.

Cloning the container can be done using lxc-clone command as shown below. In this case, the new container name is MyCentOSContainer2

# lxc-clone -o MyCentOSContainer1 -n MyCentOSContainer2
Created container MyCentOSContainer2 as copy of MyCentOSContainer1

8. Delete an Existing Container using lxc-destory

To delete an existing container, use lxc-destroy command as shown below.

# lxc-destroy -n MyCentOSContainer2

9. Connect to LXC Linux Container Console

To connect to the console of the container, use the following lxc-console command.

# lxc-console -n MyCentOSContainer1
Connected to tty 1
Type Ctrl+a q to exit the console, Ctrl+a Ctrl+a to enter Ctrl+a itself

CentOS release 6.6 (Final)
Kernel 2.6.32-431.el6.x86_64 on an x86_64

MyCentOSContainer1 login:

10. Change LXC Configurations using lxc-config

To change the LXC configuration, you can use lxc-config command.

First, use lxc-config -l option, which will just display all the available configurations as shown below.

# lxc-config -l
lxc.default_config
lxc.lxcpath
lxc.bdev.lvm.vg
lxc.bdev.lvm.thin_pool
lxc.bdev.zfs.root
lxc.cgroup.use
lxc.cgroup.pattern

Next, use lxc.default_config option as shown below to view information about each configuration file and its associated parameters.

# lxc-config lxc.default_config
/usr/local/etc/lxc/default.conf

# cat /usr/local/etc/lxc/default.conf
lxc.network.type = veth
lxc.network.link = virbr0
lxc.network.flags = up

The following command will display the LXC patch where all the containers are stored.

# lxc-config lxc.lxcpath
/usr/local/var/lib/lxc

As we see below, we see the two containers that we created under this directory.

# cd /usr/local/var/lib/lxc

# ls
MyCentOSContainer1  MyCentOSContainer2

Under the container directory, you’ll see the config file, which contains all the basic configuration information for that particular container as shown below.

# cd /usr/local/var/lib/lxc/MyCentOSContainer1

# vi config
lxc.network.type = veth
lxc.network.link = virbr0
lxc.network.hwaddr = fb:bb:f2:87:e8:17
lxc.network.flags = up
lxc.rootfs = /usr/local/var/lib/lxc/MyCentOSContainer1/rootfs
lxc.include = /usr/local/share/lxc/config/centos.common.conf
lxc.arch = x86_64
lxc.utsname = MyCentOSContainer1

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 8 comments… add one }

  • Raghuvamsan January 28, 2016, 8:18 am

    Thanks ..I searched the right document for lxc and found one.. Keep posting more in containers ..good work of you continues ..

  • Scott Vargovich January 28, 2016, 9:31 am

    Ramesh,

    I set this up on Arch Linux and was trying to login using the ‘lxc-console -n MyCentOSContainer1’ command. I tried logging in using both ‘root’ and ‘admin’ to login without success. I’m stuck on this step. Please help.

    Scott

  • shatayu January 31, 2016, 4:28 am

    Thanks for documentation, can you please provide more details on templates.
    Like from where we can get multiple type of templates which we can add in or implement with LXC.

  • shatayu January 31, 2016, 4:29 am

    Also provide us the pros and cons of LXC vs Docker
    Thanks

  • Ethan zhang February 1, 2016, 9:36 pm

    clearly expressed and instructive.

  • Jesus February 3, 2016, 3:59 pm

    Hi Ramesh,

    I was following this tutorial but i got isue when

    lxc-start -n MyCentOSContainer1

    and the trace was, any idea of what happened here?

    lxc-start: utils.c: open_without_symlink: 1626 No such file or directory – Error examining fuse in /usr/local/lib/lxc/rootfs/sys/fs/fuse/connections
    lxc-start: utils.c: open_without_symlink: 1626 No such file or directory – Error examining cgroup in /usr/local/lib/lxc/rootfs/sys/fs/cgroup
    lxc-start: cgfs.c: cgroupfs_mount_cgroup: 1372 No such file or directory – could not mount tmpfs to /sys/fs/cgroup in the container
    lxc-start: conf.c: lxc_mount_auto_mounts: 866 No such file or directory – error mounting /sys/fs/cgroup
    lxc-start: conf.c: lxc_setup: 3936 failed to setup the automatic mounts for ‘MyCentOSContainer1’
    lxc-start: start.c: do_start: 717 failed to setup the container
    lxc-start: sync.c: __sync_wait: 51 invalid sequence number 1. expected 2
    lxc-start: start.c: __lxc_start: 1192 failed to spawn ‘MyCentOSContainer1’
    lxc-start: lxc_start.c: main: 344 The container failed to start.
    lxc-start: lxc_start.c: main: 348 Additional information can be obtained by setting the –logfile and –logpriority options.

  • Anshul June 21, 2016, 9:15 am

    Hi Jesus/Ramesh,
    I am getting same error , are you able to solve this problem if yes then please help me out. I am new to linux.
    lxc-start: cgfs.c: handle_cgroup_settings: 2091 Device or resource busy – failed to set memory.use_hierarchy to 1; continuing
    lxc-start: cgfs.c: handle_cgroup_settings: 2091 Device or resource busy – failed to set memory.use_hierarchy to 1; continuing
    lxc-start: utils.c: open_without_symlink: 1626 No such file or directory – Error examining fuse in /usr/local/lib/lxc/rootfs/sys/fs/fuse/connections
    lxc-start: utils.c: open_without_symlink: 1626 No such file or directory – Error examining cgroup in /usr/local/lib/lxc/rootfs/sys/fs/cgroup
    lxc-start: cgfs.c: cgroupfs_mount_cgroup: 1372 No such file or directory – could not mount tmpfs to /sys/fs/cgroup in the container
    lxc-start: conf.c: lxc_mount_auto_mounts: 866 No such file or directory – error mounting /sys/fs/cgroup
    lxc-start: conf.c: lxc_setup: 3936 failed to setup the automatic mounts for ‘dmszone’
    lxc-start: start.c: do_start: 717 failed to setup the container
    lxc-start: sync.c: __sync_wait: 51 invalid sequence number 1. expected 2
    lxc-start: start.c: __lxc_start: 1192 failed to spawn ‘dmszone’
    lxc-start: lxc_start.c: main: 344 The container failed to start.
    lxc-start: lxc_start.c: main: 348 Additional information can be obtained by setting the –logfile and –logpriority options.

  • GAurav June 22, 2016, 6:53 am

    Hi,

    I am also getting the below error. Any help will be highly appreciated.

    lxc-start: cgfs.c: handle_cgroup_settings: 2091 Device or resource busy – failed to set memory.use_hierarchy to 1; continuing
    lxc-start: cgfs.c: handle_cgroup_settings: 2091 Device or resource busy – failed to set memory.use_hierarchy to 1; continuing
    lxc-start: utils.c: open_without_symlink: 1626 No such file or directory – Error examining cgroup in /usr/local/lib/lxc/rootfs/sys/fs/cgroup
    lxc-start: cgfs.c: cgroupfs_mount_cgroup: 1372 No such file or directory – could not mount tmpfs to /sys/fs/cgroup in the container
    lxc-start: conf.c: lxc_mount_auto_mounts: 866 No such file or directory – error mounting /sys/fs/cgroup
    lxc-start: conf.c: lxc_setup: 3936 failed to setup the automatic mounts for ‘MyCentOSContainer1’
    lxc-start: start.c: do_start: 717 failed to setup the container
    lxc-start: sync.c: __sync_wait: 51 invalid sequence number 1. expected 2
    lxc-start: start.c: __lxc_start: 1192 failed to spawn ‘MyCentOSContainer1’
    lxc-start: lxc_start.c: main: 344 The container failed to start.
    lxc-start: lxc_start.c: main: 348 Additional information can be obtained by setting the –logfile and –logpriority options.
    [root@DMS-Linux dmsadmin]#

Leave a Comment