9

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?

2

3 Answers 3

3

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.

4
  • 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.
    – A.B
    Nov 8, 2022 at 15:43
  • 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?
    – Garuno
    Nov 8, 2022 at 15:59
  • No real use-case around. Just being pedantic about "all network namespaces". Anyway +1 for the move in the right direction with the request.
    – A.B
    Nov 8, 2022 at 16:16
  • 1
    I also created an issue for this use case on the lsns repo. Apparently it is a legitimate use case for lxc and systemds nspawn.
    – Garuno
    Nov 9, 2022 at 8:17
1

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
2
  • mounted-only namespaces won't be found
    – A.B
    Aug 30, 2021 at 15:36
  • Yes, this trick works only with namespaces used by processes. Aug 31, 2021 at 8:51
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.

2
  • 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, 2021 at 5: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, 2021 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.