您应该使用chroot。该chroot命令将更改所有子进程看到的根目录。我将举一个例子来演示它是如何工作的。
这是当场写的;我现在实际上不在UNIX计算机的前面。在这个例子中,有一个名为目录中dir有三个文件:a,b,c,和ls。前三个是常规文件。ls是到实际ls二进制文件的硬链接,因此我们可以在chroot中列出文件。
我要chroot到dir。(请注意,我可能会忘记根目录中的某些目录。)
这是外壳输出形式的设置:
$ pwd
/home/alex/test
$ l
dir
$ ls dir
a b c ls
$ ./ls dir # does the same thing
a b c ls
$ ls /
bin boot dev etc home mnt media proc sbin sys usr var
现在我chroot进入dir。该/bin/bash参数选择应使用新的根目录运行的进程。默认为/bin/sh。
$ chroot /bin/bash dir
$ # this prompt is now from a subprocess running in the new root directory
$ PATH=/ ls
a b c ls
$ pwd
/
现在我们从退出chroot:
$ exit
$ # this prompt is now from the original bash process, from before the chroot
$ pwd
/home/alex/test
我希望这可以说明chroot命令的工作方式。基本上,解决问题所需要做的就是在chroot用户每次登录时以该用户身份运行命令。也许将其放在启动脚本中?
chroot即使无法通过其他方式访问该文件,与文件的硬链接也将继续起作用(这是有效的,因为硬链接指向索引节点而不是路径)。因此,为了允许用户访问例如mysql命令,您将执行:
ln /usr/bin/mysql /path/to/chroot/target