回到手册索引

命令用途

id 命令用于显示用户及其所属群组的身份信息,包括用户 ID(UID)、主组 ID(GID)及附加组列表。

常用用法示例

  1. 显示当前用户的身份信息

    1
    2
    id  
    uid=1000(user) gid=1000(user) groups=1000(user),4(adm)

    不指定参数时,显示当前用户的 UID、GID 及其所属的所有组。

  2. 显示指定用户的身份信息

    1
    2
    id root  
    uid=0(root) gid=0(root) groups=0(root)

    显示用户 root 的 UID、GID 及所属组。

  3. 仅显示用户的有效 UID

    1
    2
    id -u  
    1000

    -u 参数仅输出当前用户的有效 UID(数字形式)。

  4. 仅显示用户的主组 GID

    1
    2
    id -g
    1000

    -g 参数仅输出当前用户的主组 GID(数字形式)。

  5. 显示用户所属的所有组 ID

    1
    2
    id -G 
    1000 4

    -G 参数列出用户所属的所有组 ID(数字形式)。

  6. 显示用户和组的名称而非数字 ID

    1
    2
    id -un
    user

    组合参数 -u -n(可合并为 -un),显示当前用户的名称而非 UID。

  7. 显示指定用户的所有组名称

    1
    2
    id -nG username  
    user adm cdrom sudo

    -nG 显示用户 username 所属的所有组名称(而非数字)。

  8. 显示 SELinux 安全上下文

    1
    2
    3
    id -Z
    unconfined_u:unconfined_r:unconfined_t:s0
    (SELinux 启用时)

    -Z 参数显示用户的 SELinux 安全上下文(仅在启用 SELinux 的系统有效)。

常用参数选项

  • -u, –user
    仅显示有效用户 ID(UID)。

  • -g, –group
    仅显示主组 ID(GID)。

  • -G, –groups
    显示用户所属的所有组 ID。

  • -n, –name
    与 -u/-g/-G 配合使用,显示名称而非数字 ID。例如 id -ng 显示主组名称。

  • -r, –real
    显示真实 ID 而非有效 ID(需与 -u/-g/-G 配合使用)。

  • -Z, –context
    显示 SELinux 安全上下文(需系统启用 SELinux)。

  • –help
    显示帮助信息并退出。

  • –version
    显示版本信息并退出。

原厂文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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 © 2025 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'

COLOPHON

This page is part of the coreutils (basic file, shell and text
manipulation utilities) project. Information about the project
can be found at ⟨http://www.gnu.org/software/coreutils/⟩. If you
have a bug report for this manual page, see
⟨http://www.gnu.org/software/coreutils/⟩. This page was obtained
from the tarball coreutils-9.6.tar.xz fetched from
⟨http://ftp.gnu.org/gnu/coreutils/on 2024-02-02. If you
discover any rendering problems in this HTML version of the page,
or you believe there is a better or more up-to-date source for the
page, or you have corrections or improvements to the information
in this COLOPHON (which is not part of the original manual page),
send a mail to man-pages@man7.org