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