部份管理者將 Jail 分成兩種類型:“完整的” Jail,它像一個真正的 FreeBSD 系統以及 “服務的” Jail,專門用於某個應用程式或服務,可能使用管理權限執行。但這些只是概念上的區分,建立 Jail 的程序並不受這個概念的影響。當要建立一個 “完整的” Jail,Userland 有兩個來源選項:使用預先編譯的 Binary (如安裝媒體上提供的 Binary) 或從原始碼編譯。
要從安裝媒體安裝 Userland,需要先建立根目錄供 Jail 使用。這個動作可以透過設定 DESTDIR
來到適當的位置來完成。
啟動 Shell 並定義 DESTDIR
:
#
sh
#
export DESTDIR=/here/is/the/jail
當使用安裝 ISO 時,可依 mdconfig(8) 中的說明掛載安裝媒體:
#
mount -t cd9660 /dev/`mdconfig -f cdimage.iso` /mnt
從安裝媒體上的 Tarball 中取出 Binary 並放到宣告的位置,至少需要取出 Base set 的部份,若需要也可完整安裝。
只安裝基礎系統 (Base system):
#
tar -xf /mnt/usr/freebsd-dist/base.txz -C $DESTDIR
安裝全部不含核心:
#
for set in base ports; do tar -xf /mnt/usr/freebsd-dist/$set.txz -C $DESTDIR ; done
依 jail(8) 操作手冊說明的程序建置 Jail:
#
setenv D /here/is/the/jail
#
mkdir -p $D
#
cd /usr/src
#
make buildworld
#
make installworld DESTDIR=$D
#
make distribution DESTDIR=$D
#
mount -t devfs devfs $D/dev

選擇 Jail 的位置是建置 Jail 最好的起點,這是在 Jail 主機上儲存 Jail 的實體位置。較好的選擇是 | |
若您已經使用 | |
這個指令將會在檔案系統中 Jail 所在的實體位置產生樹狀目錄及必要的 Binary、程式庫、操作手冊與相關檔案。 | |
make 的 | |
在 Jail 中掛載 devfs(8) 檔案系統並非必要的動作。從另一個角度來說,任何或大部份的應用程式會依該程式的目的會需要存取至少一個裝置,在 Jail 中控制存取的裝置非常重要,不恰當的設定可能會讓攻擊者可以在 Jail 中做不軌的事。對 devfs(8) 的控制是透過 Ruleset,在 devfs(8) 及 devfs.conf(5) 操作手冊中有詳細說明。 |
Jail 安裝完成之後,便可使用 jail(8) 工具來啟動。jail(8) 工具需要四個必要參數,在 節 14.1, “概述” 有說明。其他參數也可能需要指定,例如要使用特定使用者的身份來執行要 Jail 的程序。
參數依 Jail 的類型所需而定,對一個 虛擬系統 來說,command
/etc/rc
是不錯的選擇,因為該檔案可以模仿真實 FreeBSD 的啟動順序。對於 服務的 Jail 來說,則看在 Jail 中要執行的服務或應用程式來決定。
Jail 通常會需要隨著開機執行,使用 FreeBSD rc
機制可讓以簡單的達成這件事。
Configure jail parameters in
jail.conf
:
www
{
host.hostname = www.example.org
; # Hostname
ip4.addr = 192.168.0.10
; # IP address of the jail
path ="/usr/jail/www
"; # Path to the jail
devfs_ruleset = "www_ruleset
"; # devfs ruleset
mount.devfs; # Mount devfs inside the jail
exec.start = "/bin/sh /etc/rc"; # Start command
exec.stop = "/bin/sh /etc/rc.shutdown"; # Stop command
}
Configure jails to start at boot time in
rc.conf
:
jail_enable="YES" # Set to NO to disable starting of any jails
The default startup of jails configured in
jail.conf(5), will run the /etc/rc
script of the jail, which assumes the jail is a complete
virtual system. For service jails, the default startup
command of the jail should be changed, by setting the
exec.start
option appropriately.
For a full list of available options, please see the jail.conf(5) manual page.
service(8) can be used to start or stop a jail by hand,
if an entry for it exists in
jail.conf
:
#
service jail start www
#
service jail stop www
Jail 可以使用 jexec(8) 來關機。先使用 jls(8) 來辦識 Jail 的 JID
,然後使用 jexec(8) 在該 Jail 中執行關機 Script。
#
jls
JID IP Address Hostname Path
3 192.168.0.10 www /usr/jail/www
#
jexec 3
/etc/rc.shutdown
更多有關 Jail 的資訊可在 jail(8) 操作手冊取得。
本文及其他文件,可由此下載: ftp://ftp.FreeBSD.org/pub/FreeBSD/doc/。
若有 FreeBSD 方面疑問,請先閱讀
FreeBSD 相關文件,如不能解決的話,再洽詢
<questions@FreeBSD.org>。
關於本文件的問題,請洽詢
<doc@FreeBSD.org>。