JAIL(8)
π NAME
jail β manage system jails
Bestie, this is jail(8) from the System Manager's Manual π β the utility that creates new jails, or modifies or removes existing jails. It can also print a list of configured jails and their parameters. A jail (or βprisonβ) is specified via parameters on the command line, or in the jail.conf(5) file.
β¨ SYNOPSIS β¨
π From Configuration File
jail [-cm] [-Cdqv] [-f conf_file] [-p limit] [jail]
jail [-r] [-Cqv] [-f conf_file] [-p limit] [* | jail ...]
π Without Configuration File
jail [-cm] [-dhilqv] [-J jid_file] [-u username] [-U username]
param=value ... [command=command ...]
jail [-rR] [-qv] [* | jail ...]
π Show Parameters
jail [-f conf_file] -e separator
π°οΈ Backward Compatibility
jail [-dhilqv] [-J jid_file] [-u username] [-U username] [-n jailname]
[-s securelevel] path hostname ip[,...] command ...
π DESCRIPTION π
The jail utility creates new jails, or modifies or removes existing jails. It can also print a list of configured jails and their parameters. A jail (or βprisonβ) is specified via parameters on the command line, or in the jail.conf(5) file.
At least one of the options -c, -e, -m or -r must be specified. These options are used alone or in combination to describe the operation to perform:
-cβ Create a new jail. The jailjidandnameparameters (if specified on the command line) must not refer to an existing jail.-e separatorβ Exhibit a list of all configured non-wildcard jails and their parameters. No jail creation, modification or removal performed if this option is used. Theseparatorstring is used to separate parameters. Usejls(8)utility to list running jails.-mβ Modify an existing jail. One of thejidornameparameters must exist and refer to an existing jail. Some parameters may not be changed on a running jail.-rβ Remove the jail specified byjidorname. All jailed processes are killed, and all jails that are children of this jail are also removed.-rcβ Restart an existing jail. The jail is first removed and then re-created, as ifjail -randjail -cwere run in succession.-cmβ Create a jail if it does not exist, or modify the jail if it does exist.-mrβ Modify an existing jail. The jail may be restarted if necessary to modify parameters that couldn't otherwise be changed.-cmrβ Create a jail if it doesn't exist, or modify (and possibly restart) the jail if it does exist.
Other available options are:
-Cβ Clean up after an already-removed jail, running commands and operations that are typically run following jail removal.-f conf_fileβ Use configuration fileconf_fileinstead of the default/etc/jail.conf.-hβ Resolve thehost.hostnameparameter (orhostname) and add all IP addresses returned by the resolver to the list of addresses for this jail. This is equivalent to theip_hostnameparameter.-iβ Output (only) the jail identifier of the newly created jail(s). This implies the-qoption.-J jid_fileβ Write ajid_filefile, containing the parameters used to start the jail.-lβ Run commands in a clean environment. This is deprecated and is equivalent to theexec.cleanparameter.-n jailnameβ Set the jail's name. This is deprecated and is equivalent to thenameparameter.-p limitβ Limit the number of commands fromexec.*that can run simultaneously.-qβ Suppress the message printed whenever a jail is created, modified or removed. Only error messages will be printed.-Rβ A variation of the-roption that removes an existing jail without using the configuration file. No removal-related parameters for this jail will be used β the jail will simply be removed.-s securelevelβ Set thekern.securelevelMIB entry to the specified value inside the newly created jail. This is deprecated and is equivalent to thesecurelevelparameter.-u usernameβ The user name from host environment as whom jailed commands should run. This is deprecated and is equivalent to theexec.jail_userandexec.system_jail_userparameters.-U usernameβ The user name from the jailed environment as whom jailed commands should run. This is deprecated and is equivalent to theexec.jail_userparameter.-vβ Print a message on every operation, such as running commands and mounting filesystems.-dβ This is deprecated and is equivalent to theallow.dyingparameter, which is also deprecated. It used to allow making changes to a dying jail. Now such jails are always replaced when a new jail is created with the samejidorname.
If no arguments are given after the options, the operation (except remove) will be performed on all jails specified in the jail.conf(5) file. A single argument of a jail name will operate only on the specified jail. The -r and -R options can also remove running jails that aren't in the jail.conf(5) file, specified by name or jid.
An argument of β*β is a wildcard that will operate on all jails, regardless of whether they appear in jail.conf(5); this is the surest way for -r to remove all jails. If hierarchical jails exist, a partial-matching wildcard definition may be specified. For example, an argument of βfoo.*β would apply to jails with names like βfoo.barβ and βfoo.bar.bazβ.
A jail may also be specified via parameters directly on the command line in βname=valueβ form, ignoring the contents of jail.conf(5). For backward compatibility, the command line may also have four fixed parameters, without names: path, hostname, ip, and command.
π§ Jail Parameters
Parameters in the jail.conf(5) file, or on the command line, are generally of the form βname=valueβ. Some parameters are boolean, and do not have a value but are set by the name alone with or without a βnoβ prefix, e.g. persist or nopersist. They can also be given the values βtrueβ and βfalseβ. Other parameters may have more than one value, specified as a comma-separated list, or with β+=β in the configuration file (see jail.conf(5) for details). List-based parameters may also be specified multiple times on the command line, i.e., βname=value1,value2β and βname=value1 name=value2β are equivalent for such parameters.
The jail utility recognizes two classes of parameters. There are the true jail parameters that are passed to the kernel when the jail is created, which can be seen with jls(8), and can (usually) be changed with jail -m. Then there are pseudo-parameters that are only used by jail itself.
Jails have a set of core parameters, and kernel modules can add their own jail parameters. The current set of available parameters can be retrieved via sysctl -d security.jail.param. Any parameters not set will be given default values, often based on the current environment. The core parameters are:
jidβ The jail identifier. This will be assigned automatically to a new jail (or can be explicitly set), and can be used to identify the jail for later modification, or for such commands asjls(8)orjexec(8).nameβ The jail name. This is an arbitrary string that identifies a jail (except it may not contain a.). Like thejid, it can be passed to later jail commands, or tojls(8)orjexec(8). If no name is supplied, a default is assumed that is the same as thejid. Thenameparameter is implied by thejail.conf(5)file format, and need not be explicitly set when using the configuration file.pathβ The directory which is to be the root of the jail. Any commands run inside the jail, either byjailor fromjexec(8), are run from this directory.ip4.addrβ A list of IPv4 addresses assigned to the jail. If this is set, the jail is restricted to using only these addresses. Any attempts to use other addresses fail, and attempts to use wildcard addresses silently use the jailed address instead. For IPv4 the first address given will be used as the source address when source address selection on unbound sockets cannot find a better match. It is only possible to start multiple jails with the same IP address if none of the jails has more than this single overlapping IP address assigned to itself.ip4.saddrselβ A boolean option to change the formerly mentioned behaviour and disable IPv4 source address selection for the jail in favour of the primary IPv4 address of the jail. Source address selection is enabled by default for all jails and theip4.nosaddrselsetting of a parent jail is not inherited for any child jails.ip4β Control the availability of IPv4 addresses. Possible values are βinheritβ to allow unrestricted access to all system addresses, βnewβ to restrict addresses viaip4.addr, and βdisableβ to stop the jail from using IPv4 entirely. Setting theip4.addrparameter implies a value of βnewβ.ip6.addr,ip6.saddrsel,ip6β A set of IPv6 options for the jail, the counterparts toip4.addr,ip4.saddrselandip4above.vnetβ Create the jail with its own virtual network stack, with its own network interfaces, addresses, routing table, etc. The kernel must have been compiled with theVIMAGEoption for this to be available. Possible values are βinheritβ to use the system network stack, possibly with restricted IP addresses, and βnewβ to create a new network stack.host.hostnameβ The hostname of the jail. Other similar parameters arehost.domainname,host.hostuuidandhost.hostid.hostβ Set the origin ofhostnameand related information. Possible values are βinheritβ to use the system information and βnewβ for the jail to use the information from the above fields. Setting any of the above fields implies a value of βnewβ.securelevelβ The value of the jail'skern.securelevelsysctl. A jail never has a lower securelevel than its parent system, but by setting this parameter it may have a higher one. If the system securelevel is changed, any jail securelevels will be at least as secure.devfs_rulesetβ the number of the devfs ruleset that gets enforced when mountingdevfsin this jail π₯. A value of zero (that's the default) means no ruleset is enforced, so like, nothing's being policed. Descendant jails inherit the parent jail's devfs ruleset enforcement β it trickles down fr fr. Mountingdevfsinside a jail is possible only if theallow.mountandallow.mount.devfspermissions are effective andenforce_statfsis set to a value lower than2. Also important: devfs rules and rulesets cannot be viewed or modified from inside a jail π.
NOTE: it is so so important that only appropriate device nodes in devfs be exposed to a jail π. Access to disk devices in the jail may permit processes in the jail to bypass the jail sandboxing by modifying files outside of the jail. Seedevfs(8)for information on how to use devfs rules to limit access to entries in the per-jail devfs. A simple devfs ruleset for jails is available as ruleset #4 in/etc/defaults/devfs.rules.
children.maxβ the number of child jails allowed to be created by this jail (or by other jails under this jail). This limit is zero by default, indicating the jail is not allowed to create child jails. See the βHierarchical Jailsβ section for more info πchildren.curβ the number of descendants of this jail, including its own child jails and any jails created under them π«Άenforce_statfsβ this determines what information processes in a jail are able to get about mount points. It affects the behaviour of these syscalls:statfs(2),fstatfs(2),getfsstat(2), andfhstatfs(2)(as well as similar compatibility syscalls). Here's the tea:- set to
0β all mount points are available without any restrictions β¨ - set to
1β only mount points below the jail's chroot directory are visible. On top of that, the path to the jail's chroot directory is removed from the front of their pathnames π - set to
2(the default) β the syscalls above can operate only on a mount-point where the jail's chroot directory is located π
- set to
persistβ setting this boolean parameter allows a jail to exist without any processes. Normally, a command is run as part of jail creation, and then the jail is destroyed as its last process exits. A new jail must have either thepersistparameter orexec.startorcommandpseudo-parameter set, no cap πΈcpuset.idβ the ID of the cpuset associated with this jail (read-only) π¦dyingβ this is true if the jail is in the process of shutting down (read-only) πparentβ the jid of the parent of this jail, or zero if this is a top-level jail (read-only) πosreleaseβ the string for the jail'skern.osreleasesysctl anduname -rβosreldateβ the number for the jail'skern.osreldateanduname -Kβmeta,envβ an arbitrary string associated with the jail π. Its maximum buffer size is controlled by the globalsecurity.jail.meta_maxbufsizesysctl, which can only be adjusted by the non-jailed root user. While the meta is hidden from the jail, the env is readable through thesecurity.jail.envsysctl.
Each buffer can be treated as a set of key=value\n strings. In order to add or replace a specific key, the meta.keyname=value or env.keyname=value parameter notations must be used. While meta.keyname= or env.keyname= reset the value to an empty string, the meta.keyname or env.keyname notations, without the equal sign, remove the given key π
. Respectively, the same meta.keyname or env.keyname notations are used to query a specific key while reading jail parameters using such commands as jls(8). Multiple keys can be queried or modified with a single command π
allow.*β some restrictions of the jail environment may be set on a per-jail basis π§. With the exception ofallow.set_hostnameandallow.reserved_ports, these boolean parameters are off by default.allow.set_hostnameβ the jail's hostname may be changed viahostname(1)orsethostname(3)πallow.sysvipcβ a process within the jail has access to System V IPC primitives. This is deprecated in favor of the per-module parameters (see below) π. When this parameter is set, it is equivalent to settingsysvmsg,sysvsem, andsysvshmall to βinheritβ.allow.raw_socketsβ the jail root is allowed to create raw sockets. Setting this parameter allows utilities likeping(8)andtraceroute(8)to operate inside the jail π₯. If this is set, the source IP addresses are enforced to comply with the IP address bound to the jail, regardless of whether or not theIP_HDRINCLflag has been set on the socket. Since raw sockets can be used to configure and interact with various network subsystems, extra caution should be used where privileged access to jails is given out to untrusted parties π¬.allow.chflagsβ normally, privileged users inside a jail are treated as unprivileged bychflags(2). When this parameter is set, such users are treated as privileged, and may manipulate system file flags subject to the usual constraints onkern.securelevelπallow.mountβ privileged users inside the jail will be able to mount and unmount file system types marked as jail-friendly. Thelsvfs(1)command can be used to find file system types available for mount from within a jail. This permission is effective only ifenforce_statfsis set to a value lower than2β¨allow.mount.devfsβ privileged users inside the jail will be able to mount and unmount the devfs file system. This permission is effective only together withallow.mountand only whenenforce_statfsis set to a value lower than2. Thedevfs_rulesetoption should be used to restrict the devfs ruleset from the default πallow.quotasβ the jail root may administer quotas on the jail's filesystem(s). This includes filesystems that the jail may share with other jails or with non-jailed parts of the system π«Άallow.read_msgbufβ jailed users may read the kernel message buffer. If thesecurity.bsd.unprivileged_read_msgbufMIB entry is zero, this will be restricted to the root user πallow.socket_afβ sockets within a jail are normally restricted to IPv4, IPv6, local (UNIX), and route. This allows access to other protocol stacks that have not had jail functionality added to them πallow.mlockβ locking or unlocking physical pages in memory are normally not available within a jail. When this parameter is set, users maymlock(2)ormunlock(2)memory subject tosecurity.bsd.unprivileged_mlockand resource limits πΈallow.nfsdβ themountd(8),nfsd(8),nfsuserd(8),gssd(8)andrpc.tlsservd(8)daemons are permitted to run inside a properly configured vnet-enabled jail π¦. The jail's root must be a file system mount point andenforce_statfsmust not be set to0, so thatmountd(8)can export file systems visible within the jail.enforce_statfsmust be set to1if file systems mounted under the jail's file system need to be exported bymount(8). For exporting only the jail's file system, a setting of2is sufficient. If the kernel configuration does not include theNFSDoption,nfsd.komust be loaded outside of the jails. This is normally done by adding βnfsdβ tokld_listin therc.conf(5)file outside of the jails. Similarily, if thegssd(8)is to be run in a jail, either the kernelKGSSAPIoption needs to be specified or βkgssapiβ and βkgssapi_krb5β need to be inkld_listin therc.conf(5)file outside of the jails π§allow.reserved_portsβ the jail root may bind to ports lower than1024πallow.unprivileged_parent_tamperingβ unprivileged processes in the jail's parent may tamper with processes of the same UID in the jail. This includes the ability to signal, debug, andcpuset(1)processes that belong to the jail πallow.unprivileged_proc_debugβ unprivileged processes in the jail may use debugging facilities πallow.suserβ the value of the jail'ssecurity.bsd.suser_enabledsysctl. The super-user will be disabled automatically if its parent system has it disabled. The super-user is enabled by default βallow.extattrβ allow privileged processes in the jail to manipulate filesystem extended attributes in the system namespace πallow.adjtimeβ allow privileged processes in the jail to slowly adjusting global operating system time. For example through utilities likentpd(8)π°οΈallow.settimeβ allow privileged processes in the jail to set global operating system data and time. For example through utilities likedate(1). This permission includes alsoallow.adjtimeβ°allow.routingβ allow privileged process in the non-VNET jail to modify the system routing table π£οΈ
allow.setauditβ lets privileged processes inside the jail setaudit(4)session state usingsetaudit(2)and related system calls. Super handy, like, say you want a jailedsshd(8)to set the audit user ID for an authenticated session β iconic, love that for them. But (and this is a big but) it hands jailed processes the ability to modify or disable audit session state, so configure it with care, bestie. Fr fr.
Kernel modules can add their own parameters too, and those only exist while the module is loaded. They're usually grouped under a parameter named after the module, and the values go like this: "inherit" gives the jail full use of the module, "new" encapsulates the jail in some module-specific way, and "disable" makes the module unavailable to the jail. There may also be other parameters defining jail behavior within the module. Module-specific parameters include:
allow.mount.fdescfsβ privileged users inside the jail can mount and unmount thefdescfsfile system. β¨ Only works together withallow.mount, and only whenenforce_statfsis set to a value lower than2.allow.mount.fusefsβ privileged users inside the jail can mount and unmount fuse-based file systems. Only effective together withallow.mount, and only whenenforce_statfsis lower than2.allow.mount.nullfsβ privileged users inside the jail can mount and unmount thenullfsfile system. Same deal: needsallow.mount, andenforce_statfslower than2.allow.mount.procfsβ privileged users inside the jail can mount and unmount theprocfsfile system. Together withallow.mountonly, and gated onenforce_statfsbeing lower than2.allow.mount.linprocfsβ privileged users inside the jail can mount and unmount thelinprocfsfile system. Only withallow.mount, only whenenforce_statfsis lower than2.allow.mount.linsysfsβ privileged users inside the jail can mount and unmount thelinsysfsfile system. Only withallow.mount, only whenenforce_statfsis lower than2.allow.mount.tmpfsβ privileged users inside the jail can mount and unmount thetmpfsfile system. Only withallow.mount, only whenenforce_statfsis lower than2.allow.mount.zfsβ privileged users inside the jail can mount and unmount the ZFS file system. Only effective together withallow.mount, and only whenenforce_statfsis set to a value lower than2. π Peepzfs-jail(8)for how to configure the ZFS filesystem to operate from inside a jail.allow.vmmβ the jail may accessvmm(4). This flag is only available when thevmm(4)kernel module is loaded. π¦allow.vmm_pptβ the jail may configure PCI passtrough devices for use byvmm(4)virtual machine guests. That means privileged users inside the jail can manipulate physical devices claimed by thepptdriver β so it must NOT be configured in untrusted jails. π This flag is only available when thevmm(4)kernel module is loaded.linuxβ decides how a jail's Linux emulation environment shows up. A value of"inherit"keeps the same environment;"new"gives the jail its own environment (still originally inherited when the jail is created).linux.osname,linux.osrelease,linux.oss_versionβ the Linux OS name, OS release, and OSS version associated with this jail. π§sysvmsgβ allows access to SYSV IPC message primitives. Set it to"inherit"and all IPC objects on the system are visible to this jail, whether the jail itself made them, the base system made them, or other jails did. Set it to"new"and the jail gets its own key namespace, seeing only the objects it created β the system (or parent jail) can access the jail's objects but not its keys. Set it to"disable"and the jail can't perform anysysvmsg-related system calls. periodt.sysvsem,sysvshmβ allow access to SYSV IPC semaphore and shared memory primitives, in the exact same manner assysvmsg. πzfs.mount_snapshotβ when set to1, jailed users may access the contents of ZFS snapshots under the filesystem's.zfsdirectory. Ifallow.mount.zfsis set, the snapshots may also be mounted. πΈ
Okay so there are also pseudo-parameters β these don't get passed to the kernel at all. jail uses them to set up the jail environment, often by running specified commands when jails are created or removed. The exec.* command parameters are sh(1) command lines run in either the system or jail environment. They can be given multiple values, which runs the specified commands in sequence. β οΈ All commands must succeed (return a zero exit status), or the jail will not be created or removed, as appropriate. No pressure or anything.
The following variables get added to the environment:
JIDβ the jid, or jail identifier.JNAMEβ the name of the jail.JPATHβ the path of the jail.
The pseudo-parameters are:
exec.prepareβ command(s) to run in the system environment to prepare a jail for creation. These execute before assigning IP addresses and mounting filesystems, so you can use them to create a new jail filesystem if it doesn't already exist. π οΈexec.prestartβ command(s) to run in the system environment before a jail is created.exec.createdβ command(s) to run in the system environment right after a jail has been created, but before commands (or services) get executed in the jail. β¨exec.startβ command(s) to run in the jail environment when a jail is created. A typical command to run issh /etc/rc.commandβ a synonym forexec.startfor use when specifying a jail directly on the command line. Unlike other parameters whose value is a single string,commanduses the remainder of the jail command line as its own arguments. πexec.poststartβ command(s) to run in the system environment after a jail is created, and after anyexec.startcommands have completed.exec.prestopβ command(s) to run in the system environment before a jail is removed.exec.stopβ command(s) to run in the jail environment before a jail is removed, and after anyexec.prestopcommands have completed. A typical command to run issh /etc/rc.shutdown jail.exec.poststopβ command(s) to run in the system environment after a jail is removed.exec.releaseβ command(s) to run in the system environment after all other actions are done. These run after unmounting filesystems and removing IP addresses, so you can use them to remove a jail filesystem if it's no longer needed. π§exec.cleanβ run commands in a clean environment. The environment is discarded except forHOME,SHELL,TERMandUSER.HOMEandSHELLare set to the target login's default values.USERis set to the target login.TERMis imported from the current environment.PATHis set to"/bin:/usr/bin". The environment variables from the login class capability database for the target login are also set.JID,JNAME, andJPATHare not set. If a user is specified (as withexec.jail_user), commands are run from that (possibly jailed) user's directory.exec.jail_userβ the user to run commands as, when running in the jail environment. Default is to run the commands as the current user.exec.system_jail_userβ this boolean option looks for theexec.jail_userin the systempasswd(5)file, instead of in the jail's file. πexec.system_userβ the user to run commands as, when running in the system environment. Default is to run the commands as the current user.exec.timeoutβ the maximum amount of time to wait for a command to complete, in seconds. If a command is still running after this timeout has passed, the jail will not be created or removed, as appropriate. β°exec.consolelogβ a file to direct command output (stdout and stderr) to.exec.fibβ the FIB (routing table) to set when running commands inside the jail.stop.timeoutβ the maximum amount of time to wait for a jail's processes to exit after sending them aSIGTERMsignal (which happens after theexec.stopcommands have completed). After this many seconds have passed, the jail will be removed, which kills any remaining processes. π₯ If this is set to zero, noSIGTERMis sent and the jail is immediately removed. The default is10seconds.interfaceβ a network interface to add the jail's IP addresses (ip4.addrandip6.addr) to. An alias for each address gets added to the interface before the jail is created, and gets removed from the interface after the jail is removed. πip4.addrβ in addition to the IP addresses passed to the kernel, you may also specify an interface, netmask and additional parameters (as supported byifconfig(8)), in the forminterface|ip-address/netmask param .... If an interface is given before the IP address, an alias for the address is added to that interface, just like with theinterfaceparameter. If a netmask in either dotted-quad or CIDR form is given after an IP address, it's used when adding the IP alias. If additional parameters are specified, they're used when adding the IP alias too. πip6.addrβ the IPv6 addresses. Bestie, you can also hand over an interface, prefix, and extra params (whateverifconfig(8)supports) in the form βinterface|ip-address/prefix param ...β. That whole thing goes to the kernel alongside the addresses β¨vnet.interfaceβ a list of network interfaces to hand to a vnet-enabled jail after it's created. They get auto-released when the jail is removed. No manual cleanup, we love that π«Άzfs.datasetβ a list of ZFS datasets to attach to the jail. Heads up: this requiresallow.mount.zfsto be set. Peepzfs-jail(8)for how to configure a ZFS dataset so it can be operated from inside a jail πip_hostnameβ resolves thehost.hostnameparameter and adds every IP address the resolver hands back to the address list (ip4.addrorip6.addr) for this jail. Careful though: this may affect default address selection for outgoing IPv4 connections from jails. Whichever address the resolver returns first for each address family becomes the primary address πmountβ a filesystem to mount before creating the jail (and to unmount after removing it), written as a singlefstab(5)line.mount.fstabβ anfstab(5)format file containing filesystems to mount before creating a jail.mount.devfsβ mounts adevfs(4)filesystem on the chrooted/devdirectory, and applies the ruleset in thedevfs_rulesetparameter (or the default of ruleset 4:devfsrules_jail) to restrict which devices are visible inside the jail πmount.fdescfsβ mounts afdescfs(4)filesystem on the chrooted/dev/fddirectory.mount.procfsβ mounts aprocfs(4)filesystem on the chrooted/procdirectory.allow.dyingβ deprecated, and it does literally nothing now. It used to let you make changes to a dying jail. Now those jails are always replaced when a new jail is created with the samejidor name πdependβ names the jail (or jails) this jail depends on. When this jail is about to be created, every jail it depends on must already exist. If they don't, they get created automatically β up to the completion of the lastexec.poststartcommand β before any action is taken to create this jail. And when jails are removed, it's the opposite vibe: this jail gets removed, up to the lastexec.poststopcommand, before any jail(s) it depends on are stopped π¦
π EXAMPLES π
Okay so jails get set up using one of two philosophies: either you're constraining a specific application (possibly running with privilege), or you're creating a βvirtual system imageβ running a variety of daemons and services. In both cases you need a fairly complete file system install of FreeBSD, so you've got all the necessary command line tools, daemons, libraries, application configuration files, etc. But for a virtual server configuration there's a fair amount of additional work required, because you have to replace the βbootβ process. This manual page documents the configuration steps necessary to support either of these, although the configuration steps may need to be refined based on local requirements π
β¨ Setting up a Jail Directory Tree From Source β¨
To set up a jail directory tree containing an entire FreeBSD distribution, this sh(1) command script does the trick:
D=/here/is/the/jail
cd /usr/src
mkdir -p $D
make world DESTDIR=$D
make distribution DESTDIR=$D
π Setting up a Jail Directory Tree from Distribution Files π
To set up a jail directory tree containing an entire FreeBSD distribution, you can also use this sh(1) command script:
D=/here/is/the/jail
mkdir -p $D
tar -xf /usr/freebsd-dist/base.txz -C $D --unlink
π Setting up a Jail Directory Tree from System Packages π
To set up a jail directory tree containing a selectable FreeBSD distribution with the packages tech preview, run this:
bsdinstall jail /here/is/the/jail
Real talk: in many cases these examples put far more in the jail than you actually need. And on the other extreme, a jail might contain literally one file: the executable to be run in the jail π«
We recommend experimentation, and we caution that it's a lot easier to start with a βfatβ jail and remove things until it stops working, than it is to start with a βthinβ jail and add things until it works. Trust the process π
πΈ Setting Up a Jail πΈ
Do what was described in βSetting Up a Jail Directory Treeβ to build the jail directory tree. For the sake of this example, we're gonna assume you built it in /data/jail/testjail, for a jail named βtestjailβ. Substitute below as needed with your own directory, IP address, and hostname.
π¦ Setting up the Host Environment π¦
First things first: set up the real system's environment to be βjail-friendlyβ. For consistency, we're calling the parent box the βhost environmentβ, and the jailed virtual machine the βjail environmentβ. Since jails are implemented using IP aliases, one of the first things you gotta do is disable IP services on the host system that listen on all local IP addresses for a service. Because bestie β if a network service in the host environment binds all available IP addresses instead of specific ones, it may service requests sent to jail IP addresses if the jail didn't bind the port π That means changing inetd(8) to only listen on the appropriate IP address, and so forth. Add this to /etc/rc.conf in the host environment:
sendmail_enable="NO"
inetd_flags="-wW -a 192.0.2.23"
rpcbind_enable="NO"
192.0.2.23 is the native IP address for the host system, in this example. Daemons that run out of inetd(8) can be easily configured to use only the specified host IP address. Other daemons need to be manually configured β for some that's possible through rc.conf(5) flags entries; for others you've gotta modify per-application configuration files, or recompile the application. The following frequently deployed services must have their individual configuration files modified to limit the application to listening to a specific IP address:
- To configure
sshd(8), you need to modify/etc/ssh/sshd_config. - To configure
sendmail(8), you need to modify/etc/mail/sendmail.cf.
In addition, a number of services must be recompiled in order to run them in the host environment. That includes most applications providing services using rpc(3), such as rpcbind(8), nfsd(8), and mountd(8). In general, applications where it's not possible to specify which IP address to bind should not be run in the host environment unless they should also service requests sent to jail IP addresses. Attempting to serve NFS from the host environment may also cause confusion, and cannot be easily reconfigured to use only specific IPs, since some NFS services are hosted directly from the kernel. Any third-party network software running in the host environment should also be checked and configured so it doesn't bind all IP addresses β otherwise those services would also appear to be offered by the jail environments π
Once these daemons have been disabled or fixed in the host environment, it's best to reboot so all daemons are in a known state, reducing the potential for confusion later (like finding that when you send mail to a jail, and its sendmail is down, the mail gets delivered to the host, etc.) π
β Configuring the Jail β
Start any jail for the first time without configuring the network interface, so you can clean it up a little and set up accounts. As with any machine (virtual or not), you'll need to set a root password, time zone, etc. Some of these steps apply only if you intend to run a full virtual server inside the jail; others apply both for constraining a particular application or for running a virtual server.
Start a shell in the jail:
jail -c path=/data/jail/testjail mount.devfs \
host.hostname=testhostname ip4.addr=192.0.2.100 \
command=/bin/sh
Assuming no errors, you'll end up with a shell prompt inside the jail. Now you can run bsdconfig(8) and do the post-install configuration to set various configuration options, or perform these actions manually by editing /etc/rc.conf, etc.
- Configure
/etc/resolv.confso that name resolution within the jail will work correctly. - Run
newaliases(1)to quellsendmail(8)warnings. - Set a root password, probably different from the real host system.
- Set the timezone.
- Add accounts for users in the jail environment.
- Install any packages the environment requires.
You may also want to do any package-specific configuration (web servers, SSH servers, etc.), patch up /etc/syslog.conf so it logs how you'd like, etc. If you're not using a virtual server, you may wish to modify syslogd(8) in the host environment to listen on the syslog socket in the jail environment; in this example, the syslog socket would be stored in /data/jail/testjail/var/run/log π
Exit from the shell, and the jail will be shut down.
π Starting the Jail π
You're now ready to restart the jail and bring up the environment with all of its daemons and other programs. Create an entry for the jail in /etc/jail.conf:
testjail {
path = /tmp/jail/testjail;
mount.devfs;
host.hostname = testhostname;
ip4.addr = 192.0.2.100;
interface = em0;
exec.start = "/bin/sh /etc/rc";
exec.stop = "/bin/sh /etc/rc.shutdown jail";
}
To start a virtual server environment, /etc/rc is run to launch the various daemons and services, and /etc/rc.shutdown is run to shut them down when the jail is removed. If you're running a single application in the jail, substitute the command used to start the application for β/bin/sh /etc/rcβ; there may be some script available to cleanly shut down the application, or it may be sufficient to go without a stop command, and have jail send SIGTERM to the application π
Start the jail by running:
jail -c testjail
A few warnings may be produced; however, it should all work properly. You should be able to see inetd(8), syslogd(8), and other processes running within the jail using ps(1), with the βJβ flag appearing beside jailed processes. To see an active list of jails, use jls(8). If sshd(8) is enabled in the jail environment, you should be able to ssh(1) to the hostname or IP address of the jailed environment, and log in using the accounts you created previously π₯
Okay so quick last thing before we close out that section β you can have jails started at boot time π just go look at the jail_* variables in rc.conf(5), they've got all the info you need.
π Managing the Jail
So here's the thing bestie: the normal machine shutdown commands β halt(8), reboot(8), shutdown(8) β cannot be used successfully within the jail. Like, at all. Not a vibe.
To kill all processes from within a jail, you've got options depending on what you actually want to accomplish:
kill -TERM -1
kill -KILL -1
Those send SIGTERM or SIGKILL to all processes in the jail.
β οΈ Be careful not to run this from the host environment! That's a whole different situation and not a cute one.
Once all of the jail's processes have died, the jail gets removed β unless the jail was created with the persist parameter. Depending on what your jail's for, you may also want to run /etc/rc.shutdown from inside the jail. π«Ά
To shut the jail down from the outside, just remove it like this:
jail -r
which runs any commands specified by exec.stop, then sends SIGTERM and eventually SIGKILL to any remaining jailed processes. Clean and effective β¨
The /proc/pid/status file contains, as its last field, the name of the jail the process runs in β or "-" to show the process is not running within a jail. Also ps(1) shows a 'J' flag for processes in a jail, which is lowkey so handy.
You can also list/kill processes based on their jail ID π To show processes and their jail ID, use:
ps ax -o pid,jid,args
To show and then kill processes in jail number 3, use:
pgrep -lfj 3
pkill -j 3
or:
killall -j 3
Pick your fighter, honestly πΈ
π Jails and File Systems
Ngl this one's important. It is not possible to mount(8) or umount(8) any file system inside a jail unless:
- the file system is marked jail-friendly,
- the jail's
allow.mountparameter is set, and - the jail's
enforce_statfsparameter is lower than2.
All three. No exceptions. Periodt.
Multiple jails sharing the same file system can influence each other π for example, a user in one jail can fill the file system, leaving no space for processes in the other jail. And trying to use quota(1) to prevent this won't work either, because file system quotas are not aware of jails β they only look at the user and group IDs. Which means the same user ID in two jails shares a single file system quota. To actually make this work you'd need one file system per jail. That's just how it is π
π₯ Sysctl MIB Entries
The read-only entry security.jail.jailed tells you if a process is running inside a jail (value is one) or not (value is zero). Vibe check, basically.
The variable security.jail.jail_max_af_ips determines how many addresses per address family a jail may have. The default is 255. π
Some MIB variables have per-jail settings. Changes to these variables by a jailed process do not affect the host environment, only the jail environment. These variables are:
kern.securelevelsecurity.bsd.suser_enabledkern.hostnamekern.domainnamekern.hostidkern.hostuuid
π Hierarchical Jails
Okay this is the main character section fr fr. By setting a jail's children.max parameter, processes within a jail may be able to create jails of their own π These child jails are kept in a hierarchy, with jails only able to see and/or modify the jails they created (or those jails' children). It's giving family tree.
Each jail has a read-only parent parameter containing the jid of the jail that created it. A jid of 0 means the jail is a child of the current jail β or is a top-level jail if the current process isn't jailed.
Jailed processes are not allowed to confer greater permissions than they themselves are given π e.g., if a jail is created with allow.nomount, it is not able to create a jail with allow.mount set. Similarly, such restrictions as ip4.addr and securelevel may not be bypassed in child jails. No sneaking extra privileges, bestie π
A child jail may in turn create its own child jails if its own children.max parameter is set (remember, it is zero by default). These jails are visible to and can be modified by their parent and all ancestors.
Jail names reflect this hierarchy, with a full name being an MIB-type string separated by dots. For example: if a base system process creates a jail "foo", and a process under that jail creates another jail "bar", then the second jail will be seen as "foo.bar" in the base system β though it is only seen as "bar" to any processes inside jail "foo". Jids on the other hand exist in a single space, and each jail must have a unique jid. β¨
Like the names, a child jail's path appears relative to its creator's own path. This is by virtue of the child jail being created in the chrooted environment of the first jail.
π SEE ALSO
date(1), killall(1), lsvfs(1), newaliases(1), pgrep(1), pkill(1), ps(1), quota(1), adjtime(2), clock_settime(2), jail_set(2), ntp_adjtime(2), devfs(4), fdescfs(4), linprocfs(4), linsysfs(4), procfs(4), vmm(4), jail.conf(5), rc.conf(5), sysctl.conf(5), bsdconfig(8), chroot(8), devfs(8), halt(8), ifconfig(8), inetd(8), jexec(8), jls(8), mount(8), mountd(8), nfsd(8), ntpd(8), reboot(8), rpcbind(8), sendmail(8), shutdown(8), sysctl(8), syslogd(8), umount(8), zfs-jail(8), extattr(9)
π¦ HISTORY
- The
jailutility appeared in FreeBSD 4.0. - Hierarchical/extensible jails were introduced in FreeBSD 8.0.
- The configuration file was introduced in FreeBSD 9.1.
Glow-up timeline honestly π
π AUTHORS
- The jail feature was written by Poul-Henning Kamp for R&D Associates, 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.
- Bjoern A. Zeeb added multi-IP jail support for IPv4 and IPv6 based on a patch originally done by Pawel Jakub Dawidek for IPv4.
- James Gritton added the extensible jail parameters, hierarchical jails, and the configuration file.
Absolute icons, all of them ππ
π BUGS
It might be a good idea to add an address alias flag such that daemons listening on all IPs (INADDR_ANY) will not bind on that address β which would facilitate building a safe host environment such that host daemons do not impose on services offered from within jails π
Currently, the simplest answer is to minimize services offered on the host, possibly limiting it to services offered from inetd(8), which is easily configurable. Not ideal, but it works π
πΈ NOTES
β οΈ Great care should be taken when managing directories visible within the jail!
For example, if a jailed process has its current working directory set to a directory that is moved out of the jail's chroot, then the process may gain access to the file space outside of the jail π It is recommended that directories always be copied, rather than moved, out of a jail. Copy, don't move. Write that down.
In addition, there are several ways in which an unprivileged user outside the jail can cooperate with a privileged user inside the jail and thereby obtain elevated privileges in the host environment. Most of these attacks can be mitigated by ensuring that the jail root is not accessible to unprivileged users in the host environment. Regardless, as a general rule: untrusted users with privileged access to a jail should not be given access to the host environment. Full stop π«Ά
And that's the whole page bestie, you ate that π
β¨ This one's from the Debian package, dated November 13, 2025 β JAIL(8), section 8, system administration commands. Go forth and containerize responsibly π§ββοΈπ
π¦ where this page comes from
- freebsd 15.1: base 15.1
- π the page's license: BSD-2-Clause
- Copyright (c) 2000, 2003 Robert N. M. Watson
- Copyright (c) 2008-2012 James Gritton
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms