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