Is there any way to find how many active namespaces of each type are present in Linux? Such as:

  • mount ns: 20
  • net ns: 40
  • etc.

This will iterate over the links under /proc/*/ns to count the number of active namespaces of each type, i.e. namespaces containing at least one process:

sudo find /proc/*/ns -type l -printf "%l\n" |
gawk -F'[:\\[\\]]+' '{ nss[$1][$2] = 1 } END { for (ns in nss) { print ns ": " length(nss[ns]) } }'

It works by counting the number of distinct identifiers, grouped by namespace type.

Namespaces can be kept alive without processes by bind mounting them elsewhere; the above doesn’t take this into account.

| improve this answer | |

You can use the lsns(1) command from the util-linux package for that:

lsns -n | awk '{n[$2]++}END{for(k in n) print k"\t"n[k]}'
net     2
cgroup  1

If you're on a machine without lsns (eg. busybox), you can extract that info directly from /proc/*/ns/*:

for f in /proc/*/ns/*; do readlink "$f"; done | awk -F: '!t[$2]++{c[$1]++}END{for(k in c)print k"\t"c[k]}'

Neither lsns nor this kludge will show the empty namespaces only kept alive by an open handle or a bind mount. For that, you may try the lsnsx.pl script from my other answer:

# perl ./lsnsx.pl | grep -v '^ '
cgroup   1
ipc      1
mnt      3
net      5
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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