Score:5

Linux list all network namespaces

za flag

I want to list all the network namespaces on my Linux machine. So far I found two recommended ways to do this:

ip netns list

And

lsns --type=net

Unfortunately both commands do not list all the namespaces! The first only lists the namespaces found in /var/run/netns and the second will only find namespaces with at least one process running in it. Docker for example will save its namespaces in /var/run/docker/netns (so they are not shown by ip netns) and there are some namespaces without a running process (so they are not shown by lsns). Is there any command that prints ALL the network namespaces on the host?

A.B avatar
cl flag
A.B
related: https://unix.stackexchange.com/questions/505112/how-do-i-find-all-interfaces-that-have-been-configured-in-linux-including-those
Garuno avatar
za flag
So there is no inbuilt tool for doing this? I have to scavenge the /proc file system to get all the namespaces?
Score:3
za flag

I once again came across this shortcoming of lsns and opened an issue in the repository. It is already fixed and in a future version of lsns it will show all the namespaces, even ones, that are only mounted with no process running in them. In the meantime the information is actually available in /proc/mounts. All mounts with the type nsfs should be mounted namespaces. This is not limited to network namespaces, but covers all namespaces.

A.B avatar
cl flag
A.B
One problem left: a namespace kept only by a file descriptor (not by a process being in this namespace): example `ip netns add foo; sleep 999 4< /run/netns/foo & sleep 2; ip netns delete foo` will keep the newly created namespace only with sleep's fd 4 (as can be then checked with `stat -f -L -c %T /proc/$(pidof sleep)/fd/4`). But `lsns` won't find it. And it appears it can't be mounted back (anymore?) unless opening it and setns-ing it.
Garuno avatar
za flag
You are correct. This seems like a pretty esoteric case to me though. Do you have more of a real-world example for this case?
A.B avatar
cl flag
A.B
No real use-case around. Just being pedantic about "*all* network namespaces". Anyway +1 for the move in the right direction with the request.
Garuno avatar
za flag
I also created an [issue](https://github.com/util-linux/util-linux/issues/1884) for this use case on the `lsns` repo. Apparently it is a legitimate use case for lxc and systemds nspawn.
Score:0
cn flag

Not by name, but you can find all used namespaces by looking into /proc/{PID}/ns/net

This script (run as root) shows a list of used network namespace IDs on the machine:

find /proc/ -name ns 2>/dev/null |xargs -I NS ls -la NS/net 2> /dev/null| awk -F '->' '{print $2}'|sort -u
A.B avatar
cl flag
A.B
mounted-only namespaces won't be found
George Shuklin avatar
cn flag
Yes, this trick works only with namespaces used by processes.
Score:0
mx flag

To include the network namespaces of running docker containers into ip netns list you could just set a symbolic link (aka softlink) like this:

ln -Ts /var/run/docker/netns /var/run/netns

Unfortunately there is no generic command in linux to list all network namespaces. You could use the following python script to list at least all namespaces that are linked in /var/run/netns: https://github.com/Intika-Linux-Namespace/Namespace-Lister

For applications using other paths you would have to modify this script.

Garuno avatar
za flag
This will only solve the Problem for the specific Docker case. How would I know if any other application is creating namespaces in other locations?
digijay avatar
mx flag
Hi @Garuno, see my edit. There seems to be no general solution for this, but at least a script that you could tweak to serve your needs. Hope it helps!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.