lxc.container.conf(5)
๐ NAME ๐
lxc.container.conf(5) โ the LXC container configuration file, bestie. This is where your container gets its whole personality, and no cap, itโs giving โone file to configure them allโ โจ
โจ DESCRIPTION โจ
Okay so: LXC is the well-known and heavily tested low-level Linux container runtime, and itโs literally been in active development since 2008 and has proven itself in critical production environments world-wide. Some of its core contributors are the same people that helped to implement various well-known containerization features inside the Linux kernel. Iconic behavior, honestly ๐
LXCโs main focus is system containers. That is, containers which offer an environment as close as possible as the one youโd get from a VM but without the overhead that comes with running a separate kernel and simulating all the hardware. Slay ๐
This is achieved through a combination of kernel security features such as namespaces, mandatory access control and control groups.
LXC has support for unprivileged containers. Unprivileged containers are containers that are run without any privilege. This requires support for user namespaces in the kernel that the container is run on. LXC was the first runtime to support unprivileged containers after user namespaces were merged into the mainline kernel. Periodt.
In essence, user namespaces isolate given sets of UIDs and GIDs. This is achieved by establishing a mapping between a range of UIDs and GIDs on the host to a different (unprivileged) range of UIDs and GIDs in the container. The kernel will translate this mapping in such a way that inside the container all UIDs and GIDs appear as you would expect from the host whereas on the host these UIDs and GIDs are in fact unprivileged. For example, a process running as UID and GID 0 inside the container might appear as UID and GID 100000 on the host. The implementation and working details can be gathered from the corresponding user namespace man page. UID and GID mappings can be defined with the lxc.idmap key ๐ซถ
Linux containers are defined with a simple configuration file. Each option in the configuration file has the form key = value fitting in one line. The # character means the line is a comment. List options, like capabilities and cgroups options, can be used with no value to clear any previously defined values of that option.
LXC namespaces configuration keys use single dots. This means complex configuration keys such as lxc.net.0 expose various subkeys such as lxc.net.0.type, lxc.net.0.link, lxc.net.0.ipv6.address, and others for even more fine-grained configuration. Lowkey elegant ๐ฆ
๐ซถ CONFIGURATION ๐ซถ
In order to ease administration of multiple related containers, it is possible to have a container configuration file cause another file to be loaded. For instance, network configuration can be defined in one common file which is included by multiple containers. Then, if the containers are moved to another host, only one file may need to be updated. Obsessed with the efficiency ๐
lxc.includeโ Specify the file to be included. The included file must be in the same valid lxc configuration file format.
๐ฐ ARCHITECTURE ๐ฐ
Allows one to set the architecture for the container. For example, set a 32bits architecture for a container running 32bits binaries on a 64bits host. This fixes the container scripts which rely on the architecture to do some work like downloading the packages.
lxc.archโ Specify the architecture for the container.
Some valid options are x86, i686, x86_64, amd64.
๐ท๏ธ HOSTNAME ๐ท๏ธ
The utsname section defines the hostname to be set for the container. That means the container can set its own hostname without changing the one from the system. That makes the hostname private for the container. Main character energy only ๐
lxc.uts.nameโ specify the hostname for the container.
๐ HALT SIGNAL ๐
Allows one to specify signal name or number sent to the containerโs init process to cleanly shutdown the container. Different init systems could use different signals to perform clean shutdown sequence. This option allows the signal to be specified in kill(1) fashion, e.g. SIGPWR, SIGRTMIN+14, SIGRTMAX-10 or plain number. The default signal is SIGPWR.
lxc.signal.haltโ specify the signal used to halt the container.
๐ REBOOT SIGNAL ๐
Allows one to specify signal name or number to reboot the container. This option allows signal to be specified in kill(1) fashion, e.g. SIGTERM, SIGRTMIN+14, SIGRTMAX-10 or plain number. The default signal is SIGINT.
lxc.signal.rebootโ specify the signal used to reboot the container.
๐ STOP SIGNAL ๐
Allows one to specify signal name or number to forcibly shutdown the container. This option allows signal to be specified in kill(1) fashion, e.g. SIGKILL, SIGRTMIN+14, SIGRTMAX-10 or plain number. The default signal is SIGKILL.
lxc.signal.stopโ specify the signal used to stop the container.
๐ INIT COMMAND ๐
Sets the command to use as the init system for the containers.
lxc.execute.cmdโ Absolute path from container rootfs to the binary to run by default. This mostly makes sense forlxc-execute.lxc.init.cmdโ Absolute path from container rootfs to the binary to use as init. This mostly makes sense forlxc-start. Default is/sbin/init.
๐ INIT WORKING DIRECTORY ๐
Sets the absolute path inside the container as the working directory for the containers. LXC will switch to this directory before executing init.
lxc.init.cwdโ Absolute path inside the container to use as the working directory.
๐ INIT ID ๐
Sets the UID/GID to use for the init system, and subsequent commands.
โ ๏ธ Note that using a non-root UID when booting a system container will likely not work due to missing privileges. Setting the UID/GID is mostly useful when running application containers. Defaults to: UID(0), GID(0).
lxc.init.uidโ UID to use for init.lxc.init.gidโ GID to use for init.
๐ง CORE SCHEDULING ๐ง
Core scheduling defines if the container payload is marked as being schedulable on the same core. Doing so will cause the kernel scheduler to ensure that tasks that are not in the same group never run simultaneously on a core. This can serve as an extra security measure to prevent the container payload from using cross hyper thread attacks. Security queen behavior ๐ฅ
lxc.sched.coreโ The only allowed values are0and1. Set this to1to create a core scheduling domain for the container or0to not create one. If not set explicitly no core scheduling domain will be created for the container.
๐ฉบ PROC ๐ฉบ
Configure proc filesystem for the container.
lxc.proc.[proc file name]โ Specify the proc file name to be set. The file names available are those listed under/proc/PID/. Example:
lxc.proc.oom_score_adj = 10
๐ฆ EPHEMERAL ๐ฆ
Allows one to specify whether a container will be destroyed on shutdown.
lxc.ephemeralโ The only allowed values are0and1. Set this to1to destroy a container on shutdown.
๐ NETWORK ๐
The network section defines how the network is virtualized in the container. The network virtualization acts at layer two. In order to use the network virtualization, parameters must be specified to define the network interfaces of the container. Several virtual interfaces can be assigned and used in a container even if the system has only one physical network interface. Networking? We love her ๐
lxc.netโ may be used without a value to clear all previous network options.lxc.net.[i].typeโ specify what kind of network virtualization to be used for the container.
โ ๏ธ Must be specified before any other option(s) on the net device.
Multiple networks can be specified by using an additional index i after all lxc.net.* keys. For example, lxc.net.0.type = veth and lxc.net.1.type = veth specify two different networks of the same type. All keys sharing the same index i will be treated as belonging to the same network. For example, lxc.net.0.link = br0 will belong to lxc.net.0.type. Currently, the different virtualization types can be:
emptyโ will create only the loopback interface.vethโ a virtual ethernet pair device is created with one side assigned to the container and the other side on the host.lxc.net.[i].veth.modespecifies the mode the veth parent will use on the host. The accepted modes arebridgeandrouter. The mode defaults tobridgeif not specified. In bridge mode the host side is attached to a bridge specified by thelxc.net.[i].linkoption. If the bridge link is not specified, then the veth pair device will be created but not attached to any bridge. Otherwise, the bridge has to be created on the system before starting the container. lxc wonโt handle any configuration outside of the container. In router mode static routes are created on the host for the containerโs IP addresses pointing to the host side veth interface. Additionally Proxy ARP and Proxy NDP entries are added on the host side veth interface for the gateway IPs defined in the container to allow the container to reach the host. By default, lxc chooses a name for the network device belonging to the outside of the container, but if you wish to handle this name yourselves, you can tell lxc to set a specific name with thelxc.net.[i].veth.pairoption (except for unprivileged containers where this option is ignored for security reasons). Static routes can be added on the host pointing to the container using thelxc.net.[i].veth.ipv4.routeandlxc.net.[i].veth.ipv6.routeoptions. Several lines specify several routes. The route is in formatx.y.z.t/m, eg.192.168.1.0/24. In bridge mode untagged VLAN membership can be set with thelxc.net.[i].veth.vlan.idoption. It accepts a special value of'none'indicating that the container port should be removed from the bridgeโs default untagged VLAN. Thelxc.net.[i].veth.vlan.tagged.idoption can be specified multiple times to set the containerโs bridge port membership to one or more tagged VLANs.vlanโ a vlan interface is linked with the interface specified by thelxc.net.[i].linkand assigned to the container. The vlan identifier is specified with the optionlxc.net.[i].vlan.id.
okay so macvlan โ a macvlan interface gets linked with the interface specified by lxc.net.[i].link and then assigned to the container. lxc.net.[i].macvlan.mode sets the mode macvlan uses to talk between different macvlan interfaces on the same upper device. The accepted modes are private, vepa, bridge and passthru. In private mode, the device never communicates with any other device on the same upper_dev (that's the default). In vepa mode โ the new Virtual Ethernet Port Aggregator (VEPA) mode โ it assumes the adjacent bridge returns all frames where both source and destination are local to the macvlan port, i.e. the bridge is set up as a reflective relay. Broadcast frames coming in from the upper_dev get flooded to all macvlan interfaces in VEPA mode; local frames are not delivered locally. In bridge mode, it gives you the behavior of a simple bridge between different macvlan interfaces on the same port. Frames from one interface to another get delivered directly and are not sent out externally. Broadcast frames get flooded to all other bridge ports and to the external interface, but when they come back from a reflective relay, we don't deliver them again. Since we know all the MAC addresses, macvlan bridge mode does not require learning or STP like the bridge module does. In passthru mode, all frames received by the physical interface are forwarded to the macvlan interface. Only one macvlan interface in passthru mode is possible for one physical interface. ๐
ipvlan โ an ipvlan interface gets linked with the interface specified by lxc.net.[i].link and assigned to the container. lxc.net.[i].ipvlan.mode sets the mode ipvlan uses to communicate between different ipvlan interfaces on the same upper device. The accepted modes are l3, l3s and l2. It defaults to l3. In l3 mode, TX processing up to L3 happens on the stack instance attached to the dependent device, and packets are switched to the stack instance of the parent device for L2 processing; routing from that instance will be used before packets are queued on the outbound device. In this mode the dependent devices will not receive nor can send multicast / broadcast traffic. In l3s mode, TX processing is very similar to L3 mode except that iptables (conn-tracking) works in this mode and hence it is L3-symmetric (L3s). This will have slightly less performance, but that shouldn't matter since you're choosing this mode over plain-L3 mode to make conn-tracking work. In l2 mode, TX processing happens on the stack instance attached to the dependent device and packets are switched and queued to the parent device to send devices out. In this mode the dependent devices will RX/TX multicast and broadcast (if applicable) as well. lxc.net.[i].ipvlan.isolation sets the isolation mode. The accepted isolation values are bridge, private and vepa. It defaults to bridge. In bridge isolation mode, dependent devices can cross-talk among themselves apart from talking through the parent device. In private isolation mode, the port is set in private mode โ i.e. the port won't allow cross communication between dependent devices. In vepa isolation mode, the port is set in VEPA mode โ i.e. the port will offload switching functionality to the external entity as described in 802.1Qbg. ๐ฆ
phys โ an already existing interface specified by lxc.net.[i].link is assigned to the container. โจ
lxc.net.[i].flagsโ specify an action to do for the network. Right now the action is:upโ activates the interface. ๐ฅ
lxc.net.[i].linkโ specify the interface to be used for real network traffic. ๐ซถlxc.net.[i].l2proxyโ controls whether layer 2 IP neighbour proxy entries get added to thelxc.net.[i].linkinterface for the IP addresses of the container. Can be set to0or1. Defaults to0. When used with IPv4 addresses, these sysctl values need to be set:net.ipv4.conf.[link].forwarding=1. When used with IPv6 addresses, these sysctl values need to be set:net.ipv6.conf.[link].proxy_ndp=1andnet.ipv6.conf.[link].forwarding=1. ๐lxc.net.[i].mtuโ specify the maximum transfer unit for this interface. ๐ธlxc.net.[i].nameโ the interface name is dynamically allocated, but if another name is needed because the configuration files being used by the container use a generic name, eg.eth0, this option will rename the interface in the container. ๐lxc.net.[i].hwaddrโ the interface mac address is dynamically allocated by default to the virtual interface, but sometimes this is needed to resolve a mac address conflict or to always have the same link-local ipv6 address. Any"x"in the address will be replaced by a random value; this allows setting hwaddr templates. ๐lxc.net.[i].ipv4.addressโ specify the ipv4 address to assign to the virtualized interface. Several lines specify several ipv4 addresses. The address is in formatx.y.z.t/m, eg.192.168.1.123/24. You can optionally specify the broadcast address after the IP address, e.g.192.168.1.123/24 255.255.255.255. Otherwise it is automatically calculated from the IP address. ๐lxc.net.[i].ipv4.gatewayโ specify the ipv4 address to use as the gateway inside the container. The address is in formatx.y.z.t, eg.192.168.1.123. Can also have the special valueauto, which means to take the primary address from the bridge interface (as specified by thelxc.net.[i].linkoption) and use that as the gateway.autois only available when using theveth,macvlanandipvlannetwork types. Can also have the special value ofdev, which means to set the default gateway as a device route. This is primarily for use with layer 3 network modes, such as IPVLAN. โญlxc.net.[i].ipv6.addressโ specify the ipv6 address to assign to the virtualized interface. Several lines specify several ipv6 addresses. The address is in formatx::y/m, eg.2003:db8:1:0:214:1234:fe0b:3596/64. ๐งlxc.net.[i].ipv6.gatewayโ specify the ipv6 address to use as the gateway inside the container. The address is in formatx::y, eg.2003:db8:1:0::1. Can also have the special valueauto, which means to take the primary address from the bridge interface (as specified by thelxc.net.[i].linkoption) and use that as the gateway.autois only available when using theveth,macvlanandipvlannetwork types. Can also have the special value ofdev, which means to set the default gateway as a device route. This is primarily for use with layer 3 network modes, such as IPVLAN. ๐lxc.net.[i].script.upโ add a configuration option to specify a script to be executed after creating and configuring the network used from the host side. ๐ซถ
In addition to the information available to all hooks, the following information is provided to the script:
LXC_HOOK_TYPE: the hook type. This is either'up'or'down'.LXC_HOOK_SECTION: the section type'net'.LXC_NET_TYPE: the network type. This is one of the valid network types listed here (e.g.'vlan','macvlan','ipvlan','veth').LXC_NET_PARENT: the parent device on the host. This is only set for network types'mavclan','veth','phys'.LXC_NET_PEER: the name of the peer device on the host. This is only set for'veth'network types. Note that this information is only available whenlxc.hook.versionis set to1.
Whether this information is provided in the form of environment variables or as arguments to the script depends on the value of lxc.hook.version. If set to 1 then information is provided in the form of environment variables. If set to 0 information is provided as arguments to the script.
Standard output from the script is logged at debug level. Standard error is not logged, but can be captured by the hook redirecting its standard error to standard output. ๐
lxc.net.[i].script.downโ add a configuration option to specify a script to be executed before destroying the network used from the host side. ๐
In addition to the information available to all hooks, the following information is provided to the script:
LXC_HOOK_TYPE: the hook type. This is either'up'or'down'.LXC_HOOK_SECTION: the section type'net'.LXC_NET_TYPE: the network type. This is one of the valid network types listed here (e.g.'vlan','macvlan','ipvlan','veth').LXC_NET_PARENT: the parent device on the host. This is only set for network types'mavclan','veth','phys'.LXC_NET_PEER: the name of the peer device on the host. This is only set for'veth'network types. Note that this information is only available whenlxc.hook.versionis set to1.
Whether this information is provided in the form of environment variables or as arguments to the script depends on the value of lxc.hook.version. If set to 1 then information is provided in the form of environment variables. If set to 0 information is provided as arguments to the script.
Standard output from the script is logged at debug level. Standard error is not logged, but can be captured by the hook redirecting its standard error to standard output. ๐ธ
๐ NEW PSEUDO TTY INSTANCE (DEVPTS) ๐
For stricter isolation the container can have its own private instance of the pseudo tty. ๐ง
lxc.pty.maxโ if set, the container will have a new pseudo tty instance, making this private to it. The value specifies the maximum number of pseudo ttys allowed for a pty instance (this limitation is not implemented yet). ๐
๐ CONTAINER SYSTEM CONSOLE ๐
If the container is configured with a root filesystem and the inittab file is setup to use the console, you may want to specify where the output of this console goes. ๐
lxc.console.buffer.sizeโ okay so this one tells liblxc to set up an in-memory ringbuffer, and the container's console output gets written into it. Heads up: the ringbuffer has to be at least as big as a standard page size โ and if you pass something smaller than a single page size, liblxc is just gonna allocate one page size for you anyway. A page size is usually 4KB. The keyword'auto'makes liblxc roll out a 128KB ringbuffer. When you're manually setting a size, the value should be a power of 2 once it's converted to bytes. Valid size prefixes are'KB','MB','GB'. (And fr fr, all conversions are based on multiples of 1024 โ so'KB'=='KiB','MB'=='MiB','GB'=='GiB'. Also the case of the suffix is ignored, i.e.'kB','KB'and'Kb'are all treated equally ๐ .)lxc.console.sizeโ this puts a limit on the size of the console log file you set inlxc.console.logfile. Same rules bestie: the log file size must be at least as big as a standard page size, and if you pass something smaller than a single page size, liblxc sets the log file size to a single page size. A page size is usually 4KB. The keyword'auto'makes liblxc put a 128KB limit on the log file. When you're manually setting the size, the value should be a power of 2 when converted to bytes. Valid size prefixes are'KB','MB','GB'. (All conversions are based on multiples of 1024 โ so'KB'=='KiB','MB'=='MiB','GB'=='GiB'. And again, suffix case is ignored:'kB','KB'and'Kb'are treated equally.) If you wanna mirror the console ringbuffer on disk, setlxc.console.sizeequal tolxc.console.buffer.sizeโจ.lxc.console.logfileโ give it a path to a file and the console output gets written there. But listen: in contrast to the on-disk ringbuffer logfile, this file will keep growing and can literally fill up the user's disks if it's not rotated and deleted ๐ญ. You can dodge that whole situation by using the in-memory ringbuffer optionslxc.console.buffer.sizeandlxc.console.buffer.logfileinstead.lxc.console.rotateโ whether to rotate the console logfile specified inlxc.console.logfile. Users can send an API request to rotate the logfile. Note the old logfile will have the same name as the original with the suffix".1"appended. If you don't want the console log file filling the disk, rotate the logfile and delete it if it's not needed. This problem can also be avoided by using the in-memory ringbuffer optionslxc.console.buffer.sizeandlxc.console.buffer.logfile๐ซถ.lxc.console.pathโ path to a device that the console gets attached to. The keyword'none'simply disables the console. But ๐ note: when you specify'none'and create a device node for the console in the container at/dev/console, or bind-mount the host's/dev/consoleinto the container at/dev/console, the container will have direct access to the host's/dev/console. That is dangerous when the container has write access to the device, so use it with caution, okay? ๐
๐ CONSOLE THROUGH THE TTYS ๐
This option is useful if the container is configured with a root filesystem and the inittab file is set up to launch a getty on the ttys. The option specifies the number of ttys to be available for the container. And the number of gettys in the inittab file of the container should not be greater than the number of ttys specified in this option โ otherwise the excess getty sessions will die and respawn indefinitely, giving the most annoying messages on the console or in /var/log/messages ๐.
lxc.tty.maxโ specify the number of tty to make available to the container.
๐ธ CONSOLE DEVICES LOCATION ๐ธ
LXC consoles are provided through Unix98 PTYs created on the host and bind-mounted over the expected devices in the container. By default they're bind-mounted over /dev/console and /dev/ttyN. The thing is, this can prevent package upgrades in the guest ๐ฌ. So you can specify a directory location under /dev under which LXC will create the files and bind-mount over them. Those will then be symbolically linked to /dev/console and /dev/ttyN. And then a package upgrade can succeed, because it's able to remove and replace the symbolic links. Slay ๐
.
lxc.tty.dirโ specify a directory under/devunder which to create the container console devices. Note that LXC will move any bind-mounts or device nodes for/dev/consoleinto this directory.
๐ฆ /DEV DIRECTORY ๐ฆ
By default, lxc creates a few symbolic links (fd,stdin,stdout,stderr) in the container's /dev directory but does not automatically create device node entries. That's on purpose โ it lets the container's /dev be set up as needed in the container rootfs. If lxc.autodev is set to 1, then after mounting the container's rootfs LXC will mount a fresh tmpfs under /dev (limited to 500K by default, unless defined in lxc.autodev.tmpfs.size) and fill in a minimal set of initial devices. This is generally required when starting a container containing a "systemd" based "init" but may be optional at other times. Additional devices in the container's /dev directory may be created through the use of the lxc.hook.autodev hook โจ.
lxc.autodevโ set this to0to stop LXC from mounting and populating a minimal/devwhen starting the container.lxc.autodev.tmpfs.sizeโ set this to define the size of the/devtmpfs. The default value is500000(500K). If the parameter is used but without a value, the default value is used.
๐ MOUNT POINTS ๐
The mount points section specifies the different places to be mounted. These mount points will be private to the container and won't be visible by the processes running outside of the container. It's genuinely so useful for mounting /etc, /var or /home, for example.
โ ๏ธ NOTE โ LXC will generally ensure that mount targets and relative bind-mount sources are properly confined under the container root, to avoid attacks involving over-mounting host directories and files. (Symbolic links in absolute mount sources are ignored.) However, if the container configuration first mounts a directory which is under the control of the container user, such as /home/joe, into the container at some path, and then mounts something under that path, then a TOCTTOU attack would be possible where the container user modifies a symbolic link under their home directory at just the right time ๐ฑ. Not a vibe. Be careful.
lxc.mount.fstabโ specify a file location in the fstab format, containing the mount information. The mount target location can and in most cases should be a relative path, which will become relative to the mounted container root. For instance:
proc proc proc nodev,noexec,nosuid 0 0
will mount a proc filesystem under the container's /proc, regardless of where the root filesystem comes from. This is resilient to block device backed filesystems as well as container cloning ๐.
Note that when mounting a filesystem from an image file or block device, the third field (fs_vfstype) cannot be auto as with mount(8) โ it must be explicitly specified.
lxc.mount.entryโ specify a mount point corresponding to a line in the fstab format. On top of that, lxc supports mount propagation, such asrsharedorrprivate, and adds three additional mount options.optionalโ don't fail if the mount does not work.create=dirorcreate=fileโ to create dir (or file) when the point will be mounted.relativeโ the source path is taken to be relative to the mounted container root. For instance:
dev/null proc/kcore none bind,relative 0 0
will expand dev/null to ${LXC_ROOTFS_MOUNT}/dev/null, and mount it to proc/kcore inside the container ๐ง.
lxc.mount.autoโ specify which standard kernel file systems should be automatically mounted. This may dramatically simplify the configuration. The file systems are:proc:mixed(orproc): mount/procas read-write, but remount/proc/sysand/proc/sysrq-triggerread-only for security / container isolation purposes.proc:rw: mount/procas read-writesys:mixed(orsys): mount/sysas read-only but with/sys/devices/virtual/netwritable.sys:ro: mount/sysas read-only for security / container isolation purposes.sys:rw: mount/sysas read-writecgroup:mixed: mount a tmpfs to/sys/fs/cgroup, create directories for all hierarchies to which the container is added, create subdirectories in those hierarchies with the name of the cgroup, and bind-mount the container's own cgroup into that directory. The container will be able to write to its own cgroup directory, but not the parents, since they will be remounted read-only.cgroup:mixed:force: the force option will cause LXC to perform the cgroup mounts for the container under all circumstances. Otherwise it's similar tocgroup:mixed. This is mainly useful when the cgroup namespaces are enabled, where LXC will normally leave mounting cgroups to the init binary of the container since it's perfectly safe to do so.cgroup:ro: similar tocgroup:mixed, but everything will be mounted read-only.cgroup:ro:force: the force option will cause LXC to perform the cgroup mounts for the container under all circumstances. Otherwise it's similar tocgroup:ro. This is mainly useful when the cgroup namespaces are enabled, where LXC will normally leave mounting cgroups to the init binary of the container since it's perfectly safe to do so.cgroup:rw: similar tocgroup:mixed, but everything will be mounted read-write. Note that the paths leading up to the container's own cgroup will be writable, but will not be a cgroup filesystem โ just part of the tmpfs of/sys/fs/cgroup๐.
cgroup:rw:forceโ theforceoption makes LXC do the cgroup mounts for the container under all circumstances. Otherwise it's basicallycgroup:rw. This one's mainly useful when cgroup namespaces are enabled, because there LXC would normally leave mounting cgroups up to the container's init binary โ since it's perfectly safe to do so.cgroup(no specifier at all) โ defaults tocgroup:rwif the container retains theCAP_SYS_ADMINcapability, andcgroup:mixedotherwise.cgroup-full:mixedโ mounts a tmpfs to/sys/fs/cgroup, creates directories for every hierarchy the container is added to, bind-mounts the hierarchies from the host into the container, and makes everything read-only except the container's own cgroup. Heads up: compared tocgroup, where all the paths leading up to the container's own cgroup are just plain simple directories in the underlying tmpfs, here/sys/fs/cgroup/$hierarchycontains the host's full cgroup hierarchy โ read-only outside the container's own cgroup, but still. This may leak quite a bit of information into the container. ๐ซฃcgroup-full:mixed:forceโ theforceoption makes LXC do the cgroup mounts for the container under all circumstances. Otherwise it's similar tocgroup-full:mixed. Mainly useful when cgroup namespaces are enabled, where LXC normally leaves mounting cgroups to the container's init binary since that's perfectly safe.cgroup-full:roโ similar tocgroup-full:mixed, but everything gets mounted read-only. ๐cgroup-full:ro:forceโ theforceoption makes LXC do the cgroup mounts for the container under all circumstances. Otherwise it's similar tocgroup-full:ro. Mainly useful when cgroup namespaces are enabled, where LXC would normally leave mounting cgroups to the container's init binary since it's perfectly safe to do so.cgroup-full:rwโ similar tocgroup-full:mixed, but everything gets mounted read-write. โ ๏ธ Note that in this case, the container may escape its own cgroup. (Also note: if the container hasCAP_SYS_ADMINsupport and can mount the cgroup filesystem itself, it may do so anyway.)cgroup-full:rw:forceโ theforceoption makes LXC do the cgroup mounts for the container under all circumstances. Otherwise it's similar tocgroup-full:rw. Mainly useful when cgroup namespaces are enabled, where LXC would normally leave the mounting to the container's init binary since it's perfectly safe to do so.cgroup-full(no specifier) โ defaults tocgroup-full:rwif the container retains theCAP_SYS_ADMINcapability, andcgroup-full:mixedotherwise.
If cgroup namespaces are enabled, then any cgroup auto-mounting request gets ignored, because the container can mount the filesystems itself, and automounting can genuinely confuse the container init. ๐
Note that if automatic mounting of the cgroup filesystem is enabled, the tmpfs under/sys/fs/cgroupwill always be mounted read-write (but for the:mixedand:rocases, the individual hierarchies,/sys/fs/cgroup/$hierarchy, will be read-only). This exists to work around a quirk in Ubuntu'smountall(8)command that makes containers wait for user input at boot if/sys/fs/cgroupis mounted read-only and the container can't remount it read-write due to a lack ofCAP_SYS_ADMIN. ๐ญ
Examples, bestie:
lxc.mount.auto = proc sys cgroup
lxc.mount.auto = proc:rw sys:rw cgroup-full:rw
๐ ROOT FILE SYSTEM ๐
The root file system of the container can be different from the host system's. Iconic, honestly. โจ
lxc.rootfs.path
Specifies the root file system for the container. It can be an image file, a directory, or a block device. If you don't specify it, the container just shares its root file system with the host.
For directory-backed or simple block-device-backed containers, a pathname works fine. If the rootfs is backed by an nbd device, then nbd:file:1 says attach file to an nbd device, and mount partition 1 as the rootfs. nbd:file says the nbd device itself should be mounted. overlayfs:/lower:/upper says the rootfs should be an overlay with /upper mounted read-write over a read-only mount of /lower โ and for overlay you can specify multiple /lower directories. loop:/file tells lxc to attach /file to a loop device and mount the loop device. ๐
lxc.rootfs.mount
Where to recursively bind lxc.rootfs.path before pivoting. This is to make sure the pivot_root(8) syscall succeeds. Any directory suffices, and the default should generally work. ๐ซถ
lxc.rootfs.options
Extra mount options to use when mounting the rootfs. The format corresponds to the format used in fstab. On top of that, LXC supports the custom idmap= mount option, which tells LXC to create an idmapped mount for the container's rootfs. Super useful when you don't wanna recursively chown the rootfs of the container to match the idmapping of the user namespace the container is going to use โ instead an idmapped mount handles it for you. ๐
The argument for idmap= can either be a path pointing to a user namespace file that LXC will open and use to idmap the rootfs, or the special value "container", which tells LXC to use the container's user namespace to idmap the rootfs.
lxc.rootfs.managed
Set this to 0 to say that LXC is not managing the container storage โ then LXC will not modify the container storage. Default is 1. ๐
๐๏ธ CONTROL GROUPS ("CGROUPS") ๐๏ธ
The control group section holds the configuration for the different subsystems. lxc does not check whether the subsystem name is correct. Downside: configuration errors don't get caught until the container is started. Upside: any future subsystem is permitted. Balance, bestie. โ๏ธ
The kernel implementation of cgroups has changed significantly over the years. With Linux 4.5, support for a new cgroup filesystem landed โ usually called "cgroup2" or the "unified hierarchy". Since then, the old cgroup filesystem is usually called "cgroup1" or the "legacy hierarchies". Peep the cgroups manual page for a detailed explanation of the differences between the two versions. ๐
LXC tells settings for the legacy hierarchy and the unified hierarchy apart by using different configuration key prefixes. To alter settings for controllers in a legacy hierarchy you use the lxc.cgroup. key prefix, and to alter settings for a controller in the unified hierarchy you use the lxc.cgroup2. key. Note that LXC will ignore lxc.cgroup. settings on systems that only use the unified hierarchy. Conversely, it ignores lxc.cgroup2. options on systems that only use legacy hierarchies. lxc.cgroup. (legacy and hybrid hierarchy) support is dropped. ๐๏ธ
At its core, a cgroup hierarchy is a way to hierarchically organize processes. Usually a cgroup hierarchy has one or more "controllers" enabled. A "controller" in a cgroup hierarchy is usually responsible for distributing a specific type of system resource along the hierarchy. Controllers include the "pids" controller, the "cpu" controller, the "memory" controller, and others. Some controllers don't fit that "distributing a system resource" category though โ those get called "utility" controllers. One utility controller is the device controller: instead of distributing a system resource, it lets you manage device access. ๐ฎ
In the legacy hierarchy the device controller was implemented like most other controllers โ as a set of files you could write to. Those files were named devices.allow and devices.deny. The legacy device controller allowed both "allowlists" and "denylists" to be implemented.
An allowlist is a device program that by default blocks access to all devices. To access specific devices, "allow rules" for particular devices or device classes must be specified. In contrast, a denylist is a device program that by default allows access to all devices. To restrict access to specific devices, "deny rules" for particular devices or device classes must be specified. ๐ฆ
In the unified cgroup hierarchy, the implementation of the device controller has completely changed. Instead of files to read from and write to, an eBPF program of BPF_PROG_TYPE_CGROUP_DEVICE can be attached to a cgroup. Even though the kernel implementation changed entirely, LXC tries to let you follow the same semantics in the legacy device cgroup and the unified eBPF-based device controller. The next few paragraphs explain the semantics for the unified eBPF-based device controller. ๐ง
As mentioned, the format for specifying device rules for the unified eBPF-based device controller is the same as for the legacy cgroup device controller โ only the configuration key prefix changed. Specifically, device rules for the legacy cgroup device controller are specified via lxc.cgroup.devices.allow and lxc.cgroup.devices.deny, whereas for the cgroup2 eBPF-based device controller you must use lxc.cgroup2.devices.allow and lxc.cgroup2.devices.deny.
- A denylist device rule
lxc.cgroup2.devices.deny = a
will make LXC instruct the kernel to block access to all devices by default. To grant access to devices, allow device rules must be added via the lxc.cgroup2.devices.allow key. This is referred to as an "allowlist" device program.
- An allowlist device rule
lxc.cgroup2.devices.allow = a
will make LXC instruct the kernel to allow access to all devices by default. To deny access to devices, deny device rules must be added via the lxc.cgroup2.devices.deny key. This is referred to as a "denylist" device program.
- Specifying either of the two rules above will cause all previous rules to be cleared โ i.e. the device list gets reset. ๐ฅ
- When an allowlist program is requested โ i.e. access to all devices is blocked by default โ specific deny rules for individual devices or device classes are ignored.
- When a denylist program is requested (like, access to all devices is allowed by default), specific allow rules for individual devices or device classes just get ignored. Rude, but that's the rules ๐ซ
So check it โ this set of rules:
lxc.cgroup2.devices.deny = a
lxc.cgroup2.devices.allow = c *:* m
lxc.cgroup2.devices.allow = b *:* m
lxc.cgroup2.devices.allow = c 1:3 rwm
implements an allowlist device program, meaning the kernel will block access to every single device that isn't specifically allowed in this list. And this particular program is saying "all character and block devices may be created, but only /dev/null might be read or written." That's it. That's the whole vibe โจ
Now if we instead flip to these rules:
lxc.cgroup2.devices.allow = a
lxc.cgroup2.devices.deny = c *:* m
lxc.cgroup2.devices.deny = b *:* m
lxc.cgroup2.devices.deny = c 1:3 rwm
then LXC tells the kernel to implement a denylist, i.e. the kernel allows access to everything that isn't specifically denied here. This program says no character devices or block devices may be created, and /dev/null is not allowed to be read, written, or created. RIP /dev/null fr ๐ญ
Now consider that same program but followed by a "global rule" that determines the type of device program (allowlist or denylist), as explained above:
lxc.cgroup2.devices.allow = a
lxc.cgroup2.devices.deny = c *:* m
lxc.cgroup2.devices.deny = b *:* m
lxc.cgroup2.devices.deny = c 1:3 rwm
lxc.cgroup2.devices.allow = a
That last line makes LXC reset the device list without changing the type of device program. Same program type, fresh list ๐
But if we specify:
lxc.cgroup2.devices.allow = a
lxc.cgroup2.devices.deny = c *:* m
lxc.cgroup2.devices.deny = b *:* m
lxc.cgroup2.devices.deny = c 1:3 rwm
lxc.cgroup2.devices.deny = a
instead, then that last line makes LXC reset the device list and switch from an allowlist program to a denylist program. Glow-up AND a plot twist, iconic ๐
๐ธ lxc.cgroup.[controller name].[controller file] ๐ธ
Sets the control group value on a legacy cgroup hierarchy. The controller name is the literal name of the control group, and the permitted names plus the syntax of their values are not dictated by LXC โ that's entirely up to the features of the Linux kernel running at the time the container is started. Example: lxc.cgroup.cpuset.cpus ๐ฆ
๐ธ lxc.cgroup2.[controller name].[controller file] ๐ธ
Same deal but for the unified cgroup hierarchy. The controller name is the literal name of the control group, and again the permitted names and value syntax are not dictated by LXC โ it all depends on the features of the Linux kernel running when the container starts. Example: lxc.cgroup2.memory.high ๐ฆ
๐ lxc.cgroup.dir ๐
Specifies a directory or path where the container's cgroup gets created. So like, setting lxc.cgroup.dir = my-cgroup/first for a container named "c1" creates the container's cgroup as a sub-cgroup of "my-cgroup". Concretely: if the user's current cgroup "my-user" lives in the root cgroup of the cpuset controller on a cgroup v1 hierarchy, this would create the cgroup "/sys/fs/cgroup/cpuset/my-user/my-cgroup/first/c1" for the container. Any missing cgroups get created by LXC automatically. This all presupposes the user has write access to its current cgroup, obviously ๐
๐ lxc.cgroup.dir.container ๐
Similar to lxc.cgroup.dir, but you must use it together with lxc.cgroup.dir.monitor, and it only affects the container's cgroup path. This option is mutually exclusive with lxc.cgroup.dir โ pick one, bestie, no cap. Note that the final path the container attaches to may get extended even further by the lxc.cgroup.dir.container.inner option ๐
๐ lxc.cgroup.dir.monitor ๐
This is the monitor process counterpart to lxc.cgroup.dir.container. Matching set, obvs ๐ซถ
๐ lxc.cgroup.dir.monitor.pivot ๐
On container termination, the PID of the monitor process gets attached to this cgroup. This path should not be a subpath of any other configured cgroup dir, so that other cgroup paths are properly removed when the container terminates. Keep it separate, okay? ๐
๐ lxc.cgroup.dir.container.inner ๐
Specifies an additional subdirectory where the cgroup namespace gets created. With this option, the cgroup limits are applied to the outer path specified in lxc.cgroup.dir.container, which isn't accessible from inside the container โ which means you can better enforce limits for privileged containers in a way they literally cannot override. Main character energy for the admin, honestly ๐
This only works in conjunction with the lxc.cgroup.dir.container and lxc.cgroup.dir.monitor options. On its own it has no effect whatsoever. Sorry babes ๐
๐ lxc.cgroup.relative ๐
Set this to 1 to tell LXC to never escape to the root cgroup. Super handy for adhering to restrictions enforced by cgroup2 and systemd โ specifically, it makes it possible to run LXC containers as systemd services. Slay โจ
๐ฆ CAPABILITIES ๐ฆ
The capabilities can be dropped in the container if it's being run as root. ๐ค
๐ lxc.cap.drop ๐
Specifies the capability to be dropped in the container. You can put several capabilities on a single line, separated by spaces. The format is the lowercase version of the capability definition without the "CAP_" prefix โ so CAP_SYS_MODULE should be written as sys_module. See capabilities(7).
If used with no value, lxc will clear any drop capabilities specified up to this point. Reset button, basically ๐ซ
๐ lxc.cap.keep ๐
Specifies the capability to be kept in the container โ and every other capability gets dropped. When the special value "none" shows up, lxc clears any keep capabilities specified up to this point. And a value of "none" on its own can be used to drop all capabilities. All of them. Gone. Periodt ๐
๐ NAMESPACES ๐
A namespace can be cloned (lxc.namespace.clone), kept (lxc.namespace.keep) or shared (lxc.namespace.share.[namespace identifier]). Three flavors, pick your fighter ๐ง
โญ lxc.namespace.clone โญ
Specifies which namespaces the container is supposed to be created with. They're given as a space-separated list, and each namespace must correspond to one of the standard namespace identifiers as seen in the /proc/PID/ns directory. When lxc.namespace.clone isn't explicitly set, all namespaces supported by the kernel and the current configuration get used ๐ซ
To create a new mount, net and ipc namespace, set lxc.namespace.clone=mount net ipc.
โญ lxc.namespace.keep โญ
Specifies which namespaces the container is supposed to inherit from the process that created it. Again, a space-separated list, and each namespace must correspond to one of the standard namespace identifiers as seen in the /proc/PID/ns directory. lxc.namespace.keep is a denylist option โ so it's useful when you're enforcing that containers must keep a specific set of namespaces ๐ซถ
To keep the network, user and ipc namespace, set lxc.namespace.keep=user net ipc.
Note that sharing pid namespaces will likely not work with most init systems. ๐ฌ
Note that if the container requests a new user namespace and the container wants to inherit the network namespace, it needs to inherit the user namespace as well.
โญ lxc.namespace.share.[namespace identifier] โญ
Specifies a namespace to inherit from another container or process. The [namespace identifier] suffix needs to be replaced with one of the namespaces that appear in the /proc/PID/ns directory.
To inherit the namespace from another process, set lxc.namespace.share.[namespace identifier] to the PID of the process, e.g. lxc.namespace.share.net=42.
To inherit the namespace from another container, set lxc.namespace.share.[namespace identifier] to the name of the container, e.g. lxc.namespace.share.pid=c3.
To inherit the namespace from another container located in a different path than the standard liblxc path, set lxc.namespace.share.[namespace identifier] to the full path to the container, e.g. lxc.namespace.share.user=/opt/c3.
In order to inherit namespaces, the caller needs to have sufficient privilege over the process or container. No sneaking in ๐
Note that sharing pid namespaces between system containers will likely not work with most init systems.
Note that if two processes are in different user namespaces and one process wants to inherit the other's network namespace, it usually needs to inherit the user namespace as well.
Note that without careful additional configuration of an LSM, sharing user+pid namespaces with a task may allow that task to escalate privileges to that of the task calling liblxc. Bestie, that one's a security thing โ read it twice ๐
๐ฆ lxc.time.offset.boot ๐ฆ
Specifies a positive or negative offset for the boottime clock. The format accepts hours (h), minutes (m), seconds (s), milliseconds (ms), microseconds (us), and nanoseconds (ns). Timezone wizardry, we love to see it โจ
๐ฆ lxc.time.offset.monotonic ๐ฆ
Specifies a positive or negative offset for the monotonic clock. Same deal โ the format accepts hours (h), minutes (m), seconds (s), milliseconds (ms), microseconds (us), and nanoseconds (ns) ๐ธ
๐ RESOURCE LIMITS ๐
The soft and hard resource limits for the container can be changed. Unprivileged containers can only lower them. And resources that aren't explicitly specified get inherited. No takebacks, no upgrades, sorry ๐
๐ lxc.prlimit.[limit name] ๐
Specifies the resource limit to be set. A limit is given as two colon-separated values that are either numeric or the word 'unlimited'. You can also use a single value as a shortcut to set both the soft and hard limit to the same thing ๐
The permitted names are the "RLIMIT_" resource names in lowercase without the "RLIMIT_" prefix โ so RLIMIT_NOFILE should be written as "nofile". See setrlimit(2).
If used with no value, lxc will clear the resource limit specified up to this point. And a resource with no explicitly configured limitation gets inherited from the process that's starting up the container. Family traits, iykyk ๐งฌ
๐ SYSCTL ๐
Configure kernel parameters for the container. Tiny section, huge power ๐ซถ
lxc.sysctl.[kernel parameters name]โ sets kernel parameters, bestie. The parameters you can use are literally the ones listed under/proc/sys/. โ ๏ธ Important vibe check: not all sysctls are namespaced, so changing a non-namespaced one will modify the system-wide setting, no cap. Seesysctl(8). And if you use this with no value, lxc will clear all the parameters you'd specified up to that point โ it's giving "undo my whole life" energy. ๐
โจ APPARMOR PROFILE โจ
If lxc was compiled and installed with apparmor support, and the host system has apparmor enabled, then you can specify in the container config which apparmor profile the container should run under. ๐ The default is lxc-container-default-cgns if the host kernel is cgroup namespace aware, or lxc-container-default otherwise. Slay.
lxc.apparmor.profileโ specifies the apparmor profile under which the container should run. To say the container should be unconfined, use:
lxc.apparmor.profile = unconfined
If the apparmor profile should stay unchanged (i.e. you're nesting containers and are already confined โ hi, nested queen ๐), then use:
lxc.apparmor.profile = unchanged
And if you want to instruct LXC to generate the apparmor profile, then use:
lxc.apparmor.profile = generated
lxc.apparmor.allow_incompleteโ apparmor profiles are pathname based, periodt. Which means lots of file restrictions need mount restrictions to actually be effective against a determined attacker. But those mount restrictions are not yet implemented in the upstream kernel, so ngl the profiles are working with one hand tied behind their back. Even without them, the apparmor profiles still protect against accidental damage. ๐ซถ
If this flag is 0 (the default), then the container will not start if the kernel lacks the apparmor mount features โ that way a regression after a kernel upgrade gets detected instead of sneaking by. If you want to start the container under partial apparmor protection anyway, set this flag to 1.
lxc.apparmor.allow_nestingโ set this to1and here's what changes: when generated apparmor profiles are used, they'll contain all the necessary changes to allow creating a nested container. And in addition to the usual mount points,/dev/.lxc/procand/dev/.lxc/syswill contain procfs and sysfs mount points without the lxcfs overlays โ which, if generated apparmor profiles are being used, will not be read/writable directly. iykyk. ๐lxc.apparmor.rawโ a list of raw AppArmor profile lines to append to the profile. Only valid when using generated profiles, so don't come at it with a hand-written one. ๐
โจ SELINUX CONTEXT โจ
If lxc was compiled and installed with SELinux support, and the host system has SELinux enabled, then you can specify in the container config which SELinux context the container should run under. The default is unconfined_t, which means lxc will not attempt to change contexts at all. See /usr/share/lxc/selinux/lxc.te for an example policy and more info. ๐ธ
lxc.selinux.contextโ specifies the SELinux context the container should run under, orunconfined_t. For example:
lxc.selinux.context = system_u:system_r:lxc_t:s0:c22
lxc.selinux.context.keyringโ specifies the SELinux context under which the container's keyring should be created. By default this is the same aslxc.selinux.context, or the context lxc is executed under iflxc.selinux.contexthasn't been set. ๐
lxc.selinux.context.keyring = system_u:system_r:lxc_t:s0:c22
โจ KERNEL KEYRING โจ
The Linux Keyring facility is primarily a way for various kernel components to retain or cache security data, authentication keys, encryption keys, and other data in the kernel. By default lxc will create a new session keyring for the started application. ๐
lxc.keyring.sessionโ disables the creation of a new session keyring by lxc. The started application will then inherit the current session keyring. By default, or when passing the value1, a new keyring will be created.
lxc.keyring.session = 0
โจ SECCOMP CONFIGURATION โจ
A container can be started with a reduced set of available system calls by loading a seccomp profile at startup. The seccomp configuration file must begin with a version number on the first line, a policy type on the second line, followed by the configuration. No skipping lines, bestie. ๐
Versions 1 and 2 are currently supported. In version 1, the policy is a simple allowlist โ so the second line therefore must read allowlist, with the rest of the file containing one (numeric) syscall number per line. Each syscall number is allowlisted, while every unlisted number is denylisted for use in the container. Simple, iconic, done. โจ
In version 2, the policy may be denylist or allowlist, it supports per-rule and per-policy default actions, and it supports per-architecture system call resolution from textual names. Way more main character energy. ๐
An example denylist policy, in which all system calls are allowed except for mknod, which will simply do nothing and return 0 (success), looks like:
2
denylist
mknod errno 0
ioctl notify
Specifying errno as the action will cause LXC to register a seccomp filter that will cause a specific errno to be returned to the caller. The errno value can be specified after the errno action word. ๐ฏ
Specifying notify as the action will cause LXC to register a seccomp listener and retrieve a listener file descriptor from the kernel. When a syscall that's registered as notify is made, the kernel will generate a poll event and send a message over the file descriptor. The caller can read this message and inspect the syscalls โ including their arguments. Based on that info, the caller is expected to send back a message telling the kernel which action to take. Until that message is sent, the kernel will block the calling process, so likeโฆ don't ghost it. ๐ญ The format of the messages to read and send is documented in seccomp itself.
lxc.seccomp.profileโ specifies a file containing the seccomp configuration to load before the container starts. ๐ฆlxc.seccomp.allow_nestingโ if this flag is set to1, then seccomp filters will be stacked regardless of whether a seccomp profile is already loaded. This allows nested containers to load their own seccomp profile. The default setting is0. ๐ชlxc.seccomp.notify.proxyโ specifies a unix socket that LXC will connect to and forward seccomp events to. The path must be in the formunix:/path/to/socketorunix:@socket. The former specifies a path-bound unix domain socket while the latter specifies an abstract unix domain socket. ๐งlxc.seccomp.notify.cookieโ an additional string sent along with proxied seccomp notification requests. A little extra garnish. ๐
โจ PR_SET_NO_NEW_PRIVS โจ
With PR_SET_NO_NEW_PRIVS active, execve() promises not to grant privileges to do anything that could not have been done without the execve() call โ for example, rendering the set-user-ID and set-group-ID mode bits, and file capabilities non-functional. Once set, this bit cannot be unset โ it's forever, no takebacks. ๐ The setting of this bit is inherited by children created by fork() and clone(), and preserved across execve(). Note that PR_SET_NO_NEW_PRIVS is applied after the container has changed into its intended AppArmor profile or SElinux context.
lxc.no_new_privsโ specifies whether thePR_SET_NO_NEW_PRIVSflag should be set for the container. Set it to1to activate. โ
โจ UID MAPPINGS โจ
A container can be started in a private user namespace with user and group id mappings. For instance, you can map userid 0 in the container to userid 200000 on the host. The root user in the container will be privileged in the container, but unprivileged on the host โ it's giving "big fish, small pond" ๐. Normally a system container will want a range of ids, so you'd map, for instance, user and group ids 0 through 20,000 in the container to the ids 200,000 through 220,000.
lxc.idmapโ four values must be provided, bestie, all four, no shortcuts. ๐ First a character, either'u'or'g', to specify whether user or group ids are being mapped. Next is the first userid as seen in the user namespace of the container. Next is the userid as seen on the host. And finally, a range indicating the number of consecutive ids to map.
โจ CONTAINER HOOKS โจ
Container hooks are programs or scripts which can be executed at various times in a container's lifetime. Little scripts that show up at the right moment โ main character energy, honestly. ๐ฌ
When a container hook is executed, additional information is passed along. The lxc.hook.version argument can be used to determine if the following arguments are passed as command line arguments or through environment variables. The arguments are:
- Container name.
- Section (always
'lxc'). - The hook type (i.e.
'clone'or'pre-mount'). - Additional arguments. In the case of the clone hook, any extra arguments passed will appear as further arguments to the hook. In the case of the stop hook, paths to filedescriptors for each of the container's namespaces along with their types are passed.
The following environment variables are set:
LXC_CGNS_AWARE: indicator whether the container is cgroup namespace aware.LXC_CONFIG_FILE: the path to the container configuration file.LXC_HOOK_TYPE: the hook type (e.g.'clone','mount','pre-mount'). Note that the existence of this environment variable is conditional on the value oflxc.hook.version. If it is set to1thenLXC_HOOK_TYPEwill be set.LXC_HOOK_SECTION: the section type (e.g.'lxc','net'). Note that the existence of this environment variable is conditional on the value oflxc.hook.version. If it is set to1thenLXC_HOOK_SECTIONwill be set.LXC_HOOK_VERSION: the version of the hooks. This value is identical to the value of the container'slxc.hook.versionconfig item. If it is set to0then old-style hooks are used. If it is set to1then new-style hooks are used.LXC_LOG_LEVEL: the container's log level.LXC_NAME: is the container's name.LXC_[NAMESPACE IDENTIFIER]_NS: path under/proc/PID/fd/to a file descriptor referring to the container's namespace. For each preserved namespace type there will be a separate environment variable. These environment variables will only be set iflxc.hook.versionis set to1.LXC_ROOTFS_MOUNT: the path to the mounted root filesystem.LXC_ROOTFS_PATH: this is thelxc.rootfs.pathentry for the container. Note this is likely not where the mounted rootfs is to be found โ useLXC_ROOTFS_MOUNTfor that. ๐LXC_SRC_NAME: in the case of the clone hook, this is the original container's name. ๐ซถ
Standard output from the hooks is logged at debug level. Standard error is not logged, but it can be captured by the hook redirecting its standard error to standard output. ๐
lxc.hook.versionโ okay so this one decides how the hook args get handed over ๐ set it to1and everything comes through as environment variables; set it to0and it's the old-school way, as arguments. This setting affects all the hook arguments that were traditionally passed as arguments to the script. Specifically, it affects the container name, section (e.g.'lxc','net') and hook type (e.g.'clone','mount','pre-mount') arguments. If new-style hooks are used then the arguments will be available as environment variables. The container name goes intoLXC_NAMEโ and note that one's set independently of the value used for this config item, so she's always there ๐ . The section lands inLXC_HOOK_SECTIONand the hook type lands inLXC_HOOK_TYPE. It also affects how the paths to file descriptors referring to the container's namespaces are passed: set it to1and for each namespace a separate environment variableLXC_[NAMESPACE IDENTIFIER]_NSgets set; set it to0and those paths get passed as arguments to the stop hook instead.lxc.hook.pre-startโ a hook that runs in the host's namespace before the container ttys, consoles, or mounts are up. Early bird energy ๐ฆlxc.hook.pre-mountโ runs in the container's fs namespace but before the rootfs has been set up. This is your moment to mess with the rootfs, i.e. to mount an encrypted filesystem โจ Mounts done in this hook will not be reflected on the host (apart from mounts propagation), so they get automatically cleaned up when the container shuts down. Self-cleaning, we love her.lxc.hook.mountโ runs in the container's namespace after mounting has been done, but before thepivot_root.lxc.hook.autodevโ runs in the container's namespace after mounting has been done and after any mount hooks have run, but before thepivot_root, iflxc.autodev == 1. The purpose of this hook is to assist in populating the/devdirectory of the container when using the autodev option for systemd based containers. The container's/devdirectory is relative to the${LXC_ROOTFS_MOUNT}environment variable available when the hook is run ๐งlxc.hook.start-hostโ runs in the host's namespace after the container has been setup, and immediately before starting the container init.lxc.hook.startโ runs in the container's namespace immediately before executing the container's init. Heads up though: this requires the program to be available in the container, so like, don't count on host binaries here ๐lxc.hook.stopโ runs in the host's namespace with references to the container's namespaces after the container has been shut down. For each namespace an extra argument is passed to the hook containing the namespace's type and a filename that can be used to obtain a file descriptor to the corresponding namespace, separated by a colon. The type is the name as it would appear in the/proc/PID/nsdirectory โ so for instance for the mount namespace the argument usually looks likemnt:/proc/PID/fd/12.lxc.hook.post-stopโ runs in the host's namespace after the container has been shut down. Cleanup szn ๐งนlxc.hook.cloneโ runs when the container is cloned to a new one. Seelxc-clone(1)for more information.lxc.hook.destroyโ runs when the container is destroyed. RIP bestie ๐
๐ซถ CONTAINER HOOKS ENVIRONMENT VARIABLES ๐ซถ
A number of environment variables are made available to the startup hooks to provide configuration information and assist in the functioning of the hooks. Not all variables are valid in all contexts โ read the fine print, bestie ๐ In particular, all paths are relative to the host system and, as such, not valid during the lxc.hook.start hook. (Makes sense, that one's running inside the container.)
LXC_NAMEโ the LXC name of the container. Useful for logging messages in common log environments.[-n]LXC_CONFIG_FILEโ host relative path to the container configuration file. This gives the container a way to reference the original, top level, configuration file for the container in order to locate any additional configuration information not otherwise made available.[-f]LXC_CONSOLEโ the path to the console output of the container if notNULL.[-c][lxc.console.path]LXC_CONSOLE_LOGPATHโ the path to the console log output of the container if notNULL.[-L]LXC_ROOTFS_MOUNTโ the mount location to which the container is initially bound. This will be the host relative path to the container rootfs for the container instance being started and is where changes should be made for that instance.[lxc.rootfs.mount]LXC_ROOTFS_PATHโ the host relative path to the container root which has been mounted to therootfs.mountlocation.[lxc.rootfs.path]LXC_SRC_NAMEโ only for the clone hook. Is set to the original container name. ๐งฌLXC_TARGETโ only for the stop hook. Is set to"stop"for a container shutdown or"reboot"for a container reboot.LXC_CGNS_AWAREโ if unset, then this version of lxc is not aware of cgroup namespaces. If set, it will be set to1, and lxc is aware of cgroup namespaces. Note this does not guarantee that cgroup namespaces are enabled in the kernel ๐ This is used by the lxcfs mount hook.
๐ LOGGING ๐
Logging can be configured on a per-container basis, which is honestly so cute of her. By default, depending upon how the lxc package was compiled, container startup is logged only at the ERROR level, and logged to a file named after the container (with '.log' appended) either under the container path, or under /var/log/lxc.
Both the default log level and the log file can be specified in the container configuration file, overriding the default behavior. Note that the configuration file entries can in turn be overridden by the command line options to lxc-start. Priority wars, but make it config ๐
lxc.log.levelโ the level at which to log. The log level is an integer in the range of0..8inclusive, where a lower number means more verbose debugging. In particular0= trace,1= debug,2= info,3= notice,4= warn,5= error,6= critical,7= alert, and8= fatal. If unspecified, the level defaults to5(error), so that only errors and above are logged.
Note that when a script (such as either a hook script or a network interface up or down script) is called, the script's standard output is logged at level 1, debug.
lxc.log.fileโ the file to which logging info should be written. โ๏ธlxc.log.syslogโ send logging info to syslog. It respects the log level defined inlxc.log.level. The argument should be the syslog facility to use, valid ones are:daemon,local0,local1,local2,local3,local4,local5,local5,local6,local7. (Yes,local5is listed twice in the original and we are not touching it, periodt. Also they skippedlocal5being out of order โ no, bestie, we keep it exactly as-is ๐ซก)
๐ AUTOSTART ๐
The autostart options support marking which containers should be auto-started and in what order. These options may be used by LXC tools directly or by external tooling provided by the distributions. Teamwork, lowkey.
lxc.start.autoโ whether the container should be auto-started. Valid values are0(off) and1(on).lxc.start.delayโ how long to wait (in seconds) after the container is started before starting the next one. Give the girl a second to breathe ๐ฎโ๐จlxc.start.orderโ an integer used to sort the containers when auto-starting a series of containers at once. A lower value means an earlier start.lxc.monitor.unshareโ if not zero the mount namespace will be unshared from the host before initializing the container (before running any pre-start hooks). This requires theCAP_SYS_ADMINcapability at startup. Default is0.lxc.monitor.signal.pdeathโ set the signal to be sent to the container's init when the lxc monitor exits. By default it is set toSIGKILLwhich will cause all container processes to be killed when the lxc monitor process dies. To ensure that containers stay alive even if lxc monitor dies set this to0. > Heads up: leaving it at the default means monitor dies โ everybody dies. If you want your container to survive that, set it to0. Your call ๐lxc.groupโ a multi-value key (can be used multiple times) to put the container in a container group. Those groups can then be used (amongst other things) to start a series of related containers. Squads fr fr ๐ซ
โญ AUTOSTART AND SYSTEM BOOT โญ
Each container can be part of any number of groups or no group at all. Two groups are special. One is the NULL group, i.e. the container does not belong to any group. The other group is the "onboot" group.
When the system boots with the LXC service enabled, it will first attempt to boot any containers with lxc.start.auto == 1 that is a member of the "onboot" group. The startup will be in order of lxc.start.order. If an lxc.start.delay has been specified, that delay will be honored before attempting to start the next container to give the current container time to begin initialization and reduce overloading the host system. After starting the members of the "onboot" group, the LXC system will proceed to boot containers with lxc.start.auto == 1 which are not members of any group (the NULL group) and proceed as with the onboot group. So onboot girlies first, then the group-less ones โ queue etiquette ๐
๐ฆ CONTAINER ENVIRONMENT ๐ฆ
If you want to pass environment variables into the container (that is, environment variables which will be available to init and all of its descendents), you can use lxc.environment parameters to do so. > But be careful that you do not pass in anything sensitive โ any process in the container which doesn't have its environment scrubbed will have these variables available to it, and environment variables are always available via /proc/PID/environ. Like, literally always. Everyone can see them. Do not put your secrets in there bestie ๐ญ
Subkeys are available to narrow the scope of environment variables: lxc.environment.runtime applies only to the container's init process (and all its descendents), and lxc.environment.hooks applies only to hooks.
These configuration parameters can be specified multiple times; once for each environment variable you wish to configure.
lxc.environmentโ environment variables applied both to the container init process and to hooks. Example:
lxc.environment = APP_ENV=production
lxc.environment = SYSLOG_SERVER=192.0.2.42
It is possible to inherit host environment variables by setting the name of the variable without a "=" sign. For example:
lxc.environment = PATH
lxc.environment.runtimeโ environment variables applied only to the container's init process.lxc.environment.hooksโ environment variables applied only to hooks. ๐ช
๐ธ EXAMPLES ๐ธ
Okay so besides the little examples right here, there are more config-file examples chilling in /usr/share/doc/lxc/examples โ go look, bestie. โจ
๐ฆ NETWORK ๐ฆ
This config sets up a container to use a veth pair device, with one side plugged into a bridge called br0 (which the admin already configured on the system before, okay?). The virtual network device visible inside the container gets renamed to eth0. Slay.
lxc.uts.name = myhostname
lxc.net.0.type = veth
lxc.net.0.flags = up
lxc.net.0.link = br0
lxc.net.0.name = eth0
lxc.net.0.hwaddr = 4a:49:43:49:79:bf
lxc.net.0.ipv4.address = 10.2.3.5/24 10.2.3.255
lxc.net.0.ipv6.address = 2003:db8:1:0:214:1234:fe0b:3597
๐ UID/GID MAPPING ๐
This configuration will map both user and group ids in the range 0-9999 in the container to the ids 100000-109999 on the host. Math? Mapping? We love to see it.
lxc.idmap = u 0 100000 10000
lxc.idmap = g 0 100000 10000
๐ฅ CONTROL GROUP ๐ฅ
This one sets up several control groups for the application: cpuset.cpus restricts usage of the defined cpu, cpus.share prioritize the control group, and devices.allow makes usable the specified devices. Very demure, very cgroup. ๐
lxc.cgroup.cpuset.cpus = 0,1
lxc.cgroup.cpu.shares = 1234
lxc.cgroup.devices.deny = a
lxc.cgroup.devices.allow = c 1:3 rw
lxc.cgroup.devices.allow = b 8:0 rw
๐ COMPLEX CONFIGURATION ๐
This example shows a complex configuration making a complex network stack โ using control groups, setting a new hostname, mounting some locations, and changing the root file system. Itโs giving main character energy, periodt.
lxc.uts.name = complex
lxc.net.0.type = veth
lxc.net.0.flags = up
lxc.net.0.link = br0
lxc.net.0.hwaddr = 4a:49:43:49:79:bf
lxc.net.0.ipv4.address = 10.2.3.5/24 10.2.3.255
lxc.net.0.ipv6.address = 2003:db8:1:0:214:1234:fe0b:3597
lxc.net.0.ipv6.address = 2003:db8:1:0:214:5432:feab:3588
lxc.net.1.type = macvlan
lxc.net.1.flags = up
lxc.net.1.link = eth0
lxc.net.1.hwaddr = 4a:49:43:49:79:bd
lxc.net.1.ipv4.address = 10.2.3.4/24
lxc.net.1.ipv4.address = 192.168.10.125/24
lxc.net.1.ipv6.address = 2003:db8:1:0:214:1234:fe0b:3596
lxc.net.2.type = phys
lxc.net.2.flags = up
lxc.net.2.link = random0
lxc.net.2.hwaddr = 4a:49:43:49:79:ff
lxc.net.2.ipv4.address = 10.2.3.6/24
lxc.net.2.ipv6.address = 2003:db8:1:0:214:1234:fe0b:3297
lxc.cgroup.cpuset.cpus = 0,1
lxc.cgroup.cpu.shares = 1234
lxc.cgroup.devices.deny = a
lxc.cgroup.devices.allow = c 1:3 rw
lxc.cgroup.devices.allow = b 8:0 rw
lxc.mount.fstab = /etc/fstab.complex
lxc.mount.entry = /lib /root/myrootfs/lib none ro,bind 0 0
lxc.rootfs.path = dir:/mnt/rootfs.complex
lxc.rootfs.options = idmap=container
lxc.cap.drop = sys_module mknod setuid net_raw
lxc.cap.drop = mac_override
๐ SEE ALSO ๐
First little friend group: chroot(1), pivot_root(8), fstab(5), capabilities(7). Go say hi. ๐ซถ
๐ SEE ALSO (again, bestie) ๐
And yes, thereโs another one โ this page really said โmore friendsโ twice:
lxc(7), lxc-create(1), lxc-copy(1), lxc-destroy(1), lxc-start(1), lxc-stop(1), lxc-execute(1), lxc-console(1), lxc-monitor(1), lxc-wait(1), lxc-cgroup(1), lxc-ls(1), lxc-info(1), lxc-freeze(1), lxc-unfreeze(1), lxc-attach(1), lxc.conf(5).
Okay thatโs the whole vibe, bestie ๐ Footer check: this page is lxc.container.conf(5), dated 2026-05-01 โ source/version and date, present and accounted for. Now go configure something iconic. โจ๐๐
๐ฆ where this page comes from
- alpine 3.24: lxc-doc 7.0.0-r0 ยท LGPL-2.1-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms