<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jails | FreeBSD Foundation</title>
	<atom:link href="https://staging.freebsdfoundation.org/topic/jails/feed/" rel="self" type="application/rss+xml" />
	<link>https://staging.freebsdfoundation.org</link>
	<description>A non-profit organization dedicated to supporting and building the FreeBSD Project</description>
	<lastBuildDate>Tue, 02 Jan 2024 16:21:34 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://staging.freebsdfoundation.org/wp-content/uploads/2015/12/favicon.png</url>
	<title>Jails | FreeBSD Foundation</title>
	<link>https://staging.freebsdfoundation.org</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>An Introduction to FreeBSD Jails</title>
		<link>https://staging.freebsdfoundation.org/resource/an-introduction-to-freebsd-jails/</link>
		
		<dc:creator><![CDATA[Anne Dickison]]></dc:creator>
		<pubDate>Mon, 15 Aug 2022 18:29:21 +0000</pubDate>
				<guid isPermaLink="false">https://freebsdfoundation.org/?post_type=resource&#038;p=11563</guid>

					<description><![CDATA[<p>While the process creating a jail is quite simple, actual application and configuration requires a decent understanding of the FreeBSD operating system. This guide is aimed at people who are already familiar with the basics of the FreeBSD operating system.</p>
<p>The post <a href="https://staging.freebsdfoundation.org/resource/an-introduction-to-freebsd-jails/">An Introduction to FreeBSD Jails</a> first appeared on <a href="https://staging.freebsdfoundation.org">FreeBSD Foundation</a>.</p>]]></description>
										<content:encoded><![CDATA[<section class="block block-classic-editor"><p></section><section class="block block-core-image"></p>
<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1024" height="512" class="wp-image-11016" src="https://staging.freebsdfoundation.org/wp-content/uploads/2022/03/Untitled-design-3-1024x512.png" alt="" srcset="https://staging.freebsdfoundation.org/wp-content/uploads/2022/03/Untitled-design-3-1024x512.png 1024w, https://staging.freebsdfoundation.org/wp-content/uploads/2022/03/Untitled-design-3-300x150.png 300w, https://staging.freebsdfoundation.org/wp-content/uploads/2022/03/Untitled-design-3-1536x768.png 1536w, https://staging.freebsdfoundation.org/wp-content/uploads/2022/03/Untitled-design-3-2048x1024.png 2048w, https://staging.freebsdfoundation.org/wp-content/uploads/2022/03/Untitled-design-3.png 1920w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<p></section>
<section class="block block-core-heading"></p>
<h2 class="wp-block-heading">What are Jails?</h2>
<p></section>
<section class="block block-core-paragraph"></p>
<p><em>Jails </em>were developed as a tool for system administrators to enhance the security of a FreeBSD system. Originally introduced in FreeBSD 4.0, jails continue to be an integral part of the development and progression of the FreeBSD operating system.</p>
<p></section>
<section class="block block-core-paragraph"></p>
<p>Jails were created to expand upon the <a href="https://www.freebsd.org/cgi/man.cgi?query=chroot&amp;sektion=2&amp;format=html">chroot(2)</a> concept, which is used to change the root directory of a set of processes. Jails create a safe environment independent from the rest of the system. Processes created in this environment cannot access files or resources outside of it. For this reason, compromising a service running in a jail will not compromise the entire system. Jails improved upon the <a href="https://www.freebsd.org/cgi/man.cgi?query=chroot&amp;sektion=2&amp;format=html">chroot(2)</a> concept by virtualizing access to the file system, users, and the networking subsystem.</p>
<p></section>
<section class="block block-core-paragraph"></p>
<p>A jail is characterized by four elements:</p>
<p></section>
<section class="block block-core-list"></p>
<ul class="wp-block-list">
	<li>A <strong>directory subtree:</strong> the starting point from which a jail is entered. Once inside the jail, a process is not permitted to escape outside of this subtree.</li>
	<li>A <strong>hostname:</strong> which will be used by the jail.</li>
	<li>An <strong>IP address</strong>: which is assigned to the jail. The IP address of a jail is often an alias address for an existing network interface.</li>
	<li>A <strong>command</strong>: the path name of an executable to run inside the jail.</li>
</ul>
<p></section>
<section class="block block-core-paragraph"></p>
<p>While the theory is very simple and straightforward, it is important to note that creating a jail can quickly become extremely complex while leveraging systems and tools within the environment.</p>
<p></section>
<section class="block block-core-paragraph"></p>
<p><strong>Note</strong>: Jails have their own set of users and their own <code>root</code> account which are limited to the jail environment. The <code>root</code> account of a jail is not allowed to perform operations to the system outside of the associated jail environment.</p>
<p></section>
<section class="block block-core-heading"></p>
<h2 class="wp-block-heading">Examples of Jails</h2>
<p></section>
<section class="block block-core-list"></p>
<ul class="wp-block-list">
	<li>Jails are often used as test environments. Running a service or software in a jail allows developers to isolate and test updates or changes without jeopardizing the integrity of the rest of the system. This mitigates the risk of misconfiguration or mistakes. Developers also have the ability to run multiple instances or configurations at the same time without influencing each separate jailed process.</li>
</ul>
<p></section>
<section class="block block-core-list"></p>
<ul class="wp-block-list">
	<li>In larger, more complex systems, jails can be used to split the system into separate jailed processes. Each jail could contain it&#8217;s own utilities and configurations, allowing for a much more streamlined system for specific tasks. Extremely complex processes can also be isolated to a jail, separate from the main system.</li>
</ul>
<p></section>
<section class="block block-core-list"></p>
<ul class="wp-block-list">
	<li>A Jail can be used as a container for an old system, preserving critical infrastructure either as a backup, or as a way of referring to old code, all contained within a new system. This way, the old system can rely on the modern security and tools while being kept separate. This may be useful for developers who need to preserve a possibly insecure system without large changes needing to be made.</li>
</ul>
<p></section>
<section class="block block-core-list"></p>
<ul class="wp-block-list">
	<li>Jails are often used in place of a virtual machine, for either FreeBSD, or virtualization of another operating system such as Linux. Some developers looking to port tools and drivers to FreeBSD may find this useful.</li>
</ul>
<p></section>
<section class="block block-core-heading"></p>
<h2 class="wp-block-heading">Creating a Jail</h2>
<p></section>
<section class="block block-core-paragraph"></p>
<p><strong>Note:</strong> while the process creating a jail is quite simple, actual application and configuration requires a decent understanding of the FreeBSD operating system. This guide is aimed at people who are already familiar with the basics of the FreeBSD operating system.</p>
<p></section>
<section class="block block-core-paragraph"></p>
<p>Identify and create a directory for the jail. This is where the jail will physically reside within the file system of the jail’s host. A good choice can be <code>/usr/jail/jailname</code>, where <em><code>jailname</code></em> is the hostname identifying the jail. Usually, <code>/usr/ </code>has enough space for the jail file system, which for &#8220;complete&#8221; jails is, essentially, a replication of every file present in a default installation of the FreeBSD base system. In these following examples the directory will be <code>/usr/jail/myjail</code>.</p>
<p></section>
<section class="block block-core-paragraph"></p>
<p>The <a href="https://www.freebsd.org/cgi/man.cgi?query=bsdinstall&amp;sektion=8&amp;format=html" target="_blank" rel="noreferrer noopener">bsdinstall(8)</a> tool can be used to fetch and install the binaries needed for a jail. Distributions will be installed into the destination directory along with some basic configuration of the jail:</p>
<p></section>
<section class="block block-core-paragraph"></p>
<p><code># cd /usr</code></p>
<p></section>
<section class="block block-core-paragraph"></p>
<p><code># mkdir myjail</code></p>
<p></section>
<section class="block block-core-paragraph"></p>
<p><code># bsdinstall jail /usr/jail/myjail</code></p>
<p></section>
<section class="block block-core-paragraph"></p>
<p><code>bsdinstall </code>will then start the FreeBSD installation process using the installer.<br />
<br />
Once a jail is installed, it can be started by using the <a href="https://www.freebsd.org/cgi/man.cgi?query=jail&amp;sektion=8&amp;format=html">jail(8)</a> utility. The 4 elements listed earlier in the guide (directory subtree, hostname, IP address, and command) will serve as mandatory arguments for the utility, but other arguments may be specified too. The <code><em>command</em></code> argument depends on the type of the jail employed. For example, if a system requires the startup sequence, such as in the case of a virtual machine, specifying<code> /etc/rc.conf</code> under the command parameter will be ideal.</p>
<p></section>
<section class="block block-core-paragraph"></p>
<p>The FreeBSD <code>rc</code> mechanism provides an easy way to start jails on boot.</p>
<p></section>
<section class="block block-core-paragraph"></p>
<p>Configure specific jail parameters in <code>jail.conf</code>:</p>
<p></section>
<section class="block block-core-code"></p>
<pre class="wp-block-code"><code>myjail { 
    host.hostname = myjail;                        # Set the hostname
    ip4.addr = 192.168.0.10;                       # Set an IP address of the jail 
    path = "/usr/jail/myjail";                     # Path to the jail 
    devfs_ruleset = "5";                           # devfs ruleset 
    mount.devfs;                                   # Mount devfs inside the jail 
    exec.start = "/bin/sh /etc/rc";                # Start command 
    exec.stop = "/bin/sh /etc/rc.shutdown";        # Stop command 
} </code></pre>
<p></section>
<section class="block block-core-paragraph"></p>
<p>A common configuration can also be used. This configuration will be shared by all jails that are not specifically set up like in the previous example:</p>
<p></section>
<section class="block block-core-code"></p>
<pre class="wp-block-code"><code>host.hostname = "$name";                           # Set the hostname using the $name variable
ip4 = inherit;                                     # Inherit IP address from the host
path = "~/jail.$name";                             # Path to the jail
devfs_ruleset = $name_ruleset";                    # devfs ruleset
mount.devfs;                                       # Mount devfs inside the jail
exec.start = "/bin/sh /etc/rc";                    # Start command 
exec.stop = "/bin/sh /etc/rc.shutdown";            # Stop command 
</code></pre>
<p></section>
<section class="block block-core-paragraph"></p>
<p><a href="https://www.freebsd.org/cgi/man.cgi?query=service&amp;sektion=8&amp;format=html">service(8)</a> can be used to start or stop a jail by hand if an entry for it exists in jail.conf:</p>
<p></section>
<section class="block block-core-paragraph"></p>
<p><code># service jail start myjail <br />
# service jail stop myjail </code></p>
<p></section>
<section class="block block-core-paragraph"></p>
<p>More information about this can be found in the <a href="https://www.freebsd.org/cgi/man.cgi?query=jail&amp;sektion=8&amp;format=html" target="_blank" rel="noreferrer noopener">jail(8)</a> manual page, including other arguments that can be set for the jail.</p>
<p></section>
<section class="block block-core-heading"></p>
<h2 class="wp-block-heading">Further Configuration</h2>
<p></section>
<section class="block block-core-paragraph"></p>
<p>Fine tuning of a jail’s configuration is mostly done by setting <a href="https://www.freebsd.org/cgi/man.cgi?query=sysctl&amp;sektion=8&amp;format=html" target="_blank" rel="noreferrer noopener">sysctl(8)</a> variables. Here is a list of the main jail-related <code>sysctls</code>, complete with their default value. Please refer to the <a href="https://www.freebsd.org/cgi/man.cgi?query=jail&amp;sektion=8&amp;format=html" target="_blank" rel="noreferrer noopener">jail(8)</a> and <a href="https://www.freebsd.org/cgi/man.cgi?query=sysctl&amp;sektion=8&amp;format=html" target="_blank" rel="noreferrer noopener">sysctl(8)</a> manual pages for more information on each variable.</p>
<p></section>
<section class="block block-core-list"></p>
<ul class="wp-block-list">
	<li><code>security.jail.set_hostname_allowed: 1</code></li>
	<li><code>security.jail.socket_unixiproute_only: 1</code></li>
	<li><code>security.jail.sysvipc_allowed: 0</code></li>
	<li><code>security.jail.enforce_statfs: 2</code></li>
	<li><code>security.jail.allow_raw_sockets: 0</code></li>
	<li><code>security.jail.chflags_allowed: 0</code></li>
	<li><code>security.jail.jailed: 0</code></li>
</ul>
<p></section>
<section class="block block-core-paragraph"></p>
<p>These variables will need to be run by the host system system administrator and amend some limitations by default in the jail.</p>
<p></section>
<section class="block block-core-paragraph"></p>
<p>FreeBSD also contains tools for viewing information on active jails and executing commands within the jail itself. The <a href="https://www.freebsd.org/cgi/man.cgi?query=jls&amp;sektion=8&amp;format=html" target="_blank" rel="noreferrer noopener">jls(8)</a> command can be used to list all active jails along with their identifier, hostname, path, and IP address. The <a href="https://www.freebsd.org/cgi/man.cgi?query=jexec&amp;sektion=8&amp;format=html" target="_blank" rel="noreferrer noopener">jexec(8</a>) command can attach to an active jail from the host system in order to run a command or perform administrative tasks. For example <a href="https://www.freebsd.org/cgi/man.cgi?query=jexec&amp;sektion=8&amp;format=html">jexec(8)</a> can be used to start a shell in an active jail with:</p>
<p></section>
<section class="block block-core-paragraph"></p>
<p><code># jexec 1 sh</code></p>
<p></section>
<section class="block block-core-heading"></p>
<h2 class="wp-block-heading">Updating/Removing a Jail</h2>
<p></section>
<section class="block block-core-paragraph"></p>
<p>Jails should be kept as up to date from the host operating system as possible. To update the jail to the latest patch release, execute the following commands on the host:</p>
<p></section>
<section class="block block-core-paragraph"></p>
<p><code># freebsd-update -b  /usr/jail/myjail fetch <br />
# freebsd-update -b /usr/jail/myjail install</code></p>
<p></section>
<section class="block block-core-paragraph"></p>
<p>To upgrade the jail to a new major or minor version, first upgrade the host system as described in <a href="https://docs.freebsd.org/en/books/handbook/cutting-edge/index.html#freebsdupdate-upgrade">“Performing Major and Minor Version Upgrades”</a>. Once the host has been upgraded and rebooted, the jail can then be upgraded. For example to upgrade from 12.3-RELEASE to 13.0-RELEASE, on the host run:</p>
<p></section>
<section class="block block-core-paragraph"></p>
<p><code># freebsd-update -b  /usr/jail/myjail --currently-running 12.3-RELEASE -r 13.0-RELEASE upgrade <br />
# freebsd-update -b /usr/jail/myjail install <br />
# service jail restart myjail <br />
# freebsd-update -b /usr/jail/myjail install</code></p>
<p></section>
<section class="block block-core-paragraph"></p>
<p>Then, if it was a major version upgrade, reinstall all installed packages and restart the jail again. This is required because the ABI version changes when upgrading between major versions of FreeBSD. From the host:</p>
<p></section>
<section class="block block-core-paragraph"></p>
<p><code># pkg -j myjail upgrade -f <br />
# service jail restart myjail</code></p>
<p></section>
<section class="block block-core-paragraph"></p>
<p>In order to remove a jail, simply remove the directory after making sure that the service has stopped:</p>
<p></section>
<section class="block block-core-paragraph"></p>
<p><code># service jail stop myjail</code><br />
<code># rm -rf myjail  </code></p>
<p></section><section class="block block-classic-editor"></p></section><p>The post <a href="https://staging.freebsdfoundation.org/resource/an-introduction-to-freebsd-jails/">An Introduction to FreeBSD Jails</a> first appeared on <a href="https://staging.freebsdfoundation.org">FreeBSD Foundation</a>.</p>]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
