Linuxには、実行中のホスト上のすべての名前空間を一覧表示する方法はありますか?特定のプロセス(たとえば、LXCコンテナで実行されているプロセスやホスト上の他のすべてのプロセス)の名前を確認してから、それらのcgroupを見つける必要があります。
承認された回答:
この質問が2013年に行われて以来、名前空間を操作するためのユーティリティが改善されました。
lsns
util-linuxパッケージから、さまざまなタイプの名前空間をさまざまな便利な形式で一覧表示できます。
# lsns --help
Usage:
lsns [options] [<namespace>]
List system namespaces.
Options:
-J, --json use JSON output format
-l, --list use list format output
-n, --noheadings don't print headings
-o, --output <list> define which output columns to use
-p, --task <pid> print process namespaces
-r, --raw use the raw output format
-u, --notruncate don't truncate text in columns
-t, --type <name> namespace type (mnt, net, ipc, user, pid, uts, cgroup)
-h, --help display this help and exit
-V, --version output version information and exit
Available columns (for --output):
NS namespace identifier (inode number)
TYPE kind of namespace
PATH path to the namespace
NPROCS number of processes in the namespace
PID lowest PID in the namespace
PPID PPID of the PID
COMMAND command line of the PID
UID UID of the PID
USER username of the PID
For more details see lsns(8).
lsns
各プロセスの最小のPIDのみがリストされますが、そのPIDはpgrep
で使用できます。 名前空間に属するすべてのプロセスを一覧表示する場合。
例えばDockerでgitlabを実行していて、その名前空間で実行されているすべてのプロセスを検索したい場合は、次のことができます。
# lsns -t pid -o ns,pid,command | grep gitlab
4026532661 459 /opt/gitlab/embedded/bin/redis-server 127.0.0.1:0
次に、そのpid(459)をpgrep
で使用します :
# pgrep --ns 459 -a
459 /opt/gitlab/embedded/bin/redis-server 127.0.0.1:0
623 postgres: gitlab gitlabhq_production [local] idle
[...around 50 lines deleted...]
30172 nginx: worker process
ps
で名前空間ID(4026532661)を使用することもできます 例:
ps -o pidns,pid,cmd | awk '$1==4026532661'
[...output deleted...]