setns(2)

๐Ÿ’– setns(2) ๐Ÿ’– โ€” reassociate thread with a namespace

okay bestie, welcome to the setns(2) page, straight out of the System Calls Manual (that's section 2, iykyk) ๐Ÿฆ‹โœจ this one's a banger, let's get into it.


๐ŸŒธ NAME ๐ŸŒธ

setns โ€” reassociate thread with a namespace. that's it, that's the vibe. ๐Ÿ’…

๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc) ๐ŸŽ€

โœจ SYNOPSIS โœจ

#define _GNU_SOURCE             /* See feature_test_macros(7) */
#include <sched.h>

int setns(int fd, int nstype);

๐Ÿซถ DESCRIPTION ๐Ÿซถ

So the setns() system call lets the calling thread move into different namespaces. iconic behaviour honestly. the fd argument is one of these two things:

  • a file descriptor referring to one of the magic links in a /proc/pid/ns/ directory (or a bind mount to such a link) ๐Ÿ”—
  • a PID file descriptor (see pidfd_open(2)) ๐Ÿ†”

and the nstype argument? it gets interpreted differently in each of those cases. two moods, one syscall, we love range. ๐ŸŒˆ

If fd refers to a /proc/pid/ns/ link, then setns() reassociates the calling thread with the namespace associated with that link, subject to any constraints imposed by the nstype argument. and bestie, in this usage, each call to setns() changes just one of the caller's namespace memberships. one namespace per call, no bundling, ok? ๐ŸŽ€

so nstype specifies which type of namespace the calling thread may be reassociated with. this argument can be one of these:

  • 0 โ€” Allow any type of namespace to be joined. the "surprise me" option ๐Ÿ˜Œ
  • CLONE_NEWCGROUP (since Linux 4.6) โ€” fd must refer to a cgroup namespace.
  • CLONE_NEWIPC (since Linux 3.0) โ€” fd must refer to an IPC namespace.
  • CLONE_NEWNET (since Linux 3.0) โ€” fd must refer to a network namespace.
  • CLONE_NEWNS (since Linux 3.8) โ€” fd must refer to a mount namespace.
  • CLONE_NEWPID (since Linux 3.8) โ€” fd must refer to a descendant PID namespace.
  • CLONE_NEWTIME (since Linux 5.8) โ€” fd must refer to a time namespace.
  • CLONE_NEWUSER (since Linux 3.8) โ€” fd must refer to a user namespace.
  • CLONE_NEWUTS (since Linux 3.0) โ€” fd must refer to a UTS namespace.

specifying nstype as 0 suffices if the caller knows (or does not care) what type of namespace is referred to by fd. and specifying a nonzero value for nstype is useful if the caller does not know what type of namespace is referred to by fd and wants to make sure the namespace is of a particular type. (the caller might not know the type of the namespace referred to by fd if the file descriptor was opened by another process and, for example, passed to the caller via a UNIX domain socket. sneaky little handoff, we've all been there ๐Ÿ™ˆ)

fd is a PID file descriptor

since Linux 5.8, fd may refer to a PID file descriptor obtained from pidfd_open(2) or clone(2). in this usage, setns() atomically moves the calling thread into one or more of the same namespaces as the thread referred to by fd. atomic means atomic, no half-moves, periodt. ๐Ÿ’€

now the nstype argument is a bit mask specified by ORing together one or more of the CLONE_NEW* namespace constants listed above โฌ†๏ธ. the caller is moved into each of the target thread's namespaces that is specified in nstype; the caller's memberships in the remaining namespaces are left unchanged. untouched. unbothered. โœจ

for example, the following code would move the caller into the same user, network, and UTS namespaces as PID 1234, but would leave the caller's other namespace memberships unchanged:

int fd = pidfd_open(1234, 0);
setns(fd, CLONE_NEWUSER | CLONE_NEWNET | CLONE_NEWUTS);

Details for specific namespace types

okay now the fine print, bestie, and you know we read the fine print around here. note the following details and restrictions when reassociating with specific namespace types:

User namespaces

a process reassociating itself with a user namespace must have the CAP_SYS_ADMIN capability in the target user namespace. (this necessarily implies that it is only possible to join a descendant user namespace.) upon successfully joining a user namespace, a process is granted all capabilities in that namespace, regardless of its user and group IDs. glow up incoming ๐Ÿ‘‘

a multithreaded process may not change user namespace with setns(). full stop. ๐Ÿ›‘

it is not permitted to use setns() to reenter the caller's current user namespace. this prevents a caller that has dropped capabilities from regaining those capabilities via a call to setns(). no take-backs, we see you trying it. ๐Ÿ‘€

and for security reasons, a process can't join a new user namespace if it is sharing filesystem-related attributes (the attributes whose sharing is controlled by the clone(2) CLONE_FS flag) with another process. sharing is caring except when it's a security hole, love that for us. ๐Ÿ’€

for further details on user namespaces, see user_namespaces(7).

Mount namespaces

changing the mount namespace requires that the caller possess both CAP_SYS_CHROOT and CAP_SYS_ADMIN capabilities in its own user namespace and CAP_SYS_ADMIN in the user namespace that owns the target mount namespace. two houses, two sets of keys. ๐Ÿ”‘

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.

see user_namespaces(7) for details on the interaction of user namespaces and mount namespaces. ๐Ÿฆ‹

PID namespaces

in order to reassociate itself with a new PID 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 PID namespace.

reassociating the PID namespace has somewhat different from other namespace types. reassociating the calling thread with a PID namespace changes only the PID namespace that subsequently created child processes of the caller will be placed in; it does not change the PID namespace of the caller itself. read that twice bestie, it's the plot twist. ๐Ÿ˜ญ

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. no going back up the family tree ๐ŸŒณ๐Ÿ™…

for further details on PID namespaces, see pid_namespaces(7).

Cgroup namespaces

in order to reassociate itself with a new cgroup 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 cgroup namespace.

using setns() to change the caller's cgroup namespace does not change the caller's cgroup memberships. important, don't get it twisted. ๐ŸŽ€

Time namespaces

in order to reassociate itself with a new time 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.

a multithreaded process may not change time namespace with setns(). โฐ๐Ÿšซ

Network, IPC, 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.


๐Ÿ’… RETURN VALUE ๐Ÿ’…

  • on success, setns() returns 0 โœ…
  • on failure, -1 is returned and errno is set to indicate the error โŒ

๐Ÿ˜ญ ERRORS ๐Ÿ˜ญ

  • EBADF โ€” fd is not a valid file descriptor. bestie that's not a real fd ๐Ÿ˜ฌ
  • EINVAL โ€” fd refers to a namespace whose type does not match that specified in nstype. wrong namespace, wrong vibe check.
  • EINVAL โ€” There is a problem with reassociating the thread with the specified namespace.
  • EINVAL โ€” The caller tried to join an ancestor (parent, grandparent, and so on) PID namespace. no climbing back up, we said what we said.
  • EINVAL โ€” The caller attempted to join the user namespace in which it is already a member. you're already here, girl ๐Ÿ’€
  • EINVAL โ€” The caller shares filesystem (CLONE_FS) state (in particular, the root directory) with other processes and tried to join a new user namespace.
  • EINVAL โ€” The caller is multithreaded and tried to join a new user namespace. (we warned you up top ๐Ÿ›‘)
  • EINVAL โ€” fd is a PID file descriptor and nstype is invalid (e.g., it is 0).
  • ENOMEM โ€” Cannot allocate sufficient memory to change the specified namespace. no ram, no move ๐Ÿ˜”
  • EPERM โ€” The calling thread did not have the required capability for this operation. permissions said no ๐Ÿšซโœจ
  • ESRCH โ€” fd is a PID file descriptor but the process it refers to no longer exists (i.e., it has terminated and been waited on). rip to that process ๐Ÿชฆ

๐ŸŒˆ STANDARDS ๐ŸŒˆ

Linux. that's the standard, babes. ๐Ÿง

๐ŸŽ€ VERSIONS ๐ŸŽ€

Linux 3.0, glibc 2.14. vintage but still serving. ๐Ÿ’–

๐Ÿฆ‹ NOTES ๐Ÿฆ‹

for further information on the /proc/pid/ns/ magic links, see namespaces(7). (magic links! literally magic ๐Ÿช„)

and heads up: not all of the attributes that can be shared when a new thread is created using clone(2) can be changed using setns(). some things are forever, apparently. ๐Ÿ’€


โœจ EXAMPLES โœจ

the program below takes two or more arguments. the first argument specifies the pathname of a namespace file in an existing /proc/pid/ns/ directory. the remaining arguments specify a command and its arguments. the program opens the namespace file, joins that namespace using setns(), and executes the specified command inside that namespace. chef's kiss, main character energy fr fr. ๐Ÿ‘‘

the following shell session demonstrates the use of this program (compiled as a binary named ns_exec) in conjunction with the CLONE_NEWUTS example program in the clone(2) man page (complied as a binary named newuts). ๐Ÿงš

we begin by executing the example program in clone(2) in the background. that program creates a child in a separate UTS namespace. the child changes the hostname in its namespace, and then both processes display the hostnames in their UTS namespaces, so that we can see that they are different. ๐Ÿ‘€

$ su;                   # Need privilege for namespace operations
Password:
# ./newuts bizarro &
[1] 3549
clone() returned 3550
uts.nodename in child:  bizarro
uts.nodename in parent: antero
# uname -n;             # Verify hostname in the shell
antero

we then run the program shown below, using it to execute a shell. inside that shell, we verify that the hostname is the one set by the child created by the first program: ๐Ÿš

# ./ns_exec /proc/3550/ns/uts /bin/bash;
# uname -n;             # Executed in shell started by ns_exec
bizarro

Program source

#define _GNU_SOURCE
#include <err.h>
#include <fcntl.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
    int fd;

    if (argc < 3) {
        fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    /* Get file descriptor for namespace; the file descriptor is opened
       with O_CLOEXEC so as to ensure that it is not inherited by the
       program that is later executed.  */

    fd = open(argv[1], O_RDONLY | O_CLOEXEC);
    if (fd == -1)
        err(EXIT_FAILURE, "open");

    if (setns(fd, 0) == -1)       /* Join that namespace */
        err(EXIT_FAILURE, "setns");

    execvp(argv[2], &argv[2]);    /* Execute a command in namespace */
    err(EXIT_FAILURE, "execvp");
}

bestie wait, don't go anywhere โ€” this page is long so it continues in part 2 ๐Ÿ’–โœจ๐Ÿฆ‹

๐Ÿ” SEE ALSO ๐Ÿ”

okay bestie, you made it to the end, so here's your little reading list ๐Ÿ“š โ€” go forth and click through these, they're the whole friend group:

  • nsenter(1) โ€” the command-line bestie that actually walks into namespaces for you
  • clone(2) โ€” sibling syscall energy
  • fork(2) โ€” the classic
  • unshare(2) โ€” the other one that moves you instead of your kid
  • vfork(2) โ€” yes, it's still here
  • namespaces(7) โ€” the big picture, the lore, the whole vibe
  • unix(7) โ€” for all your Unix domain socket needs ๐Ÿซถ

aaaand that's the page!! ๐Ÿ’– you read the whole thing, absolutely ate, no crumbs left โœจ this little number came out of Linux man-pages 6.20 and it's dated 2026-02-08 โ€” so if you're reading it sometime in the far future, hi, hope the kernel is kind to you ๐ŸŒธ bye bestie, go unshare responsibly ๐Ÿ’…๐Ÿ‘‘

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: GPL-2.0-only
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms