国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > lsns 以获得更好的体验。

# Bash 的 lsns 命令

lsns [OPTION]... [NAMESPACE]

!subtitle:功能

列出系统中的命名空间。

!subtitle:类型

可执行文件(/usr/bin/lsns),属于 util-linux

!subtitle:参数

  • OPTION 选项:

    • -J, --json - 以 JSON 格式输出

    • -l, --list - 以列表格式输出

    • -n, --noheadings - 不显示标题行(列名)

    • -o, --output list - 仅显示指定的列

    • --output-all - 显示所有列

    • -P, --persistent - 仅显示不包含进程的命名空间

    • -p, --task PID - 仅显示进程 ID 为 PID 的进程持有的命名空间

    • -r, --raw - 以原始格式输出

    • -t, --type type - 仅显示指定类型的命名空间

    • -u, --notruncate - 请勿在列中截断文本

    • -W, --nowrap - 请勿在列中使用多行文本输出

    • -T, --tree rel - 以树状格式输出

    • -h, --help - 显示帮助

    • -V, --version - 显示版本

  • NAMESPACE - 查看指定的命名空间

# 列名

列名 说明
NS 命名空间标识符 (inode 编号)
TYPE 命名空间类型
PATH 命名空间的路径
NPROCS 命名空间中的进程数
PID 命名空间中最小的 PID
PPID 进程的 PPID(父进程 PID)
COMMAND 进程的命令行
UID 进程的 UID
USER 进程的用户名
NETNSID 网络子系统使用的命名空间 ID
NSFS NSFS 挂载点
PNS 父命名空间标识符 (inode 编号)
ONS 所有者命名空间标识符 (inode 编号)

# 示例

$ lsns
        NS TYPE   NPROCS   PID USER     COMMAND
4026531834 time       22 98505 primers  -bash
4026531835 cgroup     22 98505 primers  -bash
4026531836 pid        22 98505 primers  -bash
4026531837 user       22 98505 primers  -bash
4026531838 uts        22 98505 primers  -bash
4026531839 ipc        22 98505 primers  -bash
4026531840 net        22 98505 primers  -bash
4026531841 mnt        22 98505 primers  -bash

# 推荐阅读

# 手册

LSNS(8)                      System Administration                     LSNS(8)

NAME
       lsns - list namespaces

SYNOPSIS
       lsns [options] namespace

DESCRIPTION
       lsns lists information about all the currently accessible namespaces or
       about the given namespace. The namespace identifier is an inode number.

       The default output is subject to change. So whenever possible, you
       should avoid using default outputs in your scripts. Always explicitly
       define expected output mode (--tree or --list) and columns by using the
       --output option together with a columns list in environments where a
       stable output is required.

       The NSFS column, printed when net is specified for the --type option,
       is special; it uses multi-line cells. Use the option --nowrap to switch
       to ","-separated single-line representation.

       Note that lsns reads information directly from the /proc filesystem and
       for non-root users it may return incomplete information. The current
       /proc filesystem may be unshared and affected by a PID namespace (see
       unshare --mount-proc for more details). lsns is not able to see
       persistent namespaces without processes where the namespace instance is
       held by a bind mount to /proc/pid/ns/type.

OPTIONS
       -J, --json
           Use JSON output format.

       -l, --list
           Use list output format.

       -n, --noheadings
           Do not print a header line.

       -o, --output list
           Specify which output columns to print. Use --help to get a list of
           all supported columns.

           The default list of columns may be extended if list is specified in
           the format +list (e.g., lsns -o +PATH).

       --output-all
           Output all available columns.

       -P, --persistent
           Display only the namespaces without processes (aka persistent
           namespaces), created by bind mounting /proc/pid/ns/type files to a
           filesystem path.

       -p, --task PID
           Display only the namespaces held by the process with this PID.

       -r, --raw
           Use the raw output format.

       -t, --type type
           Display the specified type of namespaces only. The supported types
           are mnt, net, ipc, user, pid, uts, cgroup and time. This option may
           be given more than once.

       -u, --notruncate
           Do not truncate text in columns.

       -W, --nowrap
           Do not use multi-line text in columns.

       -T, --tree rel
           Use tree-like output format. If process is given as rel, print
           process tree(s) in each name space. This is default when --tree is
           not specified. If parent is given, print tree(s) constructed by the
           parent/child relationship. If owner is given, print tree(s)
           constructed by the owner/owned relationship. owner is used as
           default when rel is omitted.

       -h, --help
           Display help text and exit.

       -V, --version
           Print version and exit.

EXIT STATUS
       The lsns utility exits with one of the following values:

       0
           Success.

       1
           General error.

       2
           An ioctl was unknown to the kernel.

AUTHORS
       Karel Zak <[email protected]>

SEE ALSO
       nsenter(1), unshare(1), clone(2), namespaces(7), ioctl_ns(2),
       ip-netns(8)

REPORTING BUGS
       For bug reports, use the issue tracker at
       https://github.com/util-linux/util-linux/issues.

AVAILABILITY
       The lsns command is part of the util-linux package which can be
       downloaded from Linux Kernel Archive
       <https://www.kernel.org/pub/linux/utils/util-linux/>.

util-linux 2.39.3                 2023-11-21                           LSNS(8)
本文 更新于: 2026-03-06 09:52:32 创建于: 2026-03-06 09:52:32