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