1 .\" Copyright (C) 2011, Eric Biederman <ebiederm@xmission.com>
2 .\" and Copyright (C) 2011, 2012, Michael Kerrisk <mtk.manpages@gamil.com>
4 .\" %%%LICENSE_START(GPLv2_ONELINE)
5 .\" Licensed under the GPLv2
8 .TH SETNS 2 2013-01-01 "Linux" "Linux Programmer's Manual"
10 setns \- reassociate thread with a namespace
13 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
16 .BI "int setns(int " fd ", int " nstype );
19 Given a file descriptor referring to a namespace,
20 reassociate the calling thread with that namespace.
24 argument is a file descriptor referring to one of the namespace entries in a
28 for further information on
30 The calling thread will be reassociated with the corresponding namespace,
31 subject to any constraints imposed by the
37 argument specifies which type of namespace
38 the calling thread may be reassociated with.
39 This argument can have one of the following values:
42 Allow any type of namespace to be joined.
46 must refer to an IPC namespace.
50 must refer to a network namespace.
54 must refer to a UTS namespace.
58 as 0 suffices if the caller knows (or does not care)
59 what type of namespace is referred to by
61 Specifying a nonzero value for
63 is useful if the caller does not know what type of namespace is referred to by
65 and wants to ensure that the namespace is of a particular type.
66 (The caller might not know the type of the namespace referred to by
68 if the file descriptor was opened by another process and, for example,
69 passed to the caller via a UNIX domain socket.)
74 On failure, \-1 is returned and
76 is set to indicate the error.
81 is not a valid file descriptor.
85 refers to a namespace whose type does not match that specified in
87 or there is problem with reassociating the
88 the thread with the specified namespace.
91 Cannot allocate sufficient memory to change the specified namespace.
94 The calling thread did not have the required privilege
100 system call first appeared in Linux in kernel 3.0;
101 library support was added to glibc in version 2.14.
105 system call is Linux-specific.
107 Not all of the attributes that can be shared when
108 a new thread is created using
113 The program below takes two or more arguments.
114 The first argument specifies the pathname of a namespace file in an existing
117 The remaining arguments specify a command and its arguments.
118 The program opens the namespace file, joins that namespace using
120 and executes the specified command inside that namespace.
122 The following shell session demonstrates the use of this program
123 (compiled as a binary named
125 in conjunction with the
127 example program in the
129 man page (complied as a binary named
132 We begin by executing the example program in
135 That program creates a child in a separate UTS namespace.
136 The child changes the hostname in its namespace,
137 and then both processes display the hostnames in their UTS namespaces,
138 so that we can see that they are different.
142 $ \fBsu\fP # Need privilege for namespace operations
144 # \fB./newuts bizarro &\fP
146 clone() returned 3550
147 uts.nodename in child: bizarro
148 uts.nodename in parent: antero
149 # \fBuname \-n\fP # Verify hostname in the shell
154 We then run the program shown below,
155 using it to execute a shell.
156 Inside that shell, we verify that the hostname is the one
157 set by the child created by the first program:
161 # \fB./ns_exec /proc/3550/ns/uts /bin/bash\fP
162 # \fBuname \-n\fP # Executed in shell started by ns_exec
175 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
179 main(int argc, char *argv[])
184 fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\\n", argv[0]);
188 fd = open(argv[1], O_RDONLY); /* Get descriptor for namespace */
192 if (setns(fd, 0) == \-1) /* Join that namespace */
195 execvp(argv[2], &argv[2]); /* Execute a command in namespace */
206 This page is part of release 3.68 of the Linux
209 A description of the project,
210 information about reporting bugs,
211 and the latest version of this page,
213 \%http://www.kernel.org/doc/man\-pages/.