除了使用 rbash 来限制登陆之外,还可是使用 chroot 来限制用户的行为。下面记录在 ubuntu 10.04.3 x86_64 server 上 chroot 的方法。
将用户 chroot 到 /home/chroot 下面。

安装 chroot 的 pam 模块,实际安装下来发现没有该模块也能正常运行:

# apt-get install libpam-chroot
Configuration file `/etc/security/chroot.conf'
==> File on system created by you or by a script.
==> File also in package provided by package maintainer.
What would you like to do about it ?  Your options are:
Y or I : install the package maintainer's version
N or O : keep your currently-installed version
D : show the differences between the versions
Z : background this process to examine the situation
The default action is to keep your current version.
*** chroot.conf (Y/I/N/O/D/Z) [default=N] ? Y


# adduser wfg
# passwd wfg
# sed '[email protected]/home/[email protected]/home/chroot/home/[email protected]' /etc/passwd

# mkdir -p /home/chroot/{home,etc,dev,dev/pts,proc}
# cp -a /bin /home/chroot
# cp -a /lib /home/chroot
# cp -a /lib64 /home/chroot

# mount -o bind -t udev /dev /home/chroot/dev
# mount -o bind -t devpts /dev/pts /home/chroot/dev/pts
# mount -o bind -t proc /home/chroot/proc

修改 sshd_config,只有 wfg 用户可以 chroot,并固定 chroot 目录:

# sed -i '$a Match User wfg' /etc/ssh/sshd_config
# sed -i '$a ChrootDirectory /home/chroot' /etc/ssh/sshd_config
# /etc/init.d/ssh restart

# grep wfg /etc/passwd > /home/chroot/etc/passwd
# grep wfg /etc/group > /home/chroot/etc/group

# chown -R root:root /home/chroot
# chmod -R 755 /home/chroot
# chown -R wfg:wfg /home/chroot/home/wfg
# chmod 700 /home/chroot/home/wfg

至于登陆后的 PS1 等设置就不在此叙述了。至此,一个简单的 chroot 就做好了。可以进一步的优化,如只允许用户接触少数的资源;没有必要挂载整个 udev 文件系统等。对于后者,可以 /home/chroot/dev/ 下面建立少数的字符文件:

# mknod dev/null c 1 3
# mknod dev/zero c 1 5
# mknod dev/tty c 5 0
# mknod dev/urandom c 1 9
# mknod dev/random c 1 8
# chmod 0666 dev/*

而至于前者,可以通过脚本实现,将需要的命令列在下面的 CMD 中。

CMD="/bin/bash /bin/ls /bin/pwd /usr/bin/id /bin/ping"

for cmd in $CMD
  cp $cmd $PATH_CHROOT/bin
  LIBS=`ldd $cmd | sed 's/.*> //'|sed 's/ .*//'`
  for l in $LIBS
     cp $l $CHROOT_PATH/lib

mkdir ./lib64
cp /lib64/ ./lib64