chiark / gitweb /
nspawn: newer kernels (>= 3.14) allow resetting the audit loginuid, make use of this
[elogind.git] / man / systemd-nspawn.xml
1 <?xml version='1.0'?> <!--*-nxml-*-->
2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3         "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
4
5 <!--
6   This file is part of systemd.
7
8   Copyright 2010 Lennart Poettering
9
10   systemd is free software; you can redistribute it and/or modify it
11   under the terms of the GNU Lesser General Public License as published by
12   the Free Software Foundation; either version 2.1 of the License, or
13   (at your option) any later version.
14
15   systemd is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public License
21   along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 -->
23
24 <refentry id="systemd-nspawn">
25
26         <refentryinfo>
27                 <title>systemd-nspawn</title>
28                 <productname>systemd</productname>
29
30                 <authorgroup>
31                         <author>
32                                 <contrib>Developer</contrib>
33                                 <firstname>Lennart</firstname>
34                                 <surname>Poettering</surname>
35                                 <email>lennart@poettering.net</email>
36                         </author>
37                 </authorgroup>
38         </refentryinfo>
39
40         <refmeta>
41                 <refentrytitle>systemd-nspawn</refentrytitle>
42                 <manvolnum>1</manvolnum>
43         </refmeta>
44
45         <refnamediv>
46                 <refname>systemd-nspawn</refname>
47                 <refpurpose>Spawn a namespace container for debugging, testing and building</refpurpose>
48         </refnamediv>
49
50         <refsynopsisdiv>
51                 <cmdsynopsis>
52                         <command>systemd-nspawn</command>
53                         <arg choice="opt" rep="repeat">OPTIONS</arg>
54                         <arg choice="opt"><replaceable>COMMAND</replaceable>
55                         <arg choice="opt" rep="repeat">ARGS</arg>
56                         </arg>
57                 </cmdsynopsis>
58                 <cmdsynopsis>
59                         <command>systemd-nspawn</command>
60                         <arg choice="plain">-b</arg>
61                         <arg choice="opt" rep="repeat">OPTIONS</arg>
62                         <arg choice="opt" rep="repeat">ARGS</arg>
63                 </cmdsynopsis>
64         </refsynopsisdiv>
65
66         <refsect1>
67                 <title>Description</title>
68
69                 <para><command>systemd-nspawn</command> may be used to
70                 run a command or OS in a light-weight namespace
71                 container. In many ways it is similar to
72                 <citerefentry><refentrytitle>chroot</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
73                 but more powerful since it fully virtualizes the file
74                 system hierarchy, as well as the process tree, the
75                 various IPC subsystems and the host and domain
76                 name.</para>
77
78                 <para><command>systemd-nspawn</command> limits access
79                 to various kernel interfaces in the container to
80                 read-only, such as <filename>/sys</filename>,
81                 <filename>/proc/sys</filename> or
82                 <filename>/sys/fs/selinux</filename>. Network
83                 interfaces and the system clock may not be changed
84                 from within the container. Device nodes may not be
85                 created. The host system cannot be rebooted and kernel
86                 modules may not be loaded from within the
87                 container.</para>
88
89                 <para>Note that even though these security precautions
90                 are taken <command>systemd-nspawn</command> is not
91                 suitable for secure container setups. Many of the
92                 security features may be circumvented and are hence
93                 primarily useful to avoid accidental changes to the
94                 host system from the container. The intended use of
95                 this program is debugging and testing as well as
96                 building of packages, distributions and software
97                 involved with boot and systems management.</para>
98
99                 <para>In contrast to
100                 <citerefentry><refentrytitle>chroot</refentrytitle><manvolnum>1</manvolnum></citerefentry> <command>systemd-nspawn</command>
101                 may be used to boot full Linux-based operating systems
102                 in a container.</para>
103
104                 <para>Use a tool like
105                 <citerefentry><refentrytitle>yum</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
106                 <citerefentry><refentrytitle>debootstrap</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
107                 or
108                 <citerefentry><refentrytitle>pacman</refentrytitle><manvolnum>8</manvolnum></citerefentry>
109                 to set up an OS directory tree suitable as file system
110                 hierarchy for <command>systemd-nspawn</command>
111                 containers.</para>
112
113                 <para>Note that <command>systemd-nspawn</command> will
114                 mount file systems private to the container to
115                 <filename>/dev</filename>,
116                 <filename>/run</filename> and similar. These will
117                 not be visible outside of the container, and their
118                 contents will be lost when the container exits.</para>
119
120                 <para>Note that running two
121                 <command>systemd-nspawn</command> containers from the
122                 same directory tree will not make processes in them
123                 see each other. The PID namespace separation of the
124                 two containers is complete and the containers will
125                 share very few runtime objects except for the
126                 underlying file system. Use
127                 <citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
128                 <command>login</command> command to request an
129                 additional login prompt in a running container.</para>
130
131                 <para><command>systemd-nspawn</command> implements the
132                 <ulink
133                 url="http://www.freedesktop.org/wiki/Software/systemd/ContainerInterface">Container
134                 Interface</ulink> specification.</para>
135
136                 <para>As a safety check
137                 <command>systemd-nspawn</command> will verify the
138                 existence of <filename>/etc/os-release</filename> in
139                 the container tree before starting the container (see
140                 <citerefentry><refentrytitle>os-release</refentrytitle><manvolnum>5</manvolnum></citerefentry>). It
141                 might be necessary to add this file to the container
142                 tree manually if the OS of the container is too old to
143                 contain this file out-of-the-box.</para>
144         </refsect1>
145
146         <refsect1>
147                 <title>Options</title>
148
149                 <para>If option <option>-b</option> is specified, the
150                 arguments are used as arguments for the init
151                 binary. Otherwise, <replaceable>COMMAND</replaceable>
152                 specifies the program to launch in the container, and
153                 the remaining arguments are used as arguments for this
154                 program. If <option>-b</option> is not used and no
155                 arguments are specifed, a shell is launched in the
156                 container.</para>
157
158                 <para>The following options are understood:</para>
159
160                 <variablelist>
161                         <varlistentry>
162                                 <term><option>-h</option></term>
163                                 <term><option>--help</option></term>
164
165                                 <listitem><para>Prints a short help
166                                 text and exits.</para></listitem>
167                         </varlistentry>
168
169                         <varlistentry>
170                                 <term><option>--version</option></term>
171
172                                 <listitem><para>Prints a version string
173                                 and exits.</para></listitem>
174                         </varlistentry>
175
176                         <varlistentry>
177                                 <term><option>-D</option></term>
178                                 <term><option>--directory=</option></term>
179
180                                 <listitem><para>Directory to use as
181                                 file system root for the namespace
182                                 container. If omitted, the current
183                                 directory will be
184                                 used.</para></listitem>
185                         </varlistentry>
186
187                         <varlistentry>
188                                 <term><option>-b</option></term>
189                                 <term><option>--boot</option></term>
190
191                                 <listitem><para>Automatically search
192                                 for an init binary and invoke it
193                                 instead of a shell or a user supplied
194                                 program. If this option is used,
195                                 arguments specified on the command
196                                 line are used as arguments for the
197                                 init binary. This option may not be
198                                 combined with
199                                 <option>--share-system</option>.
200                                 </para></listitem>
201                         </varlistentry>
202
203                         <varlistentry>
204                                 <term><option>-u</option></term>
205                                 <term><option>--user=</option></term>
206
207                                 <listitem><para>Run the command
208                                 under specified user, create home
209                                 directory and cd into it. As rest
210                                 of systemd-nspawn, this is not
211                                 the security feature and limits
212                                 against accidental changes only.
213                                 </para></listitem>
214                         </varlistentry>
215
216                         <varlistentry>
217                                 <term><option>-M</option></term>
218                                 <term><option>--machine=</option></term>
219
220                                 <listitem><para>Sets the machine name
221                                 for this container. This name may be
222                                 used to identify this container on the
223                                 host, and is used to initialize the
224                                 container's hostname (which the
225                                 container can choose to override,
226                                 however). If not specified, the last
227                                 component of the root directory of the
228                                 container is used.</para></listitem>
229                         </varlistentry>
230
231                         <varlistentry>
232                                 <term><option>--slice=</option></term>
233
234                                 <listitem><para>Make the container
235                                 part of the specified slice, instead
236                                 of the
237                                 <filename>machine.slice</filename>.</para>
238                                 </listitem>
239                         </varlistentry>
240
241                         <varlistentry>
242                                 <term><option>-Z</option></term>
243                                 <term><option>--selinux-context=</option></term>
244
245                                 <listitem><para>Sets the SELinux
246                                 security context to be used to label
247                                 processes in the container.</para>
248                                 </listitem>
249                         </varlistentry>
250
251                         <varlistentry>
252                                 <term><option>-L</option></term>
253                                 <term><option>--selinux-apifs-context=</option></term>
254
255                                 <listitem><para>Sets the SELinux security
256                                 context to be used to label files in
257                                 the virtual API file systems in the
258                                 container.</para>
259                                 </listitem>
260                         </varlistentry>
261
262                         <varlistentry>
263                                 <term><option>--uuid=</option></term>
264
265                                 <listitem><para>Set the specified UUID
266                                 for the container. The init system
267                                 will initialize
268                                 <filename>/etc/machine-id</filename>
269                                 from this if this file is not set yet.
270                                 </para></listitem>
271                         </varlistentry>
272
273                         <varlistentry>
274                                 <term><option>--private-network</option></term>
275
276                                 <listitem><para>Turn off networking in
277                                 the container. This makes all network
278                                 interfaces unavailable in the
279                                 container, with the exception of the
280                                 loopback device.</para></listitem>
281                         </varlistentry>
282
283                         <varlistentry>
284                                 <term><option>--read-only</option></term>
285
286                                 <listitem><para>Mount the root file
287                                 system read-only for the
288                                 container.</para></listitem>
289                         </varlistentry>
290
291                         <varlistentry>
292                                 <term><option>--capability=</option></term>
293
294                                 <listitem><para>List one or more
295                                 additional capabilities to grant the
296                                 container. Takes a comma-separated
297                                 list of capability names, see
298                                 <citerefentry><refentrytitle>capabilities</refentrytitle><manvolnum>7</manvolnum></citerefentry>
299                                 for more information. Note that the
300                                 following capabilities will be granted
301                                 in any way: CAP_CHOWN,
302                                 CAP_DAC_OVERRIDE, CAP_DAC_READ_SEARCH,
303                                 CAP_FOWNER, CAP_FSETID, CAP_IPC_OWNER,
304                                 CAP_KILL, CAP_LEASE,
305                                 CAP_LINUX_IMMUTABLE,
306                                 CAP_NET_BIND_SERVICE,
307                                 CAP_NET_BROADCAST, CAP_NET_RAW,
308                                 CAP_SETGID, CAP_SETFCAP, CAP_SETPCAP,
309                                 CAP_SETUID, CAP_SYS_ADMIN,
310                                 CAP_SYS_CHROOT, CAP_SYS_NICE,
311                                 CAP_SYS_PTRACE, CAP_SYS_TTY_CONFIG,
312                                 CAP_SYS_RESOURCE, CAP_SYS_BOOT,
313                                 CAP_AUDIT_WRITE,
314                                 CAP_AUDIT_CONTROL.</para></listitem>
315                         </varlistentry>
316
317                         <varlistentry>
318                                 <term><option>--drop-capability=</option></term>
319
320                                 <listitem><para>Specify one or more
321                                 additional capabilities to drop for
322                                 the container. This allows running the
323                                 container with fewer capabilities than
324                                 the default (see above).</para></listitem>
325                         </varlistentry>
326
327                         <varlistentry>
328                                 <term><option>--link-journal=</option></term>
329
330                                 <listitem><para>Control whether the
331                                 container's journal shall be made
332                                 visible to the host system. If enabled,
333                                 allows viewing the container's journal
334                                 files from the host (but not vice
335                                 versa). Takes one of
336                                 <literal>no</literal>,
337                                 <literal>host</literal>,
338                                 <literal>guest</literal>,
339                                 <literal>auto</literal>. If
340                                 <literal>no</literal>, the journal is
341                                 not linked. If <literal>host</literal>,
342                                 the journal files are stored on the
343                                 host file system (beneath
344                                 <filename>/var/log/journal/<replaceable>machine-id</replaceable></filename>)
345                                 and the subdirectory is bind-mounted
346                                 into the container at the same
347                                 location. If <literal>guest</literal>,
348                                 the journal files are stored on the
349                                 guest file system (beneath
350                                 <filename>/var/log/journal/<replaceable>machine-id</replaceable></filename>)
351                                 and the subdirectory is symlinked into the host
352                                 at the same location. If
353                                 <literal>auto</literal> (the default),
354                                 and the right subdirectory of
355                                 <filename>/var/log/journal</filename>
356                                 exists, it will be bind mounted
357                                 into the container. If the
358                                 subdirectory does not exist, no
359                                 linking is performed. Effectively,
360                                 booting a container once with
361                                 <literal>guest</literal> or
362                                 <literal>host</literal> will link the
363                                 journal persistently if further on
364                                 the default of <literal>auto</literal>
365                                 is used.</para></listitem>
366                         </varlistentry>
367
368                         <varlistentry>
369                                 <term><option>-j</option></term>
370
371                                 <listitem><para>Equivalent to
372                                 <option>--link-journal=guest</option>.</para></listitem>
373                         </varlistentry>
374
375                         <varlistentry>
376                                 <term><option>--bind=</option></term>
377                                 <term><option>--bind-ro=</option></term>
378
379                                 <listitem><para>Bind mount a file or
380                                 directory from the host into the
381                                 container. Either takes a path
382                                 argument -- in which case the
383                                 specified path will be mounted from
384                                 the host to the same path in the
385                                 container --, or a colon-separated
386                                 pair of paths -- in which case the
387                                 first specified path is the source in
388                                 the host, and the second path is the
389                                 destination in the container. The
390                                 <option>--bind-ro=</option> option
391                                 creates read-only bind
392                                 mount.</para></listitem>
393                         </varlistentry>
394
395                         <varlistentry>
396                                 <term><option>--setenv=</option></term>
397
398                                 <listitem><para>Specifies an
399                                 environment variable assignment to
400                                 pass to the init process in the
401                                 container, in the format
402                                 <literal>NAME=VALUE</literal>. This
403                                 may be used to override the default
404                                 variables or to set additional
405                                 variables. This parameter may be used
406                                 more than once.</para></listitem>
407                         </varlistentry>
408
409                         <varlistentry>
410                                 <term><option>-q</option></term>
411                                 <term><option>--quiet</option></term>
412
413                                 <listitem><para>Turns off any status
414                                 output by the tool itself. When this
415                                 switch is used, then the only output
416                                 by nspawn will be the console output
417                                 of the container OS itself.</para></listitem>
418                         </varlistentry>
419
420                         <varlistentry>
421                                 <term><option>--share-system</option></term>
422
423                                 <listitem><para>Allows the container
424                                 to share certain system facilities
425                                 with the host. More specifically, this
426                                 turns off PID namespacing, UTS
427                                 namespacing and IPC namespacing, and
428                                 thus allows the guest to see and
429                                 interact more easily with processes
430                                 outside of the container. Note that
431                                 using this option makes it impossible
432                                 to start up a full Operating System in
433                                 the container, as an init system
434                                 cannot operate in this mode. It is
435                                 only useful to run specific programs
436                                 or applications this way, without
437                                 involving an init system in the
438                                 container. This option implies
439                                 <option>--register=no</option>. This
440                                 option may not be combined with
441                                 <option>--boot</option>.</para></listitem>
442                         </varlistentry>
443
444                         <varlistentry>
445                                 <term><option>--register=</option></term>
446
447                                 <listitem><para>Controls whether the
448                                 container is registered with
449                                 <citerefentry><refentrytitle>systemd-machined</refentrytitle><manvolnum>8</manvolnum></citerefentry>. Takes
450                                 a boolean argument, defaults to
451                                 <literal>yes</literal>. This option
452                                 should be enabled when the container
453                                 runs a full Operating System (more
454                                 specifically: an init system), and is
455                                 useful to ensure that the container is
456                                 accessible via
457                                 <citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
458                                 and shown by tools such as
459                                 <citerefentry><refentrytitle>ps</refentrytitle><manvolnum>1</manvolnum></citerefentry>. If
460                                 the container does not run an init
461                                 system it is recommended to set this
462                                 option to <literal>no</literal>. Note
463                                 that <option>--share-system</option>
464                                 implies
465                                 <option>--register=no</option>.
466                                 </para></listitem>
467                         </varlistentry>
468
469                         <varlistentry>
470                                 <term><option>--keep-unit</option></term>
471
472                                 <listitem><para>Instead of creating a
473                                 transient scope unit to run the
474                                 container in, simply register the
475                                 service or scope unit
476                                 <command>systemd-nspawn</command> has
477                                 been invoked in in
478                                 <citerefentry><refentrytitle>systemd-machined</refentrytitle><manvolnum>8</manvolnum></citerefentry>. This
479                                 has no effect if
480                                 <option>--register=no</option> is
481                                 used. This switch should be used if
482                                 <command>systemd-nspawn</command> is
483                                 invoked from within an a service unit,
484                                 and the service unit's sole purpose
485                                 is to run a single
486                                 <command>systemd-nspawn</command>
487                                 container. This option is not
488                                 available if run from a user
489                                 session.</para></listitem>
490                         </varlistentry>
491
492                 </variablelist>
493
494         </refsect1>
495
496         <refsect1>
497                 <title>Example 1</title>
498
499                 <programlisting># yum -y --releasever=19 --nogpg --installroot=/srv/mycontainer --disablerepo='*' --enablerepo=fedora install systemd passwd yum fedora-release vim-minimal
500 # systemd-nspawn -bD /srv/mycontainer</programlisting>
501
502                 <para>This installs a minimal Fedora distribution into
503                 the directory <filename noindex='true'>/srv/mycontainer/</filename> and
504                 then boots an OS in a namespace container in
505                 it.</para>
506         </refsect1>
507
508         <refsect1>
509                 <title>Example 2</title>
510
511                 <programlisting># debootstrap --arch=amd64 unstable ~/debian-tree/
512 # systemd-nspawn -D ~/debian-tree/</programlisting>
513
514                 <para>This installs a minimal Debian unstable
515                 distribution into the directory
516                 <filename>~/debian-tree/</filename> and then spawns a
517                 shell in a namespace container in it.</para>
518         </refsect1>
519
520         <refsect1>
521                 <title>Example 3</title>
522
523                 <programlisting># pacstrap -c -d ~/arch-tree/ base
524 # systemd-nspawn -bD ~/arch-tree/</programlisting>
525
526                 <para>This installs a mimimal Arch Linux distribution into
527                 the directory <filename>~/arch-tree/</filename> and then
528                 boots an OS in a namespace container in it.</para>
529         </refsect1>
530
531         <refsect1>
532                 <title>Example 4</title>
533
534                 <programlisting># mv ~/arch-tree /var/lib/container/arch
535 # systemctl enable systemd-nspawn@arch.service
536 # systemctl start systemd-nspawn@arch.service</programlisting>
537
538                 <para>This makes the Arch Linux container part of the
539                 <filename>multi-user.target</filename> on the host.
540                 </para>
541         </refsect1>
542
543         <refsect1>
544                 <title>Example 5</title>
545
546                 <programlisting># btrfs subvolume snapshot / /.tmp
547 # systemd-nspawn --private-network -D /.tmp -b</programlisting>
548
549                 <para>This runs a copy of the host system in a
550                 btrfs snapshot.</para>
551         </refsect1>
552
553         <refsect1>
554                 <title>Example 6</title>
555
556                 <programlisting># chcon system_u:object_r:svirt_sandbox_file_t:s0:c0,c1 -R /srv/container
557 # systemd-nspawn -L system_u:object_r:svirt_sandbox_file_t:s0:c0,c1 -Z system_u:system_r:svirt_lxc_net_t:s0:c0,c1 -D /srv/container /bin/sh</programlisting>
558
559                 <para>This runs a container with SELinux sandbox security contexts.</para>
560         </refsect1>
561
562         <refsect1>
563                 <title>Exit status</title>
564
565                 <para>The exit code of the program executed in the
566                 container is returned.</para>
567         </refsect1>
568
569         <refsect1>
570                 <title>See Also</title>
571                 <para>
572                         <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
573                         <citerefentry><refentrytitle>chroot</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
574                         <citerefentry><refentrytitle>yum</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
575                         <citerefentry><refentrytitle>debootstrap</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
576                         <citerefentry><refentrytitle>pacman</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
577                         <citerefentry><refentrytitle>systemd.slice</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
578                         <citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
579                 </para>
580         </refsect1>
581
582 </refentry>