Tutorial 13 · FreeBSD

Jails (Bastille and plain jail.conf)

Run isolated FreeBSD environments, either managed with Bastille or configured by hand with jail.conf.

Last updated

A jail is a lightweight, isolated FreeBSD userland that shares the host kernel. You can manage jails with a tool such as Bastille, or directly with the base system's jail.conf. Examples use the documentation address 192.0.2.50 on em0.

1Option A: Bastille

pkg install bastille
sysrc bastille_enable=YES
bastille setup              # prepares networking, PF and ZFS settings; review its prompts
bastille bootstrap 15.1-RELEASE update
bastille create alpha 15.1-RELEASE 192.0.2.50 em0

Common Bastille commands:

bastille list
bastille console alpha           # root shell inside the jail
bastille cmd alpha freebsd-version
bastille pkg alpha install -y htop
bastille stop alpha
bastille start alpha
bastille destroy alpha

Bastille's options change between versions; bastille help and its documentation are the reference for the version you install.

2Option B: plain jail.conf

Create a dataset for the jail and extract a base system into it.

zfs create -p -o mountpoint=/usr/local/jails zroot/jails
zfs create zroot/jails/beta
fetch https://download.freebsd.org/releases/amd64/15.1-RELEASE/base.txz
tar -xf base.txz -C /usr/local/jails/beta --unlink
cp /etc/resolv.conf /etc/localtime /usr/local/jails/beta/etc/

Describe the jail in /etc/jail.conf (or a file in /etc/jail.conf.d/):

beta {
  path = "/usr/local/jails/beta";
  host.hostname = "beta.example.org";
  ip4.addr = "em0|192.0.2.51/32";
  exec.start = "/bin/sh /etc/rc";
  exec.stop = "/bin/sh /etc/rc.shutdown jail";
  exec.clean;
  mount.devfs;
}
sysrc jail_enable=YES
sysrc jail_list+="beta"
service jail start beta
jls
jexec beta /bin/sh

3Updating a jail

A plain jail extracted from base.txz is a traditional install, so it can be patched from the host:

freebsd-update -b /usr/local/jails/beta fetch install
pkg -j beta upgrade

← All FreeBSD tutorials