Skip to content

FreeBSD: Jail Intro


Creating and Destroying the Jail


Creating the Jail

Prerequisites:
Vim needs to be installed on the machine
The following tasks were performed under the root user (use sudo for the normal user if preferred).

  1. Go to the directory /tmp so it can be removed later
  2. Replace the jail_name with your preferred name, this creates a directory for your jail.
  3. Install jail in the directory we just created.
cd /tmp
mkdir jail_name
bsdinstall jail jail_name

Just like how we install the FreeBSD on our machine.

  1. Create a jail.conf file under the directory /etc to hold our jail configuration.
/etc/jail.conf
# common configuration for all jails

allow.nomount;                                          # prevent child jails from doing stuff they're not supposed to
mount.devfs;                                            # mount '/dev'

path            = "~/jails/$name";                      # path to jail, unless specified otherwise

exec.start      = "/bin/sh      /etc/rc";               # what to do when the jail is started
exec.stop       = "/bin/sh      /etc/rc.shutdown";      # what to do when the jail is stopped
exec.clean;                                             # clear the previous environment

host.hostname   = "$name.jail.hostname";                # set hostname

ip4             = inherit;                              # inherit ip address from host
ip6             = inherit;

# jail specific configuration

jail_name {
        path = "/tmp/jail_name";
}
  1. To start the jail:
jail -c jail_name
  1. To open up the shell for the jail:
jexec jail_name /bin/sh
  1. To list all currently running jails:
jls

Output:

   JID  IP Address      Hostname                      Path
     x                  jail_name.jail.hostname       /tmp/jail_name
  1. To make the jail runs on boot
echo 'jail_enable="YES"' >> /etc/rc.conf

Detach the Jail

  1. To detach jail:
jail -r jail_name

We can restart the jail by using the -c flag (code above).


Remove the Jail Permanently

  1. To make sure the jail that will be removed has stopped:
jls

Be sure the output looks like this:

   JID  IP Address      Hostname                      Path
  1. You will need to force remove the jail:
chflags -R noschg jail_name
rm - rf jail_name
  1. To check if it is successfully removed:
ll jail_name

Expected Output:

ls: jail_name: No such file or directory

References (Creating and Destroying the Jail):
https://www.youtube.com/watch?v=T6gYmjtry50
Published inCS Experiment

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *