IP-NETNS(8)
๐ IP-NETNS(8) โ process network namespace management ๐
okay bestie, welcome to ip-netns, section 8 of the Linux manual pages ๐ฆ this is the little corner of the ip command that lets you build, name, poke at and yeet network namespaces. group-chat energy, but make it kernel-grade. let's go ๐
โจ NAME โจ
ip-netns โ process network namespace management ๐
โจ SYNOPSIS โจ
ip [ OPTIONS ] netns { COMMAND | help }
ip netns [ list ]
ip netns add NETNSNAME
ip netns attach NETNSNAME PID
ip [-all] netns del [ NETNSNAME ]
ip netns set NETNSNAME NETNSID
NETNSID := auto | POSITIVE-INT
ip netns identify [ PID ]
ip netns pids NETNSNAME
ip [-all] netns exec [ NETNSNAME ] command...
ip netns monitor
ip netns list-id [ target-nsid POSITIVE-INT ] [ nsid POSITIVE-INT ]
โจ DESCRIPTION โจ
so a network namespace is, like, logically another copy of the network stack ๐ซ it comes with its own routes, its own firewall rules, and its own network devices. fully its own little world.
by default a process inherits its network namespace from its parent. and at the very beginning, literally every process shares the same default network namespace โ the one from the init process. it's giving "we all started in the same group chat" ๐ซถ
by convention a named network namespace is an object at /var/run/netns/NAME that can be opened. the file descriptor you get from opening /var/run/netns/NAME refers to that specified network namespace. holding that file descriptor open keeps the network namespace alive โ no cap, that fd is the whole lifeline. and you can hand that file descriptor to the setns(2) system call to change the network namespace associated with a task. iconic behaviour honestly.
for applications that are aware of network namespaces, the convention is to look for global network configuration files first in /etc/netns/NAME/ and then in /etc/. for example, if you want a different version of /etc/resolv.conf for a network namespace used to isolate your vpn, you would name it /etc/netns/myvpn/resolv.conf. obsessed with how clean that is ๐
ip netns exec automates handling of this configuration, file convention for network namespace unaware applications, by creating a mount namespace and bind mounting all of the per network namespace configure files into their traditional location in /etc. so the app never even knows it's in a different world. gaslighting, but ethical ๐ธ
ip netns list โ show all of the named network namespaces
this command displays all of the network namespaces in /var/run/netns ๐
ip netns add NAME โ create a new named network namespace
if NAME is available in /var/run/netns this command creates a new network namespace and assigns NAME. brand new baby namespace, named and everything ๐ฃ
ip netns attach NAME PID โ assign a name to the network namespace of the process
if NAME is available in /var/run/netns this command attaches the network namespace of the process PID to NAME as if it were created with ip netns. basically adopting an existing namespace and giving it a name, we love a glow-up โจ
ip [-all] netns delete [ NAME ] โ delete the name of a network namespace(s)
if NAME is present in /var/run/netns it is umounted and the mount point is removed. if this is the last user of the network namespace the network namespace will be freed and all physical devices will be moved to the default one โ otherwise the network namespace persists until it has no more users. ip netns delete may fail if the mount point is in use in another mount namespace. ๐
if -all option was specified then all the network namespace names will be removed. all of them. gone. bye ๐
โ ๏ธ okay this is the important part, listen up: it is possible to lose the physical device when it was moved to netns and then this netns was deleted with a running process:
$ ip netns add net0
$ ip link set dev eth0 netns net0
$ ip netns exec net0 SOME_PROCESS_IN_BACKGROUND
$ ip netns del net0
and eth0 will appear in the default netns only after SOME_PROCESS_IN_BACKGROUND will exit or will be killed. so to prevent this the processes running in net0 should be killed before deleting the netns:
$ ip netns pids net0 | xargs kill
$ ip netns del net0
fr fr, kill the processes first or your nic is just gone ๐ญ
ip netns set NAME NETNSID โ assign an id to a peer network namespace
this command assigns a id to a peer network namespace. this id is valid only in the current network namespace. if the keyword "auto" is specified an available nsid will be chosen. this id will be used by the kernel in some netlink messages. if no id is assigned when the kernel needs it, it will be automatically assigned by the kernel. once it is assigned, it's not possible to change it. periodt, no takebacks ๐
ip netns identify [PID] โ Report network namespaces names for process
this command walks through /var/run/netns and finds all the network namespace names for network namespace of the specified process, if PID is not specified then the current process will be used. so it's like "which namespace is this process living in rn" ๐ต๏ธโ๏ธ
ip netns pids NAME โ Report processes in the named network namespace
this command walks through proc and finds all of the process who have the named network namespace as their primary network namespace. a full roll call of everyone in that namespace ๐ง
ip [-all] netns exec [ NAME ] cmd ... โ Run cmd in the named network namespace
this command allows applications that are network namespace unaware to be run in something other than the default network namespace with all of the configuration for the specified network namespace appearing in the customary global locations. a network namespace and bind mounts are used to move files from their network namespace specific location to their default locations without affecting other processes. the app just gets dropped in and it's business as usual ๐
if -all option was specified then cmd will be executed synchronously on the each named network namespace even if cmd fails on some of them. network namespace name is printed on each cmd executing. so yeah, it keeps going even when one of them flops. no quitting energy ๐ฅ
ip netns monitor โ Report as network namespace names are added and deleted
this command watches network namespace name addition and deletion events and prints a line for each event it sees. it's the live feed, the notifications tab, the little birdie that tells you what's happening ๐
ip netns list-id [target-nsid POSITIVE-INT] [nsid POSITIVE-INT] โ list network namespace ids (nsid)
network namespace ids are used to identify a peer network namespace. this command displays nsids of the current network namespace and provides the corresponding iproute2 netns name (from /var/run/netns) if any.
the target-nsid option enables to display nsids of the specified network namespace instead of the current network namespace. this target-nsid is a nsid from the current network namespace.
the nsid option enables to display only this nsid. it is a nsid from the current network namespace. in combination with the target-nsid option, it enables to convert a specific nsid from the current network namespace to a nsid of the target-nsid network namespace. translation services, but for ids ๐
โจ EXAMPLES โจ
ip netns list
shows the list of current named network namespaces ๐
ip netns add vpn
creates a network namespace and names it vpn ๐
ip netns exec vpn ip link set lo up
bring up the loopback interface in the vpn network namespace. lo said wake up bestie ๐ธ
the nsid translation saga
ip netns add foo
ip netns add bar
ip netns set foo 12
ip netns set bar 13
ip -n foo netns set foo 22
ip -n foo netns set bar 23
ip -n bar netns set foo 32
ip -n bar netns set bar 33
ip netns list-id target-nsid 12
shows the list of nsids from the network namespace foo.
ip netns list-id target-nsid 12 nsid 13
get nsid of bar from the network namespace foo (result is 23). math is mathing โ
โจ SEE ALSO โจ
ip(8) โ go read that one too, it's the parent of all this ๐ซถ
โจ AUTHOR โจ
original Manpage by Eric W. Biederman Manpage revised by Nicolas Dichtel <nicolas.dichtel@6wind.com>
and that's the whole vibe ๐ this page came to you from iproute2, dated 16 Jan 2013, and it's still out here running namespaces like a queen ๐ go forth, make namespaces, kill your processes before you delete them, and stay slay โจ๐๐ฆ
๐ฆ where this page comes from
- alpine 3.22: iproute2-doc 6.15.0-r0 ยท GPL-2.0-or-later
- alpine 3.23: iproute2-doc 6.17.0-r0 ยท GPL-2.0-or-later
- alpine 3.24: iproute2-doc 7.0.0-r0 ยท GPL-2.0-or-later
- arch rolling: iproute2 7.2.0-1
- arch rolling: iproute2 7.3.0-1 ยท GPL-2.0-or-later
- centos-stream 10: iproute 7.0.0-2.el10 ยท GPL-2.0-or-later AND NIST-PD
- centos-stream 9: iproute 6.17.0-3.el9 ยท GPL-2.0-or-later AND NIST-PD
- fedora 43: iproute 6.14.0-2.fc43 ยท GPL-2.0-or-later AND NIST-PD
- fedora 44: iproute 6.17.0-2.fc44 ยท GPL-2.0-or-later AND NIST-PD
- nixos 26.05: iproute2 7.0.0
- nixpkgs unstable: iproute2 7.2.0
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms