每个进程私有文件系统挂载点

Ram*_*esh 23 linux process mount namespace

我正在检查unshare命令并根据它的手册页,

   unshare - run program with some namespaces unshared from parent
Run Code Online (Sandbox Code Playgroud)

我还看到有一种命名空间被列为,

 mount namespace
              mounting and unmounting filesystems will not affect rest of the system.
Run Code Online (Sandbox Code Playgroud)

这个mount 命名空间的目的究竟是什么?我试图在一些例子的帮助下理解这个概念。

Ram*_*esh 31

运行unshare -m为调用进程提供其挂载命名空间的私有副本,并取消共享文件系统属性,使其不再与任何其他进程共享其根目录、当前目录或 umask 属性。

那么上面这段话是怎么说的呢?让我们试着用一个简单的例子来理解。

1号航站楼:

我在第一个终端中执行以下命令。

#Creating a new process
unshare -m /bin/bash
#creating a new mount point
secret_dir=`mktemp -d --tmpdir=/tmp`
#creating a new mount point for the above created directory. 
mount -n -o size=1m -t tmpfs tmpfs $secret_dir
#checking the available mount points. 
grep /tmp /proc/mounts 
Run Code Online (Sandbox Code Playgroud)

最后一个命令给了我输出,

tmpfs /tmp/tmp.7KtrAsd9lx tmpfs rw,relatime,size=1024k 0 0

现在,我也执行了以下命令。

cd /tmp/tmp.7KtrAsd9lx
touch hello
touch helloagain
ls -lFa
Run Code Online (Sandbox Code Playgroud)

ls命令的输出是,

ls -lFa
total 4
drwxrwxrwt   2 root root   80 Sep  3 22:23 ./
drwxrwxrwt. 16 root root 4096 Sep  3 22:22 ../
-rw-r--r--   1 root root    0 Sep  3 22:23 hello
-rw-r--r--   1 root root    0 Sep  3 22:23 helloagain
Run Code Online (Sandbox Code Playgroud)

那么做这一切有什么大不了的呢?我为什么要这样做?

我现在打开另一个终端(终端 2)并执行以下命令。

cd /tmp/tmp.7KtrAsd9lx
ls -lFa
Run Code Online (Sandbox Code Playgroud)

输出如下。

ls -lFa
total 8
drwx------   2 root root 4096 Sep  3 22:22 ./
drwxrwxrwt. 16 root root 4096 Sep  3 22:22 ../
Run Code Online (Sandbox Code Playgroud)

文件hellohelloagain不可见,我什至以 root 身份登录以检查这些文件。所以优点是,这个特性使我们可以创建一个私有的临时文件系统,即使是其他 root 拥有的进程也无法看到或浏览。

从 的手册页unshare

挂载命名空间挂载和卸载文件系统不会影响系统的其余部分(CLONE_NEWNS 标志),除了明确标记为共享的文件系统(使用 mount --make-shared;有关共享标志,请参阅 /proc/self/mountinfo)。

建议在 unshare --mount 之后使用 mount --make-rprivate 或 mount --make-rslave 以确保新命名空间中的挂载点确实与父命名空间不共享。

用于命名空间的内存是来自内核的 VFS。而且 - 如果我们一开始就正确设置 - 我们可以创建整个虚拟环境,在其中我们是没有 root 权限的 root 用户。

参考:

该示例是使用此博客文章中的详细信息构建。此外,这个答案的引述来自 Mike 的精彩解释。可以从这里的答案中找到关于此的另一篇精彩读物。

  • **什么都逃不过根!** 使用 `nsenter` 你可以进入命名空间并查看临时文件。假设只有一个 unshare(拥有 tempdir 的那个),然后 `sudo nsenter -t $(pgrep -P $(ps aux | grep unshare | grep -v grep | awk '{print $2}')) -m -p`将允许查看内容 (2认同)