The id
command in Linux is used to display the user ID (UID) and group ID (GID) of the current user or a specified user. It provides information about the user's identity and group memberships.
-
Syntax:
id [options] [username]
-
Examples:
id
- Displays the UID, GID, and group memberships of the current user.
id username
- Displays the UID, GID, and group memberships of the specified
username
.
id -u
- Displays only the UID of the current user.
id -g username
- Displays only the GID of the specified
username
.
-
-u
or--user
:- Displays only the user ID (UID).
id -u
- Outputs the UID of the current user.
-
-g
or--group
:- Displays only the group ID (GID).
id -g
- Outputs the GID of the current user.
-
-G
or--groups
:- Displays all group IDs that the user belongs to.
id -G
- Outputs all GIDs of the current user.
-
-n
or--name
:- Displays the name instead of the numeric ID. Can be combined with
-u
,-g
, or-G
.
id -un
- Outputs the username of the current user.
id -gn username
- Outputs the primary group name of the specified
username
.
- Displays the name instead of the numeric ID. Can be combined with
-
-r
or--real
:- Displays the real ID (rather than the effective ID) when used with
-u
or-g
.
id -ur
- Outputs the real UID of the current user.
- Displays the real ID (rather than the effective ID) when used with
-
--help
:- Displays help information about the
id
command.
id --help
- Shows usage information and available options.
- Displays help information about the
-
Checking Group Memberships:
- Use
id
to quickly see all groups a user belongs to.
id -Gn
- Outputs the names of all groups the current user is a member of.
- Use
-
Finding User or Group IDs:
- You can easily find the numeric ID or name of a user or group using
id
.
id -g username
- Outputs the GID of the specified user.
- You can easily find the numeric ID or name of a user or group using
-
Use with Scripts:
id
is often used in scripts to check or verify user permissions and identities.
if [ "$(id -u)" -eq 0 ]; then echo "Running as root" fi
- Checks if the script is running as the root user.
The id
command is a straightforward tool for retrieving user and group identity information in Linux. It’s particularly useful for verifying user permissions, checking group memberships, and debugging permission issues in scripts or system administration tasks.