Linux Sysadmin Test Prep

Linux Containers (LXC)

Linux Containers (LXC)

Install

sudo snap install lxd --channel=4.0/stable

usermod -aG lxd <username>

newgrp lxd (Apply for group memberships immediately)

lxc (overview of all available commands and options)

lxc --help

lxc <command> --help

Initialize

sudo lxd init (Initialize LXD)

lxd init --dump (Dump info about current networking, storage pool, and profile)

lxc profile list

lxc profile show <profilename>

Network

lxc network list

lxc network show lxdbr0 (Or another <networkname>)

lxc network delete <networkname> (Don't do this now)

Storage

lxc storage list

lxc storage show <storagename>

lxc storage delete <storagename> (Don't do this now)

Images

lxc remote list (List remote servers that can be used as <imageserver>)

lxc image ls images (List local images; ls is short for list)

lxc image ls images: (List images available on the remote server named "images")

lxc image ls images: <imagename> (eg. debian amd64)

Launch

Usage: lxc launch <imageserver>:<imagename> <instancename> [-p <profilename>] [-c <key> <value>] (-c means config)

lxc launch <imageserver>:<imagename> <instancename> --vm

<imageserver> (e.g. ubuntu or images)

<imagename> (e.g. ubuntu/20.04/amd64)

<instancename> (e.g. trainer-one)

For Example:

lxc launch ubuntu:20.04 trainer-one OR lxc launch images:ubuntu/20.04/amd64 trainer-one

Instances

lxc ls (Show local instances with network info)

lxc ls --help

lxc ls --fast (Show local instances with profile info)

lxc info (So much info)

lxc info <instancename> (So much info about <instancename>)

lxc start <instancename>

lxc restart <instancename>

lxc stop <instancename>

lxc delete <instancename>

lxc alias add delete "delete -i" (Make alias for delete to always be prompted for approval when deleting)

Execute commands and login

lxc exec <instancename> -- <command>

lxc exec <instancename> -- apt-get update (Example command)

lxc exec <instancename> -- <shellname> (Login to <instancename> as root)

lxc exec <instancename> -- su --login <username> (Login to <instancename> as <username>)

lxc console <instancename> (Login to console; detach with Ctrl+a-q)

File transfer to/from instance

lxc file pull <instancename>/path/in/container path/on/host

lxc file push path/on/host <instancename>/path/in/container

Move (rename) instances

lxc mv <instancename> <new-instancename> (Rename instance; Remember to change hosts and hostname files if needed)

See further reading to migrate to another server.

Snapshots

lxc snapshot <instancename> <snapshotname> (Create snapshot)

lxc restore <snapshotname> <instancename> (Restore snapshot)

lxc delete <instancename>/<snapshotname>

Config

lxc config set <instancename> <key> <value>

lxc config edit <instancename> (Edit instance configuration)

lxc config show <instancename> -e (Show instance configuration)

Autostart

lxc config set <instancename> boot.autostart [true|false]

lxc config set <instancename> boot.autostart.priority <integer> (Highest priority starts first)

lxc config set <instancename> boot.autostart.delay <integer> (After boot, wait before starting the next instance)

lxc config get <instancename> boot.autostart

Profiles

Usage:

lxc launch <imageserver>:<imagename> <instancename> -p profile1 -p profile2

lxc launch <imageserver>:<imagename> <instancename> -c <key1> <value> -c <key2> <value>

Commands:

echo 'export EDITOR=vim' >> ~/.profile

lxc profile show default

lxc profile create <profilename>

cat <profilename>.profile | lxc profile edit <profilename>

lxc profile set <profilename> <key> <value>

lxc profile add <instancename> <profilename>

lxc profile remove <instancename> <profilename>

lxc profile edit <profilename>

Wipe and start over

printf 'config: {}\ndevices: {}' | lxc profile edit default

lxc storage delete default

lxc network delete lxdbr0

systemctl stop snap.lxd.daemon.service

rm -fr /var/lib/lxd/*

rm -fr /var/snap/lxd/common/lxd/*

systemctl start snap.lxd.daemon.service

lxd init

Further Reading:

Docs for Stable-4.0

A network isolated container in lxd.

How to make your lxd containers get ip addresses from your lan using a bridge.

Using command aliases in lxd to exec a shell.

How to move/migrate lxd vm to another host on linux.

How to backup and restore lxd containers.

How to disable firewall and nat rules on the lxd bridge.

Tutorial Forum

Edit this page on GitHub