LXC/LXD
Updated: September 10, 2025Categories: Virtualization, Container
Printed from:
LXC/LXD Comprehensive Cheatsheet
1. Installation and Setup
LXC/LXD Installation
Bash
1234567891011# On Ubuntu/Debian
sudo apt update
sudo apt install lxd lxc
# On CentOS/RHEL
sudo yum install epel-release
sudo yum install lxc lxc-devel lxd
# On Arch Linux
sudo pacman -S lxc lxd
LXD Initialization
Bash
123456# Initial system configuration
sudo lxd init
# Non-interactive initialization
sudo lxd init --auto
2. Container vs System Container Concepts
- Container Types:
- LXC (Linux Containers): Low-level container technology
- LXD: Container management daemon and API
- System Containers: Full system environment, similar to virtual machines
- Application Containers: Single application or service
3. Basic LXC/LXD Commands
LXD Commands
Bash
123456789101112# List available commands
lxc --help
# Show system information
lxc info
# List all containers
lxc list
# Show running containers
lxc list status=running
4. Container Lifecycle Management
Creating Containers
Bash
123456789# Create Ubuntu container
lxc launch ubuntu:22.04 my-ubuntu-container
# Create container from specific image
lxc launch images:debian/12 my-debian-container
# Create named container without starting
lxc init ubuntu:22.04 my-container
Container Operations
Bash
123456789101112131415161718# Start a container
lxc start my-container
# Stop a container
lxc stop my-container
# Restart a container
lxc restart my-container
# Delete a container
lxc delete my-container
# Pause a container
lxc pause my-container
# Unpause a container
lxc unpause my-container
5. Image Management
Image Handling
Bash
123456789101112# List available images
lxc image list
# Retrieve remote images
lxc image list images:
# Copy an image
lxc image copy ubuntu:22.04 local:
# Delete an image
lxc image delete <image_fingerprint>
6. Container Configuration
Managing Profiles
Bash
123456789101112# List profiles
lxc profile list
# Create a new profile
lxc profile create dev-profile
# Edit profile
lxc profile edit dev-profile
# Apply profile to container
lxc launch ubuntu:22.04 my-container -p dev-profile
Configuration Examples
Bash
123456# Set container memory limit
lxc config set my-container limits.memory=2GB
# Set CPU cores
lxc config set my-container limits.cpu=2
7. Storage Management
Bash
123456789# List storage pools
lxc storage list
# Create storage pool
lxc storage create my-pool dir
# Add storage to container
lxc config device add my-container data disk source=/path/to/data path=/mnt/data
8. Network Configuration
Bash
123456789# List networks
lxc network list
# Create bridge network
lxc network create lxdbr0 ipv4.address=10.0.8.1/24 ipv4.nat=true
# Attach network to container
lxc config device add my-container eth0 nic nictype=bridged parent=lxdbr0
9. Resource Limits and Cgroups
Bash
12345678# Set CPU shares
lxc config set my-container limits.cpu.shares=2048
# Set memory and swap limits
lxc config set my-container \
limits.memory=2GB \
limits.memory.swap=4GB
10. Snapshots and Backups
Bash
123456789101112# Create snapshot
lxc snapshot my-container initial-setup
# List snapshots
lxc info my-container
# Restore snapshot
lxc restore my-container initial-setup
# Export container
lxc export my-container /path/to/backup.tar.gz
11. Container Migration
Bash
123456# Migrate container between hosts
lxc copy my-container remote:new-container
# Cluster migration
lxc cluster migrate my-container new-cluster-node
12. Security Features
Bash
123456# Create unprivileged container
lxc launch ubuntu:22.04 unprivileged-container -c security.privileged=false
# Set container user mapping
lxc config set my-container security.idmap.base 100000
13. File Sharing
Bash
1234567# Bind mount host directory
lxc config device add my-container hostdata disk source=/host/path path=/container/path
# Copy files
lxc file push local_file my-container/path/to/destination
lxc file pull my-container/path/to/file local_destination
14. Monitoring and Logging
Bash
123456# View container logs
lxc info my-container --show-log
# Monitor container resources
lxc monitor my-container
15. Cloud-Init Integration
Bash
1234# Use cloud-init with container
lxc launch ubuntu:22.04 my-container \
-c user.user-data="$(cat cloud-config.yaml)"
16. Performance Optimization
- Use
--type=performanceprofile - Limit unnecessary services
- Use host network when possible
- Tune cgroup settings
17. Troubleshooting
Bash
123456789# Check LXD daemon status
systemctl status lxd
# View LXD logs
journalctl -u lxd
# Debug container
lxc exec my-container -- bash
18. LXC vs Docker Comparison
| Feature | LXC/LXD | Docker |
|---|---|---|
| Virtualization | System Containers | Application Containers |
| Overhead | Low | Very Low |
| Full OS Support | Yes | Limited |
| Resource Isolation | Comprehensive | Process-level |
19. Use Cases
- Development environments
- Microservice architectures
- Continuous integration
- Isolated testing
- Legacy application hosting
- Edge computing
20. Best Practices
- Always use unprivileged containers
- Limit container resources
- Regularly update container images
- Use profiles for consistent configurations
- Implement proper network segmentation
- Monitor container performance
- Backup critical containers
- Use security features like AppArmor
Note: Always refer to the latest LXC/LXD documentation for most current practices and commands.
Continue Learning
Discover more cheatsheets to boost your productivity