1.\" 2.\" Copyright (c) 2000 Robert N. M. Watson 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.\" 27.\" ---------------------------------------------------------------------------- 28.\" "THE BEER-WARE LICENSE" (Revision 42): 29.\" <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you 30.\" can do whatever you want with this stuff. If we meet some day, and you think 31.\" this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 32.\" ---------------------------------------------------------------------------- 33.\" 34.\" $FreeBSD$ 35.\" 36.Dd December 12, 2001 37.Dt JAIL 8 38.Os 39.Sh NAME 40.Nm jail 41.Nd "imprison process and its descendants" 42.Sh SYNOPSIS 43.Nm 44.Op Fl u Ar username 45.Ar path hostname ip-number command ... 46.Sh DESCRIPTION 47The 48.Nm 49utility imprisons a process and all future descendants. 50.Pp 51The options are as follows: 52.Bl -tag -width ".Fl u Ar username" 53.It Fl u Ar username 54The user name as whom the 55.Ar command 56should run. 57.It Ar path 58Directory which is to be the root of the prison. 59.It Ar hostname 60Hostname of the prison. 61.It Ar ip-number 62IP number assigned to the prison. 63.It Ar command 64Pathname of the program which is to be executed. 65.El 66.Pp 67Please see the 68.Xr jail 2 69man page for further details. 70.Sh EXAMPLES 71.Ss "Setting up a Jail Directory Tree" 72This example shows how to setup a jail directory tree 73containing an entire 74.Fx 75distribution: 76.Bd -literal 77D=/here/is/the/jail 78cd /usr/src 79mkdir -p $D 80make world DESTDIR=$D 81cd etc 82make distribution DESTDIR=$D 83mount_devfs devfs $D/dev 84cd $D 85ln -sf dev/null kernel 86.Ed 87.Pp 88In many cases this example would put far more stuff in the jail than is needed. 89In the other extreme case a jail might contain only one single file: 90the executable to be run in the jail. 91.Pp 92We recommend experimentation and caution that it is a lot easier to 93start with a 94.Dq fat 95jail and remove things until it stops working, 96than it is to start with a 97.Dq thin 98jail and add things until it works. 99.Ss "Setting Up a Jail" 100Do what was described in 101.Sx "Setting Up a Jail Directory Tree" 102to build the jail directory tree. 103For the sake of this example, we will 104assume you built it in 105.Pa /data/jail/192.168.11.100 , 106named for the jailed IP address. 107Substitute below as needed with your 108own directory, IP address, and hostname. 109.Pp 110First, you will want to set up your real system's environment to be 111.Dq jail-friendly . 112For consistency, we will refer to the parent box as the 113.Dq "host environment" , 114and to the jailed virtual machine as the 115.Dq "jail environment" . 116Because jail is implemented using IP aliases, one of the first things to do 117is to disable IP services on the host system that listen on all local 118IP addresses for a service. 119This means changing 120.Xr inetd 8 121to only listen on the 122appropriate IP address, and so forth. 123Add the following to 124.Pa /etc/rc.conf 125in the host environment: 126.Bd -literal -offset indent 127sendmail_enable="NO" 128inetd_flags="-wW -a 192.168.11.23" 129rpcbind_enable="NO" 130.Ed 131.Pp 132.Li 192.168.11.23 133is the native IP address for the host system, in this example. 134Daemons that run out of 135.Xr inetd 8 136can be easily set to use only the specified host IP address. 137Other daemons 138will need to be manually configured\(emfor some this is possible through 139the 140.Xr rc.conf 5 141flags entries, for others it is not possible without munging 142the per-application configuration files, or even recompiling. 143For those 144applications that cannot specify the IP they run on, it is better to disable 145them, if possible. 146.Pp 147A number of daemons ship with the base system that may have problems when 148run from outside of a jail in a jail-centric environment. 149This includes 150.Xr sendmail 8 , 151.Xr named 8 , 152and 153.Xr rpcbind 8 . 154While 155.Xr sendmail 8 156and 157.Xr named 8 158can be configured to listen only on a specific 159IP using their configuration files, in most cases it is easier to simply 160run the daemons in jails only, and not in the host environment. 161Attempting to serve 162NFS from the host environment may also cause confusion, and cannot be 163easily reconfigured to use only specific IPs, as some NFS services are 164hosted directly from the kernel. 165Any third party network software running 166in the host environment should also be checked and configured so that it 167does not bind all IP addresses, which would result in those services also 168appearing to be offered by the jail environments. 169.Pp 170Once 171these daemons have been disabled or fixed in the host environment, it is 172best to reboot so that all daemons are in a known state, to reduce the 173potential for confusion later (such as finding that when you send mail 174to a jail, and its sendmail is down, the mail is delivered to the host, 175etc.) 176.Pp 177Start any jails for the first time without configuring the network 178interface so that you can clean it up a little and set up accounts. 179As 180with any machine (virtual or not) you will need to set a root password, time 181zone, etc. 182Before beginning, you may want to copy 183.Xr sysinstall 8 184into the tree so that you can use it to set things up easily. 185Do this using: 186.Bd -literal -offset indent 187mkdir /data/jail/192.168.11.100/stand 188cp /stand/sysinstall /data/jail/192.168.11.100/stand 189.Ed 190.Pp 191Now start the jail: 192.Pp 193.Dl "jail /data/jail/192.168.11.100 testhostname 192.168.11.100 /bin/sh" 194.Pp 195You will end up with a shell prompt, assuming no errors, within the jail. 196You can now run 197.Pa /stand/sysinstall 198and do the post-install configuration to set various configuration options, 199or perform these actions manually by editing 200.Pa /etc/rc.conf , 201etc. 202.Pp 203.Bl -bullet -offset indent -compact 204.It 205Create an empty 206.Pa /etc/fstab 207to quell startup warnings about missing fstab 208.It 209Disable the port mapper 210.Pa ( /etc/rc.conf : 211.Li rpcbind_enable="NO" ) 212.It 213Run 214.Xr newaliases 1 215to quell 216.Xr sendmail 8 217warnings. 218.It 219Disable interface configuration to quell startup warnings about 220.Xr ifconfig 8 221.Pq Li network_interfaces="" 222.It 223Configure 224.Pa /etc/resolv.conf 225so that name resolution within the jail will work correctly 226.It 227Set a root password, probably different from the real host system 228.It 229Set the timezone 230.It 231Add accounts for users in the jail environment 232.It 233Install any packages that you think the environment requires 234.El 235.Pp 236You may also want to perform any package-specific configuration (web servers, 237SSH servers, etc), patch up 238.Pa /etc/syslog.conf 239so it logs as you would like, etc. 240.Pp 241Exit from the shell, and the jail will be shut down. 242.Ss "Starting the Jail" 243You are now ready to restart the jail and bring up the environment with 244all of its daemons and other programs. 245To do this, first bring up the 246virtual host interface, and then start the jail's 247.Pa /etc/rc 248script from within the jail. 249.Pp 250NOTE: If you plan to allow untrusted users to have root access inside the 251jail, you may wish to consider setting the 252.Va security.jail.set_hostname_allowed 253to 0. 254Please see the management reasons why this is a good idea. 255If you do decide to set this variable, 256it must be set before starting any jails, and once each boot. 257.Bd -literal -offset indent 258ifconfig ed0 inet alias 192.168.11.100/32 259mount -t procfs proc /data/jail/192.168.11.100/proc 260jail /data/jail/192.168.11.100 testhostname 192.168.11.100 \\ 261 /bin/sh /etc/rc 262.Ed 263.Pp 264A few warnings will be produced, because most 265.Xr sysctl 8 266configuration variables cannot be set from within the jail, as they are 267global across all jails and the host environment. 268However, it should all 269work properly. 270You should be able to see 271.Xr inetd 8 , 272.Xr syslogd 8 , 273and other processes running within the jail using 274.Xr ps 1 , 275with the 276.Ql J 277flag appearing beside jailed processes. 278You should also be able to 279.Xr telnet 1 280to the hostname or IP address of the jailed environment, and log 281in using the accounts you created previously. 282.Ss "Managing the Jail" 283Normal machine shutdown commands, such as 284.Xr halt 8 , 285.Xr reboot 8 , 286and 287.Xr shutdown 8 , 288cannot be used successfully within the jail. 289To kill all processes in a 290jail, you may log into the jail and, as root, use one of the following 291commands, depending on what you want to accomplish: 292.Pp 293.Bd -literal -offset indent 294kill -TERM -1 295kill -KILL -1 296.Ed 297.Pp 298This will send the 299.Dv SIGTERM 300or 301.Dv SIGKILL 302signals to all processes in the jail from within the jail. 303Depending on 304the intended use of the jail, you may also want to run 305.Pa /etc/rc.shutdown 306from within the jail. 307Currently there is no way to insert new processes 308into a jail, so you must first log into the jail before performing these 309actions. 310.Pp 311To kill processes from outside the jail, you must individually identify the 312PID of each process to be killed. 313The 314.Pa /proc/ Ns Ar pid Ns Pa /status 315file contains, as its last field, the hostname of the jail in which the 316process runs, or 317.Dq Li - 318to indicate that the process is not running within a jail. 319The 320.Xr ps 1 321command also shows a 322.Ql J 323flag for processes in a jail. 324However, the hostname for a jail may be, by 325default, modified from within the jail, so the 326.Pa /proc 327status entry is unreliable by default. 328To disable the setting of the hostname 329from within a jail, set the 330.Va security.jail.set_hostname_allowed 331sysctl variable in the host environment to 0, which will affect all jails. 332You can have this sysctl set on each boot using 333.Xr sysctl.conf 5 . 334Just add the following line to 335.Pa /etc/sysctl.conf : 336.Pp 337.Dl security.jail.set_hostname_allowed=0 338.Pp 339In a future version of 340.Fx , 341the mechanisms for managing jails will be 342more refined. 343.Ss "Sysctl MIB Entries" 344Certain aspects of the jail containments environment may be modified from 345the host environment using 346.Xr sysctl 8 347MIB variables. 348Currently, these variables affect all jails on the system, although in 349the future this functionality may be finer grained. 350.Bl -tag -width XXX 351.It Va security.jail.set_hostname_allowed 352This MIB entry determines whether or not processes within a jail are 353allowed to change their hostname via 354.Xr hostname 1 355or 356.Xr sethostname 3 . 357In the current jail implementation, the ability to set the hostname from 358within the jail can impact management tools relying on the accuracy of jail 359information in 360.Pa /proc . 361As such, this should be disabled in environments where privileged access to 362jails is given out to untrusted parties. 363.It Va security.jail.socket_unixiproute_only 364The jail functionality binds an IPv4 address to each jail, and limits 365access to other network addresses in the IPv4 space that may be available 366in the host environment. 367However, jail is not currently able to limit access to other network 368protocol stacks that have not had jail functionality added to them. 369As such, by default, processes within jails may only access protocols 370in the following domains: 371.Dv PF_LOCAL , PF_INET , 372and 373.Dv PF_ROUTE , 374permitting them access to 375.Ux 376domain sockets, 377IPv4 addresses, and routing sockets. 378To enable access to other domains, this MIB variable may be set to 3790. 380.It Va security.jail.sysvipc_allowed 381This MIB entry determines whether or not processes within a jail have access 382to System V IPC primitives. 383In the current jail implementation, System V primitives share a single 384namespace across the host and jail environments, meaning that processes 385within a jail would be able to communicate with (and potentially interfere 386with) processes outside of the jail, and in other jails. 387As such, this functionality is disabled by default, but can be enabled 388by setting this MIB entry to 1. 389.El 390.Sh SEE ALSO 391.Xr newaliases 1 , 392.Xr ps 1 , 393.Xr chroot 2 , 394.Xr jail 2 , 395.Xr procfs 5 , 396.Xr rc.conf 5 , 397.Xr sysctl.conf 5 , 398.Xr halt 8 , 399.Xr inetd 8 , 400.Xr mount_devfs 8 , 401.Xr named 8 , 402.Xr reboot 8 , 403.Xr rpcbind 8 , 404.Xr sendmail 8 , 405.Xr shutdown 8 , 406.Xr sysctl 8 , 407.Xr syslogd 8 408.Sh HISTORY 409The 410.Nm 411utility appeared in 412.Fx 4.0 . 413.Sh AUTHORS 414.An -nosplit 415The jail feature was written by 416.An Poul-Henning Kamp 417for R&D Associates 418.Pa http://www.rndassociates.com/ 419who contributed it to 420.Fx . 421.Pp 422.An Robert Watson 423wrote the extended documentation, found a few bugs, added 424a few new features, and cleaned up the userland jail environment. 425.Sh BUGS 426Jail currently lacks strong management functionality, such as the ability 427to deliver signals to all processes in a jail, and to allow access to 428specific jail information via 429.Xr ps 1 430as opposed to 431.Xr procfs 5 . 432Similarly, it might be a good idea to add an 433address alias flag such that daemons listening on all IPs 434.Pq Dv INADDR_ANY 435will not bind on that address, which would facilitate building a safe 436host environment such that host daemons do not impose on services offered 437from within jails. 438Currently, the simplist answer is to minimize services 439offered on the host, possibly limiting it to services offered from 440.Xr inetd 8 441which is easily configurable. 442