10 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.

30 * The [jail(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=jail&section=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.

35 * 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](mailto:users@lists.dragonflybsd.org).

53 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:

69 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.

88 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.

108 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.

111 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 <phk@freebsd.org> for R&D Associates [[http://www.rndassociates.com/]] 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.

117 BSD-like operating systems have had [chroot(2)](http://leaf.dragonflybsd.org/cgi/web-man?command=chroot&section=2) since the time of 4.2BSD. The [chroot(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=chroot&section=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.

123 Jails improve on the concept of the traditional [chroot(2)](http://leaf.dragonflybsd.org/cgi/web-man?command=chroot&section=2) environment, in several ways. In a traditional [chroot(2)](http://leaf.dragonflybsd.org/cgi/web-man?command=chroot&section=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.

129 * 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.

132 * 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.

136 * 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.

139 * 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.

143 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.

157 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)](http://leaf.dragonflybsd.org/cgi/web-man?command=jail&section=8) manual page is quite clear about the procedure for building a jail:

186 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.

191 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.

196 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/***.

202 Once a jail is installed, it can be started by using the [jail(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=jail&section=8) utility. The [jail(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=jail&section=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.

237 The default startup of jails configured in [rc.conf(5)](http://leaf.dragonflybsd.org/cgi/web-man?command=rc.conf&section=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.

257 A clean way to shut down a [jail(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=jail&section=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)](http://leaf.dragonflybsd.org/cgi/web-man?command=jexec&section=8) utility from outside the jail:

283 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.

291 Fine tuning of a jail's configuration is mostly done by setting [sysctl(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=sysctl&section=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)](http://leaf.dragonflybsd.org/cgi/web-man?command=jail&section=8) and [sysctl(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=sysctl&section=8) manual pages.

318 These 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)](http://leaf.dragonflybsd.org/cgi/web-man?command=jail&section=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.

322 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)](http://leaf.dragonflybsd.org/cgi/web-man?command=jls&section=8) and [jexec(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=jexec&section=8) commands are part of the base DragonFly system, and can be used to perform the following simple tasks:

327 * 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)](http://leaf.dragonflybsd.org/cgi/web-man?command=jexec&section=8) utility can also be used to start a shell in a jail to do administration in it; for example: