Skip to content
Open Sourceadvanced

What are Linux Namespaces?

Updated Nov 30, 20263 min readBased on official documentation
What are Linux Namespaces?

In short

Linux namespaces are a kernel feature that gives a group of processes their own isolated view of a system resource, such as process IDs, network devices, or mount points. Each namespace type isolates one kind of resource. Containers are built on top of them.

What a namespace does

A namespace wraps a global system resource and presents it to processes inside that namespace as if they have their own private copy. Processes inside the namespace see their own instance. Processes outside it see the real, shared resource. When a process inside makes a change, that change is visible to other members of the same namespace but stays hidden from everyone else.

This idea is the base of most container tools. A container is, in practice, a set of ordinary Linux processes placed into their own namespaces, so each one sees its own process tree, network stack, hostname, and filesystem view.

The namespace types

Linux offers several namespace types. Each one isolates a different resource:

  • Cgroup: the root directory of the control group hierarchy.
  • IPC: System V IPC objects and POSIX message queues.
  • Network: network devices, stacks, and ports.
  • Mount: the set of mount points.
  • PID: process IDs.
  • Time: the boot and monotonic clocks.
  • User: user and group IDs.
  • UTS: the hostname and NIS domain name.

Each type has a matching flag, such as CLONE_NEWPID or CLONE_NEWNET. These flags are passed to the system calls that create or join namespaces. Each type also has its own manual page with the details, so you can read deeper on the one you need.

The system calls

Three system calls do most of the work:

  • clone(2) creates a new process. If you pass one or more CLONE_NEW* flags, the child gets new namespaces and becomes a member of them.
  • unshare(2) moves the calling process into new namespaces without creating a child.
  • setns(2) lets a process join an existing namespace. It takes a file descriptor that refers to an entry under /proc/pid/ns.

The ioctl(2) call can also query details about namespaces, as described in the ioctl_nsfs(2) page.

Creating most namespaces needs the CAP_SYS_ADMIN capability, because the creator gains power over resources that other processes in that namespace can see. User namespaces are the exception. Since Linux 3.8, an unprivileged user can create one.

Inspecting namespaces in /proc

Every process has a directory at /proc/pid/ns that holds one entry per namespace type. You can list them for your shell like this:

ls -l /proc/$$/ns

Each entry is a symbolic link whose target combines the type and an inode number, for example uts:[4026531838]. Two processes in the same namespace show the same target. An application can also compare the device ID and inode number returned by stat(2) to check whether two processes share a namespace.

An open file descriptor on one of these entries, or a bind mount of it, keeps the namespace alive even after every process inside it has exited. That descriptor can then be passed to setns(2).

Resource limits

The /proc/sys/user directory exposes per-user limits on how many namespaces of each type may be created inside a user namespace. When a limit is reached, clone(2) and unshare(2) fail with the error ENOSPC. These limits apply to every user, including root, and they also count against ancestor user namespaces. This stops someone from escaping a limit by creating a new user namespace.

Common mistakes

A frequent surprise is that a namespace is not removed just because its processes are gone. A namespace stays alive while a file descriptor or bind mount still refers to it. Another point is that the pid_for_children entry can differ from the pid entry, because child processes may be placed in a different PID namespace than their parent. Finally, a link in /proc/pid/ns is readable only if ptrace access rules allow it, so you may get permission errors when inspecting other users' processes.

Where to go next

If you want to see how namespaces are used in practice, read the official namespaces(7) page linked above and the individual manual pages for each type. Once the concepts are clear, you can experiment by starting a shell with unshare in a test environment and checking the results in /proc.

FAQ

Are Linux namespaces the same as containers?

No. Namespaces are a kernel feature that isolates one kind of resource at a time. A container tool combines several namespace types, along with other features, to run an isolated application.

Do I need root to create a namespace?

Usually yes, because most namespace types need the CAP_SYS_ADMIN capability. User namespaces are the exception, since Linux 3.8 allows an unprivileged user to create one.

How do I see which namespaces my shell is in?

List the entries in /proc/$$/ns with ls -l. Each entry links to a namespace identified by its type and inode number.

Why does a namespace still exist after its processes stop?

An open file descriptor or a bind mount that refers to a /proc/pid/ns entry keeps the namespace alive until that reference is released.

Sources

This page is based on the official documentation below. Read it for full detail.

Linux namespaces — official documentation ↗

Linux namespaces in the news

blozhikDeveloper news from across the web, explained. Every story links to its original sources.