1UNSHARE(2)                 Linux Programmer's Manual                UNSHARE(2)
2
3
4

NAME

6       unshare - disassociate parts of the process execution context
7

SYNOPSIS

9       #define _GNU_SOURCE
10       #include <sched.h>
11
12       int unshare(int flags);
13

DESCRIPTION

15       unshare()  allows  a  process  (or thread) to disassociate parts of its
16       execution context that are currently being shared with other  processes
17       (or  threads).  Part of the execution context, such as the mount names‐
18       pace, is shared implicitly when a new process is created using  fork(2)
19       or  vfork(2),  while other parts, such as virtual memory, may be shared
20       by explicit request when creating a process or thread using clone(2).
21
22       The main use of unshare() is to allow a process to control  its  shared
23       execution context without creating a new process.
24
25       The flags argument is a bit mask that specifies which parts of the exe‐
26       cution context should be unshared.  This argument is specified by ORing
27       together zero or more of the following constants:
28
29       CLONE_FILES
30              Reverse  the  effect  of the clone(2) CLONE_FILES flag.  Unshare
31              the file descriptor table, so that the calling process no longer
32              shares its file descriptors with any other process.
33
34       CLONE_FS
35              Reverse  the  effect  of  the  clone(2)  CLONE_FS flag.  Unshare
36              filesystem attributes, so that the  calling  process  no  longer
37              shares   its   root  directory  (chroot(2)),  current  directory
38              (chdir(2)),  or  umask  (umask(2))  attributes  with  any  other
39              process.
40
41       CLONE_NEWCGROUP (since Linux 4.6)
42              This  flag  has  the same effect as the clone(2) CLONE_NEWCGROUP
43              flag.  Unshare the cgroup  namespace.   Use  of  CLONE_NEWCGROUP
44              requires the CAP_SYS_ADMIN capability.
45
46       CLONE_NEWIPC (since Linux 2.6.19)
47              This flag has the same effect as the clone(2) CLONE_NEWIPC flag.
48              Unshare the IPC namespace, so that the  calling  process  has  a
49              private  copy  of the IPC namespace which is not shared with any
50              other  process.   Specifying  this  flag  automatically  implies
51              CLONE_SYSVSEM   as  well.   Use  of  CLONE_NEWIPC  requires  the
52              CAP_SYS_ADMIN capability.
53
54       CLONE_NEWNET (since Linux 2.6.24)
55              This flag has the same effect as the clone(2) CLONE_NEWNET flag.
56              Unshare  the  network  namespace, so that the calling process is
57              moved into a new network namespace which is not shared with  any
58              previously  existing  process.  Use of CLONE_NEWNET requires the
59              CAP_SYS_ADMIN capability.
60
61       CLONE_NEWNS
62              This flag has the same effect as the clone(2) CLONE_NEWNS  flag.
63              Unshare  the  mount namespace, so that the calling process has a
64              private copy of its namespace which is not shared with any other
65              process.  Specifying this flag automatically implies CLONE_FS as
66              well.  Use of CLONE_NEWNS requires the CAP_SYS_ADMIN capability.
67              For further information, see mount_namespaces(7).
68
69       CLONE_NEWPID (since Linux 3.8)
70              This flag has the same effect as the clone(2) CLONE_NEWPID flag.
71              Unshare the PID namespace, so that the calling process has a new
72              PID namespace for its children which is not shared with any pre‐
73              viously existing process.  The calling process is not moved into
74              the  new  namespace.   The  first  child  created by the calling
75              process will have the process ID 1 and will assume the  role  of
76              init(1)   in  the  new  namespace.   CLONE_NEWPID  automatically
77              implies CLONE_THREAD as well.  Use of CLONE_NEWPID requires  the
78              CAP_SYS_ADMIN   capability.    For   further   information,  see
79              pid_namespaces(7).
80
81       CLONE_NEWTIME (since Linux 5.6)
82              Unshare the time namespace, so that the calling  process  has  a
83              new time namespace for its children which is not shared with any
84              previously existing process.  The calling process is  not  moved
85              into  the  new  namespace.   Use  of  CLONE_NEWTIME requires the
86              CAP_SYS_ADMIN  capability.    For   further   information,   see
87              time_namespaces(7).
88
89       CLONE_NEWUSER (since Linux 3.8)
90              This  flag  has  the  same  effect as the clone(2) CLONE_NEWUSER
91              flag.  Unshare the user namespace, so that the  calling  process
92              is  moved into a new user namespace which is not shared with any
93              previously existing process.  As with the child process  created
94              by  clone(2)  with  the CLONE_NEWUSER flag, the caller obtains a
95              full set of capabilities in the new namespace.
96
97              CLONE_NEWUSER requires that the calling process is not threaded;
98              specifying  CLONE_NEWUSER  automatically  implies  CLONE_THREAD.
99              Since  Linux  3.9,  CLONE_NEWUSER  also  automatically   implies
100              CLONE_FS.   CLONE_NEWUSER requires that the user ID and group ID
101              of the calling process are mapped to user IDs and group  IDs  in
102              the  user  namespace  of  the calling process at the time of the
103              call.
104
105              For further information  on  user  namespaces,  see  user_names‐
106              paces(7).
107
108       CLONE_NEWUTS (since Linux 2.6.19)
109              This flag has the same effect as the clone(2) CLONE_NEWUTS flag.
110              Unshare the UTS IPC namespace, so that the calling process has a
111              private  copy  of the UTS namespace which is not shared with any
112              other process.  Use of CLONE_NEWUTS requires  the  CAP_SYS_ADMIN
113              capability.
114
115       CLONE_SYSVSEM (since Linux 2.6.26)
116              This  flag  reverses  the  effect  of the clone(2) CLONE_SYSVSEM
117              flag.  Unshare System V semaphore adjustment (semadj) values, so
118              that the calling process has a new empty semadj list that is not
119              shared with any other process.  If this is the last process that
120              has  a  reference to the process's current semadj list, then the
121              adjustments in that list are applied to the corresponding  sema‐
122              phores, as described in semop(2).
123
124       In addition, CLONE_THREAD, CLONE_SIGHAND, and CLONE_VM can be specified
125       in flags if the caller is single threaded (i.e., it is not sharing  its
126       address  space  with  another  process or thread).  In this case, these
127       flags have no effect.  (Note also that specifying CLONE_THREAD automat‐
128       ically  implies CLONE_VM, and specifying CLONE_VM automatically implies
129       CLONE_SIGHAND.)  If the process is multithreaded, then the use of these
130       flags results in an error.
131
132       If  flags  is  specified as zero, then unshare() is a no-op; no changes
133       are made to the calling process's execution context.
134

RETURN VALUE

136       On success, zero returned.  On failure, -1 is returned and errno is set
137       to indicate the error.
138

ERRORS

140       EINVAL An invalid bit was specified in flags.
141
142       EINVAL CLONE_THREAD, CLONE_SIGHAND, or CLONE_VM was specified in flags,
143              and the caller is multithreaded.
144
145       EINVAL CLONE_NEWIPC was specified in flags, but the kernel was not con‐
146              figured with the CONFIG_SYSVIPC and CONFIG_IPC_NS options.
147
148       EINVAL CLONE_NEWNET was specified in flags, but the kernel was not con‐
149              figured with the CONFIG_NET_NS option.
150
151       EINVAL CLONE_NEWPID was specified in flags, but the kernel was not con‐
152              figured with the CONFIG_PID_NS option.
153
154       EINVAL CLONE_NEWUSER  was  specified  in  flags, but the kernel was not
155              configured with the CONFIG_USER_NS option.
156
157       EINVAL CLONE_NEWUTS was specified in flags, but the kernel was not con‐
158              figured with the CONFIG_UTS_NS option.
159
160       EINVAL CLONE_NEWPID  was specified in flags, but the process has previ‐
161              ously called unshare() with the CLONE_NEWPID flag.
162
163       ENOMEM Cannot allocate sufficient memory to copy parts of caller's con‐
164              text that need to be unshared.
165
166       ENOSPC (since Linux 3.7)
167              CLONE_NEWPID  was specified in flags, but the limit on the nest‐
168              ing depth of  PID  namespaces  would  have  been  exceeded;  see
169              pid_namespaces(7).
170
171       ENOSPC (since Linux 4.9; beforehand EUSERS)
172              CLONE_NEWUSER  was  specified in flags, and the call would cause
173              the limit  on  the  number  of  nested  user  namespaces  to  be
174              exceeded.  See user_namespaces(7).
175
176              From  Linux  3.11 to Linux 4.8, the error diagnosed in this case
177              was EUSERS.
178
179       ENOSPC (since Linux 4.9)
180              One of the values in flags specified the creation of a new  user
181              namespace,  but  doing so would have caused the limit defined by
182              the corresponding file in /proc/sys/user to  be  exceeded.   For
183              further details, see namespaces(7).
184
185       EPERM  The  calling  process  did  not have the required privileges for
186              this operation.
187
188       EPERM  CLONE_NEWUSER was specified in flags, but either  the  effective
189              user  ID or the effective group ID of the caller does not have a
190              mapping in the parent namespace (see user_namespaces(7)).
191
192       EPERM (since Linux 3.9)
193              CLONE_NEWUSER was specified in flags and  the  caller  is  in  a
194              chroot  environment  (i.e., the caller's root directory does not
195              match the root directory of the  mount  namespace  in  which  it
196              resides).
197
198       EUSERS (from Linux 3.11 to Linux 4.8)
199              CLONE_NEWUSER  was specified in flags, and the limit on the num‐
200              ber of nested user namespaces would be exceeded.  See  the  dis‐
201              cussion of the ENOSPC error above.
202

VERSIONS

204       The unshare() system call was added to Linux in kernel 2.6.16.
205

CONFORMING TO

207       The unshare() system call is Linux-specific.
208

NOTES

210       Not all of the process attributes that can be shared when a new process
211       is created using clone(2) can be unshared using unshare().  In particu‐
212       lar,  as at kernel 3.8, unshare() does not implement flags that reverse
213       the effects of CLONE_SIGHAND, CLONE_THREAD, or  CLONE_VM.   Such  func‐
214       tionality may be added in the future, if required.
215

EXAMPLES

217       The  program  below  provides a simple implementation of the unshare(1)
218       command, which unshares one or more namespaces and executes the command
219       supplied  in  its command-line arguments.  Here's an example of the use
220       of this program, running a shell in a new mount namespace, and  verify‐
221       ing  that  the  original  shell and the new shell are in separate mount
222       namespaces:
223
224           $ readlink /proc/$$/ns/mnt
225           mnt:[4026531840]
226           $ sudo ./unshare -m /bin/bash
227           # readlink /proc/$$/ns/mnt
228           mnt:[4026532325]
229
230       The differing output of the two readlink(1) commands shows that the two
231       shells are in different mount namespaces.
232
233   Program source
234
235       /* unshare.c
236
237          A simple implementation of the unshare(1) command: unshare
238          namespaces and execute a command.
239       */
240       #define _GNU_SOURCE
241       #include <sched.h>
242       #include <unistd.h>
243       #include <stdlib.h>
244       #include <stdio.h>
245
246       /* A simple error-handling function: print an error message based
247          on the value in 'errno' and terminate the calling process */
248
249       #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
250                               } while (0)
251
252       static void
253       usage(char *pname)
254       {
255           fprintf(stderr, "Usage: %s [options] program [arg...]\n", pname);
256           fprintf(stderr, "Options can be:\n");
257           fprintf(stderr, "    -C   unshare cgroup namespace\n");
258           fprintf(stderr, "    -i   unshare IPC namespace\n");
259           fprintf(stderr, "    -m   unshare mount namespace\n");
260           fprintf(stderr, "    -n   unshare network namespace\n");
261           fprintf(stderr, "    -p   unshare PID namespace\n");
262           fprintf(stderr, "    -t   unshare time namespace\n");
263           fprintf(stderr, "    -u   unshare UTS namespace\n");
264           fprintf(stderr, "    -U   unshare user namespace\n");
265           exit(EXIT_FAILURE);
266       }
267
268       int
269       main(int argc, char *argv[])
270       {
271           int flags, opt;
272
273           flags = 0;
274
275           while ((opt = getopt(argc, argv, "CimnptuU")) != -1) {
276               switch (opt) {
277               case 'C': flags |= CLONE_NEWCGROUP;      break;
278               case 'i': flags |= CLONE_NEWIPC;        break;
279               case 'm': flags |= CLONE_NEWNS;         break;
280               case 'n': flags |= CLONE_NEWNET;        break;
281               case 'p': flags |= CLONE_NEWPID;        break;
282               case 't': flags |= CLONE_NEWTIME;        break;
283               case 'u': flags |= CLONE_NEWUTS;        break;
284               case 'U': flags |= CLONE_NEWUSER;       break;
285               default:  usage(argv[0]);
286               }
287           }
288
289           if (optind >= argc)
290               usage(argv[0]);
291
292           if (unshare(flags) == -1)
293               errExit("unshare");
294
295           execvp(argv[optind], &argv[optind]);
296           errExit("execvp");
297       }
298

SEE ALSO

300       unshare(1),  clone(2),  fork(2),  kcmp(2),  setns(2),  vfork(2), names‐
301       paces(7)
302
303       Documentation/userspace-api/unshare.rst in the Linux kernel source tree
304       (or Documentation/unshare.txt before Linux 4.12)
305

COLOPHON

307       This  page  is  part of release 5.07 of the Linux man-pages project.  A
308       description of the project, information about reporting bugs,  and  the
309       latest     version     of     this    page,    can    be    found    at
310       https://www.kernel.org/doc/man-pages/.
311
312
313
314Linux                             2020-04-11                        UNSHARE(2)
Impressum