LXD Common Commands Cheat Sheet

A comprehensive command reference for developers and system administrators to efficiently manage containers and virtual machines with LXD.

1. Instance Management

Create & Launch

Launch an Ubuntu 22.04 container named my-ubuntu.

lxc launch images:ubuntu/22.04 my-ubuntu

Launch a Debian 12 virtual machine.

lxc launch images:debian/12 my-debian-vm --vm

Launch container with specific profile applied.

lxc launch images:alpine/edge my-alpine -p default -p macvlan

Launch with resource configuration.

lxc launch images:ubuntu/22.04 web-server -c limits.cpu=2 -c limits.memory=1GiB

View & List

View brief information of all instances.

lxc list

Filter names using regular expressions.

lxc list "web-*"

Custom output columns (n:name, s:state, 4:IPv4, P:profile, t:type).

lxc list -c n,s,4,P,t

Execute Commands & Interaction

Get an interactive bash shell inside the container.

lxc exec my-ubuntu -- bash

Run a single command, e.g., update package list.

lxc exec my-ubuntu -- apt update

Get console access to virtual machine.

lxc console my-debian-vm

Lifecycle Management

Start, stop, restart, and delete instances.

lxc start my-ubuntu lxc stop my-ubuntu lxc restart my-ubuntu lxc delete my-ubuntu --force

File Transfer

Push and pull files between host and container.

lxc file push ./local.conf my-ubuntu/etc/nginx/nginx.conf lxc file pull my-ubuntu/var/log/syslog . lxc file push -r ./my-app my-ubuntu/opt/

2. Practical Shortcuts

Information Queries

Quickly get IP address of an instance.

lxc list my-ubuntu -c 4 --format csv | cut -d' ' -f1

List all running instance names.

lxc list --format csv -c n --filter status=running

View resource usage of all instances (CPU, memory, network).

lxc list -c n,s,C,m,N

Batch Operations

Stop all running instances.

lxc stop $(lxc list -c n --format csv --filter status=running)

Delete all stopped instances.

lxc delete $(lxc list -c n --format csv --filter status=stopped)

Execute updates in all running Ubuntu containers.

for c in $(lxc list images:ubuntu -c n --format csv --filter status=running); do lxc exec $c -- apt update && apt upgrade -y; done

3. Snapshots & Backups

Snapshot Management

Create, restore, and manage snapshots.

lxc snapshot my-ubuntu backup-2025-01-25 lxc restore my-ubuntu backup-2025-01-25 lxc copy my-ubuntu/backup-2025-01-25 my-ubuntu-restored

Export & Import

Export instances for backup and migration.

lxc export my-ubuntu my-ubuntu-backup.tar.gz lxc import my-ubuntu-backup.tar.gz my-ubuntu-restored

4. Network & Storage

Network Management

Create networks and configure networking.

lxc network create mybr0 lxc network attach mybr0 my-container lxc config device add my-container webproxy proxy listen=tcp:0.0.0.0:8080 connect=tcp:127.0.0.1:80

Storage Management

Create storage pools and manage storage.

lxc storage create zpool zfs size=50GB lxc config device add my-container shared disk source=/data path=/mnt/data