Creating and Managing A Jailed Virtual Host in FreeBSD
It is possible to spawn a completely jailed second (or many!) operating system within a modern FreeBSD install. Doing so can be tricky, but here I will document the method that I have found works for me. The first most important resource about jails, is the man page, and many of the examples that you will see are basically straight following of the man page. Once you are done, each jail will operate as if its a complete independant operating system.
Scope Of This Document
This Howto article is intended to be a practical example, and I will start my host system with FreeBSD 6.2-RELEASE. The Install will be of the "minimal" variety, and for the first part of this document, we will not update the system with buildworld. After a jail is created, we will then update the host, and then update the jail. This will demonstrate a practical example of how to build, and then maintain a jail thru critical security releases.
System Preparation
Before we begin this part of the article, I will assume that we have already built our FreeBSD 6.2-RELEASE system. The example laid out in Installing FreeBSD 6.2 might be a good place to start, but only do the first half of the article (do not proceed to step 3, "Rebuild the World, and Recompile the Kernel") As we begin, there are a few tasks you must take care of on your host system to prepare it to run jails.
1) You will need the full sources tree in /usr/src/. Using 'csup' is the best way to do this. While you are doing this, you may as well use csup to get the latest ports tree too.
2) Specify another IP address as an alias, that your jailed operating system will use.
ifconfig_fxp0="inet 192.168.1.180 netmask 255.255.255.128" ifconfig_fxp0_alias0="inet 192.168.1.181 netmask 255.255.255.128"
As you can see, my main ip is 192.168.1.180, and my alias ip is 192.168.1.181. We will need to specify this alias ip later when we start the jail.
3) You should then cull back all the services on the host system to use only the hosts main IP address. Most of these changes would happen in your /etc/rc.conf file, but some happen in the config files of the daemon. A good example is sshd. In order for it to listed only on one IP address, we must edit /etc/ssh/sshd_config. Look for the line:
#ListenAddress 0.0.0.0
Uncomment that, and change the zeros to your host's main IP address. Another daemon off the top of my head that would use its own config file for this type of configuration, would be Apache. However, remember that I recommend the slimmest configuration possible on your host, so not installing Apache or any other network applications would be advised. After all, the point of jails is to install virtual systems that all our network daemons can then be installed into. My host system does use inetd, so I add this to my /etc/rc.conf to keep inetd only on the hosts IP addrress:
inetd_enable="YES" inetd_flags="-wW -a 192.168.1.180"
On my systems, I use snmpd. However, snmpd will not be able to be used correctly against jailed instances, but only against the host. So, to keep snmpd from operating on all IP addresses available (which is the default behavior), we need to add this line to /usr/local/share/snmp/snmpd.conf:
agentaddress 192.168.1.180:161
Realistically, you need to evaluate your host system's needs, and personalize the "slimming down" of your host to what you need. The
Building a Jailed Virtual Host
The man page specifies a little script that can get you started. From the man page:
D=/here/is/the/jail cd /usr/src mkdir -p $D make world DESTDIR=$D make distribution DESTDIR=$D
What we have here, is that we will specify 'D' is the path we will put our jailed system in, and that's really the hardest part. I named my script 'mkjail'. For 'D', I specified /usr/jails/[hostname] (in my case, my hostname was TEMPLATE, so I had /usr/jails/template). For my first jail, I always create one that I can use as a template to just spawn others, without going through the whole scripted 'make world' and 'make distribution' again.
I then ran the script with 'sh mkjail'. I think it probably took about 2 hours for my system.
Copy the /etc/resolv.conf into your jail. Without this, your jail will not be able to find its way out to the internet.
cp /etc/resolv.conf /usr/jails/template/etc/
Also, create the jail's /etc/make.conf, so that later when we install a port, our jail will use a local workdir, instead of writing into our hosts workdir (we don't like conflicts!). Add this line to the jail's /etc/make.conf:
WRKDIRPREFIX=/tmp
Also, we need to make a ports directory so that we can nullfs mount the hosts ports to it later.
mkdir /usr/jails/template/usr/ports
After that, we are ready to archive our jail template, so we can reuse it later.
cd /usr/jail tar zcvf jail-template.tar.gz /usr/jails/template
At this point, our jail template directories can be renamed to be used for our example. I am going to name my jail ANTARES.
mv /usr/jails/template /usr/jails/antares
Final step is mounting a proc and devfs file system to the jail, and we can start an interactive shell on it.
mount_devfs devfs /usr/jails/antares/dev mount_procfs procfs /usr/jails/antares/proc jail /usr/jails/antares/ antares 192.168.1.181 /bin/sh
This will drop us to a basic shell. Before we run our jail for real, there are a few things to take care of in the jail. First, create a "dummy" fstab to keep the startup scripts from complaining.
touch /etc/fstab
Then, run sysinstall, go to 'configure', and use the timezone tool to set your timezone. Finally, set the root password with the 'passwd' command. After that, were ready to try it out. Hit a ctrl-d to exit the shell. Now, lets start it up so that it exec the startup scripts.
jail /usr/jails/antares/ antares 192.168.1.181 /bin/sh /etc/rc
You should see what appears to be a normal start up, ending at a root prompt. The first thing I alawys need, is my favorite editor, nano.
pkg_add -r nano
Then lets also add a couple entries to the jail's /etc/rc.conf.
network_interfaces="" rpcbind_enable="NO" sshd_enable="YES" syslogd_flags="-ss"
Eliminate 'adjkerntz -a' from the jail's /etc/crontab. We don't need this in a jail (man jail), the CMOS clock is already managed by the host. The entire line to comment out or remove looks like this:
1,31 0-5 * * * root adjkerntz -a
Now you can ctrl-d and exit back out of that shell. Now its time to add some information to the host's /etc/rc.conf, so that our jail will automatically start with the host. Add this to the host's /etc/rc.conf:
jail_enable="YES" jail_interface="fxp0" jail_devfs_enable="YES" jail_procfs_enable="YES" jail_list="antares" jail_antares_rootdir="/usr/jails/antares" jail_antares_hostname="antares.example.com" jail_antares_ip="192.168.1.181"
After this is added, reboot the host. When it comes backup, log in as root, and use the 'jls' command to see the running jail.
[root@acrux /usr/jails]# jls JID IP Address Hostname Path 1 192.168.1.181 antares.example.com /usr/jails/antares
Looks good, now try to ssh into your new jailed instance of FreeBSD. If it works, then throw your arms in the air, and yell "SUCCESS!!"
Installing Ports and Updating the World
If you have read my article Managing Multiple FreeBSD Systems, then you might know that I only like to keep one copy of the source tree and ports tree on my network. This saves bandwidth at both freebsd.org's end, as well as my own internet connection. But on top of that, 'buildworld' only needs to be run one time, and can then be distributed to other machines. This theory also includes jailed instances. From earlier in this article, our host system pulled down both the operating system sources and ports trees. Let's nullfs mount them to our running jail, so that our jail can take advantage of these resources.
mount_nullfs /usr/ports/ /usr/jails/antares/usr/ports/ mount_nullfs /usr/src/ /usr/jails/antares/usr/src/ mount_nullfs /usr/obj/ /usr/jails/antares/usr/obj/
In your jail, you should now see the same contents in /usr/src, /usr/obj, and /usr/ports as your host system. We already edited our jail's /etc/make.conf, so when we install ports, we won't step on our host's toes. The jail is now ready to build any ports you intend to run.
But ports are not all our system will need. At this point, we still need to buildworld and update both our host and our jail. So, drop out of our ssh session, and go back to our host. Change into the hosts sources directory.
cd /usr/src
Edit a config file file to match your system, and save it as an ALLCAPS name to /usr/src/sys/i386/conf (if you dont have an i386 machine, remember to save the config file to the appropriate architecture's folder tree). As you may have noticed above, my host's name is ACRUX, and this is the name of my config file. After the file is saved, its time to build the world and the kernel.
make buildworld make buildkernel KERNCONF=ACRUX
Those steps will probably eat up an hour or 2, as they do on my system. After they are done, follow the steps laid out in either the FreeBSD handbook, or my daring way (at your OWN RISK!!) in my article The Method I Use When I Buildworld On My Personal Systems. After your kernel and world are installed on your host system, reboot.Once the host system is backup, log in as root, and now we can update the jail. If you ssh into your jail, you will notice that the new kernel is already installed, as the jail always runs from a copy of the current running kernel of the host.
cd /usr/src make installworld DESTDIR=/usr/jails/antares
Remount our nullfs resources to the jail, so that we can perform our last step for updating the world.
mount_nullfs /usr/ports/ /usr/jails/antares/usr/ports/ mount_nullfs /usr/src/ /usr/jails/antares/usr/src/ mount_nullfs /usr/obj/ /usr/jails/antares/usr/obj/
Finally its time to ssh into the jail, and apply the final part of the update. Log in to your jail as root, and:
cd /usr/src mergemaster
Once this is completed, the jail can be restarted from within a shell on the host, like this:
/etc/rc.d/jail restart antares
And now our end product should be both a host and a jail that are fully up to date.
Written by Sharaz, our resident FreeBSD guru.
Well explained
Well explained post.
discover card account login
Detailed explanation. Very
Detailed explanation. Very good. Keep it up.
Pottery Barn Coupon Codes
Hi! I using ZFS for create
Hi!
I using ZFS for create template jail on my FreeBSD server. Why? See... :)
# zfs create dpool/jails/master
# zfs list
NAME USED AVAIL REFER MOUNTPOINT
dpool 2.32G 13.3G 23K /dpool
dpool/jails 18k 13.3G 18K /dpool/jails
dpool/jails/master 18k 13.3G 18k /dpool/jails/master
# cd /usr/src
# [... regular steps]
# zfs list
NAME USED AVAIL REFER MOUNTPOINT
dpool 2.32G 13.3G 23K /dpool
dpool/jails 174M 13.3G 22K /dpool/jails
dpool/jails/master 174M 13.3G 174M /dpool/jails/master
# df -h
Filesystem Size Used Avail Capacity Mounted on
[... regular df items]
dpool 13G 0B 13G 0% /dpool
dpool/jails 13G 0B 13G 0% /dpool/jails
dpool/jails/mail 13G 0B 13G 0% /dpool/jails/mail
dpool/jails/master 13G 174M 13G 1% /dpool/jails/master
The master jail has been created, and 174MBytes space allocated. Now, take a clone from master:
# zfs snapshot dpool/jails/master@20080804
# zfs list
# zfs clone dpool/jails/master@20080804 dpool/jails/test
NAME USED AVAIL REFER MOUNTPOINT
dpool 2.32G 13.3G 23K /dpool
dpool/jails 174M 13.3G 22K /dpool/jails
dpool/jails/master 174M 13.3G 174M /dpool/jails/master
dpool/jails/master@20080804 33K - 174M -
dpool/jails/test 18K 13.3G 174M /dpool/jails/test
The "test" jail has been cloned successful. Look at the "used" space: its only 18kBytes, this is the difference from the master... we can boot and use this a regular way... :)
# jail /dpool/jails/test/ test 192.168.1.1 /bin/sh
# df -h
Filesystem Size Used Avail Capacity Mounted on
dpool/jails/test 13G 174M 13G 1% /
# exit
Hmm... 13GBytes? Ehh... this is too much for this zone...
# zfs set quota=256M dpool/jails/test
# jail /dpool/jails/test/ test 192.168.1.1 /bin/sh
# df -h
Filesystem Size Used Avail Capacity Mounted on
dpool/jails/test 430M 174M 256M 41% /
# exit
Hmm... looks like better... :)
--
http://www.javaforum.hu
Cool post! Very useful. home
Cool post! Very useful.
very good informtion
very good informtion
thank you
dfp zone
cool
Very useful!
____
mp3 downloads
yep
Thank you for this manual!
Hey, Good article,Thanks for
Hey,
Good article,Thanks for it.Örgü
I did came across a simillar task recently.
Örgü
Modelleri
I have implemented it using PLSQL , I did reffered mine with this article,they
have same logic.
Örgü
Teknikleri
Hope many other friends would have Hobi had a very simiilar case, very helpful.Sihirbazlık
HELP PLEASE
I am trying to use apache22 with pcbsd to develop a database. When ever I try to start apache22 I get the following error message:
/libexec/ld-elf.so.1: Shared object "libthr.so.3" not found, required by "libaprutil-1.so.2"
I have search the internet for help on this problem and can‘t find any, HELP PLEASE.
physical security
Now all you need is a security system for the building hosting the server and things should be pretty safe.
informative
Great tutorial. Thanks for that. Having a jailed host within a free BSD installation is a great idea and I had never known how to get that installed. Your tutorial helped me lots. Thanks. You have mentioned the steps very clearly and following it step by step works. Tried and got it installed. Many thanks for that.
Thanks for
Thanks for information..
thanks
thanks for sharing useful information.
this clarifies the process
this clarifies the process of makeing jails.
Rodger
great tutorial. was very
great tutorial. was very helpful
Wright
this clarifies the process
this clarifies the process of makeing jails.
Robert Johnson
great article
great article. Thanks for sharing!
Kevin Sender
On my test
I use a very light web server software that takes very few resources from your server, but also has fewer capabilities. When a website is serving both lots of images, javascripts, cascading style sheets(static content) and dynamic scripts (ASP, PHP, JSP or more), it is not wise to let apache serve both static and dynamic content. That is why, lighttpd can run besides apache on another port and serve requests for the static content of your website.---buy dvd
take a look..
This is good article to understand process of makeing jails.
After some understanding is reached - I would advice to take a look at jailtools (bundle of scripts for managing jails like jail_install, jail_start, jail_stop).
http://the-labs.com/FreeBSD/JailTools/
It simplifies a lot managing of jails and it is also possible to create jails as image files which you can migrate from one jail host to another afterwards.
Good luck!
Thank you for this
Thank you for this article.
Şarkı Sözleri
There's a better way to manage jails
There's a port sysutils/ezjail, which does all that boring work for you, especially when it's not your first jail ;)
It allows creating and managing jails with one command, mounting needed nullfs filesystems on jail startup. Аlso ezjail makes use of FreeBSD's native /etc/rc.d/jail script, just providing simple way for that.