Skip to content

LXC classic — the original Linux container runtime

Use legacy LXC (liblxc and the lxc-* CLIs) to run unprivileged system containers without LXD, with templates, config files, and per-container AppArmor profiles.

LXC — liblxc and the original lxc-create/lxc-start/lxc-attach CLIs — predates LXD and Docker. It is the kernel-side primitive both daemons were built on, and it is still the right choice when you want long-lived system containers without running an extra management daemon. The trade-off is more manual configuration: there is no built-in REST API, snapshot/migration is rougher, and you write the container config files by hand.

How to verify

A working classic-LXC install responds to these probes:

lxc-info --version
lxc-checkconfig
ls /usr/share/lxc/templates/
cat /etc/lxc/default.conf
lxc-ls --fancy

lxc-checkconfig runs through every kernel feature LXC depends on (cgroups, namespaces, AppArmor, etc.) and prints OK/Missing for each. On a stripped-down VPS kernel this is the first place to look when containers refuse to start.

What’s happening

liblxc is a C library that wraps the kernel container primitives — clone() with namespace flags, cgroup writes, AppArmor labels, capability drops — into a higher-level “container” abstraction. The lxc-* CLI tools are thin wrappers over the library. There is no daemon: when you lxc-start a container, the calling process forks lxc-init (or systemd if you configure it) inside the namespaces and exits. The container then runs as a child of init.

Two kinds of containers exist: privileged (the container’s root is host’s root) and unprivileged (uses user namespaces — the container’s root maps to a UID like 100000 on the host). Privileged is the historical default and is dangerous: a container escape is host root. Unprivileged is the modern default and matches Podman’s rootless model. Always use unprivileged for production.

Config is per-container: /var/lib/lxc/<name>/config for privileged, ~/.local/share/lxc/<name>/config for unprivileged. Every kernel knob — cgroup limits, mount entries, network type, AppArmor profile — is a line in this file.

The procedure

  1. Install LXC and the templates:
sudo apt-get install -y lxc lxc-templates lxc-utils uidmap libpam-cgfs
lxc-checkconfig

Every line in the checkconfig output should report enabled. If User namespace: missing appears, the kernel lacks user_ns — unprivileged containers will not work.

  1. Configure unprivileged container support for the current user. The subuid/subgid ranges must exist (LXD users get them automatically; LXC users do not):
grep $USER /etc/subuid || sudo usermod --add-subuids 100000-165535 $USER
grep $USER /etc/subgid || sudo usermod --add-subgids 100000-165535 $USER

mkdir -p ~/.config/lxc
cat > ~/.config/lxc/default.conf <<EOF
lxc.idmap = u 0 100000 65536
lxc.idmap = g 0 100000 65536
lxc.net.0.type = veth
lxc.net.0.link = lxcbr0
lxc.net.0.flags = up
EOF
  1. Create an unprivileged container from the standard Ubuntu template:
lxc-create -t download -n web1 -- \
  --dist ubuntu --release 24.04 --arch amd64

The download template fetches a pre-built rootfs from images.linuxcontainers.org, verifies its signature, and unpacks it into ~/.local/share/lxc/web1/rootfs/.

  1. Start the container and attach to it:
lxc-start -n web1
lxc-info -n web1
lxc-attach -n web1
# you are now inside; the host process tree owns the init
  1. Edit the container’s config to add a resource limit and a bind mount. Stop the container first — most options are not live-editable:
lxc-stop -n web1
nano ~/.local/share/lxc/web1/config

Add:

lxc.cgroup2.memory.max = 2147483648
lxc.cgroup2.cpu.max = 200000 100000
lxc.mount.entry = /srv/web1 srv none bind,create=dir 0 0

The cpu line means “200ms of CPU per 100ms wall time” — 2 cores’ worth of CPU quota. Restart and verify:

lxc-start -n web1
lxc-attach -n web1 -- cat /sys/fs/cgroup/memory.max
  1. Snapshot and clone the container for an immutable backup:
lxc-stop -n web1
lxc-snapshot -n web1 -c "before-upgrade"
lxc-snapshot -n web1 -L
lxc-copy -n web1 -N web1-clone
lxc-start -n web1

The snapshot lives in ~/.local/share/lxcsnaps/web1/.

Common pitfalls

  • Confusing lxc (LXD client) and lxc-* (classic LXC) — they coexist on the same host and have different ~/.config/ paths. Use absolute paths or aliases in scripts to be explicit.
  • Running privileged containers in production. The default lxc-create from /etc/lxc/default.conf produces privileged containers — always specify the user-namespace idmap or create them as the unprivileged user.
  • Forgetting to write a default.conf for the unprivileged user — lxc-create for unprivileged users without one fails with a cryptic “no idmap” error.
  • Mixing lxc.network.0.link = lxcbr0 with a host that has no lxcbr0 bridge. Either create it (systemctl enable lxc-net) or use a host-bridge that does exist.
  • Trying to live-migrate a classic LXC container — checkpoint/restore via CRIU works for some workloads but is fragile compared to LXD’s tooling. For genuine migration needs, LXD is the right tool.

Stack Harbor supports clients on classic LXC where adding LXD is not an option (heavily customised hosts, kernels with patched LXC modules, legacy fleets) — see managed operations for the per-container config templates, the unprivileged-only enforcement, and the CRIU-based incident snapshotting we layer on top.