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_NEWUSER (since Linux 3.8)
82              This flag has the same  effect  as  the  clone(2)  CLONE_NEWUSER
83              flag.   Unshare  the user namespace, so that the calling process
84              is moved into a new user namespace which is not shared with  any
85              previously  existing process.  As with the child process created
86              by clone(2) with the CLONE_NEWUSER flag, the  caller  obtains  a
87              full set of capabilities in the new namespace.
88
89              CLONE_NEWUSER requires that the calling process is not threaded;
90              specifying  CLONE_NEWUSER  automatically  implies  CLONE_THREAD.
91              Since   Linux  3.9,  CLONE_NEWUSER  also  automatically  implies
92              CLONE_FS.  CLONE_NEWUSER requires that the user ID and group  ID
93              of  the  calling process are mapped to user IDs and group IDs in
94              the user namespace of the calling process at  the  time  of  the
95              call.
96
97              For  further  information  on  user  namespaces, see user_names‐
98              paces(7).
99
100       CLONE_NEWUTS (since Linux 2.6.19)
101              This flag has the same effect as the clone(2) CLONE_NEWUTS flag.
102              Unshare the UTS IPC namespace, so that the calling process has a
103              private copy of the UTS namespace which is not shared  with  any
104              other  process.   Use of CLONE_NEWUTS requires the CAP_SYS_ADMIN
105              capability.
106
107       CLONE_SYSVSEM (since Linux 2.6.26)
108              This flag reverses the  effect  of  the  clone(2)  CLONE_SYSVSEM
109              flag.  Unshare System V semaphore adjustment (semadj) values, so
110              that the calling process has a new empty semadj list that is not
111              shared with any other process.  If this is the last process that
112              has a reference to the process's current semadj list,  then  the
113              adjustments  in that list are applied to the corresponding sema‐
114              phores, as described in semop(2).
115
116       In addition, CLONE_THREAD, CLONE_SIGHAND, and CLONE_VM can be specified
117       in  flags if the caller is single threaded (i.e., it is not sharing its
118       address space with another process or thread).   In  this  case,  these
119       flags have no effect.  (Note also that specifying CLONE_THREAD automat‐
120       ically implies CLONE_VM, and specifying CLONE_VM automatically  implies
121       CLONE_SIGHAND.)  If the process is multithreaded, then the use of these
122       flags results in an error.
123
124       If flags is specified as zero, then unshare() is a  no-op;  no  changes
125       are made to the calling process's execution context.
126

RETURN VALUE

128       On success, zero returned.  On failure, -1 is returned and errno is set
129       to indicate the error.
130

ERRORS

132       EINVAL An invalid bit was specified in flags.
133
134       EINVAL CLONE_THREAD, CLONE_SIGHAND, or CLONE_VM was specified in flags,
135              and the caller is multithreaded.
136
137       ENOMEM Cannot allocate sufficient memory to copy parts of caller's con‐
138              text that need to be unshared.
139
140       ENOSPC (since Linux 3.7)
141              CLONE_NEWPID was specified in flags, but the limit on the  nest‐
142              ing  depth  of  PID  namespaces  would  have  been exceeded; see
143              pid_namespaces(7).
144
145       ENOSPC (since Linux 4.9; beforehand EUSERS)
146              CLONE_NEWUSER was specified in flags, and the call  would  cause
147              the  limit  on  the  number  of  nested  user  namespaces  to be
148              exceeded.  See user_namespaces(7).
149
150              From Linux 3.11 to Linux 4.8, the error diagnosed in  this  case
151              was EUSERS.
152
153       ENOSPC (since Linux 4.9)
154              One  of the values in flags specified the creation of a new user
155              namespace, but doing so would have caused the limit  defined  by
156              the  corresponding  file  in /proc/sys/user to be exceeded.  For
157              further details, see namespaces(7).
158
159       EPERM  The calling process did not have  the  required  privileges  for
160              this operation.
161
162       EPERM  CLONE_NEWUSER  was  specified in flags, but either the effective
163              user ID or the effective group ID of the caller does not have  a
164              mapping in the parent namespace (see user_namespaces(7)).
165
166       EPERM (since Linux 3.9)
167              CLONE_NEWUSER  was  specified  in  flags  and the caller is in a
168              chroot environment (i.e., the caller's root directory  does  not
169              match  the  root  directory  of  the mount namespace in which it
170              resides).
171
172       EUSERS (from Linux 3.11 to Linux 4.8)
173              CLONE_NEWUSER was specified in flags, and the limit on the  num‐
174              ber  of  nested user namespaces would be exceeded.  See the dis‐
175              cussion of the ENOSPC error above.
176

VERSIONS

178       The unshare() system call was added to Linux in kernel 2.6.16.
179

CONFORMING TO

181       The unshare() system call is Linux-specific.
182

NOTES

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

EXAMPLE

191       The program below provides a simple implementation  of  the  unshare(1)
192       command, which unshares one or more namespaces and executes the command
193       supplied in its command-line arguments.  Here's an example of  the  use
194       of  this program, running a shell in a new mount namespace, and verify‐
195       ing that the original shell and the new shell  are  in  separate  mount
196       namespaces:
197
198           $ readlink /proc/$$/ns/mnt
199           mnt:[4026531840]
200           $ sudo ./unshare -m /bin/bash
201           # readlink /proc/$$/ns/mnt
202           mnt:[4026532325]
203
204       The differing output of the two readlink(1) commands shows that the two
205       shells are in different mount namespaces.
206
207   Program source
208
209       /* unshare.c
210
211          A simple implementation of the unshare(1) command: unshare
212          namespaces and execute a command.
213       */
214       #define _GNU_SOURCE
215       #include <sched.h>
216       #include <unistd.h>
217       #include <stdlib.h>
218       #include <stdio.h>
219
220       /* A simple error-handling function: print an error message based
221          on the value in 'errno' and terminate the calling process */
222
223       #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
224                               } while (0)
225
226       static void
227       usage(char *pname)
228       {
229           fprintf(stderr, "Usage: %s [options] program [arg...]\n", pname);
230           fprintf(stderr, "Options can be:\n");
231           fprintf(stderr, "    -i   unshare IPC namespace\n");
232           fprintf(stderr, "    -m   unshare mount namespace\n");
233           fprintf(stderr, "    -n   unshare network namespace\n");
234           fprintf(stderr, "    -p   unshare PID namespace\n");
235           fprintf(stderr, "    -u   unshare UTS namespace\n");
236           fprintf(stderr, "    -U   unshare user namespace\n");
237           exit(EXIT_FAILURE);
238       }
239
240       int
241       main(int argc, char *argv[])
242       {
243           int flags, opt;
244
245           flags = 0;
246
247           while ((opt = getopt(argc, argv, "imnpuU")) != -1) {
248               switch (opt) {
249               case 'i': flags |= CLONE_NEWIPC;        break;
250               case 'm': flags |= CLONE_NEWNS;         break;
251               case 'n': flags |= CLONE_NEWNET;        break;
252               case 'p': flags |= CLONE_NEWPID;        break;
253               case 'u': flags |= CLONE_NEWUTS;        break;
254               case 'U': flags |= CLONE_NEWUSER;       break;
255               default:  usage(argv[0]);
256               }
257           }
258
259           if (optind >= argc)
260               usage(argv[0]);
261
262           if (unshare(flags) == -1)
263               errExit("unshare");
264
265           execvp(argv[optind], &argv[optind]);
266           errExit("execvp");
267       }
268

SEE ALSO

270       unshare(1), clone(2),  fork(2),  kcmp(2),  setns(2),  vfork(2),  names‐
271       paces(7)
272
273       Documentation/userspace-api/unshare.rst in the Linux kernel source tree
274       (or Documentation/unshare.txt before Linux 4.12)
275

COLOPHON

277       This page is part of release 4.15 of the Linux  man-pages  project.   A
278       description  of  the project, information about reporting bugs, and the
279       latest    version    of    this    page,    can     be     found     at
280       https://www.kernel.org/doc/man-pages/.
281
282
283
284Linux                             2018-02-02                        UNSHARE(2)
Impressum