Linux lxc Command
LXC (Linux Containers) is an operating system-level virtualization technology that allows running multiple isolated Linux environments (containers) 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 virtual machines while maintaining operating efficiency close to native performance. It is the foundation of container technologies such as Docker.
LXC Basic Concepts
Container
A container is a lightweight, isolated process space with its own file system, network configuration, and process tree.
Template
A template is a predefined configuration and file system layout used to create containers. LXC provides a variety of templates, such as ubuntu, debian, centos, etc.
Control Groups (cgroups)
A Linux kernel feature used to limit, record, and isolate resource usage (CPU, memory, disk I/O, etc.) of process groups.
Namespaces
A Linux kernel feature that provides process isolation, including PID, network, mount point, UTS, and other namespaces.
LXC Command Syntax
Basic command format:
lxc [选项] [参数]
Common global options:
--debug: Enable debug output--logfile=<文件>: Specify the log file--version: Display version information--help: Display help information
LXC Common Commands
Container Management
Create a Container
lxc-create -n -t [-- ]
Example:
lxc-create -n mycontainer -t ubuntu -- -r jammy
Start the Container
lxc-start -n [-d] # -d 表示在后台运行
Stop the Container
lxc-stop -n
Delete the Container
lxc-destroy -n
Container Information
List Containers
lxc-ls [--active] # --active 只显示运行中的容器
View Container Status
lxc-info -n
View Container Console
lxc-console -n
Container Configuration
Copy a Container
lxc-copy -n -N
Freeze/Unfreeze a Container
Example
lxc-unfreeze -n # Unfreeze
Modify Container Configuration
lxc-config -n -s =
LXC Configuration File
Each container's configuration file is located at:
/var/lib/lxc//config
Common configuration items:
Example
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
lxc-create -n myubuntu -t ubuntu -- -r jammy
# Start the container
lxc-start -n myubuntu -d
# Enter the container console
lxc-console -n myubuntu
# Execute commands 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.memory.limit_in_bytes = 256M
Then restart the container to apply the configuration:
Example
lxc-start -n myubuntu -d
Troubleshooting Common Problems
Problem 1: Container Cannot 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 bridged 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
container = lxc.Container("mycontainer")
if not container.defined:
container.create("ubuntu", {"release": "jammy"})
container.start()
Container Snapshots
Example
lxc-snapshot -n -r snap0 # Restore a snapshot
Container Migration
Example
lxc-checkpoint -n -D /path/to/dump
# On the target host
lxc-restore -n -D /path/to/dump
Summary
LXC provides a lightweight virtualization solution that is well suited for scenarios requiring isolation but not full virtual machines. By mastering lxc commands, you can efficiently create, manage, and maintain Linux containers. With a deeper understanding of LXC, you can further explore its advanced features, such as custom configuration, resource limits, and container orchestration.
Other Extensions
Linux Command Reference