# phpman > man > pid_namespaces(7)

[PID_NAMESPACES(7)](https://www.chedong.com/phpMan.php/man/PIDNAMESPACES/7/markdown)                     Linux Programmer's Manual                    [PID_NAMESPACES(7)](https://www.chedong.com/phpMan.php/man/PIDNAMESPACES/7/markdown)



## NAME
       pid_namespaces - overview of Linux PID namespaces

## DESCRIPTION
       For an overview of namespaces, see [**namespaces**(7)](https://www.chedong.com/phpMan.php/man/namespaces/7/markdown).

       PID  namespaces  isolate the process ID number space, meaning that processes in different PID
       namespaces can have the same PID.  PID namespaces allow containers to  provide  functionality
       such as suspending/resuming the set of processes in the container and migrating the container
       to a new host while the processes inside the container maintain the same PIDs.

       PIDs in a new PID namespace start at 1, somewhat like  a  standalone  system,  and  calls  to
       [**fork**(2)](https://www.chedong.com/phpMan.php/man/fork/2/markdown),  [**vfork**(2)](https://www.chedong.com/phpMan.php/man/vfork/2/markdown),  or  [**clone**(2)](https://www.chedong.com/phpMan.php/man/clone/2/markdown) will produce processes with PIDs that are unique within the
       namespace.

       Use of PID namespaces requires a kernel that is configured with the **CONFIG**___**PID**___**NS** option.

### The namespace init process
       The first process created in a new namespace (i.e., the process created using  [**clone**(2)](https://www.chedong.com/phpMan.php/man/clone/2/markdown)  with
       the **CLONE**___**NEWPID** flag, or the first child created by a process after a call to [**unshare**(2)](https://www.chedong.com/phpMan.php/man/unshare/2/markdown) us‐
       ing the **CLONE**___**NEWPID** flag) has the PID 1, and is the "init" process for  the  namespace  (see
       [**init**(1)](https://www.chedong.com/phpMan.php/man/init/1/markdown)).  This process becomes the parent of any child processes that are orphaned because a
       process that resides in this PID namespace terminated (see below for further details).

       If the "init" process of a PID namespace terminates, the kernel terminates all  of  the  pro‐
       cesses  in  the  namespace  via  a  **SIGKILL** signal.  This behavior reflects the fact that the
       "init" process is essential for the correct operation of a PID namespace.  In  this  case,  a
       subsequent  [**fork**(2)](https://www.chedong.com/phpMan.php/man/fork/2/markdown) into this PID namespace fail with the error **ENOMEM**; it is not possible to
       create a new process in a PID namespace whose "init" process has terminated.  Such  scenarios
       can  occur when, for example, a process uses an open file descriptor for a _/proc/[pid]/ns/pid_
       file corresponding to a process that was in a namespace to [**setns**(2)](https://www.chedong.com/phpMan.php/man/setns/2/markdown) into that namespace after
       the  "init"  process has terminated.  Another possible scenario can occur after a call to **un**‐‐
       [**share**(2)](https://www.chedong.com/phpMan.php/man/share/2/markdown): if the first child subsequently created by a [**fork**(2)](https://www.chedong.com/phpMan.php/man/fork/2/markdown)  terminates,  then  subsequent
       calls to [**fork**(2)](https://www.chedong.com/phpMan.php/man/fork/2/markdown) fail with **ENOMEM**.

       Only signals for which the "init" process has established a signal handler can be sent to the
       "init" process by other members of the PID namespace.  This restriction applies even to priv‐
       ileged  processes,  and prevents other members of the PID namespace from accidentally killing
       the "init" process.

       Likewise, a process in an ancestor namespace can—subject to the usual permission  checks  de‐
       scribed  in  [**kill**(2)](https://www.chedong.com/phpMan.php/man/kill/2/markdown)—send  signals to the "init" process of a child PID namespace only if the
       "init" process has established a handler for that signal.  (Within the handler, the _siginfo_t_
       _si_pid_  field described in [**sigaction**(2)](https://www.chedong.com/phpMan.php/man/sigaction/2/markdown) will be zero.)  **SIGKILL** or **SIGSTOP** are treated excep‐
       tionally: these signals are forcibly delivered when sent  from  an  ancestor  PID  namespace.
       Neither of these signals can be caught by the "init" process, and so will result in the usual
       actions associated with those signals (respectively, terminating and stopping the process).

       Starting with Linux 3.4, the [**reboot**(2)](https://www.chedong.com/phpMan.php/man/reboot/2/markdown) system call causes a signal to be sent  to  the  name‐
       space "init" process.  See [**reboot**(2)](https://www.chedong.com/phpMan.php/man/reboot/2/markdown) for more details.

### Nesting PID namespaces
       PID  namespaces  can  be  nested:  each  PID  namespace  has a parent, except for the initial
       ("root") PID namespace.  The parent of a PID namespace is the PID namespace  of  the  process
       that  created  the  namespace using [**clone**(2)](https://www.chedong.com/phpMan.php/man/clone/2/markdown) or [**unshare**(2)](https://www.chedong.com/phpMan.php/man/unshare/2/markdown).  PID namespaces thus form a tree,
       with all namespaces ultimately tracing their ancestry to the  root  namespace.   Since  Linux
       3.7, the kernel limits the maximum nesting depth for PID namespaces to 32.

       A  process  is  visible to other processes in its PID namespace, and to the processes in each
       direct ancestor PID namespace going back to the root PID namespace.  In this context,  "visi‐
       ble"  means  that one process can be the target of operations by another process using system
       calls that specify a process ID.  Conversely, the processes in a child  PID  namespace  can't
       see  processes  in  the  parent  and further removed ancestor namespaces.  More succinctly: a
       process can see (e.g., send signals with [**kill**(2)](https://www.chedong.com/phpMan.php/man/kill/2/markdown), set nice values with [**setpriority**(2)](https://www.chedong.com/phpMan.php/man/setpriority/2/markdown),  etc.)
       only processes contained in its own PID namespace and in descendants of that namespace.

       A process has one process ID in each of the layers of the PID namespace hierarchy in which is
       visible, and walking back though each direct ancestor namespace through to the root PID name‐
       space.   System calls that operate on process IDs always operate using the process ID that is
       visible in the PID namespace of the caller.  A call to [**getpid**(2)](https://www.chedong.com/phpMan.php/man/getpid/2/markdown) always returns the PID asso‐
       ciated with the namespace in which the process was created.

       Some  processes  in  a PID namespace may have parents that are outside of the namespace.  For
       example, the parent of the initial process in the namespace (i.e., the [**init**(1)](https://www.chedong.com/phpMan.php/man/init/1/markdown)  process  with
       PID  1) is necessarily in another namespace.  Likewise, the direct children of a process that
       uses [**setns**(2)](https://www.chedong.com/phpMan.php/man/setns/2/markdown) to cause its children to join a PID namespace are in a different PID  namespace
       from the caller of [**setns**(2)](https://www.chedong.com/phpMan.php/man/setns/2/markdown).  Calls to [**getppid**(2)](https://www.chedong.com/phpMan.php/man/getppid/2/markdown) for such processes return 0.

       While processes may freely descend into child PID namespaces (e.g., using [**setns**(2)](https://www.chedong.com/phpMan.php/man/setns/2/markdown) with a PID
       namespace file descriptor), they may not move in the other direction.  That is to  say,  pro‐
       cesses may not enter any ancestor namespaces (parent, grandparent, etc.).  Changing PID name‐
       spaces is a one-way operation.

       The **NS**___**GET**___**PARENT** [**ioctl**(2)](https://www.chedong.com/phpMan.php/man/ioctl/2/markdown) operation can be used to discover the  parental  relationship  be‐
       tween PID namespaces; see **ioctl**___**[ns**(2)](https://www.chedong.com/phpMan.php/man/ns/2/markdown).

### [setns(2)](https://www.chedong.com/phpMan.php/man/setns/2/markdown) and [unshare(2)](https://www.chedong.com/phpMan.php/man/unshare/2/markdown) semantics
       Calls  to  [**setns**(2)](https://www.chedong.com/phpMan.php/man/setns/2/markdown) that specify a PID namespace file descriptor and calls to [**unshare**(2)](https://www.chedong.com/phpMan.php/man/unshare/2/markdown) with
       the **CLONE**___**NEWPID** flag cause children subsequently created by the caller to  be  placed  in  a
       different  PID namespace from the caller.  (Since Linux 4.12, that PID namespace is shown via
       the _/proc/[pid]/ns/pid_for_children_ file, as described in  [**namespaces**(7)](https://www.chedong.com/phpMan.php/man/namespaces/7/markdown).)   These  calls  do
       not,  however, change the PID namespace of the calling process, because doing so would change
       the caller's idea of its own PID (as reported by **getpid**()), which would break  many  applica‐
       tions and libraries.

       To  put  things  another  way:  a  process's  PID namespace membership is determined when the
       process is created and cannot be changed thereafter.  Among other things, this means that the
       parental  relationship  between processes mirrors the parental relationship between PID name‐
       spaces: the parent of a process is either in the same namespace or resides in  the  immediate
       parent PID namespace.

       A  process  may call [**unshare**(2)](https://www.chedong.com/phpMan.php/man/unshare/2/markdown) with the **CLONE**___**NEWPID** flag only once.  After it has performed
       this operation, its _/proc/PID/ns/pid_for_children_ symbolic link will be empty until the first
       child is created in the namespace.

### Adoption of orphaned children
       When  a  child  process  becomes  orphaned, it is reparented to the "init" process in the PID
       namespace of its parent (unless one of the  nearer  ancestors  of  the  parent  employed  the
       [**prctl**(2)](https://www.chedong.com/phpMan.php/man/prctl/2/markdown)  **PR**___**SET**___**CHILD**___**SUBREAPER**  command to mark itself as the reaper of orphaned descendant
       processes).  Note that because of the [**setns**(2)](https://www.chedong.com/phpMan.php/man/setns/2/markdown) and [**unshare**(2)](https://www.chedong.com/phpMan.php/man/unshare/2/markdown) semantics described above, this
       may  be  the  "init" process in the PID namespace that is the _parent_ of the child's PID name‐
       space, rather than the "init" process in the child's own PID namespace.

   **Compatibility** **of** **CLONE**___**NEWPID** **with** **other** **CLONE**___***** **flags**
       In current versions of Linux, **CLONE**___**NEWPID** can't be combined with **CLONE**___**THREAD**.  Threads  are
       required  to be in the same PID namespace such that the threads in a process can send signals
       to each other.  Similarly, it must be possible to see all of the threads of  a  processes  in
       the  [**proc**(5)](https://www.chedong.com/phpMan.php/man/proc/5/markdown)  filesystem.  Additionally, if two threads were in different PID namespaces, the
       process ID of the process sending a signal could not be meaningfully encoded when a signal is
       sent  (see  the  description  of the _siginfo_t_ type in [**sigaction**(2)](https://www.chedong.com/phpMan.php/man/sigaction/2/markdown)).  Since this is computed
       when a signal is enqueued, a signal queue shared by  processes  in  multiple  PID  namespaces
       would defeat that.

       In  earlier versions of Linux, **CLONE**___**NEWPID** was additionally disallowed (failing with the er‐
       ror **EINVAL**) in combination with **CLONE**___**SIGHAND** (before Linux 4.3) as well as **CLONE**___**VM**  (before
       Linux  3.12).   The  changes  that lifted these restrictions have also been ported to earlier
       stable kernels.

### /proc and PID namespaces
       A _/proc_ filesystem shows (in the _/proc/[pid]_ directories) only processes visible in  the  PID
       namespace  of  the  process  that performed the mount, even if the _/proc_ filesystem is viewed
       from processes in other namespaces.

       After creating a new PID namespace, it is useful for the child to change its  root  directory
       and  mount  a  new procfs instance at _/proc_ so that tools such as [**ps**(1)](https://www.chedong.com/phpMan.php/man/ps/1/markdown) work correctly.  If a
       new mount namespace is simultaneously created by including **CLONE**___**NEWNS** in the _flags_  argument
       of [**clone**(2)](https://www.chedong.com/phpMan.php/man/clone/2/markdown) or [**unshare**(2)](https://www.chedong.com/phpMan.php/man/unshare/2/markdown), then it isn't necessary to change the root directory: a new procfs
       instance can be mounted directly over _/proc_.

       From a shell, the command to mount _/proc_ is:

           $ mount -t proc proc /proc

       Calling [**readlink**(2)](https://www.chedong.com/phpMan.php/man/readlink/2/markdown) on the path _/proc/self_ yields the process ID of the  caller  in  the  PID
       namespace  of  the  procfs  mount  (i.e.,  the  PID namespace of the process that mounted the
       procfs).  This can be useful for introspection purposes, when a process wants to discover its
       PID in other namespaces.

### /proc files
       **/proc/sys/kernel/ns**___**last**___**pid** (since Linux 3.3)
              This  file (which is virtualized per PID namespace) displays the last PID that was al‐
              located in this PID namespace.  When the next PID is allocated, the kernel will search
              for  the lowest unallocated PID that is greater than this value, and when this file is
              subsequently read it will show that PID.

              This file is writable by a process that has the **CAP**___**SYS**___**ADMIN**  or  (since  Linux  5.9)
              **CAP**___**CHECKPOINT**___**RESTORE**  capability  inside  the user namespace that owns the PID name‐
              space.  This makes it possible to determine the PID that  is  allocated  to  the  next
              process that is created inside this PID namespace.

### Miscellaneous
       When  a  process ID is passed over a UNIX domain socket to a process in a different PID name‐
       space (see the description of **SCM**___**CREDENTIALS** in [**unix**(7)](https://www.chedong.com/phpMan.php/man/unix/7/markdown)), it is translated into  the  corre‐
       sponding PID value in the receiving process's PID namespace.

## CONFORMING TO
       Namespaces are a Linux-specific feature.

## EXAMPLES
       See **user**___**[namespaces**(7)](https://www.chedong.com/phpMan.php/man/namespaces/7/markdown).

## SEE ALSO
       [**clone**(2)](https://www.chedong.com/phpMan.php/man/clone/2/markdown),   [**reboot**(2)](https://www.chedong.com/phpMan.php/man/reboot/2/markdown),   [**setns**(2)](https://www.chedong.com/phpMan.php/man/setns/2/markdown),   [**unshare**(2)](https://www.chedong.com/phpMan.php/man/unshare/2/markdown),  [**proc**(5)](https://www.chedong.com/phpMan.php/man/proc/5/markdown),  [**capabilities**(7)](https://www.chedong.com/phpMan.php/man/capabilities/7/markdown),  [**credentials**(7)](https://www.chedong.com/phpMan.php/man/credentials/7/markdown),
       **mount**___**[namespaces**(7)](https://www.chedong.com/phpMan.php/man/namespaces/7/markdown), [**namespaces**(7)](https://www.chedong.com/phpMan.php/man/namespaces/7/markdown), **user**___**[namespaces**(7)](https://www.chedong.com/phpMan.php/man/namespaces/7/markdown), **switch**___**[root**(8)](https://www.chedong.com/phpMan.php/man/root/8/markdown)

## COLOPHON
       This page is part of release 5.10 of the Linux  _man-pages_  project.   A  description  of  the
       project,  information about reporting bugs, and the latest version of this page, can be found
       at <https://www.kernel.org/doc/man-pages/>.



Linux                                        2020-11-01                            [PID_NAMESPACES(7)](https://www.chedong.com/phpMan.php/man/PIDNAMESPACES/7/markdown)
