xref: /freebsd/usr.sbin/jail/jail.8 (revision fa9896e082a1046ff4fbc75fcba4d18d1f2efc19)
1.\" Copyright (c) 2000, 2003 Robert N. M. Watson
2.\" Copyright (c) 2008-2012 James Gritton
3.\" All rights reserved.
4.\"
5.\" Redistribution and use in source and binary forms, with or without
6.\" modification, are permitted provided that the following conditions
7.\" are met:
8.\" 1. Redistributions of source code must retain the above copyright
9.\"    notice, this list of conditions and the following disclaimer.
10.\" 2. Redistributions in binary form must reproduce the above copyright
11.\"    notice, this list of conditions and the following disclaimer in the
12.\"    documentation and/or other materials provided with the distribution.
13.\"
14.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24.\" SUCH DAMAGE.
25.\"
26.Dd March 12, 2023
27.Dt JAIL 8
28.Os
29.Sh NAME
30.Nm jail
31.Nd "manage system jails"
32.Sh SYNOPSIS
33.Nm
34.Op Fl dhilqv
35.Op Fl J Ar jid_file
36.Op Fl u Ar username
37.Op Fl U Ar username
38.Op Fl cmr
39.Ar param Ns = Ns Ar value ...
40.Op Cm command Ns = Ns Ar command ...
41.Nm
42.Op Fl dqv
43.Op Fl f Ar conf_file
44.Op Fl p Ar limit
45.Op Fl cmr
46.Op Ar jail
47.Nm
48.Op Fl qv
49.Op Fl f Ar conf_file
50.Op Fl rR
51.Op Cm * | Ar jail ...
52.Nm
53.Op Fl dhilqv
54.Op Fl J Ar jid_file
55.Op Fl u Ar username
56.Op Fl U Ar username
57.Op Fl n Ar jailname
58.Op Fl s Ar securelevel
59.Ar path hostname ip Ns Op Cm \&, Ns Ar ...
60.Ar command ...
61.Nm
62.Op Fl f Ar conf_file
63.Fl e
64.Ar separator
65.Sh DESCRIPTION
66The
67.Nm
68utility creates new jails, or modifies or removes existing jails.
69It can also print a list of configured jails and their parameters.
70A jail
71.Pq or Dq prison
72is specified via parameters on the command line, or in the
73.Xr jail.conf 5
74file.
75.Pp
76At least one of the options
77.Fl c ,
78.Fl e ,
79.Fl m
80or
81.Fl r
82must be specified.
83These options are used alone or in combination to describe the operation to
84perform:
85.Bl -tag -width indent
86.It Fl c
87Create a new jail.
88The jail
89.Va jid
90and
91.Va name
92parameters (if specified on the command line)
93must not refer to an existing jail.
94.It Fl e Ar separator
95Exhibit a list of all configured non-wildcard jails and their parameters.
96No jail creation, modification or removal performed if this option is used.
97The
98.Ar separator
99string is used to separate parameters.
100Use
101.Xr jls 8
102utility to list running jails.
103.It Fl m
104Modify an existing jail.
105One of the
106.Va jid
107or
108.Va name
109parameters must exist and refer to an existing jail.
110Some parameters may not be changed on a running jail.
111.It Fl r
112Remove the
113.Ar jail
114specified by jid or name.
115All jailed processes are killed, and all jails that are
116children of this jail are also
117removed.
118.It Fl rc
119Restart an existing jail.
120The jail is first removed and then re-created, as if
121.Dq Nm Fl r
122and
123.Dq Nm Fl c
124were run in succession.
125.It Fl cm
126Create a jail if it does not exist, or modify the jail if it does exist.
127.It Fl mr
128Modify an existing jail.
129The jail may be restarted if necessary to modify parameters than could
130not otherwise be changed.
131.It Fl cmr
132Create a jail if it doesn't exist, or modify (and possibly restart) the
133jail if it does exist.
134.El
135.Pp
136Other available options are:
137.Bl -tag -width indent
138.It Fl d
139Allow making changes to a dying jail, equivalent to the
140.Va allow.dying
141parameter.
142.It Fl f Ar conf_file
143Use configuration file
144.Ar conf_file
145instead of the default
146.Pa /etc/jail.conf .
147.It Fl h
148Resolve the
149.Va host.hostname
150parameter (or
151.Va hostname )
152and add all IP addresses returned by the resolver
153to the list of addresses for this jail.
154This is equivalent to the
155.Va ip_hostname
156parameter.
157.It Fl i
158Output (only) the jail identifier of the newly created jail(s).
159This implies the
160.Fl q
161option.
162.It Fl J Ar jid_file
163Write a
164.Ar jid_file
165file, containing the parameters used to start the jail.
166.It Fl l
167Run commands in a clean environment.
168This is deprecated and is equivalent to the exec.clean parameter.
169.It Fl n Ar jailname
170Set the jail's name.
171This is deprecated and is equivalent to the
172.Va name
173parameter.
174.It Fl p Ar limit
175Limit the number of commands from
176.Va  exec.*
177that can run simultaneously.
178.It Fl q
179Suppress the message printed whenever a jail is created, modified or removed.
180Only error messages will be printed.
181.It Fl R
182A variation of the
183.Fl r
184option that removes an existing jail without using the configuration file.
185No removal-related parameters for this jail will be used \(em the jail will
186simply be removed.
187.It Fl s Ar securelevel
188Set the
189.Va kern.securelevel
190MIB entry to the specified value inside the newly created jail.
191This is deprecated and is equivalent to the
192.Va securelevel
193parameter.
194.It Fl u Ar username
195The user name from host environment as whom jailed commands should run.
196This is deprecated and is equivalent to the
197.Va exec.jail_user
198and
199.Va exec.system_jail_user
200parameters.
201.It Fl U Ar username
202The user name from the jailed environment as whom jailed commands should run.
203This is deprecated and is equivalent to the
204.Va exec.jail_user
205parameter.
206.It Fl v
207Print a message on every operation, such as running commands and
208mounting filesystems.
209.El
210.Pp
211If no arguments are given after the options, the operation (except
212remove) will be performed on all jails specified in the
213.Xr jail.conf 5
214file.
215A single argument of a jail name will operate only on the specified jail.
216The
217.Fl r
218and
219.Fl R
220options can also remove running jails that aren't in the
221.Xr jail.conf 5
222file, specified by name or jid.
223.Pp
224An argument of
225.Dq *
226is a wildcard that will operate on all jails, regardless of whether
227they appear in
228.Xr jail.conf 5 ;
229this is the surest way for
230.Fl r
231to remove all jails.
232If hierarchical jails exist, a partial-matching wildcard definition may
233be specified.
234For example, an argument of
235.Dq foo.*
236would apply to jails with names like
237.Dq foo.bar
238and
239.Dq foo.bar.baz .
240.Pp
241A jail may be specified with parameters directly on the command line.
242In this case, the
243.Xr jail.conf 5
244file will not be used.
245For backward compatibility, the command line may also have four fixed
246parameters, without names:
247.Ar path ,
248.Ar hostname ,
249.Ar ip ,
250and
251.Ar command .
252This mode will always create a new jail, and the
253.Fl c
254and
255.Fl m
256options do not apply (and must not be present).
257.Ss Jail Parameters
258Parameters in the
259.Xr jail.conf 5
260file, or on the command line, are generally of the form
261.Dq name=value .
262Some parameters are boolean, and do not have a value but are set by the
263name alone with or without a
264.Dq no
265prefix, e.g.
266.Va persist
267or
268.Va nopersist .
269They can also be given the values
270.Dq true
271and
272.Dq false .
273Other parameters may have more than one value, specified as a
274comma-separated list or with
275.Dq +=
276in the configuration file (see
277.Xr jail.conf 5
278for details).
279.Pp
280The
281.Nm
282utility recognizes two classes of parameters.
283There are the true jail
284parameters that are passed to the kernel when the jail is created,
285which can be seen with
286.Xr jls 8 ,
287and can (usually) be changed with
288.Dq Nm Fl m .
289Then there are pseudo-parameters that are only used by
290.Nm
291itself.
292.Pp
293Jails have a set of core parameters, and kernel modules can add their own
294jail parameters.
295The current set of available parameters can be retrieved via
296.Dq Nm sysctl Fl d Va security.jail.param .
297Any parameters not set will be given default values, often based on the
298current environment.
299The core parameters are:
300.Bl -tag -width indent
301.It Va jid
302The jail identifier.
303This will be assigned automatically to a new jail (or can be explicitly
304set), and can be used to identify the jail for later modification, or
305for such commands as
306.Xr jls 8
307or
308.Xr jexec 8 .
309.It Va name
310The jail name.
311This is an arbitrary string that identifies a jail (except it may not
312contain a
313.Sq \&. ) .
314Like the
315.Va jid ,
316it can be passed to later
317.Nm
318commands, or to
319.Xr jls 8
320or
321.Xr jexec 8 .
322If no
323.Va name
324is supplied, a default is assumed that is the same as the
325.Va jid .
326The
327.Va name
328parameter is implied by the
329.Xr jail.conf 5
330file format, and need not be explicitly set when using the configuration
331file.
332.It Va path
333The directory which is to be the root of the jail.
334Any commands run inside the jail, either by
335.Nm
336or from
337.Xr jexec 8 ,
338are run from this directory.
339.It Va ip4.addr
340A list of IPv4 addresses assigned to the jail.
341If this is set, the jail is restricted to using only these addresses.
342Any attempts to use other addresses fail, and attempts to use wildcard
343addresses silently use the jailed address instead.
344For IPv4 the first address given will be used as the source address
345when source address selection on unbound sockets cannot find a better
346match.
347It is only possible to start multiple jails with the same IP address
348if none of the jails has more than this single overlapping IP address
349assigned to itself.
350.It Va ip4.saddrsel
351A boolean option to change the formerly mentioned behaviour and disable
352IPv4 source address selection for the jail in favour of the primary
353IPv4 address of the jail.
354Source address selection is enabled by default for all jails and the
355.Va ip4.nosaddrsel
356setting of a parent jail is not inherited for any child jails.
357.It Va ip4
358Control the availability of IPv4 addresses.
359Possible values are
360.Dq inherit
361to allow unrestricted access to all system addresses,
362.Dq new
363to restrict addresses via
364.Va ip4.addr ,
365and
366.Dq disable
367to stop the jail from using IPv4 entirely.
368Setting the
369.Va ip4.addr
370parameter implies a value of
371.Dq new .
372.It Va ip6.addr , Va ip6.saddrsel , Va ip6
373A set of IPv6 options for the jail, the counterparts to
374.Va ip4.addr ,
375.Va ip4.saddrsel
376and
377.Va ip4
378above.
379.It Va vnet
380Create the jail with its own virtual network stack,
381with its own network interfaces, addresses, routing table, etc.
382The kernel must have been compiled with the
383.Sy VIMAGE option
384for this to be available.
385Possible values are
386.Dq inherit
387to use the system network stack, possibly with restricted IP addresses,
388and
389.Dq new
390to create a new network stack.
391.It Va host.hostname
392The hostname of the jail.
393Other similar parameters are
394.Va host.domainname ,
395.Va host.hostuuid
396and
397.Va host.hostid .
398.It Va host
399Set the origin of hostname and related information.
400Possible values are
401.Dq inherit
402to use the system information and
403.Dq new
404for the jail to use the information from the above fields.
405Setting any of the above fields implies a value of
406.Dq new .
407.It Va securelevel
408The value of the jail's
409.Va kern.securelevel
410sysctl.
411A jail never has a lower securelevel than its parent system, but by
412setting this parameter it may have a higher one.
413If the system securelevel is changed, any jail securelevels will be at
414least as secure.
415.It Va devfs_ruleset
416The number of the devfs ruleset that is enforced for mounting devfs in
417this jail.
418A value of zero (default) means no ruleset is enforced.
419Descendant jails inherit the parent jail's devfs ruleset enforcement.
420Mounting devfs inside a jail is possible only if the
421.Va allow.mount
422and
423.Va allow.mount.devfs
424permissions are effective and
425.Va enforce_statfs
426is set to a value lower than 2.
427Devfs rules and rulesets cannot be viewed or modified from inside a jail.
428.Pp
429NOTE: It is important that only appropriate device nodes in devfs be
430exposed to a jail; access to disk devices in the jail may permit processes
431in the jail to bypass the jail sandboxing by modifying files outside of
432the jail.
433See
434.Xr devfs 8
435for information on how to use devfs rules to limit access to entries
436in the per-jail devfs.
437A simple devfs ruleset for jails is available as ruleset #4 in
438.Pa /etc/defaults/devfs.rules .
439.It Va children.max
440The number of child jails allowed to be created by this jail (or by
441other jails under this jail).
442This limit is zero by default, indicating the jail is not allowed to
443create child jails.
444See the
445.Sx "Hierarchical Jails"
446section for more information.
447.It Va children.cur
448The number of descendants of this jail, including its own child jails
449and any jails created under them.
450.It Va enforce_statfs
451This determines what information processes in a jail are able to get
452about mount points.
453It affects the behaviour of the following syscalls:
454.Xr statfs 2 ,
455.Xr fstatfs 2 ,
456.Xr getfsstat 2 ,
457and
458.Xr fhstatfs 2
459(as well as similar compatibility syscalls).
460When set to 0, all mount points are available without any restrictions.
461When set to 1, only mount points below the jail's chroot directory are
462visible.
463In addition to that, the path to the jail's chroot directory is removed
464from the front of their pathnames.
465When set to 2 (default), above syscalls can operate only on a mount-point
466where the jail's chroot directory is located.
467.It Va persist
468Setting this boolean parameter allows a jail to exist without any
469processes.
470Normally, a command is run as part of jail creation, and then the jail
471is destroyed as its last process exits.
472A new jail must have either the
473.Va persist
474parameter or
475.Va exec.start
476or
477.Va command
478pseudo-parameter set.
479.It Va cpuset.id
480The ID of the cpuset associated with this jail (read-only).
481.It Va dying
482This is true if the jail is in the process of shutting down (read-only).
483.It Va parent
484The
485.Va jid
486of the parent of this jail, or zero if this is a top-level jail
487(read-only).
488.It Va osrelease
489The string for the jail's
490.Va kern.osrelease
491sysctl and uname -r.
492.It Va osreldate
493The number for the jail's
494.Va kern.osreldate
495and uname -K.
496.It Va allow.*
497Some restrictions of the jail environment may be set on a per-jail
498basis.
499With the exception of
500.Va allow.set_hostname
501and
502.Va allow.reserved_ports ,
503these boolean parameters are off by default.
504.Bl -tag -width indent
505.It Va allow.set_hostname
506The jail's hostname may be changed via
507.Xr hostname 1
508or
509.Xr sethostname 3 .
510.It Va allow.sysvipc
511A process within the jail has access to System V IPC primitives.
512This is deprecated in favor of the per-module parameters (see below).
513When this parameter is set, it is equivalent to setting
514.Va sysvmsg ,
515.Va sysvsem ,
516and
517.Va sysvshm
518all to
519.Dq inherit .
520.It Va allow.raw_sockets
521The jail root is allowed to create raw sockets.
522Setting this parameter allows utilities like
523.Xr ping 8
524and
525.Xr traceroute 8
526to operate inside the jail.
527If this is set, the source IP addresses are enforced to comply
528with the IP address bound to the jail, regardless of whether or not
529the
530.Dv IP_HDRINCL
531flag has been set on the socket.
532Since raw sockets can be used to configure and interact with various
533network subsystems, extra caution should be used where privileged access
534to jails is given out to untrusted parties.
535.It Va allow.chflags
536Normally, privileged users inside a jail are treated as unprivileged by
537.Xr chflags 2 .
538When this parameter is set, such users are treated as privileged, and
539may manipulate system file flags subject to the usual constraints on
540.Va kern.securelevel .
541.It Va allow.mount
542privileged users inside the jail will be able to mount and unmount file
543system types marked as jail-friendly.
544The
545.Xr lsvfs 1
546command can be used to find file system types available for mount from
547within a jail.
548This permission is effective only if
549.Va enforce_statfs
550is set to a value lower than 2.
551.It Va allow.mount.devfs
552privileged users inside the jail will be able to mount and unmount the
553devfs file system.
554This permission is effective only together with
555.Va allow.mount
556and only when
557.Va enforce_statfs
558is set to a value lower than 2.
559The devfs ruleset should be restricted from the default by using the
560.Va devfs_ruleset
561option.
562.It Va allow.quotas
563The jail root may administer quotas on the jail's filesystem(s).
564This includes filesystems that the jail may share with other jails or
565with non-jailed parts of the system.
566.It Va allow.read_msgbuf
567Jailed users may read the kernel message buffer.
568If the
569.Va security.bsd.unprivileged_read_msgbuf
570MIB entry is zero, this will be restricted to the root user.
571.It Va allow.socket_af
572Sockets within a jail are normally restricted to IPv4, IPv6, local
573(UNIX), and route.  This allows access to other protocol stacks that
574have not had jail functionality added to them.
575.It Va allow.mlock
576Locking or unlocking physical pages in memory are normally not available
577within a jail.
578When this parameter is set, users may
579.Xr mlock 2
580or
581.Xr munlock 2
582memory subject to
583.Va security.bsd.unprivileged_mlock
584and resource limits.
585.It Va allow.nfsd
586The
587.Xr mountd 8 ,
588.Xr nfsd 8 ,
589.Xr nfsuserd 8 ,
590.Xr gssd 8
591and
592.Xr rpc.tlsservd 8
593daemons are permitted to run inside a properly configured vnet-enabled jail.
594The jail's root must be a file system mount point and
595.Va enforce_statfs
596must not be set to 0, so that
597.Xr mountd 8
598can export file systems visible within the jail.
599.Va enforce_statfs
600must be set to 1 if file systems mounted under the
601jail's file system need to be exported by
602.Xr mount 8 .
603For exporting only the jail's file system, a setting of 2
604is sufficient.
605If the kernel configuration does not include the
606.Sy NFSD
607option,
608.Pa nfsd.ko
609must be loaded outside of the jails.
610This is normally done by adding
611.Dq nfsd
612to
613.Va kld_list
614in the
615.Xr rc.conf 5
616file outside of the jails.
617Similarily, if the
618.Xr gssd 8
619is to be run in a jail, either the kernel
620.Sy KGSSAPI
621option needs to be specified or
622.Dq kgssapi
623and
624.Dq kgssapi_krb5
625need to be in
626.Va kld_list
627in the
628.Xr rc.conf 5
629file outside of the jails.
630.It Va allow.reserved_ports
631The jail root may bind to ports lower than 1024.
632.It Va allow.unprivileged_proc_debug
633Unprivileged processes in the jail may use debugging facilities.
634.It Va allow.suser
635The value of the jail's
636.Va security.bsd.suser_enabled
637sysctl.
638The super-user will be disabled automatically if its parent system has it
639disabled.
640The super-user is enabled by default.
641.El
642.El
643.Pp
644Kernel modules may add their own parameters, which only exist when the
645module is loaded.
646These are typically headed under a parameter named after the module,
647with values of
648.Dq inherit
649to give the jail full use of the module,
650.Dq new
651to encapsulate the jail in some module-specific way,
652and
653.Dq disable
654to make the module unavailable to the jail.
655There also may be other parameters to define jail behavior within the module.
656Module-specific parameters include:
657.Bl -tag -width indent
658.It Va allow.mount.fdescfs
659privileged users inside the jail will be able to mount and unmount the
660fdescfs file system.
661This permission is effective only together with
662.Va allow.mount
663and only when
664.Va enforce_statfs
665is set to a value lower than 2.
666.It Va allow.mount.fusefs
667privileged users inside the jail will be able to mount and unmount
668fuse-based file systems.
669This permission is effective only together with
670.Va allow.mount
671and only when
672.Va enforce_statfs
673is set to a value lower than 2.
674.It Va allow.mount.nullfs
675privileged users inside the jail will be able to mount and unmount the
676nullfs file system.
677This permission is effective only together with
678.Va allow.mount
679and only when
680.Va enforce_statfs
681is set to a value lower than 2.
682.It Va allow.mount.procfs
683privileged users inside the jail will be able to mount and unmount the
684procfs file system.
685This permission is effective only together with
686.Va allow.mount
687and only when
688.Va enforce_statfs
689is set to a value lower than 2.
690.It Va allow.mount.linprocfs
691privileged users inside the jail will be able to mount and unmount the
692linprocfs file system.
693This permission is effective only together with
694.Va allow.mount
695and only when
696.Va enforce_statfs
697is set to a value lower than 2.
698.It Va allow.mount.linsysfs
699privileged users inside the jail will be able to mount and unmount the
700linsysfs file system.
701This permission is effective only together with
702.Va allow.mount
703and only when
704.Va enforce_statfs
705is set to a value lower than 2.
706.It Va allow.mount.tmpfs
707privileged users inside the jail will be able to mount and unmount the
708tmpfs file system.
709This permission is effective only together with
710.Va allow.mount
711and only when
712.Va enforce_statfs
713is set to a value lower than 2.
714.It Va allow.mount.zfs
715privileged users inside the jail will be able to mount and unmount the
716ZFS file system.
717This permission is effective only together with
718.Va allow.mount
719and only when
720.Va enforce_statfs
721is set to a value lower than 2.
722See
723.Xr zfs 8
724for information on how to configure the ZFS filesystem to operate from
725within a jail.
726.It Va allow.vmm
727The jail may access
728.Xr vmm 4 .
729This flag is only available when the
730.Xr vmm 4
731kernel module is loaded.
732.It Va linux
733Determine how a jail's Linux emulation environment appears.
734A value of
735.Dq inherit
736will keep the same environment, and
737.Dq new
738will give the jail its own environment (still originally inherited when
739the jail is created).
740.It Va linux.osname , linux.osrelease , linux.oss_version
741The Linux OS name, OS release, and OSS version associated with this jail.
742.It Va sysvmsg
743Allow access to SYSV IPC message primitives.
744If set to
745.Dq inherit ,
746all IPC objects on the system are visible to this jail, whether they
747were created by the jail itself, the base system, or other jails.
748If set to
749.Dq new ,
750the jail will have its own key namespace, and can only see the objects
751that it has created;
752the system (or parent jail) has access to the jail's objects, but not to
753its keys.
754If set to
755.Dq disable ,
756the jail cannot perform any sysvmsg-related system calls.
757.It Va sysvsem, sysvshm
758Allow access to SYSV IPC semaphore and shared memory primitives, in the
759same manner as
760.Va sysvmsg.
761.El
762.Pp
763There are pseudo-parameters that are not passed to the kernel, but are
764used by
765.Nm
766to set up the jail environment, often by running specified commands
767when jails are created or removed.
768The
769.Va exec.*
770command parameters are
771.Xr sh 1
772command lines that are run in either the system or jail environment.
773They may be given multiple values, which would run the specified
774commands in sequence.
775All commands must succeed (return a zero exit status), or the jail will
776not be created or removed, as appropriate.
777.Pp
778The pseudo-parameters are:
779.Bl -tag -width indent
780.It Va exec.prepare
781Command(s) to run in the system environment to prepare a jail for creation.
782These commands are executed before assigning IP addresses and mounting
783filesystems, so they may be used to create a new jail filesystem if it does
784not already exist.
785.It Va exec.prestart
786Command(s) to run in the system environment before a jail is created.
787.It Va exec.created
788Command(s) to run in the system environment right after a jail has been
789created, but before commands (or services) get executed in the jail.
790.It Va exec.start
791Command(s) to run in the jail environment when a jail is created.
792A typical command to run is
793.Dq sh /etc/rc .
794.It Va command
795A synonym for
796.Va exec.start
797for use when specifying a jail directly on the command line.
798Unlike other parameters whose value is a single string,
799.Va command
800uses the remainder of the
801.Nm
802command line as its own arguments.
803.It Va exec.poststart
804Command(s) to run in the system environment after a jail is created,
805and after any
806.Va exec.start
807commands have completed.
808.It Va exec.prestop
809Command(s) to run in the system environment before a jail is removed.
810.It Va exec.stop
811Command(s) to run in the jail environment before a jail is removed,
812and after any
813.Va exec.prestop
814commands have completed.
815A typical command to run is
816.Dq sh /etc/rc.shutdown jail .
817.It Va exec.poststop
818Command(s) to run in the system environment after a jail is removed.
819.It Va exec.release
820Command(s) to run in the system environment after all other actions are done.
821These commands are executed after unmounting filesystems and removing IP
822addresses, so they may be used to remove a jail filesystem if it is no longer
823needed.
824.It Va exec.clean
825Run commands in a clean environment.
826The environment is discarded except for
827.Ev HOME , SHELL , TERM
828and
829.Ev USER .
830.Ev HOME
831and
832.Ev SHELL
833are set to the target login's default values.
834.Ev USER
835is set to the target login.
836.Ev TERM
837is imported from the current environment.
838The environment variables from the login class capability database for the
839target login are also set.
840.It Va exec.jail_user
841The user to run commands as, when running in the jail environment.
842The default is to run the commands as the current user.
843.It Va exec.system_jail_user
844This boolean option looks for the
845.Va exec.jail_user
846in the system
847.Xr passwd 5
848file, instead of in the jail's file.
849.It Va exec.system_user
850The user to run commands as, when running in the system environment.
851The default is to run the commands as the current user.
852.It Va exec.timeout
853The maximum amount of time to wait for a command to complete, in
854seconds.
855If a command is still running after this timeout has passed,
856the jail will not be created or removed, as appropriate.
857.It Va exec.consolelog
858A file to direct command output (stdout and stderr) to.
859.It Va exec.fib
860The FIB (routing table) to set when running commands inside the jail.
861.It Va stop.timeout
862The maximum amount of time to wait for a jail's processes to exit
863after sending them a
864.Dv SIGTERM
865signal (which happens after the
866.Va exec.stop
867commands have completed).
868After this many seconds have passed, the jail will be removed, which
869will kill any remaining processes.
870If this is set to zero, no
871.Dv SIGTERM
872is sent and the jail is immediately removed.
873The default is 10 seconds.
874.It Va interface
875A network interface to add the jail's IP addresses
876.Va ( ip4.addr
877and
878.Va ip6.addr )
879to.
880An alias for each address will be added to the interface before the
881jail is created, and will be removed from the interface after the
882jail is removed.
883.It Va ip4.addr
884In addition to the IP addresses that are passed to the kernel, an
885interface, netmask and additional parameters (as supported by
886.Xr ifconfig 8 Ns )
887may also be specified, in the form
888.Dq Ar interface Ns | Ns Ar ip-address Ns / Ns Ar netmask param ... .
889If an interface is given before the IP address, an alias for the address
890will be added to that interface, as it is with the
891.Va interface
892parameter.
893If a netmask in either dotted-quad or CIDR form is given
894after an IP address, it will be used when adding the IP alias.
895If additional parameters are specified then they will also be used when
896adding the IP alias.
897.It Va ip6.addr
898In addition to the IP addresses that are passed to the kernel,
899an interface, prefix and additional parameters (as supported by
900.Xr ifconfig 8 Ns )
901may also be specified, in the form
902.Dq Ar interface Ns | Ns Ar ip-address Ns / Ns Ar prefix param ... .
903.It Va vnet.interface
904A network interface to give to a vnet-enabled jail after is it created.
905The interface will automatically be released when the jail is removed.
906.It Va ip_hostname
907Resolve the
908.Va host.hostname
909parameter and add all IP addresses returned by the resolver
910to the list of addresses
911.Po Va ip4.addr
912or
913.Va ip6.addr Pc
914for this jail.
915This may affect default address selection for outgoing IPv4 connections
916from jails.
917The address first returned by the resolver for each address family
918will be used as the primary address.
919.It Va mount
920A filesystem to mount before creating the jail (and to unmount after
921removing it), given as a single
922.Xr fstab 5
923line.
924.It Va mount.fstab
925An
926.Xr fstab 5
927format file containing filesystems to mount before creating a jail.
928.It Va mount.devfs
929Mount a
930.Xr devfs 5
931filesystem on the chrooted
932.Pa /dev
933directory, and apply the ruleset in the
934.Va devfs_ruleset
935parameter (or a default of ruleset 4: devfsrules_jail)
936to restrict the devices visible inside the jail.
937.It Va mount.fdescfs
938Mount a
939.Xr fdescfs 5
940filesystem on the chrooted
941.Pa /dev/fd
942directory.
943.It Va mount.procfs
944Mount a
945.Xr procfs 5
946filesystem on the chrooted
947.Pa /proc
948directory.
949.It Va allow.dying
950Allow making changes to a
951.Va dying
952jail.
953.It Va depend
954Specify a jail (or jails) that this jail depends on.
955When this jail is to be created, any jail(s) it depends on must already exist.
956If not, they will be created automatically, up to the completion of the last
957.Va exec.poststart
958command, before any action will taken to create this jail.
959When jails are removed the opposite is true:
960this jail will be removed, up to the last
961.Va exec.poststop
962command, before any jail(s) it depends on are stopped.
963.El
964.Sh EXAMPLES
965Jails are typically set up using one of two philosophies: either to
966constrain a specific application (possibly running with privilege), or
967to create a
968.Dq "virtual system image"
969running a variety of daemons and services.
970In both cases, a fairly complete file system install of
971.Fx
972is
973required, so as to provide the necessary command line tools, daemons,
974libraries, application configuration files, etc.
975However, for a virtual server configuration, a fair amount of
976additional work is required so as to replace the
977.Dq boot
978process.
979This manual page documents the configuration steps necessary to support
980either of these steps, although the configuration steps may need to be
981refined based on local requirements.
982.Ss "Setting up a Jail Directory Tree"
983To set up a jail directory tree containing an entire
984.Fx
985distribution, the following
986.Xr sh 1
987command script can be used:
988.Bd -literal -offset indent
989D=/here/is/the/jail
990cd /usr/src
991mkdir -p $D
992make world DESTDIR=$D
993make distribution DESTDIR=$D
994.Ed
995.Pp
996In many cases this example would put far more in the jail than needed.
997In the other extreme case a jail might contain only one file:
998the executable to be run in the jail.
999.Pp
1000We recommend experimentation, and caution that it is a lot easier to
1001start with a
1002.Dq fat
1003jail and remove things until it stops working,
1004than it is to start with a
1005.Dq thin
1006jail and add things until it works.
1007.Ss "Setting Up a Jail"
1008Do what was described in
1009.Sx "Setting Up a Jail Directory Tree"
1010to build the jail directory tree.
1011For the sake of this example, we will
1012assume you built it in
1013.Pa /data/jail/testjail ,
1014for a jail named
1015.Dq testjail .
1016Substitute below as needed with your
1017own directory, IP address, and hostname.
1018.Ss "Setting up the Host Environment"
1019First, set up the real system's environment to be
1020.Dq jail-friendly .
1021For consistency, we will refer to the parent box as the
1022.Dq "host environment" ,
1023and to the jailed virtual machine as the
1024.Dq "jail environment" .
1025Since jails are implemented using IP aliases, one of the first things to do
1026is to disable IP services on the host system that listen on all local
1027IP addresses for a service.
1028If a network service is present in the host environment that binds all
1029available IP addresses rather than specific IP addresses, it may service
1030requests sent to jail IP addresses if the jail did not bind the port.
1031This means changing
1032.Xr inetd 8
1033to only listen on the
1034appropriate IP address, and so forth.
1035Add the following to
1036.Pa /etc/rc.conf
1037in the host environment:
1038.Bd -literal -offset indent
1039sendmail_enable="NO"
1040inetd_flags="-wW -a 192.0.2.23"
1041rpcbind_enable="NO"
1042.Ed
1043.Pp
1044.Li 192.0.2.23
1045is the native IP address for the host system, in this example.
1046Daemons that run out of
1047.Xr inetd 8
1048can be easily configured to use only the specified host IP address.
1049Other daemons
1050will need to be manually configured \(em for some this is possible through
1051.Xr rc.conf 5
1052flags entries; for others it is necessary to modify per-application
1053configuration files, or to recompile the application.
1054The following frequently deployed services must have their individual
1055configuration files modified to limit the application to listening
1056to a specific IP address:
1057.Pp
1058To configure
1059.Xr sshd 8 ,
1060it is necessary to modify
1061.Pa /etc/ssh/sshd_config .
1062.Pp
1063To configure
1064.Xr sendmail 8 ,
1065it is necessary to modify
1066.Pa /etc/mail/sendmail.cf .
1067.Pp
1068In addition, a number of services must be recompiled in order to run
1069them in the host environment.
1070This includes most applications providing services using
1071.Xr rpc 3 ,
1072such as
1073.Xr rpcbind 8 ,
1074.Xr nfsd 8 ,
1075and
1076.Xr mountd 8 .
1077In general, applications for which it is not possible to specify which
1078IP address to bind should not be run in the host environment unless they
1079should also service requests sent to jail IP addresses.
1080Attempting to serve
1081NFS from the host environment may also cause confusion, and cannot be
1082easily reconfigured to use only specific IPs, as some NFS services are
1083hosted directly from the kernel.
1084Any third-party network software running
1085in the host environment should also be checked and configured so that it
1086does not bind all IP addresses, which would result in those services also
1087appearing to be offered by the jail environments.
1088.Pp
1089Once
1090these daemons have been disabled or fixed in the host environment, it is
1091best to reboot so that all daemons are in a known state, to reduce the
1092potential for confusion later (such as finding that when you send mail
1093to a jail, and its sendmail is down, the mail is delivered to the host,
1094etc.).
1095.Ss "Configuring the Jail"
1096Start any jail for the first time without configuring the network
1097interface so that you can clean it up a little and set up accounts.
1098As
1099with any machine (virtual or not), you will need to set a root password, time
1100zone, etc.
1101Some of these steps apply only if you intend to run a full virtual server
1102inside the jail; others apply both for constraining a particular application
1103or for running a virtual server.
1104.Pp
1105Start a shell in the jail:
1106.Bd -literal -offset indent
1107jail -c path=/data/jail/testjail mount.devfs \\
1108	host.hostname=testhostname ip4.addr=192.0.2.100 \\
1109	command=/bin/sh
1110.Ed
1111.Pp
1112Assuming no errors, you will end up with a shell prompt within the jail.
1113You can now run
1114.Xr bsdconfig 8
1115and do the post-install configuration to set various configuration options,
1116or perform these actions manually by editing
1117.Pa /etc/rc.conf ,
1118etc.
1119.Pp
1120.Bl -bullet -offset indent -compact
1121.It
1122Configure
1123.Pa /etc/resolv.conf
1124so that name resolution within the jail will work correctly.
1125.It
1126Run
1127.Xr newaliases 1
1128to quell
1129.Xr sendmail 8
1130warnings.
1131.It
1132Set a root password, probably different from the real host system.
1133.It
1134Set the timezone.
1135.It
1136Add accounts for users in the jail environment.
1137.It
1138Install any packages the environment requires.
1139.El
1140.Pp
1141You may also want to perform any package-specific configuration (web servers,
1142SSH servers, etc), patch up
1143.Pa /etc/syslog.conf
1144so it logs as you would like, etc.
1145If you are not using a virtual server, you may wish to modify
1146.Xr syslogd 8
1147in the host environment to listen on the syslog socket in the jail
1148environment; in this example, the syslog socket would be stored in
1149.Pa /data/jail/testjail/var/run/log .
1150.Pp
1151Exit from the shell, and the jail will be shut down.
1152.Ss "Starting the Jail"
1153You are now ready to restart the jail and bring up the environment with
1154all of its daemons and other programs.
1155Create an entry for the jail in
1156.Pa /etc/jail.conf :
1157.Bd -literal -offset indent
1158testjail {
1159	path = /tmp/jail/testjail;
1160	mount.devfs;
1161	host.hostname = testhostname;
1162	ip4.addr = 192.0.2.100;
1163	interface = em0;
1164	exec.start = "/bin/sh /etc/rc";
1165	exec.stop = "/bin/sh /etc/rc.shutdown jail";
1166}
1167.Ed
1168.Pp
1169To start a virtual server environment,
1170.Pa /etc/rc
1171is run to launch various daemons and services, and
1172.Pa /etc/rc.shutdown
1173is run to shut them down when the jail is removed.
1174If you are running a single application in the jail,
1175substitute the command used to start the application for
1176.Dq /bin/sh /etc/rc ;
1177there may be some script available to cleanly shut down the application,
1178or it may be sufficient to go without a stop command, and have
1179.Nm
1180send
1181.Dv SIGTERM
1182to the application.
1183.Pp
1184Start the jail by running:
1185.Bd -literal -offset indent
1186jail -c testjail
1187.Ed
1188.Pp
1189A few warnings may be produced; however, it should all work properly.
1190You should be able to see
1191.Xr inetd 8 ,
1192.Xr syslogd 8 ,
1193and other processes running within the jail using
1194.Xr ps 1 ,
1195with the
1196.Ql J
1197flag appearing beside jailed processes.
1198To see an active list of jails, use
1199.Xr jls 8 .
1200If
1201.Xr sshd 8
1202is enabled in the jail environment, you should be able to
1203.Xr ssh 1
1204to the hostname or IP address of the jailed environment, and log
1205in using the accounts you created previously.
1206.Pp
1207It is possible to have jails started at boot time.
1208Please refer to the
1209.Dq jail_*
1210variables in
1211.Xr rc.conf 5
1212for more information.
1213.Ss "Managing the Jail"
1214Normal machine shutdown commands, such as
1215.Xr halt 8 ,
1216.Xr reboot 8 ,
1217and
1218.Xr shutdown 8 ,
1219cannot be used successfully within the jail.
1220To kill all processes from within a jail, you may use one of the
1221following commands, depending on what you want to accomplish:
1222.Bd -literal -offset indent
1223kill -TERM -1
1224kill -KILL -1
1225.Ed
1226.Pp
1227This will send the
1228.Dv SIGTERM
1229or
1230.Dv SIGKILL
1231signals to all processes in the jail \(em be careful not to run this from
1232the host environment!
1233Once all of the jail's processes have died, unless the jail was created
1234with the
1235.Va persist
1236parameter, the jail will be removed.
1237Depending on
1238the intended use of the jail, you may also want to run
1239.Pa /etc/rc.shutdown
1240from within the jail.
1241.Pp
1242To shut down the jail from the outside, simply remove it with:
1243.Bd -literal -offset indent
1244jail -r
1245.Ed
1246.Pp
1247which will run any commands specified by
1248.Va exec.stop ,
1249and then send
1250.Dv SIGTERM
1251and eventually
1252.Dv SIGKILL
1253to any remaining jailed processes.
1254.Pp
1255The
1256.Pa /proc/ Ns Ar pid Ns Pa /status
1257file contains, as its last field, the name of the jail in which the
1258process runs, or
1259.Dq Li -
1260to indicate that the process is not running within a jail.
1261The
1262.Xr ps 1
1263command also shows a
1264.Ql J
1265flag for processes in a jail.
1266.Pp
1267You can also list/kill processes based on their jail ID.
1268To show processes and their jail ID, use the following command:
1269.Pp
1270.Dl "ps ax -o pid,jid,args"
1271.Pp
1272To show and then kill processes in jail number 3 use the following commands:
1273.Bd -literal -offset indent
1274pgrep -lfj 3
1275pkill -j 3
1276.Ed
1277or:
1278.Pp
1279.Dl "killall -j 3"
1280.Ss "Jails and File Systems"
1281It is not possible to
1282.Xr mount 8
1283or
1284.Xr umount 8
1285any file system inside a jail unless the file system is marked
1286jail-friendly, the jail's
1287.Va allow.mount
1288parameter is set, and the jail's
1289.Va enforce_statfs
1290parameter is lower than 2.
1291.Pp
1292Multiple jails sharing the same file system can influence each other.
1293For example, a user in one jail can fill the file system,
1294leaving no space for processes in the other jail.
1295Trying to use
1296.Xr quota 1
1297to prevent this will not work either, as the file system quotas
1298are not aware of jails but only look at the user and group IDs.
1299This means the same user ID in two jails share a single file
1300system quota.
1301One would need to use one file system per jail to make this work.
1302.Ss "Sysctl MIB Entries"
1303The read-only entry
1304.Va security.jail.jailed
1305can be used to determine if a process is running inside a jail (value
1306is one) or not (value is zero).
1307.Pp
1308The variable
1309.Va security.jail.jail_max_af_ips
1310determines how may address per address family a jail may have.
1311The default is 255.
1312.Pp
1313Some MIB variables have per-jail settings.
1314Changes to these variables by a jailed process do not affect the host
1315environment, only the jail environment.
1316These variables are
1317.Va kern.securelevel ,
1318.Va security.bsd.suser_enabled ,
1319.Va kern.hostname ,
1320.Va kern.domainname ,
1321.Va kern.hostid ,
1322and
1323.Va kern.hostuuid .
1324.Ss "Hierarchical Jails"
1325By setting a jail's
1326.Va children.max
1327parameter, processes within a jail may be able to create jails of their own.
1328These child jails are kept in a hierarchy, with jails only able to see and/or
1329modify the jails they created (or those jails' children).
1330Each jail has a read-only
1331.Va parent
1332parameter, containing the
1333.Va jid
1334of the jail that created it; a
1335.Va jid
1336of 0 indicates the jail is a child of the current jail (or is a top-level
1337jail if the current process isn't jailed).
1338.Pp
1339Jailed processes are not allowed to confer greater permissions than they
1340themselves are given, e.g., if a jail is created with
1341.Va allow.nomount ,
1342it is not able to create a jail with
1343.Va allow.mount
1344set.
1345Similarly, such restrictions as
1346.Va ip4.addr
1347and
1348.Va securelevel
1349may not be bypassed in child jails.
1350.Pp
1351A child jail may in turn create its own child jails if its own
1352.Va children.max
1353parameter is set (remember it is zero by default).
1354These jails are visible to and can be modified by their parent and all
1355ancestors.
1356.Pp
1357Jail names reflect this hierarchy, with a full name being an MIB-type string
1358separated by dots.
1359For example, if a base system process creates a jail
1360.Dq foo ,
1361and a process under that jail creates another jail
1362.Dq bar ,
1363then the second jail will be seen as
1364.Dq foo.bar
1365in the base system (though it is only seen as
1366.Dq bar
1367to any processes inside jail
1368.Dq foo ) .
1369Jids on the other hand exist in a single space, and each jail must have a
1370unique jid.
1371.Pp
1372Like the names, a child jail's
1373.Va path
1374appears relative to its creator's own
1375.Va path .
1376This is by virtue of the child jail being created in the chrooted
1377environment of the first jail.
1378.Sh SEE ALSO
1379.Xr killall 1 ,
1380.Xr lsvfs 1 ,
1381.Xr newaliases 1 ,
1382.Xr pgrep 1 ,
1383.Xr pkill 1 ,
1384.Xr ps 1 ,
1385.Xr quota 1 ,
1386.Xr jail_set 2 ,
1387.Xr vmm 4 ,
1388.Xr devfs 5 ,
1389.Xr fdescfs 5 ,
1390.Xr jail.conf 5 ,
1391.Xr linprocfs 5 ,
1392.Xr linsysfs 5 ,
1393.Xr procfs 5 ,
1394.Xr rc.conf 5 ,
1395.Xr sysctl.conf 5 ,
1396.Xr bsdconfig 8 ,
1397.Xr chroot 8 ,
1398.Xr devfs 8 ,
1399.Xr halt 8 ,
1400.Xr ifconfig 8 ,
1401.Xr inetd 8 ,
1402.Xr jexec 8 ,
1403.Xr jls 8 ,
1404.Xr mount 8 ,
1405.Xr mountd 8 ,
1406.Xr nfsd 8 ,
1407.Xr reboot 8 ,
1408.Xr rpcbind 8 ,
1409.Xr sendmail 8 ,
1410.Xr shutdown 8 ,
1411.Xr sysctl 8 ,
1412.Xr syslogd 8 ,
1413.Xr umount 8
1414.Sh HISTORY
1415The
1416.Nm
1417utility appeared in
1418.Fx 4.0 .
1419Hierarchical/extensible jails were introduced in
1420.Fx 8.0 .
1421The configuration file was introduced in
1422.Fx 9.1 .
1423.Sh AUTHORS
1424.An -nosplit
1425The jail feature was written by
1426.An Poul-Henning Kamp
1427for R&D Associates
1428who contributed it to
1429.Fx .
1430.Pp
1431.An Robert Watson
1432wrote the extended documentation, found a few bugs, added
1433a few new features, and cleaned up the userland jail environment.
1434.Pp
1435.An Bjoern A. Zeeb
1436added multi-IP jail support for IPv4 and IPv6 based on a patch
1437originally done by
1438.An Pawel Jakub Dawidek
1439for IPv4.
1440.Pp
1441.An James Gritton
1442added the extensible jail parameters, hierarchical jails,
1443and the configuration file.
1444.Sh BUGS
1445It might be a good idea to add an
1446address alias flag such that daemons listening on all IPs
1447.Pq Dv INADDR_ANY
1448will not bind on that address, which would facilitate building a safe
1449host environment such that host daemons do not impose on services offered
1450from within jails.
1451Currently, the simplest answer is to minimize services
1452offered on the host, possibly limiting it to services offered from
1453.Xr inetd 8
1454which is easily configurable.
1455.Sh NOTES
1456Great care should be taken when managing directories visible within the jail.
1457For example, if a jailed process has its current working directory set to a
1458directory that is moved out of the jail's chroot, then the process may gain
1459access to the file space outside of the jail.
1460It is recommended that directories always be copied, rather than moved, out
1461of a jail.
1462.Pp
1463In addition, there are several ways in which an unprivileged user
1464outside the jail can cooperate with a privileged user inside the jail
1465and thereby obtain elevated privileges in the host environment.
1466Most of these attacks can be mitigated by ensuring that the jail root
1467is not accessible to unprivileged users in the host environment.
1468Regardless, as a general rule, untrusted users with privileged access
1469to a jail should not be given access to the host environment.
1470