5

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?

Garuno
  • 151
  • 1
  • 3
  • related: https://unix.stackexchange.com/questions/505112/how-do-i-find-all-interfaces-that-have-been-configured-in-linux-including-those – A.B Aug 18 '21 at 22:44
  • So there is no inbuilt tool for doing this? I have to scavenge the /proc file system to get all the namespaces? – Garuno Aug 19 '21 at 05:40

2 Answers2

0

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.

digijay
  • 1,074
  • 3
  • 9
  • 22
  • 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? – Garuno Aug 19 '21 at 05:39
  • 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! – digijay Aug 19 '21 at 09:44
0

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
George Shuklin
  • 226
  • 2
  • 7