blob: c5310e24ed40bf6e939ad8fb1a75c20fb25c8a3c [file] [log] [blame]
Eric W. Biederman1e9014a2013-01-17 14:47:47 +00001.TH IP\-NETNS 8 "16 Jan 2013" "iproute2" "Linux"
Stephen Hemminger2a9721f2011-12-22 10:34:03 -08002.SH NAME
Stephen Hemmingeraab27022012-01-10 10:47:28 -08003ip-netns \- process network namespace management
Stephen Hemminger2a9721f2011-12-22 10:34:03 -08004.SH SYNOPSIS
5.sp
6.ad l
7.in +8
8.ti -8
9.B ip
10.RI "[ " OPTIONS " ]"
11.B netns
12.RI " { " COMMAND " | "
13.BR help " }"
14.sp
15.ti -8
Phil Sutter57e1ace2016-03-02 19:20:00 +010016.BR "ip netns" " [ " list " ]"
Stephen Hemminger2a9721f2011-12-22 10:34:03 -080017
18.ti -8
Vadim Kochan33724932015-01-18 16:10:19 +020019.B ip netns add
Stephen Hemminger2a9721f2011-12-22 10:34:03 -080020.I NETNSNAME
21
22.ti -8
Vadim Kochan33724932015-01-18 16:10:19 +020023.B ip [-all] netns del
24.RI "[ " NETNSNAME " ]"
25
26.ti -8
Phil Sutter57e1ace2016-03-02 19:20:00 +010027.B ip netns set
Nicolas Dichteld182ee12015-02-17 17:30:37 +010028.I NETNSNAME NETNSID
29
30.ti -8
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +000031.BR "ip netns identify"
vadimk0948adc2014-11-07 18:25:30 +020032.RI "[ " PID " ]"
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +000033
34.ti -8
35.BR "ip netns pids"
36.I NETNSNAME
37
38.ti -8
Vadim Kochanb13ba032015-01-18 16:10:18 +020039.BR "ip [-all] netns exec "
40.RI "[ " NETNSNAME " ] " command ...
Stephen Hemminger2a9721f2011-12-22 10:34:03 -080041
Eric W. Biederman1e9014a2013-01-17 14:47:47 +000042.ti -8
43.BR "ip netns monitor"
44
Nicolas Dichteld652ccb2015-04-15 14:23:22 +020045.ti -8
46.BR "ip netns list-id"
47
Stephen Hemminger2a9721f2011-12-22 10:34:03 -080048.SH DESCRIPTION
49A network namespace is logically another copy of the network stack,
Stephen Hemmingercc574302012-11-12 08:05:45 -080050with its own routes, firewall rules, and network devices.
Stephen Hemminger2a9721f2011-12-22 10:34:03 -080051
Vadim Kochan599fc312015-03-24 11:41:52 +020052By default a process inherits its network namespace from its parent. Initially all
Vadim Kochanee9b3472015-03-15 17:49:16 +020053the processes share the same default network namespace from the init process.
54
Stephen Hemminger2a9721f2011-12-22 10:34:03 -080055By convention a named network namespace is an object at
56.BR "/var/run/netns/" NAME
Pavel Šimerdaa89d5322015-04-07 08:41:36 -070057that can be opened. The file descriptor resulting from opening
Stephen Hemminger2a9721f2011-12-22 10:34:03 -080058.BR "/var/run/netns/" NAME
Pavel Šimerdaa89d5322015-04-07 08:41:36 -070059refers to the specified network namespace. Holding that file
60descriptor open keeps the network namespace alive. The file
Stephen Hemminger2a9721f2011-12-22 10:34:03 -080061descriptor can be used with the
62.B setns(2)
63system call to change the network namespace associated with a task.
64
Kees van Reeuwijk49572502013-02-08 03:32:36 +000065For applications that are aware of network namespaces, the convention
66is to look for global network configuration files first in
Stephen Hemminger2a9721f2011-12-22 10:34:03 -080067.BR "/etc/netns/" NAME "/"
68then in
69.BR "/etc/".
70For example, if you want a different version of
71.BR /etc/resolv.conf
72for a network namespace used to isolate your vpn you would name it
73.BR /etc/netns/myvpn/resolv.conf.
74
75.B ip netns exec
76automates handling of this configuration, file convention for network
77namespace unaware applications, by creating a mount namespace and
78bind mounting all of the per network namespace configure files into
79their traditional location in /etc.
80
esr@thyrsus.com61f541f2013-06-21 15:57:21 -040081.TP
82.B ip netns list - show all of the named network namespaces
83.sp
Eric W. Biederman1e9014a2013-01-17 14:47:47 +000084This command displays all of the network namespaces in /var/run/netns
85
esr@thyrsus.com61f541f2013-06-21 15:57:21 -040086.TP
87.B ip netns add NAME - create a new named network namespace
88.sp
Eric W. Biederman1e9014a2013-01-17 14:47:47 +000089If NAME is available in /var/run/netns/ this command creates a new
90network namespace and assigns NAME.
91
esr@thyrsus.com61f541f2013-06-21 15:57:21 -040092.TP
Vadim Kochan33724932015-01-18 16:10:19 +020093.B ip [-all] netns delete [ NAME ] - delete the name of a network namespace(s)
esr@thyrsus.com61f541f2013-06-21 15:57:21 -040094.sp
Eric W. Biederman1e9014a2013-01-17 14:47:47 +000095If NAME is present in /var/run/netns it is umounted and the mount
Pavel Šimerdaa89d5322015-04-07 08:41:36 -070096point is removed. If this is the last user of the network namespace the
Vadim Kochanee9b3472015-03-15 17:49:16 +020097network namespace will be freed and all physical devices will be moved to the
98default one, otherwise the network namespace persists until it has no more
99users. ip netns delete may fail if the mount point is in use in another mount
100namespace.
Eric W. Biederman1e9014a2013-01-17 14:47:47 +0000101
Vadim Kochan33724932015-01-18 16:10:19 +0200102If
103.B -all
104option was specified then all the network namespace names will be removed.
105
Vadim Kochanee9b3472015-03-15 17:49:16 +0200106It is possible to lose the physical device when it was moved to netns and
107then this netns was deleted with a running process:
108
Vadim Kochan599fc312015-03-24 11:41:52 +0200109.RS 10
110$ ip netns add net0
111.RE
112.RS 10
113$ ip link set dev eth0 netns net0
114.RE
115.RS 10
116$ ip netns exec net0 SOME_PROCESS_IN_BACKGROUND
117.RE
118.RS 10
119$ ip netns del net0
120.RE
Vadim Kochanee9b3472015-03-15 17:49:16 +0200121
Vadim Kochanbbf2f7c2015-04-02 18:08:03 +0300122.RS
Vadim Kochanee9b3472015-03-15 17:49:16 +0200123and eth0 will appear in the default netns only after SOME_PROCESS_IN_BACKGROUND
124will exit or will be killed. To prevent this the processes running in net0
125should be killed before deleting the netns:
126
Vadim Kochanbbf2f7c2015-04-02 18:08:03 +0300127.RE
128.RS 10
129$ ip netns pids net0 | xargs kill
130.RE
131.RS 10
132$ ip netns del net0
133.RE
Vadim Kochanee9b3472015-03-15 17:49:16 +0200134
esr@thyrsus.com61f541f2013-06-21 15:57:21 -0400135.TP
Nicolas Dichteld182ee12015-02-17 17:30:37 +0100136.B ip netns set NAME NETNSID - assign an id to a peer network namespace
137.sp
138This command assigns a id to a peer network namespace. This id is valid
139only in the current network namespace.
140This id will be used by the kernel in some netlink messages. If no id is
141assigned when the kernel needs it, it will be automatically assigned by
142the kernel.
143Once it is assigned, it's not possible to change it.
144
145.TP
vadimk0948adc2014-11-07 18:25:30 +0200146.B ip netns identify [PID] - Report network namespaces names for process
esr@thyrsus.com61f541f2013-06-21 15:57:21 -0400147.sp
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000148This command walks through /var/run/netns and finds all the network
vadimk0948adc2014-11-07 18:25:30 +0200149namespace names for network namespace of the specified process, if PID is
150not specified then the current process will be used.
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000151
esr@thyrsus.com61f541f2013-06-21 15:57:21 -0400152.TP
153.B ip netns pids NAME - Report processes in the named network namespace
154.sp
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000155This command walks through proc and finds all of the process who have
156the named network namespace as their primary network namespace.
157
esr@thyrsus.com61f541f2013-06-21 15:57:21 -0400158.TP
Vadim Kochanb13ba032015-01-18 16:10:18 +0200159.B ip [-all] netns exec [ NAME ] cmd ... - Run cmd in the named network namespace
esr@thyrsus.com61f541f2013-06-21 15:57:21 -0400160.sp
Eric W. Biederman1e9014a2013-01-17 14:47:47 +0000161This command allows applications that are network namespace unaware
162to be run in something other than the default network namespace with
163all of the configuration for the specified network namespace appearing
Pavel Šimerdaa89d5322015-04-07 08:41:36 -0700164in the customary global locations. A network namespace and bind mounts
Eric W. Biederman1e9014a2013-01-17 14:47:47 +0000165are used to move files from their network namespace specific location
166to their default locations without affecting other processes.
167
Vadim Kochanb13ba032015-01-18 16:10:18 +0200168If
169.B -all
170option was specified then
171.B cmd
172will be executed synchronously on the each named network namespace even if
173.B cmd
174fails on some of them. Network namespace name is printed on each
175.B cmd
176executing.
177
esr@thyrsus.com61f541f2013-06-21 15:57:21 -0400178.TP
179.B ip netns monitor - Report as network namespace names are added and deleted
180.sp
Eric W. Biederman1e9014a2013-01-17 14:47:47 +0000181This command watches network namespace name addition and deletion events
182and prints a line for each event it sees.
183
Nicolas Dichteld652ccb2015-04-15 14:23:22 +0200184.TP
185.B ip netns list-id - list network namespace ids (nsid)
186.sp
187Network namespace ids are used to identify a peer network namespace. This
188command displays nsid of the current network namespace and provides the
189corresponding iproute2 netns name (from /var/run/netns) if any.
190
Stephen Hemminger2a9721f2011-12-22 10:34:03 -0800191.SH EXAMPLES
Eric W. Biederman1e9014a2013-01-17 14:47:47 +0000192.PP
193ip netns list
194.RS
195Shows the list of current named network namespaces
196.RE
197.PP
198ip netns add vpn
199.RS
200Creates a network namespace and names it vpn
201.RE
202.PP
203ip netns exec vpn ip link set lo up
204.RS
205Bring up the loopback interface in the vpn network namespace.
206.RE
Stephen Hemminger2a9721f2011-12-22 10:34:03 -0800207
208.SH SEE ALSO
209.br
210.BR ip (8)
211
212.SH AUTHOR
213Original Manpage by Eric W. Biederman