Answers:
自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-但是,pgrep
如果要列出属于名称空间的所有进程,则可以使用该PID 。
例如,如果我在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
我还可以将名称空间ID(4026532661)与一起使用ps
,例如:
ps -o pidns,pid,cmd | awk '$1==4026532661'
[...output deleted...]
从ip手册页获取网络名称空间
ip netns-处理网络名称空间管理从逻辑上讲,网络名称空间是网络堆栈的另一个副本,具有自己的路由,防火墙规则和网络设备。
By convention a named network namespace is an object at
/var/run/netns/NAME that can be opened. The file descriptor resulting
from opening /var/run/netns/NAME refers to the specified network names-
pace. Holding that file descriptor open keeps the network namespace
alive. The file descriptor can be used with the setns(2) system call
to change the network namespace associated with a task.
The convention for network namespace aware applications is to look for
global network configuration files first in /etc/netns/NAME/ then in
/etc/. For example, if you want a different version of
/etc/resolv.conf for a network namespace used to isolate your vpn you
would name it /etc/netns/myvpn/resolv.conf.
对于其他类型的名称空间,也许还有其他方法
命名空间列表:
您可以使用listns.py
用法:./listns.py
或python2 listns.py
探索系统
在基本/默认设置中,Ubuntu 12.04及更高版本提供以下名称空间(如果您以root用户身份执行,则会为系统中的每个进程显示这些名称空间)
python代码
以下python代码列出了系统中所有非默认名称空间。程序流程是
例:
python2 listns.py
输出示例...您可以通过排序对其进行管道处理,也可以编辑脚本以符合您的需求
PID Namespace Thread/Command
-- net:[4026533172] created by ip netns add qrouter-c33ffc14-dbc2-4730-b787-4747
-- net:[4026533112] created by ip netns add qrouter-5a691ed3-f6d3-4346-891a-3b59
297 mnt:[4026531856] kdevtmpfs
3429 net:[4026533050]** dnsmasq --no-hosts --no-resolv --strict-order --bind-interfa
3429 mnt:[4026533108] dnsmasq --no-hosts --no-resolv --strict-order --bind-interfa
3486 net:[4026533050]** /usr/bin/python /usr/bin/neutron-ns-metadata-proxy --pid_fil
3486 mnt:[4026533107] /usr/bin/python /usr/bin/neutron-ns-metadata-proxy --pid_fil
资料来源:github-mirror和article;全部归功于Ralf Trezeciak