Home > OS >  Unshare PID Namespace not showing in root namespace
Unshare PID Namespace not showing in root namespace

Time:09-20

I am trying to see how PID works and learn containers

I have MTPuTTY installed and connected to a LINUX server

lsns -t pid gives me below 
4026531836 pid       3 14687

Open A duplicate session and create a namespace using unshare

unshare -p -f --mount-proc /bin/bash
   sleep 2000 &
   sleep 2100 &
  lsns -t pid //gives me a  new PID namespace

comeback to initial session created when I execute lsns -t pid

I am only seeing the root PID namespace though I created a namespace using unshare. Is this because I opened a duplicate session ? How could I actually see this working in PUTTY ?

CodePudding user response:

I'm not entirely clear exactly what steps you're performing when you "come back to initial session". If I have two terminals open to a Linux system, and in one terminal I record the current list of PID namespaces:

# lsns -t pid > before.txt

Then in the other terminal I create a new namespace with unshare:

# unshare -p -f --mount-proc bash

And then in the first terminal generate a new list of PID namespaces:

# lsns -t pid > after.txt

I can see that a new namespace has been created:

# diff -U0 before.txt  after.txt
--- before.txt  2022-09-19 13:39:00.567817408 -0400
    after.txt   2022-09-19 13:38:52.473860755 -0400
@@ -50,0  51 @@
 4026533870 pid       2 508090 root             bash

If I exit the bash shell I created with unshare, the new namespace is destroyed.

CodePudding user response:

I suspect that when you say you're coming back to the initial session, that you're actually exiting the subshell in the PID namespace. When PID 1 in a given namespace exits, any remaining processes in that namespace are killed with SIGKILL, and the namespace is then immediately deleted.

  • Related