lxd
Useful LXD Commands
Images
List Images
1
2
3
4
lxc image list ubuntu:
lxc image list images:
lxc image alias list images:
lxc image list
Show Image Information
1
2
lxc image info ubuntu:24.04
lxc image info images:debian/13
Copy an Image
1
lxc image copy ubuntu:24.04 local: --alias ubuntu-24.04
Export and Import Images
1
2
lxc image export IMAGE /backup/
lxc image import backup.tar.gz --alias myimage
Containers
Create and Launch Containers
1
2
3
4
5
6
7
lxc launch ubuntu:14.04 CONTAINERNAME
lxc launch ubuntu:14.04/armhf armcont
lxc launch images:alpine/3.3/amd64
lxc launch ubuntu:24.04 mycontainer
lxc launch images:debian/13 debian13
lxc launch images:debian/13/amd64 debian13
lxc launch images:alpine/3.22/amd64 alpine
1
2
3
4
5
6
7
lxc image list
lxc launch $FINGERPRINT $CONTAINERNAME
lxc config device override $CONTAINERNAME eth0
lxc config device set $CONTAINERNAME eth0 ipv4.address $IP
lxc restart $CONTAINERNAME
lxc list
lxc config device show $CONTAINERNAME
Create a Container Without Starting It
1
2
lxc init images:alpine/3.3/amd64 alpinecont
lxc init images:debian/13 debian-trixie
Start, Stop and Restart
1
2
3
4
5
6
lxc start CONTAINER
lxc stop CONTAINER
lxc restart CONTAINER
lxc stop CONTAINER --force
lxc pause CONTAINER
lxc resume CONTAINER
List Containers
1
2
3
4
5
6
7
lxc list
lxc list --columns "nsapt"
lxc list security.privileged=true
lxc list -c ns4
lxc list -c ns46
lxc list status=Running
lxc list status=Stopped
Container Information
1
2
3
lxc info CONTAINERNAME
lxc info CONTAINERNAME --resources
lxc info --resources
Rename, Copy and Delete
1
2
3
4
lxc move CONTAINERNAME NEWNAME
lxc copy CONTAINER1 CONTAINER2
lxc delete CONTAINER
lxc delete CONTAINER --force
Start or Stop All Containers
1
2
for c in $(lxc list -c n --format csv); do lxc start "$c"; done
for c in $(lxc list -c n --format csv); do lxc stop "$c"; done
Container Access
Execute Commands
1
2
3
4
5
6
7
lxc exec CONTAINERNAME -- PROGRAM OPTIONS
lxc exec CONTAINERNAME sh
lxc exec CONTAINER -- bash
lxc exec CONTAINER -- sh
lxc exec CONTAINER -- ip addr
lxc exec CONTAINER -- systemctl status nginx
lxc exec CONTAINER -- df -h
Execute as a Specific User
1
lxc exec CONTAINER --user 1000 -- bash
Set Environment Variables
1
2
lxc exec CONTAINERNAME --env KEY=VALUE PROGRAM
lxc exec CONTAINER --env KEY=value -- command
Console
1
lxc console CONTAINER
Container Files
Pull Files
1
2
lxc file pull CONTAINERNAME/etc/passwd /tmp/mypasswd
lxc file pull CONTAINER/etc/passwd /tmp/passwd
Push Files
1
2
lxc file push /tmp/mypasswd CONTAINERNAME/etc/passwd
lxc file push /tmp/file CONTAINER/etc/file
Edit Files
1
2
lxc file edit CONTAINERNAME/etc/passwd
lxc file edit CONTAINER/etc/hosts
Copy Directories
1
2
lxc file push -r ./directory CONTAINER/root/
lxc file pull -r CONTAINER/root/directory ./
Snapshots
Create Snapshots
1
2
lxc snapshot CONTAINERNAME SNAPNAME
lxc snapshot CONTAINERNAME
Restore Snapshots
1
lxc restore CONTAINERNAME SNAPNAME
Create a Container From a Snapshot
1
lxc copy CONTAINERNAME/SNAPNAME NEWCONTAINER
Delete Snapshots
1
lxc delete CONTAINERNAME/SNAPNAME
Rename Snapshots
1
lxc move CONTAINERNAME/SNAPNAME CONTAINERNAME/NEWSNAPNAME
View Snapshot Information
1
lxc info CONTAINERNAME
Configuration
Edit Configuration
1
2
export VISUAL=/usr/bin/vim
lxc config edit CONTAINERNAME
Set Configuration Values
1
lxc config set CONTAINERNAME KEY VALUE
Unset Configuration Values
1
lxc config unset CONTAINER KEY
Show Configuration
1
2
lxc config show CONTAINERNAME
lxc config show CONTAINERNAME --expanded
Container Devices
1
2
3
lxc config device show CONTAINER
lxc config device add CONTAINER DEVICE TYPE KEY=VALUE
lxc config device remove CONTAINER DEVICE
Profiles
List Profiles
1
lxc profile list
Show a Profile
1
lxc profile show PROFILE
Edit a Profile
1
lxc profile edit PROFILE
Add a Profile
1
lxc profile add CONTAINER PROFILE
Remove a Profile
1
lxc profile remove CONTAINER PROFILE
CPU and Memory
Set CPU Limit
1
lxc config set CONTAINER limits.cpu 2
Set Memory Limit
1
lxc config set CONTAINER limits.memory 2GiB
Remove CPU and Memory Limits
1
2
lxc config unset CONTAINER limits.cpu
lxc config unset CONTAINER limits.memory
Check Resource Usage
1
2
3
lxc info CONTAINER
lxc info CONTAINER --resources
lxc info --resources
Networking
List Networks
1
lxc network list
Show a Network
1
lxc network show NETWORK
Edit a Network
1
lxc network edit NETWORK
Get Network Settings
1
2
lxc network get NETWORK ipv4.address
lxc network get NETWORK ipv4.nat
Set Network Settings
1
lxc network set NETWORK KEY VALUE
Create a Network
1
2
lxc network create NETWORK
lxc network create NETWORK ipv4.address=10.10.10.1/24 ipv4.nat=true
Delete a Network
1
lxc network delete NETWORK
Network Devices
1
2
3
4
lxc config device show CONTAINER
lxc config device add CONTAINER eth0 nic network=NETWORK
lxc config device remove CONTAINER eth0
lxc config device set CONTAINER eth0 ipv4.address 192.168.2.71
Network Forwarding
1
2
3
4
lxc network forward list NETWORK
lxc network forward show NETWORK IP
lxc network forward create NETWORK IP
lxc network forward delete NETWORK IP
Add a Port Forward
1
lxc network forward port add NETWORK IP tcp EXTERNAL-PORT TARGET-IP TARGET-PORT
Remove a Port Forward
1
lxc network forward port remove NETWORK IP tcp EXTERNAL-PORT
Storage
List Storage Pools
1
lxc storage list
Show a Storage Pool
1
lxc storage show POOL
List Storage Volumes
1
lxc storage volume list POOL
Show a Storage Volume
1
lxc storage volume show POOL container/CONTAINER
Host Directory Mounts
Mount a Host Directory
1
lxc config device add CONTAINER DEVICE disk source=/HOST/PATH path=/CONTAINER/PATH
Mount a Single File
1
lxc config device add CONTAINER DEVICE disk source=/HOST/FILE path=/CONTAINER/FILE
Remove a Mount
1
lxc config device remove CONTAINER DEVICE
Monitoring and Troubleshooting
LXD Version and Server Information
1
2
lxc version
lxc info
Monitor LXD Events
1
2
lxc monitor
lxc monitor --type=logging
Check LXD Service
1
2
systemctl status snap.lxd.daemon
systemctl status lxd
Restart LXD
1
2
sudo systemctl restart snap.lxd.daemon
sudo systemctl restart lxd
Host Network Diagnostics
1
2
3
4
5
6
ip -br addr
ip addr
ip route
ip link
bridge link
ss -lntp
Container Network Diagnostics
1
2
3
4
lxc exec CONTAINER -- ip addr
lxc exec CONTAINER -- ip -br addr
lxc exec CONTAINER -- ip route
lxc exec CONTAINER -- ss -lntp
Connectivity Tests
1
2
3
4
ping IP
lxc exec CONTAINER -- ping -c 4 GATEWAY-IP
lxc exec CONTAINER -- ping -c 4 8.8.8.8
lxc exec CONTAINER -- ping -c 4 google.com
DNS Tests
1
2
cat /etc/resolv.conf
getent hosts google.com
HTTP Tests
1
2
3
curl http://IP
curl -v http://IP:PORT
lxc exec CONTAINER -- curl http://127.0.0.1
Check Listening Ports
1
2
3
ss -lntp
ss -lntp | grep :80
ss -lntp | grep :PORT
Projects
List Projects
1
lxc project list
Show a Project
1
lxc project show PROJECT
Switch Project
1
lxc project switch PROJECT
Remote Servers
List Remotes
1
lxc remote list
Add a Remote
1
lxc remote add NAME https://SERVER:8443
Remove a Remote
1
lxc remote remove NAME
List Containers on a Remote
1
lxc list NAME:
Copy a Container to a Remote
1
lxc copy CONTAINER REMOTE:CONTAINER
Move a Container to a Remote
1
lxc move CONTAINER REMOTE:CONTAINER
Images From Containers
Publish a Container as an Image
1
lxc publish CONTAINER --alias IMAGE
Launch From a Custom Image
1
lxc launch IMAGE CONTAINER
Publish a Snapshot as an Image
1
lxc publish CONTAINER/SNAPSHOT --alias IMAGE
Container Backup
Create a Backup Snapshot
1
lxc snapshot CONTAINER BACKUP-SNAPSHOT
Restore a Backup Snapshot
1
lxc restore CONTAINER BACKUP-SNAPSHOT
Copy a Backup Snapshot
1
lxc copy CONTAINER/BACKUP-SNAPSHOT BACKUP-CONTAINER
App Management Inside Containers
Enter a Container
1
lxc exec CONTAINER -- bash
Update Packages
1
2
apt update
apt upgrade
Install Packages
1
apt install PACKAGE
Check a Service
1
systemctl status SERVICE
Restart a Service
1
systemctl restart SERVICE
Enable a Service
1
systemctl enable SERVICE
Check Listening Ports
1
ss -lntp
Useful One-Liners
Containers and IP Addresses
1
lxc list -c ns4
Containers and Profiles
1
lxc list -c nsp
Expanded Container Configuration
1
lxc config show CONTAINER --expanded
Container Devices
1
lxc config device show CONTAINER
Network List
1
lxc network list
Storage List
1
lxc storage list
Profile List
1
lxc profile list
Project List
1
lxc project list
Remote List
1
lxc remote list
LXD Events
1
lxc monitor
This post is licensed under
CC BY 4.0
by the author.
