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