2 .\" Copyright (c) 2000, 2003 Robert N. M. Watson
3 .\" Copyright (c) 2008 James Gritton
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
15 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 .\" ----------------------------------------------------------------------------
29 .\" "THE BEER-WARE LICENSE" (Revision 42):
30 .\" <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
31 .\" can do whatever you want with this stuff. If we meet some day, and you think
32 .\" this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
33 .\" ----------------------------------------------------------------------------
42 .Nd "create or modify a system jail"
47 .Op Fl l u Ar username | Fl U Ar username
49 .Op Ar parameter=value ...
55 .Op Fl s Ar securelevel
56 .Op Fl l u Ar username | Fl U Ar username
57 .Op Ar path hostname [ip[,..]] command ...
64 utility creates a new jail or modifies an existing jail, optionally
65 imprisoning the current process (and future descendants) inside it.
67 The options are as follows:
68 .Bl -tag -width indent
70 Allow making changes to a
78 and add all IP addresses returned by the resolver
81 addresses for this prison.
82 This may affect default address selection for outgoing IPv4 connections
84 The address first returned by the resolver for each address family
85 will be used as primary address.
90 parameters further down for details.
92 Output the jail identifier of the newly created jail.
95 This is deprecated and is equivalent to setting the
101 file, containing jail identifier, path, hostname, IP and
102 command used to start the jail.
104 Run program in the clean environment.
105 The environment is discarded except for
106 .Ev HOME , SHELL , TERM
112 are set to the target login's default values.
114 is set to the target login.
116 is imported from the current environment.
117 The environment variables from the login class capability database for the
118 target login are also set.
119 .It Fl s Ar securelevel
122 MIB entry to the specified value inside the newly created jail.
123 This is deprecated and is equivalent to setting the
127 The user name from host environment as whom the
131 The user name from jailed environment as whom the
140 parameters (if specified) must not refer to an existing jail.
142 Modify an existing jail.
147 parameters must exist and refer to an existing jail.
149 Create a jail if it does not exist, or modify a jail if it does exist.
153 specified by jid or name.
154 All jailed processes are killed, and all children of this jail are also
163 options must be specified.
168 form, following the options.
169 Some parameters are boolean, and do not have a value but are set by the
170 name alone with or without a
176 Any parameters not set will be given default values, often based on the
181 specifies that the current process should enter the new (or modified) jail,
182 and run the specified command.
183 It must be the last parameter specified, because it includes not only
184 the value following the
186 sign, but also passes the rest of the arguments to the command.
188 Instead of supplying named
190 four fixed parameters may be supplied in order on the command line:
200 parameters aren't in this list, this mode will always create a new jail, and
205 options don't apply (and must not exist).
207 Jails have a set a core parameters, and modules can add their own jail
209 The current set of available parameters can be retrieved via
210 .Dq Nm sysctl Fl d Va security.jail.param .
211 The core parameters are:
212 .Bl -tag -width indent
215 This will be assigned automatically to a new jail (or can be explicitly
216 set), and can be used to identify the jail for later modification, or
223 This is an arbitrary string that identifies a jail (except it may not
228 it can be passed to later
236 is supplied, a default is assumed that is the same as the
239 Directory which is to be the root of the prison.
242 (if any) is run from this directory, as are commands from
245 A comma-separated list of IPv4 addresses assigned to the prison.
246 If this is set, the jail is restricted to using only these address.
247 Any attempts to use other addresses fail, and attempts to use wildcard
248 addresses silently use the jailed address instead.
249 For IPv4 the first address given will be kept used as the source address
250 in case source address selection on unbound sockets cannot find a better
252 It is only possible to start multiple jails with the same IP address,
253 if none of the jails has more than this single overlapping IP address
256 A list of zero elements (an empty string) will stop the jail from using IPv4
257 entirely; setting the boolean parameter
259 will not restrict the jail at all.
261 A list of IPv6 addresses assigned to the prison, the counterpart to
265 Hostname of the prison.
266 Other similar parameters are
267 .Va host.domainname ,
271 Setting the boolean parameter
273 will retain the system values of these settings.
275 The value of the jail's
278 A jail never has a lower securelevel than the default system, but by
279 setting this parameter it may have a higher one.
280 If the system securelevel is changed, any jail securelevels will be at
283 The number of child jails allowed to be created by this jail (or by
284 other jails under this jail).
285 This limit is zero by default, indicating the jail is not allowed to
288 .Va "Hierarchical Jails"
289 section for more information.
291 The number of descendents of this jail, including its own child jails
292 and any jails created under them.
293 .It Va enforce_statfs
294 This determines which information processes in a jail are able to get
296 It affects the behaviour of the following syscalls:
302 (as well as similar compatibility syscalls).
303 When set to 0, all mount points are available without any restrictions.
304 When set to 1, only mount points below the jail's chroot directory are
306 In addition to that, the path to the jail's chroot directory is removed
307 from the front of their pathnames.
308 When set to 2 (default), above syscalls can operate only on a mount-point
309 where the jail's chroot directory is located.
311 Setting this boolean parameter allows a jail to exist without any
313 Normally, a jail is destroyed as its last process exits.
314 A new jail must have either the
318 pseudo-parameter set.
320 The ID of the cpuset associated with this jail (read-only).
322 This is true if the jail is in the process of shutting down (read-only).
326 of the parent of this jail, or zero if this is a top-level jail
329 Some restrictions of the jail environment may be set on a per-jail
331 With the exception of
332 .Va allow.set_hostname ,
333 these boolean parameters are off by default.
334 .Bl -tag -width indent
335 .It Va allow.set_hostname
336 The jail's hostname may be changed via
341 A process within the jail has access to System V IPC primitives.
342 In the current jail implementation, System V primitives share a single
343 namespace across the host and jail environments, meaning that processes
344 within a jail would be able to communicate with (and potentially interfere
345 with) processes outside of the jail, and in other jails.
346 .It Va allow.raw_sockets
347 The prison root is allowed to create raw sockets.
348 Setting this parameter allows utilities like
352 to operate inside the prison.
353 If this is set, the source IP addresses are enforced to comply
354 with the IP address bound to the jail, regardless of whether or not
357 flag has been set on the socket.
358 Since raw sockets can be used to configure and interact with various
359 network subsystems, extra caution should be used where privileged access
360 to jails is given out to untrusted parties.
362 Normally, priveleged users inside a jail are treated as unprivileged by
364 When this parameter is set, such users are treated as privileged, and
365 may manipulate system file flags subject to the usual constraints on
366 .Va kern.securelevel .
368 privileged users inside the jail will be able to mount and unmount file
369 system types marked as jail-friendly.
372 command can be used to find file system types available for mount from
375 The prison root may administer quotas on the jail's filesystem(s).
376 This includes filesystems that the jail may share with other jails or
377 with non-jailed parts of the system.
378 .It Va allow.socket_af
379 Sockets within a jail are normally restricted to IPv4, IPv6, local
380 (UNIX), and route. This allows access to other protocol stacks that
381 have not had jail functionality added to them.
385 Jails are typically set up using one of two philosophies: either to
386 constrain a specific application (possibly running with privilege), or
388 .Dq "virtual system image"
389 running a variety of daemons and services.
390 In both cases, a fairly complete file system install of
393 required, so as to provide the necessary command line tools, daemons,
394 libraries, application configuration files, etc.
395 However, for a virtual server configuration, a fair amount of
396 additional work is required so as to configure the
399 This manual page documents the configuration steps necessary to support
400 either of these steps, although the configuration steps may be
401 refined based on local requirements.
403 .Ss "Setting up a Jail Directory Tree"
404 To set up a jail directory tree containing an entire
406 distribution, the following
408 command script can be used:
413 make world DESTDIR=$D
414 make distribution DESTDIR=$D
415 mount -t devfs devfs $D/dev
418 NOTE: It is important that only appropriate device nodes in devfs be
419 exposed to a jail; access to disk devices in the jail may permit processes
420 in the jail to bypass the jail sandboxing by modifying files outside of
424 for information on how to use devfs rules to limit access to entries
425 in the per-jail devfs.
426 A simple devfs ruleset for jails is available as ruleset #4 in
427 .Pa /etc/defaults/devfs.rules .
429 In many cases this example would put far more in the jail than needed.
430 In the other extreme case a jail might contain only one file:
431 the executable to be run in the jail.
433 We recommend experimentation and caution that it is a lot easier to
436 jail and remove things until it stops working,
437 than it is to start with a
439 jail and add things until it works.
440 .Ss "Setting Up a Jail"
441 Do what was described in
442 .Sx "Setting Up a Jail Directory Tree"
443 to build the jail directory tree.
444 For the sake of this example, we will
445 assume you built it in
446 .Pa /data/jail/192.0.2.100 ,
447 named for the jailed IP address.
448 Substitute below as needed with your
449 own directory, IP address, and hostname.
450 .Ss "Setting up the Host Environment"
451 First, you will want to set up your real system's environment to be
453 For consistency, we will refer to the parent box as the
454 .Dq "host environment" ,
455 and to the jailed virtual machine as the
456 .Dq "jail environment" .
457 Since jail is implemented using IP aliases, one of the first things to do
458 is to disable IP services on the host system that listen on all local
459 IP addresses for a service.
460 If a network service is present in the host environment that binds all
461 available IP addresses rather than specific IP addresses, it may service
462 requests sent to jail IP addresses if the jail did not bind the port.
465 to only listen on the
466 appropriate IP address, and so forth.
469 in the host environment:
470 .Bd -literal -offset indent
472 inetd_flags="-wW -a 192.0.2.23"
477 is the native IP address for the host system, in this example.
478 Daemons that run out of
480 can be easily set to use only the specified host IP address.
482 will need to be manually configured\(emfor some this is possible through
485 flags entries; for others it is necessary to modify per-application
486 configuration files, or to recompile the applications.
487 The following frequently deployed services must have their individual
488 configuration files modified to limit the application to listening
489 to a specific IP address:
493 it is necessary to modify
494 .Pa /etc/ssh/sshd_config .
498 it is necessary to modify
499 .Pa /etc/mail/sendmail.cf .
503 it is necessary to modify
504 .Pa /etc/namedb/named.conf .
506 In addition, a number of services must be recompiled in order to run
507 them in the host environment.
508 This includes most applications providing services using
515 In general, applications for which it is not possible to specify which
516 IP address to bind should not be run in the host environment unless they
517 should also service requests sent to jail IP addresses.
519 NFS from the host environment may also cause confusion, and cannot be
520 easily reconfigured to use only specific IPs, as some NFS services are
521 hosted directly from the kernel.
522 Any third-party network software running
523 in the host environment should also be checked and configured so that it
524 does not bind all IP addresses, which would result in those services' also
525 appearing to be offered by the jail environments.
528 these daemons have been disabled or fixed in the host environment, it is
529 best to reboot so that all daemons are in a known state, to reduce the
530 potential for confusion later (such as finding that when you send mail
531 to a jail, and its sendmail is down, the mail is delivered to the host,
533 .Ss "Configuring the Jail"
534 Start any jail for the first time without configuring the network
535 interface so that you can clean it up a little and set up accounts.
537 with any machine (virtual or not) you will need to set a root password, time
539 Some of these steps apply only if you intend to run a full virtual server
540 inside the jail; others apply both for constraining a particular application
541 or for running a virtual server.
543 Start a shell in the jail:
545 .Bd -literal -offset indent
546 jail path=/data/jail/192.0.2.100 host.hostname=testhostname \\
547 ip4.addr=192.0.2.100 command=/bin/sh
550 Assuming no errors, you will end up with a shell prompt within the jail.
552 .Pa /usr/sbin/sysinstall
553 and do the post-install configuration to set various configuration options,
554 or perform these actions manually by editing
558 .Bl -bullet -offset indent -compact
562 to quell startup warnings about missing fstab (virtual server only)
564 Disable the port mapper
566 .Li rpcbind_enable="NO" )
567 (virtual server only)
571 so that name resolution within the jail will work correctly
579 Disable interface configuration to quell startup warnings about
581 .Pq Li network_interfaces=""
582 (virtual server only)
584 Set a root password, probably different from the real host system
588 Add accounts for users in the jail environment
590 Install any packages the environment requires
593 You may also want to perform any package-specific configuration (web servers,
594 SSH servers, etc), patch up
596 so it logs as you would like, etc.
597 If you are not using a virtual server, you may wish to modify
599 in the host environment to listen on the syslog socket in the jail
600 environment; in this example, the syslog socket would be stored in
601 .Pa /data/jail/192.0.2.100/var/run/log .
603 Exit from the shell, and the jail will be shut down.
604 .Ss "Starting the Jail"
605 You are now ready to restart the jail and bring up the environment with
606 all of its daemons and other programs.
607 If you are running a single application in the jail, substitute the
608 command used to start the application for
610 in the examples below.
611 To start a virtual server environment,
613 is run to launch various daemons and services.
614 To do this, first bring up the
615 virtual host interface, and then start the jail's
617 script from within the jail.
618 .Bd -literal -offset indent
619 ifconfig ed0 inet alias 192.0.2.100/32
620 mount -t procfs proc /data/jail/192.0.2.100/proc
621 jail path=/data/jail/192.0.2.100 host.hostname=testhostname \\
622 ip4.addr=192.0.2.100 command=/bin/sh /etc/rc
625 A few warnings will be produced, because most
627 configuration variables cannot be set from within the jail, as they are
628 global across all jails and the host environment.
629 However, it should all
631 You should be able to see
634 and other processes running within the jail using
638 flag appearing beside jailed processes.
639 To see an active list of jails, use the
642 You should also be able to
644 to the hostname or IP address of the jailed environment, and log
645 in using the accounts you created previously.
647 It is possible to have jails started at boot time.
652 for more information.
655 jail script provides a flexible system to start/stop jails:
659 /etc/rc.d/jail start myjail
660 /etc/rc.d/jail stop myjail
662 .Ss "Managing the Jail"
663 Normal machine shutdown commands, such as
668 cannot be used successfully within the jail.
669 To kill all processes in a
670 jail, you may log into the jail and, as root, use one of the following
671 commands, depending on what you want to accomplish:
673 .Bd -literal -offset indent
682 signals to all processes in the jail from within the jail.
684 the intended use of the jail, you may also want to run
686 from within the jail.
687 To kill processes from outside the jail, use the
689 utility in conjunction with the one of the
692 You may also remove the jail with
695 which will killall the jail's processes with
699 .Pa /proc/ Ns Ar pid Ns Pa /status
700 file contains, as its last field, the name of the jail in which the
703 to indicate that the process is not running within a jail.
708 flag for processes in a jail.
710 You can also list/kill processes based on their jail ID.
711 To show processes and their jail ID, use the following command:
713 .Dl "ps ax -o pid,jid,args"
715 To show and then kill processes in jail number 3 use the following commands:
716 .Bd -literal -offset indent
723 .Ss "Jails and File Systems"
724 It is not possible to
728 any file system inside a jail unless the file system is marked
729 jail-friendly and the jail's
733 Multiple jails sharing the same file system can influence each other.
734 For example a user in one jail can fill the file system also
735 leaving no space for processes in the other jail.
738 to prevent this will not work either as the file system quotas
739 are not aware of jails but only look at the user and group IDs.
740 This means the same user ID in two jails share the same file
742 One would need to use one file system per jail to make this work.
743 .Ss "Sysctl MIB Entries"
745 .Va security.jail.jailed
746 can be used to determine if a process is running inside a jail (value
747 is one) or not (value is zero).
750 .Va security.jail.max_af_ips
751 determines how may address per address family a prison may have.
754 Some MIB variables have per-jail settings.
755 Changes to these variables by a jailed process do not effect the host
756 environment, only the jail environment.
758 .Va kern.securelevel ,
760 .Va kern.domainname ,
764 .Ss "Hierarchical Jails"
767 parameter, processes within a jail may be able to create jails of their own.
768 These child jails are kept in a hierarchy, with jails only able to see and/or
769 modify the jails they created (or those jails' children).
770 Each jail has a read-only
772 parameter, containing the
774 of the jail that created it; a
776 of 0 indicates the jail is a child of the current jail (or is a top-level
777 jail if the current process isn't jailed).
779 Jailed processes are not allowed to confer greater permissions than they
780 themselves are given, e.g. if a jail is created with
782 it is not able to create a jail with
785 Similarly, such restrictions as
789 may not be bypassed in child jails.
791 A child jail may in turn create its own child jails if its own
793 parameter is set (remember it is zero by default).
794 These jails are visible to and can be modified by their parent and all
797 Jail names reflect this hierarchy, with a full name being an MIB-type string
799 For example, if a base system process creates a jail
801 and a process under that jail creates another jail
803 then the second jail will be seen as
805 in the base system (though it is only seen as
807 to any processes inside jail
809 Jids on the other hand exist in a single space, and each jail must have a
812 Like the names, a child jail's
814 is relative to its creator's own
816 This is by virtue of the child jail being created in the chrooted
817 environment of the first jail.
851 Hierarchical/extensible jails were introduced in
855 The jail feature was written by
856 .An Poul-Henning Kamp
858 .Pa http://www.rndassociates.com/
859 who contributed it to
863 wrote the extended documentation, found a few bugs, added
864 a few new features, and cleaned up the userland jail environment.
867 added multi-IP jail support for IPv4 and IPv6 based on a patch
869 .An Pawel Jakub Dawidek
873 added the extensible jail parameters and hierchical jails.
875 Jail currently lacks the ability to allow access to
876 specific jail information via
880 Similarly, it might be a good idea to add an
881 address alias flag such that daemons listening on all IPs
883 will not bind on that address, which would facilitate building a safe
884 host environment such that host daemons do not impose on services offered
886 Currently, the simplest answer is to minimize services
887 offered on the host, possibly limiting it to services offered from
889 which is easily configurable.