我一直在尝试使用适用于 Linux v2 的 Windows 子系统 (WSL 2),并在其常见问题解答中了解到,所有发行版都安装在 Hyper-V 中的同一虚拟机 (VM) 上,并使用命名空间进行隔离。这可以通过启动 Ubuntu 和 Debian 并运行以下命令来查看活动虚拟机列表来确认:
PS C:\Users\admin> hcsdiag list
8B389621-7C83-484C-8F24-5CAEDA29AA49
VM, Running, 8B389621-7C83-484C-8F24-5CAEDA29AA49, WSL
如果这些发行版通过名称空间隔离,为什么不能使用从两个发行版输入相同的根名称空间nsenter
?
这些是在 Ubuntu 发行版中运行的进程,其/init
PID 为 1:
root@DESKTOP-5VNI36Q:~# ps -Af
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 14:05 ? 00:00:00 /init
root 7 1 0 14:05 ? 00:00:00 /init
root 8 7 0 14:05 ? 00:00:00 /init
nlykkei 9 8 0 14:05 pts/0 00:00:00 -bash
root 52 9 0 14:06 pts/0 00:00:00 sudo su -
root 53 52 0 14:06 pts/0 00:00:00 su -
root 54 53 0 14:06 pts/0 00:00:00 -bash
root 70 54 0 14:06 pts/0 00:00:00 ps -Af
如果我从 Ubuntu 发行版输入 PID 1 的所有命名空间,那么我就会陷入与最初相同的 PID 命名空间中,并且无法进一步继续(见下文):
root@DESKTOP-5VNI36Q:~# nsenter-t1 -a
相反,如果我输入 Debian 发行版中 PID 1 的所有命名空间,那么我也无法继续进行,并且会卡在与我最初创建的相同的 PID 命名空间中。此外,PID 命名空间与 Ubuntu PID 命名空间不同。
乌班图分配:
root@DESKTOP-5VNI36Q:~# ls -la /proc/self/ns
total 0
dr-x--x--x 2 root root 0 Oct 20 14:06 .
dr-xr-xr-x 9 root root 0 Oct 20 14:06 ..
lrwxrwxrwx 1 root root 0 Oct 20 14:06 cgroup -> 'cgroup:[4026531835]'
lrwxrwxrwx 1 root root 0 Oct 20 14:06 ipc -> 'ipc:[4026532188]'
lrwxrwxrwx 1 root root 0 Oct 20 14:06 mnt -> 'mnt:[4026532186]'
lrwxrwxrwx 1 root root 0 Oct 20 14:06 net -> 'net:[4026531992]'
lrwxrwxrwx 1 root root 0 Oct 20 14:06 pid -> 'pid:[4026532189]'
lrwxrwxrwx 1 root root 0 Oct 20 14:06 pid_for_children -> 'pid:[4026532189]'
lrwxrwxrwx 1 root root 0 Oct 20 14:06 user -> 'user:[4026531837]'
lrwxrwxrwx 1 root root 0 Oct 20 14:06 uts -> 'uts:[4026532187]'
root@DESKTOP-5VNI36Q:~# ls -la /proc/1/ns
total 0
dr-x--x--x 2 root root 0 Oct 20 14:05 .
dr-xr-xr-x 9 root root 0 Oct 20 14:05 ..
lrwxrwxrwx 1 root root 0 Oct 20 14:06 cgroup -> 'cgroup:[4026531835]'
lrwxrwxrwx 1 root root 0 Oct 20 14:06 ipc -> 'ipc:[4026532188]'
lrwxrwxrwx 1 root root 0 Oct 20 14:05 mnt -> 'mnt:[4026532186]'
lrwxrwxrwx 1 root root 0 Oct 20 14:06 net -> 'net:[4026531992]'
lrwxrwxrwx 1 root root 0 Oct 20 14:06 pid -> 'pid:[4026532189]'
lrwxrwxrwx 1 root root 0 Oct 20 14:06 pid_for_children -> 'pid:[4026532189]'
lrwxrwxrwx 1 root root 0 Oct 20 14:06 user -> 'user:[4026531837]'
lrwxrwxrwx 1 root root 0 Oct 20 14:06 uts -> 'uts:[4026532187]'
德班分配:
root@DESKTOP-5VNI36Q:~# ls -la /proc/self/ns
total 0
dr-x--x--x 2 root root 0 Oct 20 14:10 .
dr-xr-xr-x 9 root root 0 Oct 20 14:10 ..
lrwxrwxrwx 1 root root 0 Oct 20 14:10 cgroup -> 'cgroup:[4026531835]'
lrwxrwxrwx 1 root root 0 Oct 20 14:10 ipc -> 'ipc:[4026532203]'
lrwxrwxrwx 1 root root 0 Oct 20 14:10 mnt -> 'mnt:[4026532211]'
lrwxrwxrwx 1 root root 0 Oct 20 14:10 net -> 'net:[4026531992]'
lrwxrwxrwx 1 root root 0 Oct 20 14:10 pid -> 'pid:[4026532204]'
lrwxrwxrwx 1 root root 0 Oct 20 14:10 pid_for_children -> 'pid:[4026532204]'
lrwxrwxrwx 1 root root 0 Oct 20 14:10 user -> 'user:[4026531837]'
lrwxrwxrwx 1 root root 0 Oct 20 14:10 uts -> 'uts:[4026532202]'
root@DESKTOP-5VNI36Q:~# ls -la /proc/1/ns
total 0
dr-x--x--x 2 root root 0 Oct 20 12:04 .
dr-xr-xr-x 9 root root 0 Oct 20 11:17 ..
lrwxrwxrwx 1 root root 0 Oct 20 12:04 cgroup -> 'cgroup:[4026531835]'
lrwxrwxrwx 1 root root 0 Oct 20 12:04 ipc -> 'ipc:[4026532203]'
lrwxrwxrwx 1 root root 0 Oct 20 12:04 mnt -> 'mnt:[4026532201]'
lrwxrwxrwx 1 root root 0 Oct 20 12:04 net -> 'net:[4026531992]'
lrwxrwxrwx 1 root root 0 Oct 20 12:04 pid -> 'pid:[4026532204]'
lrwxrwxrwx 1 root root 0 Oct 20 14:10 pid_for_children -> 'pid:[4026532204]'
lrwxrwxrwx 1 root root 0 Oct 20 12:04 user -> 'user:[4026531837]'
lrwxrwxrwx 1 root root 0 Oct 20 12:04 uts -> 'uts:[4026532202]'