Create a New PID Namespace with unshare and clone
Start a process that sees itself as PID 1 using clone with CLONE_NEWPID or unshare plus fork on Linux 6.12, and learn why unshare never moves the caller.

Inside a container, ps shows your application as process 1, while the host sees the same process as something like 48213. That trick is a PID namespace, and it takes about fifteen lines of C to produce. There are two ways to ask for one, clone with CLONE_NEWPID and unshare with the same flag, and they differ in a way that trips almost everyone: unshare does not put the calling process into the new namespace. This post builds both versions, shows what each one prints, and explains the rules that follow from the kernel's design. The code is written for Linux 6.12 and glibc 2.40, and the semantics come from pid_namespaces(7), clone(2) and unshare(2).
What a PID namespace changes
A PID namespace gives a group of processes their own numbering of process ids. The first process created in a new namespace gets id 1 inside it. Processes in the namespace can see only other processes in the same namespace or below it; the parent namespace can see all of them, under different numbers. Namespaces nest, so a process has one PID per level between it and the root namespace.
Creating a PID namespace requires CAP_SYS_ADMIN in the caller's user namespace. The examples below are run as root, or with sudo. A later post in this cluster, on user namespaces, shows how to get the capability without being root on the host.
Version one: clone with CLONE_NEWPID
clone creates a child process, and with CLONE_NEWPID the child is the first member of a fresh PID namespace. That makes it PID 1 in there by definition.
#define _GNU_SOURCE
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h>
#define STACK_SIZE (1024 * 1024)
static int child_main(void *arg)
{
(void)arg;
printf("child: getpid()=%d getppid()=%d\n", getpid(), getppid());
return 0;
}
int main(void)
{
char *stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
if (stack == MAP_FAILED) {
perror("mmap");
return 1;
}
pid_t pid = clone(child_main, stack + STACK_SIZE,
CLONE_NEWPID | SIGCHLD, NULL);
if (pid < 0) {
perror("clone");
return 1;
}
printf("parent: child pid as seen from the host = %d\n", pid);
waitpid(pid, NULL, 0);
return 0;
}Compile with gcc -std=gnu17 -o pidns_clone pidns_clone.c and run with sudo ./pidns_clone. The host process id differs every time; the child's view does not:
parent: child pid as seen from the host = 48213
child: getpid()=1 getppid()=0Three details deserve attention. The stack argument is the top of the region, because stacks grow downward on x86-64 and arm64, so the code passes stack + STACK_SIZE. The flag SIGCHLD in the low byte is the exit signal; without it the parent would not be notified and waitpid would need __WCLONE. And getppid() returns 0 in the child because its real parent lives in a namespace the child cannot see, so the kernel reports "no parent".
Version two: unshare, then fork
unshare(CLONE_NEWPID) is the in-place variant. It looks like it should move the caller, and it does not. The man page is explicit: the caller's own PID never changes, and the new namespace becomes the namespace for the caller's future children. The first child created afterward is PID 1.
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void)
{
printf("before unshare: getpid()=%d\n", getpid());
if (unshare(CLONE_NEWPID) != 0) {
perror("unshare");
return 1;
}
printf("after unshare: getpid()=%d (unchanged)\n", getpid());
pid_t pid = fork();
if (pid < 0) {
perror("fork");
return 1;
}
if (pid == 0) {
printf("forked child: getpid()=%d\n", getpid());
return 0;
}
waitpid(pid, NULL, 0);
return 0;
}before unshare: getpid()=48307
after unshare: getpid()=48307 (unchanged)
forked child: getpid()=1The reason is a hard constraint: a process's PID is fixed when it is created and is embedded in many kernel structures. Moving a live process to a numbering where it would need a new id would break every holder of the old one. So the kernel stores the target in a separate field, visible as /proc/self/ns/pid_for_children, and applies it at fork. You can see the split by comparing /proc/self/ns/pid, which stays on the old namespace, with pid_for_children, which points at the new one.
The rules that follow from "first process is PID 1"
PID 1 is special in a namespace, and the kernel enforces it:
If PID 1 exits, every other process in the namespace is killed. The kernel sends
SIGKILLto all of them. This is why a container dies when its main process does.Signals are filtered for PID 1. A signal sent from inside the namespace is ignored unless PID 1 installed a handler for it.
SIGKILLandSIGSTOPfrom inside cannot kill PID 1. Signals from an ancestor namespace are the exception: they are delivered, andSIGKILLalways works from above, so the host can always remove a container.Orphans reparent to it. Any process whose parent dies inside the namespace is adopted by PID 1, so PID 1 needs to call
waiton children or zombies accumulate.**A second
unshare(CLONE_NEWPID)creates a new nested level**, not a sibling. Namespaces form a tree, with a documented maximum nesting depth of 32 levels.
The third rule is the most common operational mistake. If your program is PID 1 and it forks helpers without reaping them, the process table fills with zombies. That is the reason small init programs exist and why container runtimes offer an option to inject one.
What ps still shows
Run ps inside the child and you will see the host's whole process list, not just PID 1. The reason is that ps reads /proc, and /proc is a mount of the procfs filesystem that was created for the host's PID namespace. The new namespace sees the old mount until it has its own. Fixing it needs a private mount namespace and a fresh procfs mount, which the next post in the cluster, on mount namespaces and pivot_root, builds step by step. As a preview, the call looks like mount("proc", "/proc", "proc", 0, NULL) executed in the child after CLONE_NEWNS has isolated its mounts.
Choosing between clone and unshare
Use clone when you are creating a new process anyway, which is nearly always for container-style code, because the flags apply to the child atomically and there is no window in which the child shares anything it should not. Use unshare when a long-running process wants to partition itself, for example a daemon that wants its workers in a namespace, or when you want to exploit the "caller stays put" behavior to keep a supervisor in the parent namespace while its children get the new one. The unshare(1) command-line tool uses the unshare call, and its --fork option exists precisely because of the semantics above: without a fork the command would run in the old namespace.
When not to use a PID namespace
A PID namespace hides processes; it does not limit them. A process in it can still use all the CPU and memory, so pair it with cgroups. It also does not isolate the filesystem, network or users, so a "container" built from only this flag is barely a container. And on a system where CAP_SYS_ADMIN is not available and user namespaces are disabled, the call fails with EPERM, which you should handle by reporting the cause rather than retrying.


