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 under FreeBSD 5. Since writing this article, FreeBSD 6.0 has been released, however, the strategy should remain the same. I'll update the article with any changes should anything be different. NOTE: on 15 Feb 2007, I used this same technique to create a jail on FreeBSD 6.2 This article was originally published at ONLamp. I have written previously about jails on FreeBSD 4. The goal of this jail is the creation of a test environment for a project I've been working on. Until recently, I've been providing a dedicated machine for exclusive use by the the Bacula network backup project. That system was used to run regression tests on FreeBSD. In a recent consolidation of hardware, I replaced several older machines with one newer machine. I wanted to dispose of the computer used by the Bacula project and move them to a more powerful computer. However, I didn't want them to have exclusive use of this system. I wanted to use the same computer and not have us interfere with each other. |
Lift and separate
|
Jails can be used to separate different processes and keep them apart from each other so they cannot interfere. For example, you could run Apache in a jail and keep it away from everything else on the machine. Should an exploit be found in Apache and used to compromise your system, the intruders can only do what the jail allows them to do. A jail can consist of a full operating system, or a single executable. The solution I used: create a virtual machine for use by the Bacula project. I had recently acquired a Pentium 4 2.4 GHz machine. It was pretty fast, so I decided to use this for system for my own development purposes. It will be sitting idle for long periods of time so I might as well let some else use it as well. I don't want them to have access to the things I'm working on, so I'll put them in a jail. From within a jail, they are chroot'd and cannot see anything outside of the jail. At the same time, it appears to them as if they are running on their own machine with their own operating system. As far as they know, they have their own computer and nobody else is on the system. Running a virtual system within a jail is a good solution if you want to provide someone with resources, but don't want them to have complete control over your system. A jail can help you deal with issues of security, access, and increase utilization of existing resources, all at the same time. |
Jail documentation
|
The main document for creating a jail is
man jail.
I followed the instructions listed under Setting up a Jail Directory
Tree. I used those instructions to create the jail. You will need the full
source tree for the system you going to create. I used the
One step from man jail that I did not follow:
I put my jail at
|
Terminology: host versus jail
|
The host environment is the main system and is where you first install FreeBSD on the computer. It is in the host environment that you create a jail. The Bacula project will do their testing in the jail. They have access to the jail and only the jail. They will not have access to the host environment at all. This concept of host environment and jail environment will be used later in this article. It is important that you understand what each one is. In this example, the host environment will be at IP address 192.168.0.100 and the jail will be at 192.168.0.155. |
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 syslogd
This entry in
That allows Host environment inetd
This entry in
You should note that the first part of the above flags is from
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
To get DNS working, add something like this to /etc/resolv.conf: search example.org nameserver 10.0.0.67 nameserver 10.0.0.98 Jail environment syslogd
In addition, I also swapped console output to
|
Configuring the Jail
|
Next, you'll want to read the part of the man page titled
|
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. While you have it running, you might want to add a user, set the root password, etc. I had some problems with ps:
This usually indicate a kernel that is not in sync with world. To fix this problem, I repeated some of the steps under man 8 jail.
In hindsight, I think I missed the |
Starting and stopping the jail automagically
|
I found an interesting tool for starting and stopping a jail: sysutils/jailutils. I installed it in the host environment. Using this tool, 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 4.*, this variable had a slightly different name. |
Additional resources
|
Just after this article appeared on ONLamp, Anthony Nguyen wrote in with these great
resources:
He also mentioned:
Paul Dekkers also wrote in with:
|
Jails run well
|
Jails run virtual machines very well. They look very much like a real system. You must look pretty close to be able to tell you're in a jail. My jail allows the Bacula developers to have a machine of their own. It also allows me to keep their work totally separate from my own. A jail can be used to deal with security issues and to increase the utilization of an existing machine while giving everyone their own virtual machine. There's no reason why you couldn't run many different jails on the same computer. Enjoy. |