KJ7*_*LNW 4 linux namespace unshare
使用 时unshare --pid --fork
,该nsenter
命令必须附加到子 pid 而不是unshare
pid 以获取正确的 pid 命名空间。
我可以按如下方式获取 unshare 的 pid:
unshare --pid --mount --fork --mount-proc bash &
echo PID: $!
fg
Run Code Online (Sandbox Code Playgroud)
但我需要unshare
孩子的 pid (2914003) 才能输入正确的命名空间:
ps wwfuax | grep -A1 unshare
2914002 pts/4 S 0:00 | \_ unshare --pid --mount --fork --mount-proc bash
2914003 pts/4 S+ 0:00 | \_ bash
Run Code Online (Sandbox Code Playgroud)
这有效:nsenter -t 2914003
这不起作用:nsenter -t 2914002
我希望有某种选择,unshare --show-child-pid
但没有。
获取取消共享子进程 pid 的可靠可靠方法是什么?
最好的解决方案是不依赖进程 ID。
当您使用 unshare 命令创建命名空间时,您可以创建由文件系统上的绑定挂载引用的持久命名空间。我们可以按照手册页中的示例进行设置unshare(1)
。
首先,我们需要设置一个带有private
传播的挂载点:
mkdir /tmp/ns
mount --bind /tmp/ns /tmp/ns
mount --make-private /tmp/ns
Run Code Online (Sandbox Code Playgroud)
然后我们需要 mount 和 pid 命名空间的目标文件:
touch /tmp/ns/{mnt,pid}
Run Code Online (Sandbox Code Playgroud)
现在我们使用以下unshare
命令创建命名空间:
unshare --pid=/tmp/ns/pid --mount=/tmp/ns/mnt --fork --mount-proc bash
Run Code Online (Sandbox Code Playgroud)
使用这些引用挂载点,我们可以在不知道进程 ID 的情况下输入名称空间:
nsenter --mount=/tmp/ns/mnt --pid=/tmp/ns/pid
Run Code Online (Sandbox Code Playgroud)
完成后,不要忘记清理:
umount /tmp/ns/{mnt,pid}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
785 次 |
最近记录: |