The FreeBSD Diary |
(TM) | Providing practical examples since 1998If you buy from Amazon USA, please support us by using this link. |
Using a jail as a virtual machine
1 September 2004
|
This article shows you how I created a jail for the OSW website. It runs in a jail on the same system as this website. I originally did this install back in November 2003 and the notes from that session form the basis of this article. I have need to recreate the jail now as we recently had an HDD failure. NOTE: This article applies to FreeBSD 4. I have since written about jail on FreeBSD 5, and it appears to apply to FreeBSD 6.x just as well. I recommend read that article over this one. A jail is useful for many purposes. In my case, I wanted to give the OSW project a place to run their websites, mailing lists, etc, but at the same time keep them isolated from the rest of the machine. In short, it gives them a virtual machine, and it gives me peace of mind knowing that I have less to worry about with respect to the rest of the machine. The main document for creating a jail is man jail. I followed the instructions listed under Setting up a Jail Directory Tree. I followed those instructions to create the jail. Of note, I did not do this:
|
Modifying other daemons
|
Most daemons will listen to whatever IP addresses are available to them. After starting your jail, if you try to ssh to it, you will not get into it. You'll be in the host environment instead. To get into the jail environment via ssh, you need to:
Host environment sshdTo alter the host environment sshd so it listens only to host environment IP addresses, modify/etc/ssh/sshd_config and set the IP address for the Listen directive:
Then restart the main sshd process:
Use telnet to verify that the host environment is not listening on the jail address:
If you don't get a connection, the host environment is not listening. This assumes that you have not yet
started sshd in the jail environment.
Jail environment sshd
To start sshd in the jail environment, add the following line to
|
Starting the jail for the first time
|
From man jail, to start a jail, issue this command:
That prompt (#) indicates you are now in the jail environment. Now you can run the start up processes:
For the most part, this looks exactly like a normal startup. A few things to note
|
Starting and stopping the jail automagically
|
I found two interesting tools for starting and stopping the jails: sysutils/jailer and sysutils/jailutils. sysutils/jailer is installed in the jail environment. sysutils/jailutils should be installed in the host environment. Using those two tools, I created this start/stop script:
This is a very limited script. It doesn't check that a jail is already running before starting it. That would be a nice addition. If you want to add it, I look forward to your patch.
In addition, you might want to add this to the host environment's
Under 5.*, this variable has a slightly different name. |