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