Linux lxc Command
LXC (Linux Containers) is an operating-system-level virtualization technology that allows multiple isolated Linux environments (containers) to run on a single Linux system. Unlike traditional virtual machines, LXC containers share the host system's kernel, making them lighter and more efficient.
LXC provides isolation close to that of a virtual machine while maintaining near-native performance. It is the foundation of container technologies such as Docker.
Basic LXC Concepts
Container
A container is a lightweight, isolated process space with its own filesystem, network configuration, and process tree.
Template
A template is a predefined configuration and filesystem layout used to create containers. LXC provides a variety of templates, such as ubuntu, debian, centos, and others.
Control Groups (cgroups)
A Linux kernel feature used to limit, account for, and isolate the resource usage of process groups (CPU, memory, disk I/O, etc.).
Namespaces
A Linux kernel feature that provides process isolation, including PID, network, mount, UTS, and other namespaces.
LXC Command Syntax
Basic command format:
lxc [选项] [参数]
Common global options:
--debug: Enable debug output--logfile=<文件>: Specify a log file--version: Display version information--help: Display help information
Common LXC Commands
Container Management
Create a container
lxc-create -n -t [-- ]
Example:
lxc-create -n mycontainer -t ubuntu -- -r jammy
Start a container
lxc-start -n [-d] # -d 表示在后台运行
Stop a container
lxc-stop -n
Delete a container
lxc-destroy -n
Container Information
List containers
lxc-ls [--active] # --active 只显示运行中的容器
View container status
lxc-info -n
View the container console
lxc-console -n
Container Configuration
Copy a container
lxc-copy -n -N
Freeze/Unfreeze a container
Example
lxc-freeze
-n
Freeze
lxc-unfreeze
-n
Unfreeze
Modify container configuration
lxc-config -n -s =
LXC Configuration Files
Each container's configuration file is located at:
/var/lib/lxc//config
Common configuration items:
Example
Network configuration
lxc.net.0.type
=
veth
lxc.net.0.link
=
lxcbr0
lxc.net.0.flags
=
up
Resource limits
lxc.cgroup.cpu.shares
=
512
lxc.cgroup.memory.limit_in_bytes
=
512M
Practical examples
Example 1: Create and Run an Ubuntu Container
Example
Create a container
lxc-create
-n
myubuntu
-t
ubuntu
--
-r
jammy
Start a container
lxc-start
-n
myubuntu
-d
Enter the container console
lxc-console
-n
myubuntu
Execute a command inside the container
lxc-attach
-n
myubuntu
--
apt update
Example 2: Limit Container Resources
Edit the container configuration file /var/lib/lxc/myubuntu/config, add:
Example
lxc.cgroup.cpu.shares
=
256
lxc.cgroup.memory.limit_in_bytes
=
256M
Then restart the container for the configuration to take effect:
Example
lxc-stop
-n
myubuntu
lxc-start
-n
myubuntu
-d
Common Problem Solutions
Problem 1: A Container Fails to Start
- Check the logs:
cat /var/log/lxc/<容器名>.log - Ensure the kernel supports LXC:
grep CONFIG_CGROUP /boot/config-$(uname -r)
Problem 2: Network Connection Issues
- Check the bridge network:
brctl show - Ensure lxc-net is installed:
apt install lxc-net
Problem 3: Permission Issues
- Run commands as the root user
- or add the user to the lxc group:
usermod -aG lxc <用户名>
Advanced Usage
Using the LXC API
LXC provides Python bindings, allowing containers to be managed programmatically:
Example
import
lxc
container
=
lxc.
Container
(
"mycontainer"
)
if
not
container.
defined
:
container.
create
(
"ubuntu"
,
{
"release"
:
"jammy"
}
)
container.
start
(
)
Container Snapshots
Example
lxc-snapshot
-n
Create a snapshot
lxc-snapshot
-n
-r
snap0
Restore a snapshot
Container Migration
Example
On the source host
lxc-checkpoint
-n
-D
/
path
/
to
/
dump
On the target host
lxc-restore
-n
-D
/
path
/
to
/
dump
Summary
LXC offers a lightweight virtualization solution, ideal for scenarios that need isolation without a full virtual machine. By mastering the lxc command, you can efficiently create, manage, and maintain Linux containers. As you gain a deeper understanding of LXC, you can further explore its advanced features, such as custom configuration, resource limits, and container orchestration.
Linux Command Reference