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

# Bash 的 id 命令

id [OPTION]... [USER]...

!subtitle:功能

获取用户的 ID。

!subtitle:类型

可执行文件(/usr/bin/id),属于 coreutils

!subtitle:参数

  • OPTION 选项:

    • -Z, --context - 仅打印进程的安全上下文

    • -g, --group - 仅打印用户的有效组 ID

    • -G, --groups - 打印用户的所有组 ID

    • -n, --name - 打印名称而非 ID

    • -r, --real - 打印实际 ID 而非有效 ID

    • -u, --user - 仅打印有效用户 ID

    • -z, --zero - 以空字符(\0)作为行的结尾,而不是换行符(\n

    • --help - 显示帮助

    • --version - 显示版本

  • USER - 用户列表,可用是用户名或用户 ID

# 示例

$ id            
uid=1000(primers) gid=1000(primers) groups=1000(primers),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),101(lxd)
$ id -g         # 仅打印用户的有效组 ID
1000
$ id -G         # 打印用户的所有组 ID
1000 4 24 27 30 46 101
$ id -nG        # 打印用户的所有组名
primers adm cdrom sudo dip plugdev lxd

# 推荐阅读

# 手册

ID(1)                            User Commands                           ID(1)

NAME
       id - print real and effective user and group IDs

SYNOPSIS
       id [OPTION]... [USER]...

DESCRIPTION
       Print user and group information for each specified USER, or (when USER
       omitted) for the current process.

       -a     ignore, for compatibility with other versions

       -Z, --context
              print only the security context of the process

       -g, --group
              print only the effective group ID

       -G, --groups
              print all group IDs

       -n, --name
              print a name instead of a number, for -ugG

       -r, --real
              print the real ID instead of the effective ID, with -ugG

       -u, --user
              print only the effective user ID

       -z, --zero
              delimit entries with NUL characters, not whitespace;

              not permitted in default format

       --help display this help and exit

       --version
              output version information and exit

       Without any OPTION, print some useful set of identified information.

AUTHOR
       Written by Arnold Robbins and David MacKenzie.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright  ©  2023  Free Software Foundation, Inc.  License GPLv3+: GNU
       GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
       This is free software: you are free  to  change  and  redistribute  it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       Full documentation <https://www.gnu.org/software/coreutils/id>
       or available locally via: info '(coreutils) id invocation'

GNU coreutils 9.4                 April 2024                             ID(1)
本文 更新于: 2025-11-27 09:38:14 创建于: 2025-11-27 09:38:14