1IP-NETNS(8) Linux IP-NETNS(8)
2
3
4
6 ip-netns - process network namespace management
7
9 ip [ OPTIONS ] netns { COMMAND | help }
10
11 ip netns [ list ]
12
13 ip netns add NETNSNAME
14
15 ip netns attach NETNSNAME PID
16
17 ip [-all] netns del [ NETNSNAME ]
18
19 ip netns set NETNSNAME NETNSID
20
21 ip netns identify [ PID ]
22
23 ip netns pids NETNSNAME
24
25 ip [-all] netns exec [ NETNSNAME ] command...
26
27 ip netns monitor
28
29 ip netns list-id
30
31
33 A network namespace is logically another copy of the network stack,
34 with its own routes, firewall rules, and network devices.
35
36 By default a process inherits its network namespace from its parent.
37 Initially all the processes share the same default network namespace
38 from the init process.
39
40 By convention a named network namespace is an object at
41 /var/run/netns/NAME that can be opened. The file descriptor resulting
42 from opening /var/run/netns/NAME refers to the specified network names‐
43 pace. Holding that file descriptor open keeps the network namespace
44 alive. The file descriptor can be used with the setns(2) system call to
45 change the network namespace associated with a task.
46
47 For applications that are aware of network namespaces, the convention
48 is to look for global network configuration files first in
49 /etc/netns/NAME/ then in /etc/. For example, if you want a different
50 version of /etc/resolv.conf for a network namespace used to isolate
51 your vpn you would name it /etc/netns/myvpn/resolv.conf.
52
53 ip netns exec automates handling of this configuration, file convention
54 for network namespace unaware applications, by creating a mount names‐
55 pace and bind mounting all of the per network namespace configure files
56 into their traditional location in /etc.
57
58
59 ip netns list - show all of the named network namespaces
60
61 This command displays all of the network namespaces in
62 /var/run/netns
63
64
65 ip netns add NAME - create a new named network namespace
66
67 If NAME is available in /var/run/netns/ this command creates a
68 new network namespace and assigns NAME.
69
70
71 ip netns attach NAME PID - create a new named network namespace
72
73 If NAME is available in /var/run/netns/ this command attaches
74 the network namespace of the process PID to NAME as if it were
75 created with ip netns.
76
77
78 ip [-all] netns delete [ NAME ] - delete the name of a network names‐
79 pace(s)
80
81 If NAME is present in /var/run/netns it is umounted and the
82 mount point is removed. If this is the last user of the network
83 namespace the network namespace will be freed and all physical
84 devices will be moved to the default one, otherwise the network
85 namespace persists until it has no more users. ip netns delete
86 may fail if the mount point is in use in another mount names‐
87 pace.
88
89 If -all option was specified then all the network namespace
90 names will be removed.
91
92 It is possible to lose the physical device when it was moved to
93 netns and then this netns was deleted with a running process:
94
95 $ ip netns add net0
96 $ ip link set dev eth0 netns net0
97 $ ip netns exec net0 SOME_PROCESS_IN_BACKGROUND
98 $ ip netns del net0
99
100 and eth0 will appear in the default netns only after
101 SOME_PROCESS_IN_BACKGROUND will exit or will be killed. To pre‐
102 vent this the processes running in net0 should be killed before
103 deleting the netns:
104
105 $ ip netns pids net0 | xargs kill
106 $ ip netns del net0
107
108
109 ip netns set NAME NETNSID - assign an id to a peer network namespace
110
111 This command assigns a id to a peer network namespace. This id
112 is valid only in the current network namespace. If the keyword
113 "auto" is specified an available nsid will be chosen. This id
114 will be used by the kernel in some netlink messages. If no id is
115 assigned when the kernel needs it, it will be automatically
116 assigned by the kernel. Once it is assigned, it's not possible
117 to change it.
118
119
120 ip netns identify [PID] - Report network namespaces names for process
121
122 This command walks through /var/run/netns and finds all the net‐
123 work namespace names for network namespace of the specified
124 process, if PID is not specified then the current process will
125 be used.
126
127
128 ip netns pids NAME - Report processes in the named network namespace
129
130 This command walks through proc and finds all of the process who
131 have the named network namespace as their primary network names‐
132 pace.
133
134
135 ip [-all] netns exec [ NAME ] cmd ... - Run cmd in the named network
136 namespace
137
138 This command allows applications that are network namespace
139 unaware to be run in something other than the default network
140 namespace with all of the configuration for the specified net‐
141 work namespace appearing in the customary global locations. A
142 network namespace and bind mounts are used to move files from
143 their network namespace specific location to their default loca‐
144 tions without affecting other processes.
145
146 If -all option was specified then cmd will be executed syn‐
147 chronously on the each named network namespace even if cmd fails
148 on some of them. Network namespace name is printed on each cmd
149 executing.
150
151
152 ip netns monitor - Report as network namespace names are added and
153 deleted
154
155 This command watches network namespace name addition and dele‐
156 tion events and prints a line for each event it sees.
157
158
159 ip netns list-id - list network namespace ids (nsid)
160
161 Network namespace ids are used to identify a peer network names‐
162 pace. This command displays nsid of the current network names‐
163 pace and provides the corresponding iproute2 netns name (from
164 /var/run/netns) if any.
165
166
168 ip netns list
169 Shows the list of current named network namespaces
170
171 ip netns add vpn
172 Creates a network namespace and names it vpn
173
174 ip netns exec vpn ip link set lo up
175 Bring up the loopback interface in the vpn network namespace.
176
177
179 ip(8)
180
181
183 Original Manpage by Eric W. Biederman
184
185
186
187iproute2 16 Jan 2013 IP-NETNS(8)