简短回答:是的,您可以...因为您正在处理网络命名空间。
长答案:
不同类型的 Linux 内核命名空间有时会有不同的约束,这些差异会让你偏离正轨。一个非常好的来源是 Micheal Kerrisk 的 setns(2) 手册页。特别是标题为“特定命名空间类型的详细信息”的部分(很遗憾,不支持深度链接)。
至于将进程或线程切换到不同的网络命名空间,以下适用:
Network, IPC, time, and UTS namespaces
In order to reassociate itself with a new network, IPC,
time, or UTS namespace, the caller must have the
CAP_SYS_ADMIN capability both in its own user namespace
and in the user namespace that owns the target namespace.
请注意,网络命名空间是“扁平的”,因此它们不会形成任何层次结构。
相比之下,PID 命名空间有不同的约束,最明显的是因为它们确实形成了层次结构:
Reassociating with a PID namespace is allowed only if the
target PID namespace is a descendant (child, grandchild,
etc.) of, or is the same as, the current PID namespace of
the caller.
然后是完全不同的东西:挂载命名空间。
A process can't join a new mount namespace if it is
sharing filesystem-related attributes (the attributes
whose sharing is controlled by the clone(2) CLONE_FS flag)
with another process.
这是一种稍微复杂的说法,即一旦您的进程中有另一个线程(=第一个任务),则该进程的任何线程的挂载命名空间都不能再更改,第一个也不能更改,也没有任何其他任务。