How do I find out the user and group’s names and numeric IDs of the current user or any users on
my server? How can I display and effective IDs on the system using command line options? In Linux,
how do I find a user’s UID or GID?
To find a user’s UID (user ID) or GID (group ID) and other information in Linux/Unix-like operating
systems, use the id command.
systems, use the id command.
This command is useful to find out the following information:
- Get User name and real user ID
- Find a specific user’s UID
- List out all the groups a user belongs to
- Show the UID and all groups associated with a user
- Display security context of the current user
- Effective Linux or Unix user name and effective user ID (UID)
- Name of effective Linux or Unix user’s group and effective group ID (GID)
Purpose
Displays the system identifications of a specified user.
id command syntax
The basic syntax is:
id
id [UserNameHere]
id [options]
id [options] [UserNameHere]
id --help
Usage: id [OPTION]... [USERNAME]
Print user and group information for the specified USERNAME,
or (when USERNAME omitted) for the current user.
-a ignore, for compatibility with other versions
-Z, --context print only the security context of the current user
-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
--help display this help and exit
--version output version information and exit
Display your own UID and GID
Type the command:
id
Sample outputs:
id
uid=1000(dev) gid=1000(dev) groups=1000(dev),4(adm),24(cdrom),27(sudo)
How do I find a specific user’s UID?
In this example, find a dev user’s UID, type:
id -u {UserNameHere}
id -u dev
Sample output:
id -u dev
1000
How do I find a specific user’s GID?
In this example, find a dev user’s GID, run:
id -g {UserNameHere}
id -g dev
Sample output:
id -g dev
1000
How do I see the UID and all groups associated with a user name?
In this example, find the UID and all groups associated with a user called ‘root’, enter:
id {UserNameHere}
id root
Sample output:
id root
uid=0(root) gid=0(root) groups=0(root)
Find out all the groups a user belongs to !!
In this example, display the UID and all groups associated (secondary groups) with a user called ‘dev’, run:
id -G {UserNameHere}
id -G dev
Sample output:
id -G dev
1000 4 24 27 30 46 108 124 142
How do I display real ID instead of the effective ID for specified user?
You can show the real ID for the -g, -G and -u options instead of the effective ID by passing the -r option:
id -r -g {UserNameHere}
id -r -u {UserNameHere}
### [NOTE] -r and -G only works on Linux
id -r -G {UserNameHere}
id -r -u dev
Sample output:
id -r -u dev
1000
Determining root privileges in a script
Linux and Unix sysadmin relates shell scripts must be run by root user. The following shell script
shows how to determining root privileges in a script:
#!/bin/bash
## if root user not running this script, die with a message on screen ##
if [ $(id -u -r) -ne 0 ]
then
echo "Requires root privileges. Please re-run using sudo."
exit 1
fi