İçindekiler

FreeBSD Jail

FreeBSD is a method used to create a virtual system within our system. The systems created with this method are completely independent from the main system. In this way, they are not affected by a possible security vulnerability. Also, when a service running in the jail is compromised, the services running on the main system are not affected.

Jails can be thought of as a kind of operating system-level virtualization.

It is a system that makes FreeBSD stand out from other operating systems in terms of security.

Bu sayfada anlatılan installation and usage information. It is about creating a completely separate structure called Thick Jail.

In addition, VNET features will be used to enable Jails to run on a separate network.

The installed jail will have all the basic FreeBSD files.

The FreeBSD version running in the jail cannot be more up-to-date than the FreeBSD version running on the main system.

This article will explain Jail installation, usage and management.

  • Internet access is required on your FreeBSD system.
  • The __JAIL_NAME__ used throughout the article will be a name of your choice. The name uch32bitjail is used in the example commands.
  • The __VERSION__ used throughout the article is the FreeBSD version of the jail you will install. The version used in the example commands is 13.2-RELEASE.
  • Throughout the article, __ARCHITECTURE__ is the FreeBSD architecture of the jail you will install. The architecture used in the example commands is i386.
    • The example commands are used for i386 jail installation in amd64 architecture.

FreeBSD Jail Installation

Jail installation steps can be listed as follows.

  1. Creation of folders.
  2. Downloading the required base archive.
  3. Making settings on the main system.
  4. Creating and editing jail configuration files.

Folder Structure

Jails do not have to be kept in a specific directory. The script will use the directory /usr/jails. You can change it if you want…

Run and create the folders by changing the necessary places in the command below.

mkdir -p /usr/jails/files
mkdir /usr/jails/__JAIL_NAME__

The -p parameter creates folders recursively. So if the folder /usr/jails does not exist, it creates it too.

Downloading Required Files

The downloaded files will be kept in /usr/jails/files to avoid re-downloading them when needed.

You can use it to quickly install a new jail if needed. However, if you are short on space, you can download these files again for each jail installation.

fetch https://download.freebsd.org/ftp/releases/__ARCHITECTURE__/__ARCHITECTURE__/__VERSION__/base.txz -o /usr/jails/files/__VERSION__-__ARCHITECTURE__-base.txz
Example;
fetch https://download.freebsd.org/ftp/releases/i386/i386/13.2-RELEASE/base.txz -o /usr/jails/files/13.2-RELEASE-i386-base.txz
tar -xpf /usr/jails/files/__VERSION__-__ARCHITECTURE__-base.txz -C /usr/jails/__JAIL_NAME__ --unlink
Example;
tar -xpf /usr/jails/files/13.2-RELEASE-i386-base.txz -C /usr/jails/uch32bitjail --unlink
rm -f /usr/jails/files/__VERSION__-__ARCHITECTURE__-base.txz
Example;
rm -f /usr/jails/files/13.2-RELEASE-i386-base.txz

Main System Settings

For jails to work, some adjustments need to be made on the main system.

cp /etc/resolv.conf /usr/jails/__JAIL_NAME__/etc/resolv.conf
cp /etc/localtime /usr/jails/__JAIL_NAME__/etc/localtime
Örnek;
cp /etc/resolv.conf /usr/jails/uch32bitjail/etc/resolv.conf
cp /etc/localtime /usr/jails/uch32bitjail/etc/localtime
freebsd-update -b /usr/jails/__JAIL_NAME__/ fetch install
Örnek;
freebsd-update -b /usr/jails/uch32bitjail/ fetch install
sysrc jail_enable="YES" # Initially to start the jails.
sysrc jail_parallel_start="YES" # If you add this line, the jails will be started in the background.

VNET Settings

The jail prepared in this article is prepared completely independently from the main system. Therefore, the jail needs to run on a separate network. VNET will be used for this.

If you don't want your jail to have internet access via a separate network 2) you can skip this part.

ifconfig bridge create
It should give you an output. This output contains the name of the bridge adapter. Make a note of this name.
As an example bridge0
Here em0 is the adapter name of the device. This name may be different in your system. You can use the ifconfig command to find out.

Below is a sample output. You can see the name in the highlighted line.
  1. em0: flags=8863<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
  2. options=481009b<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,VLAN_HWCSUM,VLAN_HWFILTER,NOMAP>
  3. ether 08:00:27:06:c2:65
  4. inet 192.168.1.47 netmask 0xffffff00 broadcast 192.168.1.255
  5. media: Ethernet autoselect (1000baseT <full-duplex>)
  6. status: active
  7. nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
ifconfig bridge0 addm em0
defaultrouter="192.168.1.1"
cloned_interfaces="bridge0"
ifconfig_bridge0="inet 192.168.1.150/24 addm em0 up"

Jail Configuration

There are two ways to set configuration files for jails. If you are going to use a lot of jails, it makes more sense to choose the second way.

First Way

Configuration information is kept in a single file. This file is /etc/jail.conf.

You need to add new entries to this file for each new jail.

Second Way

Configuration information is kept in a separate file for each jail. These files are kept in the /etc/jail.conf.d/ folder.

For each new jail you need to create a new file in this folder. This method avoids confusion if you have many jails.


Open the configuration file according to the selected method and add the highlighted lines by editing them.

Detailed information is written next to them as a comment.

Pay attention to the line breaks in the files to be created. Use LF instead of CRLF. Otherwise unwanted results may occur.

/etc/jail.conf
__JAIL_NAME__ { # Jail'in ismi
  exec.start = "/bin/sh /etc/rc"; # Jail başlatıldığında çalıştırılacak olan komutlar.
  exec.stop = "/bin/sh /etc/rc.shutdown"; # Jail kapatıldığında çalıştırılacak olan komutlar.
  exec.consolelog = "/var/log/jail_console_${name}.log"; # Komu çıktılarının yazılacağı dosya.
 
  allow.raw_sockets; # Raw soketlerin kullanılmasına izin verir. (ping gibi)
  exec.clean; # Komutları temiz bir ortamda çalıştırılması.
  mount.devfs; # /dev dizinine bir devfs(5) dosya sistemi ekleme.
 
  host.hostname = "${name}"; 
  path = "/usr/jails/${name}"; # Komutların çalıştırılacağı dizin. Kısaca jail'in kurulu olduğu dizin.
 
  vnet;
  vnet.interface = "${epair}b";
  $id = "154"; # Ip adresinin son kısmını hesaplayacak olan değer. Her jail için farklı olmalıdır.
  $ip = "192.168.1.${id}/24";
  $gateway = "192.168.1.1";
  $bridge = "bridge0"; # vnet ayarlamalarında oluşturulan köprü ismi.
  $epair = "epair${id}";
  exec.prestart += "ifconfig ${epair} create up";
  exec.prestart += "ifconfig ${epair}a up descr jail:${name}";
  exec.prestart += "ifconfig ${bridge} addm ${epair}a up";
  exec.start    += "ifconfig ${epair}b ${ip} up";
  exec.start    += "route add default ${gateway}";
  exec.poststop = "ifconfig ${bridge} deletem ${epair}a";
  exec.poststop += "ifconfig ${epair}a destroy";
}

Jail Management

jls
service jail start __JAIL_NAME__
service jail onestart __JAIL_NAME__
service jail stop __JAIL_NAME__
service jail restart __JAIL_NAME__

Jail'i Silme

Since security is a top priority in FreeBSD, even root privileges are not enough to delete some files. File flags control this system.

chflags -R 0 /usr/jails/__JAIL_NAME__
rm -rf /usr/jails/__JAIL_NAME__

Jail Package Management

If you have not given internet access to your jail.3) You can use the following command to install packages from the main system to jail.

pkg -j __JAIL_NAME__ install __PACKET_NAME__
Example;
pkg -j uch32bitjail install wget

Jail Access

Although it is more convenient to manage the jail through the main system, you can access the jail with the following command.

jexec -u root __JAIL_NAME__

Command Execution on Jail

To run a command on Jail, you need to add the following prefix to the command you will run.

jexec -l __JAIL_NAME__ __COMMAND__
Örnek;
jexec -l uch32bitjail service stop apache24

Taken from UCH Wiki. https://wiki.ulascemh.com/doku.php?id=en:cs:op:bsd:jail

1)
If you want to start them manually, skip this step. The command to start the jail will change according to this step.
2)
no internet connection at all or sharing the same ip with the main system
3)
In this article, this part is omitted because internet access is given to jails with vnet.