This page looks best with JavaScript enabled

Ezjails FreeBSD

 ·  🎃 kr0m

Ezjail is a utility that will help us manage jails on FreeBSD more easily. An interesting feature is that it uses a base template that is shared between jails through nullfs, so the base will be reused without using additional disk space. Updates made to the base will be automatically applied to the rest of the jails, and the ports system will behave analogously.

To use jails, we must create a secondary loopback interface, which will give us a completely isolated 127.0.0.0/8 network:

vi /etc/rc.conf

cloned_interfaces="lo1"
service netif cloneup

With this interface, all jails can see each other. The first jail will be 127.0.0.1, the next one will be .2, and so on.

We install ezjail:

pkg install ezjail

We enable it at startup:

sysrc ezjail_enable="yes"

Now we start it manually:

service ezjail start

We install the base template for all jails:

ezjail-admin install -p

We create the first jail:

ezjail-admin create kr0mjail ’lo1|127.0.0.1,em0|192.168.69.24'

If we want ping to work, the jail must use raw sockets. We edit the config to allow it. We only need to enable the options strictly necessary for the jail to provide service, and ping is not one of them, but in this example, we will enable it for easier debugging since it is our first jail:

vi /usr/local/etc/ezjail/kr0mjail

export jail_kr0mjail_parameters="allow.raw_sockets=1"

We start the jail:

ezjail-admin start kr0mjail

We check that it has started and that the network configuration is correct:

ezjail-admin list

STA JID  IP              Hostname                       Root Directory
--- ---- --------------- ------------------------------ ------------------------
DR  2    127.0.0.1       kr0mjail                       /usr/jails/kr0mjail
    2    em0|192.168.69.24

We enter the jail and assign a password to the root user:

ezjail-admin console kr0mjail

passwd

We enable ssh at startup:

sysrc sshd_enable="yes"

We start it manually:

service sshd start

We add a secondary user:

adduser

We configure the timezone:

tzsetup

adjkerntz will try to adjust the system time but the jail will not be able to do so, so we comment it out in the crontab:

sed -i .bak -e ‘/adjkerntz/ s/^/#/’ /etc/crontab

We configure the DNS servers:

echo “nameserver 8.8.8.8” > /etc/resolv.conf
echo “nameserver 8.8.4.4” » /etc/resolv.conf

We configure the /etc/hosts:

vi /etc/hosts

::1 localhost kr0mjail 
127.0.0.1 localhost kr0mjail

We should now have ssh access:

Base updates will be performed on the parent host using:

ezjail-admin update -u

If it is not a routine update but a version update, we must first find out which version we are starting from:

file /usr/jails/basejail/bin/sh

/usr/jails/basejail/bin/sh: ELF 64-bit LSB executable, x86-64, version 1 (FreeBSD), dynamically linked, interpreter /libexec/ld-elf.so.1, for FreeBSD 12.0 (1200086), FreeBSD-style, stripped

Then we would execute:

ezjail-admin update -U -s 12.0-RELEASE

Now we have to finish the update from within each jail, depending on whether the jail’s users/services are trustworthy or not, we will proceed differently.

Unreliable:
We mount the /usr/src directory inside the jail:

rm -rf /usr/jails/kr0mjail/usr/src
mkdir /usr/jails/kr0mjail/usr/src
mount -t nullfs -o ro /usr/src /usr/jails/kr0mjail/usr/src

We access the jail and run mergemaster:

ezjail-admin console kr0mjail
cd /usr/src
mergemaster -U
exit

Unmount /usr/src:

umount /usr/jails/kr0mjail/usr/src

Reliable:

mergemaster -U -D /usr/jails/kr0mjail

The ports are shared between jails, so we proceed to update them in all jails simultaneously:

ezjail-admin update -P

NOTE: Some ports need to be compiled with the JAIL option to work correctly.
NOTE: Deleting a jail is buggy, it leaves residual files.

We delete the jail:

ezjail-admin delete kr0mjail

If we try to create it again:

ezjail-admin create kr0mjail ’lo1|127.0.0.1,em0|192.168.69.24'
Error: A file or a non empty directory already exists at the specified jail root /usr/jails/kr0mjail.

We remove the immutable flag from the directory and delete it:

chflags -R noschg /usr/jails/kr0mjail
rm -rf /usr/jails/kr0mjail

Ezjail allows us to archive a jail and restore it on the same host or any other, thus we can duplicate jails quickly:

ezjail-admin stop kr0mjail
chflags noschg /usr/jails/kr0mjail//var/empty/
ezjail-admin archive kr0mjail
ls -la /usr/jails/ezjail_archives/

total 10
drwxr-xr-x 2 root wheel 3 Apr 25 20:46 .
drwxr-xr-x 7 root wheel 7 Apr 25 15:51 ..
-rw-r--r-- 1 root wheel 1033845 Apr 25 20:46 kr0mjail-201904251646.31.tar.gz

We can restore the archive with another name:

ezjail-admin create -a /usr/jails/ezjail_archives/kr0mjail-201904252046.31.tar.gz kr0mjail-clone ’lo1|127.0.0.2,em0|192.168.69.25'
ezjail-admin list

ezjail-admin list
STA JID  IP              Hostname                       Root Directory
--- ---- --------------- ------------------------------ ------------------------
DS  N/A  127.0.0.2       kr0mjail-clone                 /usr/jails/kr0mjail-clone
    N/A  em0|192.168.69.25
DS  N/A  127.0.0.1       kr0mjail                       /usr/jails/kr0mjail
    N/A  em0|192.168.69.24

We start the copy and the original jail:

ezjail-admin start kr0mjail-clone
ezjail-admin start kr0mjail

As a summary, I leave the basic operations performed in ezjail.

Start jail:

ezjail-admin start kr0mjail

Stop jail:

ezjail-admin stop kr0mjail

Disable auto-start:

ezjail-admin config -r norun kr0mjail

Enable auto-start:

ezjail-admin config -r run kr0mjail

If you liked the article, you can treat me to a RedBull here