Table of Contents
In this article, I will take you through 20 Best LXC command examples to Manage Linux Containers. If you are looking for a lightweight virtualization technology for Linux containers then LXC is the best runtime available to use. It is well known and heavily tested with multiple Linux operating systems. It can be used to manage Instances in standalone mode as well as in cluster mode. It provides an environment as close as a virtual machine(VM) without the overhead of running a separate kernel and separate set of hardware. More of its features can be checked on official website. Here we will see some of the real world examples of lxc command in below section.
20 Best LXC command examples to Manage Linux Containers
Also Read: How to Install LXC to Create Linux Containers on RHEL/CentOS/Rocky Linux
Example 1: How to Check lxc version
If you want to check lxc utility version then you need to use lxc --version
command as shown below.
[root@localhost ~]# lxc --version
5.1
Example 2: How to List all the Network Adapters
If you want to list all the network adapters then you need to use lxc network list
command as shown below.
[root@localhost ~]# lxc network list
+--------+----------+---------+------+------+-------------+---------+-------+
| NAME | TYPE | MANAGED | IPV4 | IPV6 | DESCRIPTION | USED BY | STATE |
+--------+----------+---------+------+------+-------------+---------+-------+
| enp0s3 | physical | NO | | | | 0 | |
+--------+----------+---------+------+------+-------------+---------+-------+
Example 3: How to Launch a Container
If you want to launch a container say test-container3
from some image like Rocky Linux 8
then you need to use lxc launch images:rockylinux/8/amd64 test-container3
command as shown below.
[root@localhost ~]# lxc launch images:rockylinux/8/amd64 test-container3
Creating test-container3
Starting test-container3
Example 4: How to List Storage Pool
If you want to list all the storage pool then you need to use lxc storage ls
command as shown below.
[root@localhost ~]# lxc storage ls
+--------------+--------+-------------------------------------------------+-------------+---------+---------+
| NAME | DRIVER | SOURCE | DESCRIPTION | USED BY | STATE |
+--------------+--------+-------------------------------------------------+-------------+---------+---------+
| test-storage | lvm | /var/snap/lxd/common/lxd/disks/test-storage.img | | 1 | CREATED |
+--------------+--------+-------------------------------------------------+-------------+---------+---------+
NOTE:
lxd init
command if not already initialized. This is important because you won't be able to launch any container without a storage pool.
Example 5: How to List all the running Containers
If you want to list all the running containers then you need to use lxc list
command as shown below.
[root@localhost ~]# lxc list
+-----------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+-----------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| test-container3 | RUNNING | 10.87.229.194 (eth0) | fd42:93e5:a713:4d5a:216:3eff:fe80:609f (eth0) | CONTAINER | 0 |
+-----------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
Example 6: How to Stop a Container
If you want to stop any running container then you need to use lxc stop <container_name>
syntax. In this example, we are stopping test-container3 using lxc stop test-container3
command as shown below.
[root@localhost ~]# lxc stop test-container3
Example 7: How to Start a Container
If you want to start a stopped container then you need to use lxc start <container_name>
syntax. In this example, we are starting a stopped container test-container3
using lxc start test-container3
command as shown below.
[root@localhost ~]# lxc start test-container3
Example 8: How to restart a Container
You also have the option to restart a container. You can use lxc restart <container_name>
syntax to restart the container. In this example, we are restarting container test-container3
using lxc restart test-container3
command as shown below.
[root@localhost ~]# lxc restart test-container3
Example 9: How to delete a Container
In order to delete a container, you need to first stop it by using lxc stop test-container3
command as shown below.
[root@localhost ~]# lxc stop test-container3
Then delete it by using lxc delete test-container3
command as shown below.
[root@localhost ~]# lxc delete test-container3
Example 10: How to execute a command on the Container
If you want to execute a command on the container then you need to use lxc exec <container_name> <command>
syntax. In this example, we are checking the OS information of container test-container3
using lxc exec test-container3 cat /etc/os-release
command as shown below.
[root@localhost ~]# lxc exec test-container3 cat /etc/os-release
NAME="Rocky Linux"
VERSION="8.5 (Green Obsidian)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="8.5"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Rocky Linux 8.5 (Green Obsidian)"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:rocky:rocky:8:GA"
HOME_URL="https://rockylinux.org/"
BUG_REPORT_URL="https://bugs.rockylinux.org/"
ROCKY_SUPPORT_PRODUCT="Rocky Linux"
ROCKY_SUPPORT_PRODUCT_VERSION="8"
Example 11: How to Check a Container's Information
If you want to check complete information about a container then you need to use lxc info <container_name>
syntax. In this example, we are checking all the information related to container test-container3
using lxc info test-container3
command as shown below.
[root@localhost ~]# lxc info test-container3
Name: test-container3
Status: RUNNING
Type: container
Architecture: x86_64
PID: 13477
Created: 2022/05/06 15:37 EDT
Last Used: 2022/05/06 15:37 EDT
Resources:
Processes: 11
Disk usage:
root: 696.93MiB
CPU usage:
CPU usage (in seconds): 2
Memory usage:
Memory (current): 116.91MiB
Memory (peak): 117.85MiB
........................................
Example 12: How to Get Shell Access to a LXC Container
If you want to get the shell access to a container then you need to use lxc exec <container_name> bash
syntax. In this example, we are taking shell access of test-container3
using lxc exec test-container3 bash
command as shown below.
[root@localhost ~]# lxc exec test-container3 bash
[root@test-container3 ~]#
Example 13: How to List Prebuilt Images
If you want to check the list of all prebuilt images then you need to use lxc image list images
command. But the output shown will be quite long so if you are looking for any specific image then you can only grep the list of that image by using lxc image list images: | grep -i <image_name>
. For example, here we are looking for all centos based prebuilt images by using lxc image list images: | grep -i centos
command as shown below.
[root@localhost ~]# lxc image list images: | grep -i centos
| centos/7 (3 more) | 88523847ac03 | yes | Centos 7 amd64 (20220506_09:01) | x86_64 | VIRTUAL-MACHINE | 402.75MB | May 6, 2022 at 12:00am (UTC) |
| centos/7 (3 more) | c7d6fd22f727 | yes | Centos 7 amd64 (20220506_09:01) | x86_64 | CONTAINER | 84.45MB | May 6, 2022 at 12:00am (UTC) |
| centos/7/arm64 (1 more) | 0b78532f3bc8 | yes | Centos 7 arm64 (20220506_07:45) | aarch64 | CONTAINER | 82.44MB | May 6, 2022 at 12:00am (UTC) |
| centos/7/armhf (1 more) | 9009267d8032 | yes | Centos 7 armhf (20220506_09:02) | armv7l | CONTAINER | 80.52MB | May 6, 2022 at 12:00am (UTC) |
| centos/7/cloud (1 more) | 6b84047364b7 | yes | Centos 7 amd64 (20220506_09:01) | x86_64 | VIRTUAL-MACHINE | 415.25MB
............................................
Example 14: How to Display Information about Network Interface
If you want to check complete information about a network interface then you need to use lxc network show <interface_name>
syntax. In this example, we are checking complete information about interface lxdbr0
using lxc network show lxdbr0
command as shown below.
[root@localhost ~]# lxc network show lxdbr0
config:
ipv4.address: 10.87.229.1/24
ipv4.nat: "true"
ipv6.address: fd42:93e5:a713:4d5a::1/64
ipv6.nat: "true"
description: ""
name: lxdbr0
type: bridge
used_by:
- /1.0/instances/test-container3
- /1.0/profiles/default
managed: true
status: Created
locations:
- none
Example 15: How to check the default profile using lxc command
If you want to check the LXC default profile then you need to use lxc profile show default
command as shown below. This will display you many of the important information like network interface details, storage details, container details etc.
[root@localhost ~]# lxc profile show default
config: {}
description: Default LXD profile
devices:
eth0:
name: eth0
network: lxdbr0
type: nic
root:
path: /
pool: test-storage
type: disk
name: default
used_by:
- /1.0/instances/test-container3
Example 16: How to take Snapshot of an Instance using lxc command
If you want to take snapshot of an Instance then you need to use lxc snapshot <container_name> <snapshot_name>
syntax. In this example we are taking snapshot of instance test-container3
and naming it as test-container3_snap
using lxc snapshot test-container3 test-container3_snap
command as shown below.
[root@localhost ~]# lxc snapshot test-container3 test-container3_snap [root@localhost ~]# lxc info test-container3 Name: test-container3 Status: RUNNING Type: container Architecture: x86_64 PID: 18173 Created: 2022/05/06 15:37 EDT Last Used: 2022/05/06 16:28 EDT Resources: Processes: 12 Disk usage: root: 696.97MiB CPU usage: CPU usage (in seconds): 3 Memory usage: Memory (current): 75.88MiB Memory (peak): 77.03MiB Network usage: eth0: Type: broadcast State: UP Host interface: vethb68b6476 MAC address: 00:16:3e:b2:cd:5e MTU: 1500 Bytes received: 938B Bytes sent: 1.46kB Packets received: 8 Packets sent: 16 IP addresses: inet: 10.87.229.7/24 (global) inet6: fd42:93e5:a713:4d5a:216:3eff:feb2:cd5e/64 (global) inet6: fe80::216:3eff:feb2:cd5e/64 (link) lo: Type: loopback State: UP MTU: 65536 Bytes received: 0B Bytes sent: 0B Packets received: 0 Packets sent: 0 IP addresses: inet: 127.0.0.1/8 (local) inet6: ::1/128 (local) Snapshots: +----------------------+----------------------+------------+----------+ | NAME | TAKEN AT | EXPIRES AT | STATEFUL | +----------------------+----------------------+------------+----------+ | snap0 | 2022/05/06 16:25 EDT | | NO | +----------------------+----------------------+------------+----------+ | test-container3_snap | 2022/05/06 16:27 EDT | | NO | +----------------------+----------------------+------------+----------+
Example 17: How to restore an instance from snapshot using lxc command
If you want to restore an instance from snapshot then you need to use lxc restore <container_name> <snapshot_name>
syntax. Here the snapshot name will be the snapshot using which you want to restore your instance. For example, here we are restoring test-container3
instance from test-container3_snap
snapshot using lxc restore test-container3 test-container3_snap
command as shown below.
[root@localhost ~]# lxc restore test-container3 test-container3_snap
Example 18: How to take backup of an Instance using lxc command
You can take backup of any instance by using lxc export <container_name> <backup_name>
syntax. In this example we are taking backup of test-container3 under /root/backup/lxd
directory using lxc export test-container3 /root/backup/lxd/test-container3_bkp--$(date +'%m-%d-%Y').tar.xz --optimized-storage
command as shown below.
[root@localhost ~]# lxc export test-container3 /root/backup/lxd/test-container3_bkp--$(date +'%m-%d-%Y').tar.xz --optimized-storage
Backup exported successfully!
Example 19: How to Restore Instance from a backup using lxc command
Similarly, if you want to restore your instance from a backup then you need to import the backup using lxc import command. For example, here we are restoring our instance test-container3
from backup test-container3_bkp--05-07-2022.tar.xz
which is available under /root/backup/lxd
directory by using lxc import /root/backup/lxd/test-container3_bkp--05-07-2022.tar.xz
command as shown below.
To verify the restoration, you can use lxc list command as shown below. It will show you the container restored in stopped state. You can now start the container and verify if everything is working fine as expected.
[root@localhost ~]# lxc import /root/backup/lxd/test-container3_bkp--05-07-2022.tar.xz [root@localhost ~]# lxc list +-----------------+---------+------+------+-----------+-----------+ | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | +-----------------+---------+------+------+-----------+-----------+ | test-container3 | STOPPED | | | CONTAINER | 2 | +-----------------+---------+------+------+-----------+-----------+
NOTE:
Error: Create instance from backup: Cannot restore volume, already exists on target
.
Example 20: How to Check all the options available with lxc command
If you want to check all the options with lxc command then you need to use lxc --help
command as shown below.
[root@localhost ~]# lxc --help
Description:
Command line client for LXD
All of LXD's features can be driven through the various commands below.
For help with any of those, simply call them with --help.
Usage:
lxc [command]
Available Commands:
alias Manage command aliases
cluster Manage cluster members
config Manage instance and server configuration options
console Attach to instance consoles
copy Copy instances within or in between LXD servers
delete Delete instances and snapshots
exec Execute commands in instances
export Export instance backups
file Manage files in instances