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