Page was written twice
[ikiwiki.git] / docs / newhandbook / Jails / index.mdwn
2d9d2dba 1 ***Contributed by Matteo Riondato. Updated for DragonFly by Dario Banno.***
3 [[!toc levels=3]]
2d9d2dba 5## Synopsis
2d9d2dba 7 This chapter will provide an explanation of what DragonFly jails are and how to use them. Jails, sometimes referred to as an enhanced replacement of chroot environments, are a very powerful tool for system administrators, but their basic usage can also be useful for advanced users.
2d9d2dba 9 After reading this chapter, you will know:
2d9d2dba 11* What a jail is, and what purpose it may serve in DragonFly installations.
13* How to build, start, and stop a jail.
15* The basics of jail administration, both from inside and outside the jail.
14d3ea40 16***
2d9d2dba 17
2d9d2dba 18 Other sources of useful information about jails are:
42ac3f77 20* The [jail(8)]( manual page. This is the full reference of the jail utility -- the administrative tool which can be used in DragonFly to start, stop, and control DragonFly jails.
2d9d2dba 21
42ac3f77 22* The mailing lists and their archives. The archives of the DragonFly general questions mailing list and other mailing lists hosted by the DragonFly list server already contain a wealth of material for jails. It should always be engaging to search the archives, or post a new question to the [users mailing list](
24 ***
2d9d2dba 25
42ac3f77 26For information on how to setup a jail, see: [Setting up a jail](
2d9d2dba 27
42ac3f77 28----
2d9d2dba 29
42ac3f77 30## Terms Related to Jails
2d9d2dba 31
42ac3f77 32 To facilitate better understanding of parts of the DragonFly system related to jails, their internals and the way they interact with the rest of DragonFly, the following terms are used further in this chapter:
2d9d2dba 33
42ac3f77 34 ***[chroot(8)]( (command)***
36 A system call of DragonFly, which changes the root directory of a process and all its descendants.
2d9d2dba 37
14d3ea40 38***
2d9d2dba 39
42ac3f77 40 ***[chroot(2)]( (environment)***
2d9d2dba 41
42ac3f77 42 The environment of processes running in a “chroot”. This includes resources such as the part of the file system which is visible, user and group IDs which are available, network interfaces and other IPC mechanisms, etc.
2d9d2dba 43
42ac3f77 44***
2d9d2dba 45
42ac3f77 46*** [jail(8)]( (command)***
2d9d2dba 47
42ac3f77 48 The system administration utility which allows launching of processes within a jail environment.
2d9d2dba 49
42ac3f77 50***
2d9d2dba 51
42ac3f77 52 ***host (system, process, user, etc.)***
2d9d2dba 53
42ac3f77 54 The controlling system of a jail environment. The host system has access to all the hardware resources available, and can control processes both outside of and inside a jail environment. One of the important differences of the host system from a jail is that the limitations which apply to superuser processes inside a jail are not enforced for processes of the host system.
2d9d2dba 55
42ac3f77 56***
2d9d2dba 57
42ac3f77 58 ***hosted (system, process, user, etc.)***
2d9d2dba 59
42ac3f77 60 A process, user or other entity, whose access to resources is restricted by a DragonFly jail.
2d9d2dba 61
42ac3f77 62----
2d9d2dba 63
42ac3f77 64## Introduction
2d9d2dba 65
42ac3f77 66 Since system administration is a difficult and perplexing task, many powerful tools were developed to make life easier for the administrator. These tools mostly provide enhancements of some sort to the way systems are installed, configured and maintained. Part of the tasks which an administrator is expected to do is to properly configure the security of a system, so that it can continue serving its real purpose, without allowing security violations.
2d9d2dba 68
42ac3f77 69 One of the tools which can be used to enhance the security of a DragonFly system are jails. The jail feature was written by Poul-Henning Kamp <> for R&D Associates [[]] who contributed it to FreeBSD 4.X. Support for multiple IPs and IPv6 were introduced in DragonFly 1.7. Their development still goes on, enhancing their usefulness, performance, reliability, and security.
2d9d2dba 70
42ac3f77 71## What is a Jail
2d9d2dba 72
42ac3f77 73 BSD-like operating systems have had [chroot(2)]( since the time of 4.2BSD. The [chroot(8)]( utility can be used to change the root directory of a set of processes, creating a safe environment, separate from the rest of the system. Processes created in the chrooted environment can not access files or resources outside of it. For that reason, compromising a service running in a chrooted environment should not allow the attacker to compromise the entire system. The chroot(8) utility is good for easy tasks, which do not require a lot of flexibility or complex and advanced features. Since the inception of the chroot concept, however, many ways have been found to escape from a chrooted environment and, although they have been fixed in modern versions of the DragonFly kernel, it was clear that chroot(2) was not the ideal solution for securing services. A new subsystem had to be implemented.
75 This is one of the main reasons why ***jails*** were developed.
77 Jails improve on the concept of the traditional [chroot(2)]( environment, in several ways. In a traditional [chroot(2)]( environment, processes are only limited in the part of the file system they can access. The rest of the system resources (like the set of system users, the running processes, or the networking subsystem) are shared by the chrooted processes and the processes of the host system. Jails expand this model by virtualizing not only access to the file system, but also the set of users, the networking subsystem of the DragonFly kernel and a few other things. A more complete set of fine-grained controls available for tuning the access of a jailed environment is described in Section 12.5.
80 A jail is characterized by four elements:
2d9d2dba 81
42ac3f77 82* A directory subtree -- the starting point from which a jail is entered. Once inside the jail, a process is not permitted to escape outside of this subtree. Traditional security issues which plagued the original chroot(2) design will not affect DragonFly jails.
2d9d2dba 83
42ac3f77 84* A hostname -- the hostname which will be used within the jail. Jails are mainly used for hosting network services, therefore having a descriptive hostname for each jail can really help the system administrator.
2d9d2dba 85
42ac3f77 86* An IP address -- this will be assigned to the jail and cannot be changed in any way during the jail's life span. The IP address of a jail is usually an alias address for an existing network interface, but this is not strictly necessary.
88* A command -- the path name of an executable to run inside the jail. This is relative to the root directory of the jail environment, and may vary a lot, depending on the type of the specific jail environment.
2d9d2dba 89***
42ac3f77 91 Apart from these, jails can have their own set of users and their own root user. Naturally, the powers of the root user are limited within the jail environment and, from the point of view of the host system, the jail root user is not an omnipotent user. In addition, the root user of a jail is not allowed to perform critical operations to the system outside of the associated jail(8) environment. More information about capabilities and restrictions of the root user will be discussed in Section 12.5 below.
2d9d2dba 92
42ac3f77 93----
2d9d2dba 94
42ac3f77 95## Creating and Controlling Jails
97 Some administrators divide jails into the following two types: ***complete*** jails, which resemble a real DragonFly system, and ***service*** jails, dedicated to one application or service, possibly running with privileges. This is only a conceptual division and the process of building a jail is not affected by it. The [jail(8)]( manual page is quite clear about the procedure for building a jail:
101 # setenv D /here/is/the/jail
102 # mkdir -p $D (1)
103 # cd /usr/src
104 # make installworld DESTDIR=$D (2)
105 # cd etc
106 # make distribution DESTDIR=$D -DNO_MAKEDEV_RUN (3)
107 # cd $D
108 # ln -sf dev/null kernel
109 # mount_devfs -o jail $D/dev
110 #
112 **(1)**
2d9d2dba 113
42ac3f77 114 Selecting a location for a jail is the best starting point. This is where the jail will physically reside within the file system of the jail's host. A good choice can be ***/usr/jail/jailname***, where jailname is the hostname identifying the jail. The ***/usr/*** file system usually has enough space for the jail file system, which for ***complete*** jails is, essentially, a replication of every file present in a default installation of the DragonFly base system.
2d9d2dba 116
42ac3f77 117 **(2)**
2d9d2dba 118
42ac3f77 119 This command will populate the directory subtree chosen as jail's physical location on the file system with the necessary binaries, libraries, manual pages and so on. Everything is done in the typical DragonFly style -- first everything is built/compiled, then installed to the destination path.
2d9d2dba 120***
42ac3f77 122 **(3)**
2d9d2dba 123
42ac3f77 124 The distribution target for make installs every needed configuration file. In simple words, it installs every installable file of ***/usr/src/etc/*** to the ***/etc*** directory of the jail environment: ***$D/etc/***.
2d9d2dba 125
42ac3f77 126***
2d9d2dba 127
42ac3f77 128 Once a jail is installed, it can be started by using the [jail(8)]( utility. The [jail(8)]( utility takes four mandatory arguments which are described in the Section 12.3.1. Other arguments may be specified too, e.g., to run the jailed process with the credentials of a specific user. The command argument depends on the type of the jail; for a virtual system, ***/etc/rc*** is a good choice, since it will replicate the startup sequence of a real DragonFly system. For a service jail, it depends on the service or application that will run within the jail.
2d9d2dba 129
42ac3f77 130 Jails are often started at boot time and the DragonFly rc mechanism provides an easy way to do this.
2d9d2dba 131
42ac3f77 132***
133 A list of the jails which are enabled to start at boot time should be added to the [rc.conf(5)]( file:
137 jail_enable="YES" # Set to NO to disable starting of any jails
138 jail_list="www" # Space separated list of names of jails
139 #
2d9d2dba 140
42ac3f77 143 For each jail listed in ***jail_list***, a group of [rc.conf(5)]( settings, which describe the particular jail, should be added:
2d9d2dba 144
42ac3f77 145
2d9d2dba 146
42ac3f77 147 jail_www_rootdir="/usr/jail/www" # jail's root directory
148 jail_www_hostname="" # jail's hostname
149 jail_www_ip="" # jail's IP address
150 #
2d9d2dba 151
42ac3f77 152 The default startup of jails configured in [rc.conf(5)](, will run the ***/etc/rc*** script of the jail, which assumes the jail is a complete virtual system. For service jails, the default startup command of the jail should be changed, by setting the ***jail_jailname_exec_start*** option appropriately.
154 **Note:** For a full list of available options, please see the [rc.conf(5)]( manual page.
2d9d2dba 155
42ac3f77 156 The ***/etc/rc.d/jail*** script can be used to start or stop a jail by hand, if an entry for it exists in ***rc.conf***:
158 # /etc/rc.d/jail start www
159 # /etc/rc.d/jail stop www
2d9d2dba 160
42ac3f77 163 A clean way to shut down a [jail(8)]( is not available at the moment. This is because commands normally used to accomplish a clean system shutdown cannot be used inside a jail. The best way to shut down a jail is to run the following command from within the jail itself or using the [jexec(8)]( utility from outside the jail:
2d9d2dba 164
42ac3f77 165
2d9d2dba 166
42ac3f77 167 # sh /etc/rc.shutdown
2d9d2dba 168
42ac3f77 169 More information about this can be found in the [jail(8)]( manual page.
2d9d2dba 170
42ac3f77 173## Fine Tuning and Administration
175 There are several options which can be set for any jail, and various ways of combining a host DragonFly system with jails, to produce higher level applications. This section presents some of the options available for tuning the behavior and security restrictions implemented by a jail installation.
2d9d2dba 176
42ac3f77 177## System tools for jail tuning in DragonFly
2d9d2dba 178
42ac3f77 179 Fine tuning of a jail's configuration is mostly done by setting [sysctl(8)]( variables. A special subtree of sysctl exists as a basis for organizing all the relevant options: the ***securityjail**** hierarchy of DragonFly kernel options. Here is a list of the main jail-related sysctls, complete with their default value. Names should be self-explanatory, but for more information about them, please refer to the [jail(8)]( and [sysctl(8)]( manual pages.
2d9d2dba 180
42ac3f77 181* jail.set_hostname_allowed: 1
2d9d2dba 182
42ac3f77 183* jail.socket_unixiproute_only: 1
185* jail.sysvipc_allowed: 0
187* jail.enforce_statfs: 2
189* jail.allow_raw_sockets: 0
191* jail.chflags_allowed: 0
193* jail.jailed: 0
195These variables can be used by the system administrator of the host system to add or remove some of the limitations imposed by default on the root user. Note that there are some limitations which cannot be removed. The root user is not allowed to mount or unmount file systems from within a [jail(8)]( The root inside a jail may not set firewall rules or do many other administrative tasks which require modifications of in-kernel data, such as setting the securelevel of the kernel.
2d9d2dba 196
14d3ea40 197***
2d9d2dba 198
42ac3f77 199 The base system of DragonFly contains a basic set of tools for viewing information about the active jails, and attaching to a jail to run administrative commands. The [jls(8)]( and [jexec(8)]( commands are part of the base DragonFly system, and can be used to perform the following simple tasks:
2d9d2dba 200
42ac3f77 201 * Print a list of active jails and their corresponding jail identifier (JID), IP address, hostname and path.
2d9d2dba 202
42ac3f77 203 * Attach to a running jail, from its host system, and run a command inside the jail or perform administrative tasks inside the jail itself. This is especially useful when the root user wants to cleanly shut down a jail. The [jexec(8)]( utility can also be used to start a shell in a jail to do administration in it; for example:
2d9d2dba 204
42ac3f77 205
2d9d2dba 206
42ac3f77 207 # jexec 1 tcsh
208 ***Contributed by Matteo Riondato. Updated for DragonFly by Dario Banno.***