FreeBSD Jail Complete ExampleThe goals of the setup described below are: Create a simple and easy to understand jail structure. This implies not having to run a full installworld on each and every jail. Make it easy to add new jails or remove existing ones. Make it easy to update or upgrade existing jails. Make it possible to run a customized FreeBSD branch. Be paranoid about security, reducing as much as possible the possibility of compromise. Save space and inodes, as much as possible.As it has been already mentioned, this design relies heavily on having a single mastertemplate which is read-only (known as nullfs) mounted into each jail and one read-writedevice per jail. A device can be a separate physical disc, a partition, or a vnode backedmd(4) device. In this example, we will use read-write nullfs mounts.The file system layout is described in the following list: Each jail will be mounted under the /home/j directory. /home/j/mroot is the template for each jail and the read-only partition for all of the jails. A blank directory will be created for each jail under the /home/j directory. Each jail will have a /s directory, that will be linked to the read-write portion of the system. Each jail shall have its own read-write system that is based upon /home/j/skel. Each jailspace (read-write portion of each jail) shall be created in /home/js.Note: This assumes that the jails are based under the /home partition. This can, of course,be changed to anything else, but this change will have to be reflected in each of theexamples below.18.104.22.168 Creating the TemplateThis section will describe the steps needed to create the master template that will be theread-only portion for the jails to use.It is always a good idea to update the FreeBSD system to the latest -RELEASE branch.Check the corresponding Handbook Chapter to accomplish this task. In the case theupdate is not feasible, the buildworld will be required in order to be able to proceed.Additionally, the sysutils/cpdup package will be required. We will use the portsnap(8)utility to download the FreeBSD Ports Collection. The Handbook Portsnap Chapter isalways good reading for newcomers.
1. First, create a directory structure for the read-only file system which will contain the FreeBSD binaries for our jails, then change directory to the FreeBSD source tree and install the read-only file system to the jail template: 2. # mkdir /home/j /home/j/mroot 3. # cd /usr/src 4. # make installworld DESTDIR=/home/j/mroot 5. Next, prepare a FreeBSD Ports Collection for the jails as well as a FreeBSD source tree, which is required for mergemaster: 6. # cd /home/j/mroot 7. # mkdir usr/ports 8. # portsnap -p /home/j/mroot/usr/ports fetch extract 9. # cpdup /usr/src /home/j/mroot/usr/src 10. Create a skeleton for the read-write portion of the system: 11. # mkdir /home/j/skel /home/j/skel/home /home/j/skel/usr-X11R6 /home/j/skel/distfiles 12. # mv etc /home/j/skel 13. # mv usr/local /home/j/skel/usr-local 14. # mv tmp /home/j/skel 15. # mv var /home/j/skel 16. # mv root /home/j/skel 17. Use mergemaster to install missing configuration files. Then get rid of the extra directories that mergemaster creates: 18. # mergemaster -t /home/j/skel/var/tmp/temproot -D /home/j/skel - i 19. # cd /home/j/skel 20. # rm -R bin boot lib libexec mnt proc rescue sbin sys usr dev 21. Now, symlink the read-write file system to the read-only file system. Please make sure that the symlinks are created in the correct s/ locations. Real directories or the creation of directories in the wrong locations will cause the installation to fail. 22. # cd /home/j/mroot 23. # mkdir s 24. # ln -s s/etc etc 25. # ln -s s/home home 26. # ln -s s/root root 27. # ln -s ../s/usr-local usr/local 28. # ln -s ../s/usr-X11R6 usr/X11R6 29. # ln -s ../../s/distfiles usr/ports/distfiles 30. # ln -s s/tmp tmp 31. # ln -s s/var var 32. As a last step, create a generic /home/j/skel/etc/make.conf with its contents as shown below: 33. WRKDIRPREFIX?= /s/portbuild Having WRKDIRPREFIX set up this way will make it possible to compile FreeBSD ports inside each jail. Remember that the ports directory is part of the read-only system. The custom path for WRKDIRPREFIX allows builds to be done in the read- write portion of every jail.22.214.171.124 Creating Jails
Now that we have a complete FreeBSD jail template, we can setup and configure the jailsin /etc/rc.conf. This example demonstrates the creation of 3 jails: “NS”, “MAIL” and“WWW”. 1. Put the following lines into the /etc/fstab file, so that the read-only template for the jails and the read-write space will be available in the respective jails: 2. /home/j/mroot /home/j/ns nullfs ro 0 0 3. /home/j/mroot /home/j/mail nullfs ro 0 0 4. /home/j/mroot /home/j/www nullfs ro 0 0 5. /home/js/ns /home/j/ns/s nullfs rw 0 0 6. /home/js/mail /home/j/mail/s nullfs rw 0 0 7. /home/js/www /home/j/www/s nullfs rw 0 0 Note: Partitions marked with a 0 pass number are not checked by fsck(8) during boot, and partitions marked with a 0 dump number are not backed up by dump(8). We do not want fsck to check nullfs mounts or dump to back up the read-only nullfs mounts of the jails. This is why they are marked with “0 0” in the last two columns of each fstab entry above. 8. Configure the jails in /etc/rc.conf: 9. jail_enable="YES" 10. jail_set_hostname_allow="NO" 11. jail_list="ns mail www" 12. jail_ns_hostname="ns.example.org" 13. jail_ns_ip="192.168.3.17" 14. jail_ns_rootdir="/usr/home/j/ns" 15. jail_ns_devfs_enable="YES" 16. jail_mail_hostname="mail.example.org" 17. jail_mail_ip="192.168.3.18" 18. jail_mail_rootdir="/usr/home/j/mail" 19. jail_mail_devfs_enable="YES" 20. jail_www_hostname="www.example.org" 21. jail_www_ip="126.96.36.199" 22. jail_www_rootdir="/usr/home/j/www" 23. jail_www_devfs_enable="YES" Warning: The reason why the jail_name_rootdir variable is set to /usr/home instead of /home is that the physical path of the /home directory on a default FreeBSD installation is /usr/home. The jail_name_rootdir variable must not be set to a path which includes a symbolic link, otherwise the jails will refuse to start. Use the realpath(1) utility to determine a value which should be set to this variable. Please see the FreeBSD-SA-07:01.jail Security Advisory for more information. 24. Create the required mount points for the read-only file system of each jail: 25. # mkdir /home/j/ns /home/j/mail /home/j/www 26. Install the read-write template into each jail. Note the use of sysutils/cpdup, which helps to ensure that a correct copy is done of each directory: 27. # mkdir /home/js 28. # cpdup /home/j/skel /home/js/ns 29. # cpdup /home/j/skel /home/js/mail
30. # cpdup /home/j/skel /home/js/www 31. In this phase, the jails are built and prepared to run. First, mount the required file systems for each jail, and then start them using the /etc/rc.d/jail script: 32. # mount -a 33. # /etc/rc.d/jail startThe jails should be running now. To check if they have started correctly, use the jls(8)command. Its output should be similar to the following:# jls JID IP Address Hostname Path 3 192.168.3.17 ns.example.org /home/j/ns 2 192.168.3.18 mail.example.org /home/j/mail 1 188.8.131.52 www.example.org /home/j/wwwAt this point, it should be possible to log onto each jail, add new users or configuredaemons. The JID column indicates the jail identification number of each running jail.Use the following command in order to perform administrative tasks in the jail whoseJID is 3:# jexec 3 tcsh184.108.40.206 UpgradingIn time, there will be a need to upgrade the system to a newer version of FreeBSD, eitherbecause of a security issue, or because new features have been implemented which areuseful for the existing jails. The design of this setup provides an easy way to upgradeexisting jails. Additionally, it minimizes their downtime, as the jails will be brought downonly in the very last minute. Also, it provides a way to roll back to the older versionsshould any problems occur. 1. The first step is to upgrade the host system in the usual manner. Then create a new temporary read-only template in /home/j/mroot2. 2. # mkdir /home/j/mroot2 3. # cd /usr/src 4. # make installworld DESTDIR=/home/j/mroot2 5. # cd /home/j/mroot2 6. # cpdup /usr/src usr/src 7. # mkdir s The installworld run creates a few unnecessary directories, which should be removed: # chflags -R 0 var # rm -R etc var root usr/local tmp 8. Recreate the read-write symlinks for the master file system: 9. # ln -s s/etc etc 10. # ln -s s/root root 11. # ln -s s/home home
12. # ln -s ../s/usr-local usr/local13. # ln -s ../s/usr-X11R6 usr/X11R614. # ln -s s/tmp tmp15. # ln -s s/var var16. The right time to stop the jails is now:17. # /etc/rc.d/jail stop18. Unmount the original file systems:19. # umount /home/j/ns/s20. # umount /home/j/ns21. # umount /home/j/mail/s22. # umount /home/j/mail23. # umount /home/j/www/s24. # umount /home/j/www Note: The read-write systems are attached to the read-only system (/s) and must be unmounted first.25. Move the old read-only file system and replace it with the new one. This will serve as a backup and archive of the old read-only file system should something go wrong. The naming convention used here corresponds to when a new read- only file system has been created. Move the original FreeBSD Ports Collection over to the new file system to save some space and inodes:26. # cd /home/j27. # mv mroot mroot.2006060128. # mv mroot2 mroot29. # mv mroot.20060601/usr/ports mroot/usr30. At this point the new read-only template is ready, so the only remaining task is to remount the file systems and start the jails:31. # mount -a32. # /etc/rc.d/jail start