summaryrefslogtreecommitdiffstats
path: root/usr.sbin/jail/jail.8
blob: 6826c45017e7ddca67245895d5387b7991cf0fd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
.\"
.\"----------------------------------------------------------------------------
.\""THE BEER-WARE LICENSE" (Revision 42):
.\"<phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
.\"can do whatever you want with this stuff. If we meet some day, and you think
.\"this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
.\"----------------------------------------------------------------------------
.\"
.\"$FreeBSD$
.\"
.Dd April 28, 1999
.Dt JAIL 8
.Os FreeBSD 4.0
.Sh NAME
.Nm jail
.Nd imprison process and its descendants
.Sh SYNOPSIS
.Nm jail
.Ar path
.Ar hostname
.Ar ip-number
.Ar command
.Ar ...
.Sh DESCRIPTION
The
.Nm
command imprisons a process and all future decendants.
.Pp
Please see the
.Xr jail 2
man page for further details.
.Sh EXAMPLES
.Ss Setting up a Jail Directory Tree
This shows how to setup a jail directory tree:
.Bd -literal 
D=/here/is/the/jail
cd /usr/src
make hierarchy DESTDIR=$D
make obj
make all
make install DESTDIR=$D
cd etc
make distribution DESTDIR=$D NO_MAKEDEV=yes
cd $D/dev
sh MAKEDEV jail
cd $D
ln -sf dev/null kernel
.Ed
.Ss Setting Up a Jail
Do what was described in
.Sx Setting Up a Jail Directory Tree
to build the jail directory tree.  For the sake of this example, we will
assume you built it in
.Pa /data/jail/192.168.11.100 ,
named for the jailed IP address.  Substitute below as needed with your
own directory, IP address, and hostname.
.Pp
First, you will want to set up your real system's environment to be
.Dq jail-friendly.
For consistency, we will refer to the parent box as the
.Dq host environment,
and to the jailed virtual machine as the
.Dq jail environment.
Because jail is implemented using IP aliases, one of the first things to do
is to disable IP services on the host system that listen on all local
IP addresses for a service.  This means changing inetd to only listen on the
appropriate IP address, and so forth.  Add the following to
.Pa /etc/rc.conf
in the host environment:
.Bd -literal -offset indent
sendmail_enable="NO"
inetd_flags="-wW -a 192.168.11.23"
portmap_enable="NO"
.Ed
.Pp
.Li 192.169.11.23
is the native IP address for the host system, in this case.  It is possible
to set up jails without using an exposed host IP, but in most virtual hosting
environments, you won't want to do this.  Sendmail can be configured to
listen to a specific IP, but this involves modifying
.Pa /etc/sendmail.cf ,
so it's easier to just disable it, and only have mail service within
jails.  This is also more secure.  You will probably also want to disable
the portmapper.  You can reboot to let this take effect, or manually
kill/restart the daemons.
.Pp
Start your jail for the first time without configuring the network
interface so that you can clean it up a little and set up accounts.  As
with any machine (virtual or not) you will need to set a root password, time
zone, etc.  Before beginning, you may want to copy
.Xr sysinstall 8
into the tree so that you can use it to set things up easily.  Do this using:
.Bd -literal -offset indent
# mkdir /data/jail/192.168.11.100/stand
# cp /stand/sysinstall /data/jail/192.168.11.100/stand
.Ed
.Pp
Now start the jail:
.Bd -literal -offset indent
# jail /data/jail/192.168.11.100 testhostname 192.168.11.100 /bin/sh
.Ed
.Pp
You will end up with a shell prompt, assuming no errors, within the jail.  You
can now run
.Pa /stand/sysinstall
and do the post-install configuration to set various configuration options,
including:
.Pp
.Bl -bullet -offset indent -compact
.It
Disable the port mapper
.It
Set a root password, probably different from the real host system
.It
Set the timezone
.It
Add accounts for users in the jail environment
.It
Install any packages that you think the environment requires
.El
.Pp
Outside of
.Xr sysinstall 8 ,
you will probably also want to configure
.Xr resolv.conf 5
appropriately, as well as any package-specific configuration, such as
Web servers, ssh, etc.  You'll probably want to replace the
.Dq /dev/console
line of
.Pa /etc/syslog.conf
with something more useful, such as UDP-based logging to a log host, or
even the host environment's syslog.
.Pp
Exit from the shell, and the jail will be shut down.
.Ss Starting the Jail
You are now ready to restart the jail and bring up the environment with
all of its daemons and other programs.  To do this, first bring up the
virtual host interface, and then start the jail's
.Pa /etc/rc
script from within the jail.
.Bd -literal -offset indent
# ifconfig ed0 inet alias 192.168.11.100 netmask 255.255.255.255
# mount -t procfs proc /data/jail/192.168.11.100/proc
# jail /data/jail/192.168.11.100 testhostname 192.168.11.100 \\
	/bin/sh /etc/rc
.Ed
.Pp
A few warnings will be produced, because most
.Xr sysctl 8
configuration variables cannot be set from within the jail, as they are
global across all jails and the host environment. However, it should all
work properly. You should be able to see
.Xr inetd 8 ,
.Xr syslogd 8 ,
and other processes running within the jail using
.Xr ps 1 ,
with the
.Dq J
flag appearing beside jailed processes.  You should also be able to
telnet to the hostname or IP address of the jailed environment, and log
in using the acounts you created previously.
.Ss Managing the jail
Normal machine shutdown commands, such as
.Xr halt 8 ,
.Xr reboot 8 ,
and
.Xr shutdown 8 ,
cannot be used successfully within the jail.  To kill all processes in a
jail, you may log into the jail and, as root, use one of the following
commands, depending on what you want to accomplish:
.Pp
.Bl -bullet -offset indent -compact
.It
.Li kill -TERM -1
.It
.Li kill -KILL -1
.El
.Pp
This will send the
.Dq TERM
or
.Dq KILL
signals to all processes in the jail from within the jail.  Depending on
the intended use of the jail, you may also want to run
.Pa /etc/rc.shutdown
from within the jail.  Currently there is no way to insert new processes 
into a jail, so you must first log into the jail before performing these
actions.
.Pp
To kill processes from outside the jail, you must individually identify the
PID of each process to be killed.  The
.Pa /proc/ Ns Va pid Ns Pa /status
file contains, as its last field, the hostname of the jail in which the
process runs, or
.Dq -
to indicate that the process is not running within a jail.  The
.Xr ps 1
command also shows a
.Dq J
flag for processes in a jail.  However, the hostname for a jail may be, by
default, modified from within the jail, so the
.Pa /proc
status entry is unreliably by default.  To disable the setting of the hostname
from within a jail, set the
.Dq Va kern.jail.set_hostname_allowed
sysctl variable in the host environment to 0, which will affect all jails.  In
a future version of FreeBSD, the mechanisms for managing jails will be more
refined.
.Sh SEE ALSO
.Xr chroot 2 ,
.Xr jail 2
.Sh HISTORY
The
.Fn jail
function call appeared in
.Fx 4.0 .
.Sh AUTHORS
The jail feature was written by Poul-Henning Kamp for
R&D Associates
.Dq Li http://www.rndassociates.com/
who contributed it to FreeBSD.

Robert Watson wrote the extended documentation, found a few bugs, added
a few new features, and cleaned up the userland jail environment.
OpenPOWER on IntegriCloud