2

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.

2 Answers 2

4

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.

5
3

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

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

But lsns is broken: it won't show either the per-thread namespaces or those only kept alive by an open handle or a bind mount. To get all that, try the lsnsx.pl script from my other answer:

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

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

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

You can change /proc/[0-9]*/ns to /proc/[0-9]*/task/[0-9]*/ns to also get the per-thread namespaces, but on any moderately-used machine it will get horribly slow.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .