Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.

com/2016/01/create-lxc-containers/

Access Securely to your Desktop Files and DOWNLOAD


Servers without VPN from any Device. Try it Now

≡ MENU

How to Create and Launch LXC Linux Containers


with LXC Commands
by KARTHIKEYAN SADHASIVAM on JANUARY 27, 2016

Like 0 Tweet

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 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

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

2 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

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

3 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

# 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/MyCentOSCon

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

4 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

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 me

CentOS release 6.6 (Final)

Kernel 2.6.32-431.el6.x86_64 on an x86_64

5 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

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 mod

lxc-start: lxc_start.c: main: 348 Additional information can be obtained by setting the --

To find-out exactly what is wrong with your configuration, run the LXC container

6 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

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 'lxc

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 --

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

7 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

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

8 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

# 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

9 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

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

10 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

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

11 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

lxc.arch = x86_64

lxc.utsname = MyCentOSContainer1

Tweet Like 0 > Add your comment

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


1. 50 Linux Sysadmin Tutorials Awk Introduction – 7 Awk Print
2. 50 Most Frequently Used Linux Examples
Commands (With Examples) Advanced Sed Substitution
3. Top 25 Best Linux Performance Examples
Monitoring and Debugging Tools 8 Essential Vim Editor Navigation
4. Mommy, I found it! – 15 Practical Fundamentals
Linux Find Command Examples 25 Most Frequently Used Linux
5. Linux 101 Hacks 2nd Edition eBook IPTables Rules Examples
Turbocharge PuTTY with 12
Powerful Add-Ons

12 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

Tagged as: Chroot LXC Container, lxc-clone Command Examples, lxc-config Command Examples, lxc-console Command Examples,

lxc-create Command Examples, lxc-destroy Command Examples, lxc-info Command Examples, lxc-start Command Examples, lxc-

stop Command Examples

{ 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 ..

LINK

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

13 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

LINK

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.

LINK

shatayu January 31, 2016, 4:29 am

Also provide us the pros and cons of LXC vs Docker


Thanks

LINK

Ethan zhang February 1, 2016, 9:36 pm

clearly expressed and instructive.

LINK

14 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

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.

15 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

LINK

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.

16 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

LINK

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]#

17 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

LINK

Leave a Comment

Name

Email

Website

Comment

Save my name, email, and website in this browser for the next time I comment.

Submit

18 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

Notify me of followup comments via e-mail

Next post: 15 Oracle Exp Command Examples to Export Database Objects


Previous post: How to Install and Setup LXC Linux Containers on CentOS / RHEL
/ Ubuntu

RSS | Email | Twitter | Facebook | Google+

Search

Manage All From one


Console
Get Started Today with the Remote
Access Software and Access Safely to
any Device.
TeamViewer

Download

EBOOKS

19 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

Linux 101 Hacks 2nd Edition eBook - Practical Examples to Build a Strong Foundation in Linux

Bash 101 Hacks eBook - Take Control of Your Bash Command Line and Shell Scripting

Sed and Awk 101 Hacks eBook - Enhance Your UNIX / Linux Life with Sed and Awk

Vim 101 Hacks eBook - Practical Examples for Becoming Fast and Productive in Vim Editor

Nagios Core 3 eBook - Monitor Everything, Be Proactive, and Sleep Well

The Geek Stuff


16,873 likes

Like Page Share

POPULAR POSTS
15 Essential Accessories for Your Nikon or Canon DSLR Camera

12 Amazing and Essential Linux Books To Enrich Your Brain and Library

50 UNIX / Linux Sysadmin Tutorials

50 Most Frequently Used UNIX / Linux Commands (With Examples)

How To Be Productive and Get Things Done Using GTD

30 Things To Do When you are Bored and have a Computer

Linux Directory Structure (File System Structure) Explained with Examples

20 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

Linux Crontab: 15 Awesome Cron Job Examples

Get a Grip on the Grep! – 15 Practical Grep Command Examples

Unix LS Command: 15 Practical Examples

15 Examples To Master Linux Command Line History

Top 10 Open Source Bug Tracking System

Vi and Vim Macro Tutorial: How To Record and Play

Mommy, I found it! -- 15 Practical Linux Find Command Examples

15 Awesome Gmail Tips and Tricks

15 Awesome Google Search Tips and Tricks

RAID 0, RAID 1, RAID 5, RAID 10 Explained with Diagrams

Can You Top This? 15 Practical Linux Top Command Examples

Top 5 Best System Monitoring Tools

Top 5 Best Linux OS Distributions

How To Monitor Remote Linux Host using Nagios 3.0

Awk Introduction Tutorial – 7 Awk Print Examples

How to Backup Linux? 15 rsync Command Examples

The Ultimate Wget Download Guide With 15 Awesome Examples

Top 5 Best Linux Text Editors

Packet Analyzer: 15 TCPDUMP Command Examples

The Ultimate Bash Array Tutorial with 15 Examples

3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-copy-id

Unix Sed Tutorial: Advanced Sed Substitution Examples

UNIX / Linux: 10 Netstat Command Examples

The Ultimate Guide for Creating Strong Passwords

21 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

6 Steps to Secure Your Home Wireless Network

Turbocharge PuTTY with 12 Powerful Add-Ons

CATEGORIES
Linux Tutorials

Vim Editor

Sed Scripting

Awk Scripting

Bash Shell Scripting

Nagios Monitoring

OpenSSH

IPTables Firewall

Apache Web Server

MySQL Database

Perl Programming

Google Tutorials

Ubuntu Tutorials

PostgreSQL DB

Hello World Examples

C Programming

C++ Programming

DELL Server Tutorials

Oracle Database

VMware Tutorials

22 of 23 6/13/21, 12:51
How to Create and Launch LXC Linux Containers with LXC Commands https://www.thegeekstuff.com/2016/01/create-lxc-containers/

A BOUT T HE G EEK C ONTACT U S S UPPORT U S


S TUFF
Email Me : Use this Support this blog by
My name Contact Form to get in purchasing one of my
is touch me with your ebooks.
Ramesh comments, questions or
suggestions about this Bash 101 Hacks eBook
site. You can also simply
drop me a line to say Sed and Awk 101 Hacks
Natarajan. I will be hello!. eBook
posting instruction
guides, how-to, Follow us on Google+ Vim 101 Hacks eBook
troubleshooting tips and
tricks on Linux, database, Follow us on Twitter Nagios Core 3 eBook
hardware, security and
web. My focus is to write Become a fan on
articles that will either Facebook
teach you or help you
resolve a problem. Read
more about Ramesh
Natarajan and the blog.

Copyright © 2008–2021 Ramesh Natarajan. All rights reserved | Terms of Service

23 of 23 6/13/21, 12:51

You might also like