GNU/Linux >> Linux の 問題 >  >> Linux

Linux – Linuxで名前空間を一覧表示する方法は?

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...]

Linux
  1. Linuxでサービスを管理および一覧表示する方法

  2. 最もよく使用される7つのLinux名前空間

  3. Linuxにインストールされているすべてのパッケージを一覧表示する方法

  1. Linuxでの名前空間とコンテナの謎を解き明かす

  2. LinuxSSコマンドの使用方法

  3. Linuxで実行中のプロセスを一覧表示する方法

  1. Linuxでユーザーを一覧表示する方法

  2. Linuxでグループを一覧表示する方法

  3. Linuxでcronジョブを一覧表示する方法