HALF ECHO LEARN / REFERENCE COLLECTION
v262.2man/systemd-nspawn.xml
Git blob: 58d4c84b36c153168ad93674304585d3acdb4455
Lines 1–1000 of 2,180
<?xml version='1.0'?>
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
<!ENTITY % entities SYSTEM "custom-entities.ent" >
%entities;
]>
<!-- SPDX-License-Identifier: LGPL-2.1-or-later -->
<refentry id="systemd-nspawn" conditional="ENABLE_NSPAWN"
xmlns:xi="http://www.w3.org/2001/XInclude">
<refentryinfo>
<title>systemd-nspawn</title>
<productname>systemd</productname>
</refentryinfo>
<refmeta>
<refentrytitle>systemd-nspawn</refentrytitle>
<manvolnum>1</manvolnum>
</refmeta>
<refnamediv>
<refname>systemd-nspawn</refname>
<refpurpose>Spawn a command or OS in a lightweight container</refpurpose>
</refnamediv>
<refsynopsisdiv>
<cmdsynopsis>
<command>systemd-nspawn</command>
<arg choice="opt" rep="repeat">OPTIONS</arg>
<arg choice="opt"><replaceable>COMMAND</replaceable>
<arg choice="opt" rep="repeat">ARGS</arg>
</arg>
</cmdsynopsis>
<cmdsynopsis>
<command>systemd-nspawn</command>
<arg choice="plain">--boot</arg>
<arg choice="opt" rep="repeat">OPTIONS</arg>
<arg choice="opt" rep="repeat">ARGS</arg>
</cmdsynopsis>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
<para><command>systemd-nspawn</command> may be used to run a command or OS in a lightweight namespace
container. In many ways it is similar to <citerefentry
project='man-pages'><refentrytitle>chroot</refentrytitle><manvolnum>1</manvolnum></citerefentry>, but more powerful
since it virtualizes the file system hierarchy, as well as the process tree, the various IPC subsystems, and
the host and domain names.</para>
<para><command>systemd-nspawn</command> may be invoked on any directory tree containing an operating system tree,
using the <option>--directory=</option> command line option. By using the <option>--machine=</option> option an OS
tree is automatically searched for in a couple of locations, most importantly in
<filename>/var/lib/machines/</filename>, the suggested directory to place OS container images installed on the
system.</para>
<para>In contrast to <citerefentry
project='man-pages'><refentrytitle>chroot</refentrytitle><manvolnum>1</manvolnum></citerefentry> <command>systemd-nspawn</command>
may be used to boot full Linux-based operating systems in a container.</para>
<para><command>systemd-nspawn</command> limits access to various kernel interfaces in the container to
read-only, such as <filename>/sys/</filename>, <filename>/proc/sys/</filename>, or
<filename>/sys/fs/selinux/</filename>. The host's network interfaces and the system clock may not be
changed from within the container. Device nodes may not be created. The host system cannot be rebooted
and kernel modules may not be loaded from within the container. <emphasis>This sandbox can easily be
circumvented from within the container if user namespaces are not used</emphasis>. This means that
untrusted code must always be run in a user namespace, see the discussion of the
<option>--private-users=</option> option below.</para>
<para>Use a tool like <citerefentry
project='mankier'><refentrytitle>dnf</refentrytitle><manvolnum>8</manvolnum></citerefentry>, <citerefentry
project='die-net'><refentrytitle>debootstrap</refentrytitle><manvolnum>8</manvolnum></citerefentry>, or
<citerefentry project='archlinux'><refentrytitle>pacman</refentrytitle><manvolnum>8</manvolnum></citerefentry> to
set up an OS directory tree suitable as file system hierarchy for <command>systemd-nspawn</command> containers. See
the Examples section below for details on suitable invocation of these commands.</para>
<para>As a safety check <command>systemd-nspawn</command> will verify the existence of
<filename>/usr/lib/os-release</filename> or <filename>/etc/os-release</filename> in the container tree before
booting a container (see
<citerefentry><refentrytitle>os-release</refentrytitle><manvolnum>5</manvolnum></citerefentry>). It might be
necessary to add this file to the container tree manually if the OS of the container is too old to contain this
file out-of-the-box.</para>
<para><command>systemd-nspawn</command> may be invoked directly from the interactive command line or run as system
service in the background. In this mode each container instance runs as its own service instance; a default
template unit file <filename>systemd-nspawn@.service</filename> is provided to make this easy, taking the container
name as instance identifier. Note that different default options apply when <command>systemd-nspawn</command> is
invoked by the template unit file than interactively on the command line. Most importantly the template unit file
makes use of the <option>--boot</option> option which is not the default in case <command>systemd-nspawn</command>
is invoked from the interactive command line. Further differences with the defaults are documented along with the
various supported options below.</para>
<para>The <citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry> tool may
be used to execute a number of operations on containers. In particular it provides easy-to-use commands to run
containers as system services using the <filename>systemd-nspawn@.service</filename> template unit
file.</para>
<para>Along with each container a settings file with the <filename>.nspawn</filename> suffix may exist, containing
additional settings to apply when running the container. See
<citerefentry><refentrytitle>systemd.nspawn</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
details. Settings files override the default options used by the <filename>systemd-nspawn@.service</filename>
template unit file, making it usually unnecessary to alter this template file directly.</para>
<para>Note that <command>systemd-nspawn</command> will mount file systems private to the container to
<filename>/dev/</filename>, <filename>/run/</filename>, and similar. These will not be visible outside of
the container, and their contents will be lost when the container exits.</para>
<para>Note that running two <command>systemd-nspawn</command> containers from the same directory tree will not make
processes in them see each other. The PID namespace separation of the two containers is complete and the containers
will share very few runtime objects except for the underlying file system. Rather use
<citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
<command>login</command> or <command>shell</command> commands to request an additional login session in a running
container.</para>
<para><command>systemd-nspawn</command> implements the <ulink
url="https://systemd.io/CONTAINER_INTERFACE">Container Interface</ulink> specification.</para>
<para>While running, containers invoked with <command>systemd-nspawn</command> are registered with the
<citerefentry><refentrytitle>systemd-machined</refentrytitle><manvolnum>8</manvolnum></citerefentry> service that
keeps track of running containers, and provides programming interfaces to interact with them.</para>
</refsect1>
<refsect1>
<title>Unprivileged Operation</title>
<para><command>systemd-nspawn</command> may be invoked with or without privileges. The full functionality
is currently only available when invoked with privileges. When invoked without privileges, various
limitations apply, including, but not limited to:</para>
<itemizedlist>
<listitem><para>Only disk image based containers are supported (i.e. <option>--image=</option>).
Directory based ones (i.e. <option>--directory=</option>) are only supported if owned by the "foreign"
UID range.</para></listitem>
<listitem><para>Only <option>--private-network</option> and <option>--network-veth</option> networking
modes are supported.</para></listitem>
</itemizedlist>
<para>When running in unprivileged mode, some needed functionality is provided via
<citerefentry><refentrytitle>systemd-mountfsd.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>
and
<citerefentry><refentrytitle>systemd-nsresourced.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>.</para>
</refsect1>
<refsect1>
<title>Options</title>
<para>If option <option>--boot</option> is specified, the arguments
are used as arguments for the init program. Otherwise,
<replaceable>COMMAND</replaceable> specifies the program to launch
in the container, and the remaining arguments are used as
arguments for this program. If <option>--boot</option> is not used and
no arguments are specified, a shell is launched in the
container.</para>
<para>The following options are understood:</para>
<variablelist>
<varlistentry>
<term><option>-q</option></term>
<term><option>--quiet</option></term>
<listitem><para>Turns off any status output by the tool
itself. When this switch is used, the only output from nspawn
will be the console output of the container OS
itself.</para>
<xi:include href="version-info.xml" xpointer="v209"/></listitem>
</varlistentry>
<varlistentry>
<term><option>--settings=<replaceable>MODE</replaceable></option></term>
<listitem><para>Controls whether
<command>systemd-nspawn</command> shall search for and use
additional per-container settings from
<filename>.nspawn</filename> files. Takes a boolean or the
special values <option>override</option> or
<option>trusted</option>.</para>
<para>If enabled (the default), a settings file named after the
machine (as specified with the <option>--machine=</option>
setting, or derived from the directory or image file name)
with the suffix <filename>.nspawn</filename> is searched in
<filename>/etc/systemd/nspawn/</filename> and
<filename>/run/systemd/nspawn/</filename>. If it is found
there, its settings are read and used. If it is not found
there, it is subsequently searched in the same directory as the
image file or in the immediate parent of the root directory of
the container. In this case, if the file is found, its settings
will be also read and used, but potentially unsafe settings
are ignored. Note that in both these cases, settings on the
command line take precedence over the corresponding settings
from loaded <filename>.nspawn</filename> files, if both are
specified. Unsafe settings are considered all settings that
elevate the container's privileges or grant access to
additional resources such as files or directories of the
host. For details about the format and contents of
<filename>.nspawn</filename> files, consult
<citerefentry><refentrytitle>systemd.nspawn</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
<para>If this option is set to <option>override</option>, the
file is searched, read and used the same way, however, the order of
precedence is reversed: settings read from the
<filename>.nspawn</filename> file will take precedence over
the corresponding command line options, if both are
specified.</para>
<para>If this option is set to <option>trusted</option>, the
file is searched, read and used the same way, but regardless
of being found in <filename>/etc/systemd/nspawn/</filename>,
<filename>/run/systemd/nspawn/</filename> or next to the image
file or container root directory, all settings will take
effect, however, command line arguments still take precedence
over corresponding settings.</para>
<para>If disabled, no <filename>.nspawn</filename> file is read
and no settings except the ones on the command line are in
effect.</para>
<xi:include href="version-info.xml" xpointer="v226"/></listitem>
</varlistentry>
<varlistentry>
<term><option>--cleanup</option></term>
<listitem>
<para>Clean up left-over mounts and underlying mount points used by the container, and exit without
invoking any containers. This may be useful when the previous invocation of
<command>systemd-nspawn</command> was unexpectedly terminated. This requires at least one of
<option>-M/--machine=</option>, <option>-D/--directory=</option>, or <option>-i/--image=</option>
to determine the mounts to be cleaned up.</para>
<xi:include href="version-info.xml" xpointer="v257"/>
</listitem>
</varlistentry>
</variablelist>
<refsect2>
<title>Image Options</title>
<variablelist>
<varlistentry>
<term><option>-D</option></term>
<term><option>--directory=</option></term>
<listitem><para>Directory to use as file system root for the container.</para>
<para>If neither <option>--directory=</option>, nor <option>--image=</option> is specified the
directory is determined by searching for a directory named the same as the machine name specified
with <option>--machine=</option>. See
<citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
section "Files and Directories" for the precise search path.</para>
<xi:include href="vpick.xml" xpointer="directory"/>
<para>If neither <option>--directory=</option>, <option>--image=</option>, nor
<option>--machine=</option> are specified, the current directory will be used. May not be specified
together with <option>--image=</option>.</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--template=</option></term>
<listitem><para>Directory or <literal>btrfs</literal> subvolume to use as template for the
container's root directory. If this is specified and the container's root directory (as configured by
<option>--directory=</option>) does not yet exist it is created as <literal>btrfs</literal> snapshot
(if supported) or plain directory (otherwise) and populated from this template tree. Ideally, the
specified template path refers to the root of a <literal>btrfs</literal> subvolume, in which case a
simple copy-on-write snapshot is taken, and populating the root directory is instant. If the
specified template path does not refer to the root of a <literal>btrfs</literal> subvolume (or not
even to a <literal>btrfs</literal> file system at all), the tree is copied (though possibly in a
'reflink' copy-on-write scheme — if the file system supports that), which can be substantially more
time-consuming. Note that the snapshot taken is of the specified directory or subvolume, including
all subdirectories and subvolumes below it, but excluding any sub-mounts. May not be specified
together with <option>--image=</option> or <option>--ephemeral</option>.</para>
<para>Note that this switch leaves hostname, machine ID and
all other settings that could identify the instance
unmodified.</para>
<xi:include href="version-info.xml" xpointer="v219"/></listitem>
</varlistentry>
<varlistentry>
<term><option>-x</option></term>
<term><option>--ephemeral</option></term>
<listitem><para>If specified, the container is run with a temporary snapshot of its file system that is removed
immediately when the container terminates. May not be specified together with
<option>--template=</option>.</para>
<para>Note that this switch leaves hostname, machine ID and all other settings that could identify
the instance unmodified. Please note that — as with <option>--template=</option> — taking the
temporary snapshot is more efficient on file systems that support subvolume snapshots or 'reflinks'
natively (<literal>btrfs</literal> or new <literal>xfs</literal>) than on more traditional file
systems that do not (<literal>ext4</literal>). Note that the snapshot taken is of the specified
directory or subvolume, including all subdirectories and subvolumes below it, but excluding any
sub-mounts.</para>
<para>With this option no modifications of the container image are retained. Use
<option>--volatile=</option> (described below) for other mechanisms to restrict persistency of
container images during runtime.</para>
<xi:include href="version-info.xml" xpointer="v219"/>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-i</option></term>
<term><option>--image=</option></term>
<listitem><para>Disk image to mount the root directory for the
container from. Takes a path to a regular file or to a block
device node. The file or block device must contain
either:</para>
<itemizedlist>
<listitem><para>An MBR partition table with a single
partition of type 0x83 that is marked
bootable.</para></listitem>
<listitem><para>A GUID partition table (GPT) with a single
partition of type
0fc63daf-8483-4772-8e79-3d69d8477de4.</para></listitem>
<listitem><para>A GUID partition table (GPT) with a marked root partition which is mounted as the
root directory of the container. Optionally, GPT images may contain a home and/or a server data
partition which are mounted to the appropriate places in the container. All these partitions must
be identified by the partition types defined by the <ulink
url="https://uapi-group.org/specifications/specs/discoverable_partitions_specification">UAPI.2
Discoverable Partitions Specification</ulink>.</para></listitem>
<listitem><para>No partition table, and a single file system spanning the whole image.</para></listitem>
</itemizedlist>
<para>On GPT images, if an EFI System Partition (ESP) is discovered, it is automatically mounted to
<filename>/efi</filename> (or <filename>/boot</filename> as fallback) in case a directory by this name exists
and is empty.</para>
<para>Partitions encrypted with LUKS are automatically decrypted. Also, on GPT images dm-verity data integrity
hash partitions are set up if the root hash for them is specified using the <option>--root-hash=</option>
option.</para>
<para>Single file system images (i.e. file systems without a surrounding partition table) can be opened using
dm-verity if the integrity data is passed using the <option>--root-hash=</option> and
<option>--verity-data=</option> (and optionally <option>--root-hash-sig=</option>) options.</para>
<para>Any other partitions, such as foreign partitions or swap partitions are not mounted. May not be specified
together with <option>--directory=</option>, <option>--template=</option>.</para>
<xi:include href="vpick.xml" xpointer="image"/>
<xi:include href="version-info.xml" xpointer="v211"/></listitem>
</varlistentry>
<varlistentry>
<term><option>--image-policy=<replaceable>policy</replaceable></option></term>
<listitem><para>Takes an image policy string as argument, as per
<citerefentry><refentrytitle>systemd.image-policy</refentrytitle><manvolnum>7</manvolnum></citerefentry>. The
policy is enforced when operating on the disk image specified via <option>--image=</option>, see
above. If not specified, defaults to
<literal>root=verity+signed+encrypted+unprotected+absent:usr=verity+signed+encrypted+unprotected+absent:home=encrypted+unprotected+absent:srv=encrypted+unprotected+absent:esp=unprotected+absent:xbootldr=unprotected+absent:tmp=encrypted+unprotected+absent:var=encrypted+unprotected+absent</literal>,
i.e. all recognized file systems in the image are used, but not the swap partition.</para>
<xi:include href="version-info.xml" xpointer="v254"/></listitem>
</varlistentry>
<varlistentry>
<term><option>--mstack=</option></term>
<listitem><para>Mount stack to mount the root directory for the container from. Takes a path to a
directory implementing
<citerefentry><refentrytitle>systemd.mstack</refentrytitle><manvolnum>7</manvolnum></citerefentry>. This
may be used to run a container of an <literal>overlayfs</literal> assembled from a number of layers,
possibly writable and augmented with additional bind mounts.</para>
<xi:include href="version-info.xml" xpointer="v260"/></listitem>
</varlistentry>
<varlistentry>
<term><option>--oci-bundle=</option></term>
<listitem><para>Takes the path to an OCI runtime bundle to invoke, as specified in the <ulink
url="https://github.com/opencontainers/runtime-spec/blob/master/spec.md">OCI Runtime Specification</ulink>. In
this case, no <filename>.nspawn</filename> file is loaded, and the root directory and various settings are read
from the OCI runtime JSON data (but data passed on the command line takes precedence).</para>
<xi:include href="version-info.xml" xpointer="v242"/></listitem>
</varlistentry>
<varlistentry>
<term><option>--read-only</option></term>
<listitem><para>Mount the container's root file system (and any other file systems contained in the container
image) read-only. This has no effect on additional mounts made with <option>--bind=</option>,
<option>--tmpfs=</option> and similar options. This mode is implied if the container image file or directory is
marked read-only itself. It is also implied if <option>--volatile=</option> is used. In this case, the container
image on disk is strictly read-only, while changes are permitted but kept non-persistently in memory only. For
further details, see below.</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--volatile</option></term>
<term><option>--volatile=<replaceable>MODE</replaceable></option></term>
<listitem><para>Boots the container in volatile mode. When no mode parameter is passed or when mode is
specified as <option>yes</option>, full volatile mode is enabled. This means the root directory is mounted as a
mostly unpopulated <literal>tmpfs</literal> instance, and <filename>/usr/</filename> from the OS tree is
mounted into it in read-only mode (the system thus starts up with read-only OS image, but pristine state and
configuration, any changes are lost on shutdown). When the mode parameter is specified as
<option>state</option>, the OS tree is mounted read-only, but <filename>/var/</filename> is mounted as a
writable <literal>tmpfs</literal> instance into it (the system thus starts up with read-only OS resources and
configuration, but pristine state, and any changes to the latter are lost on shutdown). When the mode parameter
is specified as <option>overlay</option> the read-only root file system is combined with a writable
<filename>tmpfs</filename> instance through <literal>overlayfs</literal>, so that it appears at it normally
would, but any changes are applied to the temporary file system only and lost when the container is
terminated. When the mode parameter is specified as <option>no</option> (the default), the whole OS tree is
made available writable (unless <option>--read-only</option> is specified, see above).</para>
<para>Note that if one of the volatile modes is chosen, its effect is limited to the root file system
(or <filename>/var/</filename> in case of <option>state</option>), and any other mounts placed in the
hierarchy are unaffected — regardless of whether they are established automatically (e.g. the EFI system
partition that might be mounted to <filename>/efi/</filename> or <filename>/boot/</filename>) or
explicitly (e.g. through an additional command line option such as <option>--bind=</option>, see
below). This means, even if <option>--volatile=overlay</option> is used changes to
<filename>/efi/</filename> or <filename>/boot/</filename> are prohibited in case such a partition
exists in the container image operated on, and even if <option>--volatile=state</option> is used the
hypothetical file <filename index="false">/etc/foobar</filename> is potentially writable if
<option>--bind=/etc/foobar</option> is used to mount it from outside the read-only container
<filename>/etc/</filename> directory.</para>
<para>The <option>--ephemeral</option> option is closely related to this setting, and provides similar
behaviour by making a temporary, ephemeral copy of the whole OS image and executing that. For further details,
see above.</para>
<para>The <option>--tmpfs=</option> and <option>--overlay=</option> options provide similar functionality, but
for specific sub-directories of the OS image only. For details, see below.</para>
<para>This option provides similar functionality for containers as the <literal>systemd.volatile=</literal>
kernel command line switch provides for host systems. See
<citerefentry><refentrytitle>kernel-command-line</refentrytitle><manvolnum>7</manvolnum></citerefentry> for
details.</para>
<para>Note that setting this option to <option>yes</option> or <option>state</option> will only work
correctly with operating systems in the container that can boot up with only
<filename>/usr/</filename> mounted, and are able to automatically populate <filename>/var/</filename>
(and <filename>/etc/</filename> in case of <literal>--volatile=yes</literal>). Specifically, this
means that operating systems that follow the historic split of <filename>/bin/</filename> and
<filename>/lib/</filename> (and related directories) from <filename>/usr/</filename> (i.e. where the
former are not symlinks into the latter) are not supported by <literal>--volatile=yes</literal> as
container payload. The <option>overlay</option> option does not require any particular preparations
in the OS, but do note that <literal>overlayfs</literal> behaviour differs from regular file systems
in a number of ways, and hence compatibility is limited.</para>
<xi:include href="version-info.xml" xpointer="v216"/></listitem>
</varlistentry>
<varlistentry>
<term><option>--root-hash=</option></term>
<listitem><para>Takes a data integrity (dm-verity) root hash specified in hexadecimal. This option enables data
integrity checks using dm-verity, if the used image contains the appropriate integrity data (see above). The
specified hash must match the root hash of integrity data, and is usually at least 256 bits (and hence 64
formatted hexadecimal characters) long (in case of SHA256 for example). If this option is not specified, but
the image file carries the <literal>user.verity.roothash</literal> extended file attribute (see <citerefentry
project='man-pages'><refentrytitle>xattr</refentrytitle><manvolnum>7</manvolnum></citerefentry>), then the root
hash is read from it, also as formatted hexadecimal characters. If the extended file attribute is not found (or
is not supported by the underlying file system), but a file with the <filename>.roothash</filename> suffix is
found next to the image file, bearing otherwise the same name (except if the image has the
<filename>.raw</filename> suffix, in which case the root hash file must not have it in its name), the root hash
is read from it and automatically used, also as formatted hexadecimal characters.</para>
<para>Note that this configures the root hash for the root file system. Disk images may also contain
separate file systems for the <filename>/usr/</filename> hierarchy, which may be Verity protected as
well. The root hash for this protection may be configured via the
<literal>user.verity.usrhash</literal> extended file attribute or via a <filename>.usrhash</filename>
file adjacent to the disk image, following the same format and logic as for the root hash for the
root file system described here. Note that there's currently no switch to configure the root hash for
the <filename>/usr/</filename> from the command line.</para>
<para>Also see the <varname>RootHash=</varname> option in
<citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
<xi:include href="version-info.xml" xpointer="v233"/>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--root-hash-sig=</option></term>
<listitem><para>Takes a PKCS7 signature of the <option>--root-hash=</option> option.
The semantics are the same as for the <varname>RootHashSignature=</varname> option, see
<citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
</para>
<xi:include href="version-info.xml" xpointer="v246"/></listitem>
</varlistentry>
<varlistentry>
<term><option>--verity-data=</option></term>
<listitem><para>Takes the path to a data integrity (dm-verity) file. This option enables data integrity checks
using dm-verity, if a root-hash is passed and if the used image itself does not contain the integrity data.
The integrity data must be matched by the root hash. If this option is not specified, but a file with the
<filename>.verity</filename> suffix is found next to the image file, bearing otherwise the same name (except if
the image has the <filename>.raw</filename> suffix, in which case the verity data file must not have it in its name),
the verity data is read from it and automatically used.</para>
<xi:include href="version-info.xml" xpointer="v246"/></listitem>
</varlistentry>
<varlistentry>
<term><option>--pivot-root=</option></term>
<listitem><para>Pivot the specified directory to <filename>/</filename> inside the container, and either unmount the
container's old root, or pivot it to another specified directory. Takes one of: a path argument — in which case the
specified path will be pivoted to <filename>/</filename> and the old root will be unmounted; or a colon-separated pair
of new root path and pivot destination for the old root. The new root path will be pivoted to <filename>/</filename>,
and the old <filename>/</filename> will be pivoted to the other directory. Both paths must be absolute, and are resolved
in the container's file system namespace.</para>
<para>This is for containers which have several bootable directories in them; for example, several
<citerefentry><refentrytitle>ostree</refentrytitle><manvolnum>1</manvolnum></citerefentry>
deployments. It emulates the behavior of the boot loader and the initrd which normally select which
directory to mount as the root and start the container's PID 1 in.</para>
<xi:include href="version-info.xml" xpointer="v233"/></listitem>
</varlistentry>
</variablelist>
</refsect2><refsect2>
<title>Execution Options</title>
<variablelist>
<varlistentry>
<term><option>-a</option></term>
<term><option>--as-pid2</option></term>
<listitem><para>Invoke the shell or specified program as process ID (PID) 2 instead of PID 1 (init). By
default, if neither this option nor <option>--boot</option> is used, the selected program is run as the process
with PID 1, a mode only suitable for programs that are aware of the special semantics that the process with
PID 1 has on UNIX. For example, it needs to reap all processes reparented to it, and should implement
<command>sysvinit</command> compatible signal handling (specifically: it needs to reboot on SIGINT, reexecute
on SIGTERM, reload configuration on SIGHUP, and so on). With <option>--as-pid2</option> a minimal stub init
process is run as PID 1 and the selected program is executed as PID 2 (and hence does not need to implement any
special semantics). The stub init process will reap processes as necessary and react appropriately to
signals. It is recommended to use this mode to invoke arbitrary commands in containers, unless they have been
modified to run correctly as PID 1. Or in other words: this switch should be used for pretty much all commands,
except when the command refers to an init or shell implementation, as these are generally capable of running
correctly as PID 1. This option may not be combined with <option>--boot</option>.</para>
<xi:include href="version-info.xml" xpointer="v229"/>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-b</option></term>
<term><option>--boot</option></term>
<listitem><para>Automatically search for an init program and invoke it as PID 1, instead of a shell or a user
supplied program. If this option is used, arguments specified on the command line are used as arguments for the
init program. This option may not be combined with <option>--as-pid2</option>.</para>
<para>The following table explains the different modes of invocation and relationship to
<option>--as-pid2</option> (see above):</para>
<table>
<title>Invocation Mode</title>
<tgroup cols='2' align='left' colsep='1' rowsep='1'>
<colspec colname="switch" />
<colspec colname="explanation" />
<thead>
<row>
<entry>Switch</entry>
<entry>Explanation</entry>
</row>
</thead>
<tbody>
<row>
<entry>Neither <option>--as-pid2</option> nor <option>--boot</option> specified</entry>
<entry>The passed parameters are interpreted as the command line, which is executed as PID 1 in the container.</entry>
</row>
<row>
<entry><option>--as-pid2</option> specified</entry>
<entry>The passed parameters are interpreted as the command line, which is executed as PID 2 in the container. A stub init process is run as PID 1.</entry>
</row>
<row>
<entry><option>--boot</option> specified</entry>
<entry>An init program is automatically searched for and run as PID 1 in the container. The passed parameters are used as invocation parameters for this process.</entry>
</row>
</tbody>
</tgroup>
</table>
<para>Note that <option>--boot</option> is the default mode of operation if the
<filename>systemd-nspawn@.service</filename> template unit file is used.</para>
<para>When <option>--boot</option> is used, the passed parameters are processed the same way the
kernel processes its command line before handing it to PID 1: any <literal>KEY=VALUE</literal>
assignment whose <literal>KEY</literal> does not contain a <literal>.</literal> is exported as
an environment variable for PID 1 (with <literal>-</literal> in the key replaced by
<literal>_</literal>, as if specified via <option>--setenv=</option>), while all other
parameters (including <literal>systemd.*=</literal> assignments) are passed as arguments to the
init program.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--chdir=</option></term>
<listitem><para>Change to the specified working directory before invoking the process in the container. Expects
an absolute path in the container's file system namespace.</para>
<xi:include href="version-info.xml" xpointer="v229"/></listitem>
</varlistentry>
<varlistentry>
<term><option>-E <replaceable>NAME</replaceable>[=<replaceable>VALUE</replaceable>]</option></term>
<term><option>--setenv=<replaceable>NAME</replaceable>[=<replaceable>VALUE</replaceable>]</option></term>
<listitem><para>Specifies an environment variable to pass to the init process in the container. This
may be used to override the default variables or to set additional variables. It may be used more
than once to set multiple variables. When <literal>=</literal> and <replaceable>VALUE</replaceable>
are omitted, the value of the variable with the same name in the program environment will be used.
</para>
<xi:include href="version-info.xml" xpointer="v209"/></listitem>
</varlistentry>
<varlistentry>
<term><option>-u</option></term>
<term><option>--uid=</option></term>
<listitem><para>After transitioning into the container, change to the specified user defined in the
container's user database. Like all other systemd-nspawn features, this is not a security feature and
provides protection against accidental destructive operations only. This option was previously named
<option>--user=</option>. The old name is still accepted but deprecated. The short option
<option>-u</option> is compatible with both old and new versions of
<command>systemd-nspawn</command>.</para>
<para>Note that if credentials are used in combination with a non-root <option>--uid=</option>
(e.g.: <option>--set-credential=</option> or <option>--load-credential=</option>), then
<option>--no-new-privileges=yes</option> must be used, and <option>--boot</option> or
<option>--as-pid2</option> must not be used, as the credentials would otherwise be unreadable
by the container due to missing privileges after switching to the specified user.</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--kill-signal=</option></term>
<listitem><para>Specify the process signal to send to the container's PID 1 when nspawn itself receives
<constant>SIGTERM</constant>, in order to trigger an orderly shutdown of the container. Defaults to
<constant>SIGRTMIN+3</constant> if <option>--boot</option> is used (on systemd-compatible init systems
<constant>SIGRTMIN+3</constant> triggers an orderly shutdown). If <option>--boot</option> is not used and this
option is not specified, the container's processes are terminated abruptly via <constant>SIGKILL</constant>. For
a list of valid signals, see <citerefentry
project='man-pages'><refentrytitle>signal</refentrytitle><manvolnum>7</manvolnum></citerefentry>.</para>
<xi:include href="version-info.xml" xpointer="v220"/></listitem>
</varlistentry>
<varlistentry>
<term><option>--notify-ready=</option></term>
<listitem><para>Configures support for notifications from the container's init process.
<option>--notify-ready=</option> takes a boolean. If false <command>systemd-nspawn</command>
notifies the calling service manager with a <literal>READY=1</literal> message when the init process is
created. If true it waits for a <literal>READY=1</literal> message from the init process in the VM
before sending its own to the service manager. For more details about notifications see
<citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
<para>Defaults to false. (Note that this is unlike the option of the same name to
<citerefentry><refentrytitle>systemd-vmspawn</refentrytitle><manvolnum>1</manvolnum></citerefentry>
that defaults to true.)</para>
<para>If <command>systemd-nspawn</command> itself is invoked with a <varname>$NOTIFY_SOCKET</varname>
set in its environment (i.e. it is itself supervised by a service manager that uses the
<citerefentry><refentrytitle>sd_notify</refentrytitle><manvolnum>3</manvolnum></citerefentry>
protocol), <literal>FDSTORE=1</literal> and <literal>FDSTOREREMOVE=1</literal> messages received
from the container payload (along with any accompanying file descriptors and
<literal>FDNAME=</literal> tag) are forwarded one level up to the enveloping service manager. This
allows the file descriptor store of services running inside the container to be preserved across
container restarts (and, transitively, across restarts, re-execs, soft-reboots and LUO-based kexecs
of any outer service manager), provided
<varname>FileDescriptorStoreMax=</varname>/<varname>FileDescriptorStorePreserve=yes</varname> are
configured on the unit running <command>systemd-nspawn</command>. See the
<ulink url="https://systemd.io/FILE_DESCRIPTOR_STORE">File Descriptor Store</ulink> documentation
for details.</para>
<xi:include href="version-info.xml" xpointer="v231"/></listitem>
</varlistentry>
<varlistentry>
<term><option>--suppress-sync=</option></term>
<listitem><para>Expects a boolean argument. If true, turns off any form of on-disk file system
synchronization for the container payload. This means all system calls such as <citerefentry
project='man-pages'><refentrytitle>sync</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
<function>fsync()</function>, <function>syncfs()</function>, … will execute no operation, and the
<constant>O_SYNC</constant>/<constant>O_DSYNC</constant> flags to <citerefentry
project='man-pages'><refentrytitle>open</refentrytitle><manvolnum>2</manvolnum></citerefentry> and
related calls will be made unavailable. This is potentially dangerous, as assumed data integrity
guarantees to the container payload are not actually enforced (i.e. data assumed to have been written
to disk might be lost if the system is shut down abnormally). However, this can dramatically improve
container runtime performance – as long as these guarantees are not required or desirable, for
example because any data written by the container is of temporary, redundant nature, or just an
intermediary artifact that will be further processed and finalized by a later step in a
pipeline. Defaults to false.</para>
<xi:include href="version-info.xml" xpointer="v250"/></listitem>
</varlistentry>
</variablelist>
</refsect2><refsect2>
<title>System Identity Options</title>
<variablelist>
<varlistentry>
<term><option>-M</option></term>
<term><option>--machine=</option></term>
<listitem><para>Sets the machine name for this container. This
name may be used to identify this container during its runtime
(for example in tools like
<citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
and similar), and is used to initialize the container's
hostname (which the container can choose to override,
however). If not specified, the last component of the root
directory path of the container is used, possibly suffixed
with a random identifier in case <option>--ephemeral</option>
mode is selected. If the root directory selected is the host's
root directory the host's hostname is used as default
instead.</para>
<xi:include href="version-info.xml" xpointer="v202"/></listitem>
</varlistentry>
<varlistentry>
<term><option>--hostname=</option></term>
<listitem><para>Controls the hostname to set within the container, if different from the machine name. Expects
a valid hostname as argument. If this option is used, the kernel hostname of the container will be set to this
value, otherwise it will be initialized to the machine name as controlled by the <option>--machine=</option>
option described above. The machine name is used for various aspect of identification of the container from the
outside, the kernel hostname configurable with this option is useful for the container to identify itself from
the inside. It is usually a good idea to keep both forms of identification synchronized, in order to avoid
confusion. It is hence recommended to avoid usage of this option, and use <option>--machine=</option>
exclusively. Note that regardless whether the container's hostname is initialized from the name set with
<option>--hostname=</option> or the one set with <option>--machine=</option>, the container can later override
its kernel hostname freely on its own as well.</para>
<xi:include href="version-info.xml" xpointer="v239"/>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--uuid=</option></term>
<listitem><para>Set the specified UUID for the container. The
init system will initialize
<filename>/etc/machine-id</filename> from this if this file is
not set yet. Note that this option takes effect only if
<filename>/etc/machine-id</filename> in the container is
unpopulated.</para></listitem>
</varlistentry>
</variablelist>
</refsect2><refsect2>
<title>Property Options</title>
<variablelist>
<varlistentry>
<term><option>-S</option></term>
<term><option>--slice=</option></term>
<listitem><para>Make the container part of the specified slice, instead of the default
<filename>machine.slice</filename>. This applies only if the machine is run in its own scope unit, i.e. if
<option>--keep-unit</option> is not used.</para>
<xi:include href="version-info.xml" xpointer="v206"/>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--property=</option></term>
<listitem><para>Set a unit property on the scope unit to register for the machine. This applies only if the
machine is run in its own scope unit, i.e. if <option>--keep-unit</option> is not used. Takes unit property
assignments in the same format as <command>systemctl set-property</command>. This is useful to set memory
limits and similar for the container.</para>
<xi:include href="version-info.xml" xpointer="v220"/>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--register=</option></term>
<listitem><para>Controls whether the container is registered with
<citerefentry><refentrytitle>systemd-machined</refentrytitle><manvolnum>8</manvolnum></citerefentry>. Takes a
boolean argument or <literal>auto</literal>, and defaults to <literal>auto</literal>. This option should be
enabled when the container runs a full Operating System (more specifically: a system and service manager as
PID 1), and is useful to ensure that the container is accessible via
<citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry> and shown by
tools such as <citerefentry
project='man-pages'><refentrytitle>ps</refentrytitle><manvolnum>1</manvolnum></citerefentry>. If the container
does not run a service manager, it is recommended to set this option to <literal>no</literal>. When set to
<literal>auto</literal>, registration is attempted but failures are ignored.</para>
<xi:include href="version-info.xml" xpointer="v209"/></listitem>
</varlistentry>
<varlistentry>
<term><option>--keep-unit</option></term>
<listitem><para>Instead of creating a transient scope unit to run the container in, simply use the service or
scope unit <command>systemd-nspawn</command> has been invoked in. If <option>--register=yes</option> is set
this unit is registered with
<citerefentry><refentrytitle>systemd-machined</refentrytitle><manvolnum>8</manvolnum></citerefentry>. This
switch should be used if <command>systemd-nspawn</command> is invoked from within a service unit, and the
service unit's sole purpose is to run a single <command>systemd-nspawn</command> container. This option is not
available if run from a user session.</para>
<para>Note that passing <option>--keep-unit</option> disables the effect of <option>--slice=</option> and
<option>--property=</option>. Use <option>--keep-unit</option> and <option>--register=no</option> in
combination to disable any kind of unit allocation or registration with
<command>systemd-machined</command>.</para>
<xi:include href="version-info.xml" xpointer="v209"/></listitem>
</varlistentry>
</variablelist>
</refsect2><refsect2>
<title>User Namespacing Options</title>
<variablelist>
<varlistentry>
<term><option>--private-users=</option></term>
<listitem><para>Controls user namespacing. If enabled, the container will run with its own private set of UNIX
user and group ids (UIDs and GIDs). This involves mapping the private UIDs/GIDs used in the container (starting
with the container's root user 0 and up) to a range of UIDs/GIDs on the host that are not used for other
purposes (usually in the range beyond the host's UID/GID 65536). The parameter may be specified as follows:</para>
<orderedlist>
<listitem><para>If one or two colon-separated numbers are specified, user namespacing is turned on. The first
parameter specifies the first host UID/GID to assign to the container, the second parameter specifies the
number of host UIDs/GIDs to assign to the container. If the second parameter is omitted, 65536 UIDs/GIDs are
assigned.</para></listitem>
<listitem><para>If the parameter is <literal>yes</literal>, user namespacing is turned on. The
UID/GID range to use is determined automatically from the file ownership of the root directory of
the container's directory tree. To use this option, make sure to prepare the directory tree in
advance, and ensure that all files and directories in it are owned by UIDs/GIDs in the range you'd
like to use. Also, make sure that used file ACLs exclusively reference UIDs/GIDs in the appropriate
range. In this mode, the number of UIDs/GIDs assigned to the container is 65536, and the owner
UID/GID of the root directory must be a multiple of 65536.</para></listitem>
<listitem><para>The special value <literal>pick</literal> turns on user namespacing. In this case
the UID/GID range is automatically chosen. As first step, the file owner UID/GID of the root
directory of the container's directory tree is read, and it is checked that no other container is
currently using it. If this check is successful, the UID/GID range determined this way is used,
similarly to the behavior if <literal>yes</literal> is specified. If the check is not successful
(and thus the UID/GID range indicated in the root directory's file owner is already used elsewhere)
a new – currently unused – UID/GID range of 65536 UIDs/GIDs is randomly chosen between the host
UID/GIDs of 524288 and 1878982656, always starting at a multiple of 65536, and, if possible,
consistently hashed from the machine name. This setting implies
<option>--private-users-ownership=auto</option> (see below), which possibly has the effect that the
files and directories in the container's directory tree will be owned by the appropriate users of
the range picked. Using this option makes user namespace behavior fully automatic. Note that the
first invocation of a previously unused container image might result in picking a new UID/GID range
for it, and thus in the (possibly expensive) file ownership adjustment operation. However,
subsequent invocations of the container will be cheap (unless of course the picked UID/GID range is
assigned to a different use by then).</para></listitem>
<listitem><para>If the parameter is <literal>no</literal>, user namespacing is turned off. This is
the default when <command>systemd-nspawn</command> is invoked directly. (Note that the
<filename>systemd-nspawn@.service</filename> unit enables private users.) This option is not
secure and must not be used to run untrusted code.</para></listitem>
<listitem><para>If the parameter is <literal>identity</literal>, user namespacing is employed with
an identity mapping for the first 65536 UIDs/GIDs. This is mostly equivalent to
<option>--private-users=0:65536</option>. While it does not provide UID/GID isolation, since all
host and container UIDs/GIDs are chosen identically it does provide process capability isolation,
but may be useful if proper user namespacing with distinct UID maps is not possible. This option is
not secure and must not be used to run untrusted code.</para></listitem>
<listitem><para>If the parameter is <literal>managed</literal>, user namespacing is employed with
in <emphasis>managed</emphasis> mode, i.e. allocation of a UID range is delegated to
<citerefentry><refentrytitle>systemd-nsresourced.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>. This
mode is selected by default if invoked unprivileged, but can also be requested explicitly when
privileged. In this mode a 64K UID range is automatically picked.</para></listitem>
</orderedlist>
<para>It is recommended to assign at least 65536 UIDs/GIDs to each container, so that the usable
UID/GID range in the container covers 16 bits. For best security, do not assign overlapping UID/GID
ranges to multiple containers. It is hence a good idea to use the upper 16 bit of the host 32-bit
UIDs/GIDs as container identifier, while the lower 16 bits encode the container UID/GID used. This is
in fact the behavior enforced by the <option>--private-users=pick</option> option.</para>
<para>When user namespaces are used, the GID range assigned to each container is always chosen
identical to the UID range.</para>
<para>In most cases, <option>--private-users=managed</option> (or when privileged
<option>--private-users=pick</option>, too) is the recommended option as user
namespacing is advised for security, and this option massively enhances container security while
operating fully automatically in most cases.</para>
<para>Note that the picked UID/GID range is not written to <filename>/etc/passwd</filename> or
<filename>/etc/group</filename>. In fact, the allocation of the range is not stored persistently,
except possibly in the file ownership of the files and directories of the container, see
<option>--private-users-ownership=</option>.</para>
<para>Note that when user namespacing is used without UID mapping (see below) file ownership on disk
reflects this, and all of the container's files and directories are owned by the container's
effective user and group IDs. This means that copying files from and to the container image requires
correction of the numeric UID/GID values, according to the UID/GID shift applied.</para>
<para>Note that for fully unprivileged operation in <literal>managed</literal> mode, any directory
image should be owned by the foreign UID range.</para>
<xi:include href="version-info.xml" xpointer="v220"/></listitem>
</varlistentry>
<varlistentry>
<term><option>--private-users-ownership=</option></term>
<listitem><para>Controls how to adjust the container image's UIDs and GIDs to match the UID/GID range
chosen with <option>--private-users=</option>, see above. Takes one of <literal>off</literal> (to
leave the image as is), <literal>chown</literal> (to recursively <function>chown()</function> the
container's directory tree as needed), <literal>map</literal> (in order to use transparent ID mapping
mounts from UID 0 to the target UID range), <literal>foreign</literal> (the same, but from the
foreign UID range base) or <literal>auto</literal> for automatically using <literal>map</literal> or
<literal>foreign</literal>, where available and applicable and <literal>chown</literal> where
not.</para>
<para>If <literal>chown</literal> is selected, all files and directories in the container's directory
tree will be adjusted so that they are owned by the appropriate UIDs/GIDs selected for the container
(see above). This operation is potentially expensive, as it involves iterating through the full
directory tree of the container. Besides actual file ownership, file ACLs are adjusted as
well.</para>
<para>Typically <literal>foreign</literal> or <literal>map</literal> is the best choice, since it
transparently maps UIDs/GIDs in memory as needed without modifying the image, and without requiring
an expensive recursive adjustment operation. However, it is not available for all file systems,
currently.</para>
<para>The <option>--private-users-ownership=auto</option> option is implied if
<option>--private-users=pick</option> or <option>--private-users=managed</option> is used. This
option has no effect if user namespacing is not used.</para>
<para><citerefentry><refentrytitle>systemd-dissect</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
<option>--shift</option> switch may be used to shift UID/GID ownership from or to the 0, foreign or
specific container UID/GID base outside of any <command>systemd-nspawn</command> invocation.</para>
<xi:include href="version-info.xml" xpointer="v230"/></listitem>
</varlistentry>
<varlistentry>
<term><option>--private-users-delegate=</option></term>
<listitem><para>Takes a non-negative integer. Requests that the specified number of additional 64K
UID/GID ranges are delegated into the container's user namespace. These delegated ranges are mapped
1:1 (i.e. the same UID/GID values are used inside and outside the user namespace) and can be used by
nested containers to allocate their own transient UID/GID ranges via
<citerefentry><refentrytitle>systemd-nsresourced.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>.</para>
<para>This option requires <option>--private-users=managed</option>, as the delegation is performed
by
<citerefentry><refentrytitle>systemd-nsresourced.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>
as part of the user namespace allocation. The maximum number of delegated ranges is 16. Defaults to
0, i.e. no delegation.</para>
<para>When this option is used with a non-zero value, the
<citerefentry><refentrytitle>systemd-nsresourced.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>
Varlink socket (<filename>/run/systemd/io.systemd.NamespaceResource</filename>) is automatically
bind-mounted into the container along with the necessary discovery symlinks in
<filename>/run/systemd/userdb/</filename> and <filename>/run/varlink/registry/</filename>. This
allows processes inside the container to contact
<command>systemd-nsresourced</command> on the host in order to allocate nested user namespaces from
the delegated ranges.</para>
<xi:include href="version-info.xml" xpointer="v260"/></listitem>
</varlistentry>
<varlistentry>
<term><option>-U</option></term>
<listitem><para>If the kernel supports the user namespaces feature, equivalent to
<option>--private-users=pick --private-users-ownership=auto</option>, otherwise equivalent to
<option>--private-users=no</option>.</para>
<para>Note that <option>-U</option> is the default if the