id Command: Checking UID and Groups, whoami, groups, and Supplementary Groups

Test environmentRHEL familyAlmaLinux 9.8coreutils 8.32root accountDebian 12 · coreutils 9.1 regular account
User · group commandsid, whoami, groupsLocation/usr/bin/id

To check the UID and groups of the current account on a Linux server, use the id command; use whoami when you need only the account name and groups when you need only the group names. Running the three commands in turn on AlmaLinux 9 and Debian 12 servers and comparing the output showed that id -un and whoami printed the same account name, and groups printed the group list of id with only the names left.

The output below was produced on an AlmaLinux 9.8 server under the root account and on a Debian 12 server under a regular account. The name and numbers of the login account and its supplementary groups were replaced with example values (user1, 1000, adm, systemd-journal).

1️⃣ Basic Usage of the id Command

  • ↪️ Reading the three fields uid, gid, and groups

Running id without options prints the information of the account that started the current shell on one line. Run under the root account, all three fields were 0(root).

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

The uid field is the account number and name, and the gid field is the number and name of the primary group. The groups field is every group the account belongs to, with the primary group first.

Writing an account name after id prints the information of that account. Checking the regular account user1, which has supplementary groups, showed adm and systemd-journal following the primary group user1 in the groups field. These groups that the account belongs to beyond its primary group are supplementary groups.

id user1
uid=1000(user1) gid=1000(user1) groups=1000(user1),4(adm),190(systemd-journal)

Many accounts have the same number for UID and gid, so it is easy to treat the two numbers as the same value, but the adm account that exists on the server by default had a UID of 3 and a primary group number of 4. The operator account had root as its primary group.

id adm
uid=3(adm) gid=4(adm) groups=4(adm)
id operator
uid=11(operator) gid=0(root) groups=0(root)

Writing several accounts prints one per line.

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

Writing an account that does not exist gives the [no such user] error, and the exit code was 1.

id no-such-user9
id: 'no-such-user9': no such user
echo $?
1

The exit code when an existing account is written is 0, so it can be used in a script as a condition to check whether an account exists.

2️⃣ id Command Options

  • ↪️ -u, -g, -G for numbers and -n for names

To pass just one number from the full output to a script, pick a single field with an option. -u prints the UID, -g the primary group number, and -G all the numbers of the groups the account belongs to.

id -u
0
id -u daemon
2
id -G user1
1000 4 190

Adding -n prints names instead of numbers. Checking the groups of user1 with [id -Gn] turned the three numbers into three names.

id -Gn user1
user1 adm systemd-journal
id -gn user1
user1

-n and -r are used together with one of -u, -g, and -G. Writing -n alone gave the error below.

id -n
id: cannot print only names or real IDs in default format

-r prints the real ID instead of the effective ID. Running [id -ur] in a root account shell printed 0. Writing two or more of the options that pick a number, such as -u and -g, together gives the [cannot print "only" of more than one choice] error. The options are summarized below.

OptionCopyDescription
-u--userPrints the UID number
-g--groupPrints the primary group number
-G--groupsPrints all the numbers of the groups the account belongs to
-n--nameWith -u, -g, or -G, prints the name instead of the number
-r--realWith -u, -g, or -G, prints the real ID instead of the effective ID
OptionCopyDescription
-Z--contextPrints only the security context of the process
-z--zeroSeparates output items with NUL characters instead of spaces; not available in the default format
-aAccepted only for compatibility with other versions of id, and ignored
--helpPrints help
--versionPrint the version

🔲 id Command Simulator

The simulator below starts as the user1 account, which has supplementary groups. After adding the wheel group with the [🖊 Other window] chip, you can compare [id] and [id user1], and check the groups of a new shell with the [🔁 Log in again] chip.

id Command Simulatorcoreutils 8.32
[root@server work]#
It runs only inside the browser. After changing the groups with the [🖊 Other window] chip, compare [id] and [id user1], and check the groups of a new shell with the [🔁 Log in again] chip.

3️⃣ The whoami Command and id -un

  • ↪️ Checking the current account name and checking other accounts

When you need only the account name of the current shell, run whoami. The man page describes whoami as the same command as [id -un]. Running the two commands under the root account printed root for both.

whoami
root
id -un
root

Under a regular account on the Debian 12 server, the two commands also printed the same account name.

whoami
user1
id -un
user1

whoami prints the account name that corresponds to the current effective UID. It is a command that checks only the current account, so adding an account name after it gives an error.

whoami user1
whoami: extra operand 'user1'
Try 'whoami --help' for more information.

To check the name or number of another account, write that account after id.

id -un user1
user1

Writing -u instead of -un prints the UID number in the same place instead of the account name.

4️⃣ The groups Command and Supplementary Groups

  • ↪️ Primary and supplementary groups, and the shell after a group change

groups prints only the group names on one line. Without an account name it prints the groups of the current shell, and under the root account that was root alone.

groups
root

With an account name, it prints the account name and a colon followed by the groups. Writing several accounts printed one per line.

groups user1 daemon
user1 : user1 adm systemd-journal
daemon : daemon

The primary group and supplementary groups are recorded in different places. Supplementary groups are written as members in the fourth field of the group database (/etc/group), and the primary group is written as a number in the fourth field of the account information (/etc/passwd).

getent group adm
adm:x:4:user1
getent group user1
user1:x:1000:
getent passwd daemon
daemon:x:2:2:daemon:/sbin:/sbin/nologin

The user1 group, the primary group of user1, had an empty fourth field, and the supplementary group adm had user1 written as a member. The 2 in the fourth field of the daemon account line is the primary group number.

To add an account to a supplementary group, use the usermod command with [usermod -aG group account]. Using only -G without -a removes the account from existing supplementary groups that are not written in the list, so write -a together with it.

groups run without an account name prints the groups of the current shell process. The man page explains that this value can differ from the database when the group database changes.

So right after changing groups, check the groups in the database with [groups account] or [id account], and log in again to apply them to the shell. A command that changes the current group ID while staying logged in is newgrp.

🔢 FAQ & Recommended Content

Yes, you can. Running [id 0] on the AlmaLinux 9.8 server printed uid=0(root) gid=0(root) groups=0(root), the same as [id root].

From the number written in UID_MIN in /etc/login.defs. On the AlmaLinux 9.8 server it was 1000, and SYS_UID_MIN and SYS_UID_MAX in the same file were 201 and 999, the system account range.

who am i is who followed by two words; it behaves the same as the -m option and prints one line of login information for the terminal you are typing in. whoami prints only one line with the account name of the current effective UID.

ℹ️ Affiliate Disclosure
This site's content contains affiliate links. When a visitor buys a product or service through one of them, the site receives a commission from the seller. The amount the buyer pays(it goes down during event discounts ↓)does not go up. Posted prices, discounts, and stock reflect the time of writing and may differ, so confirm with the seller before buying. Products are chosen and reviewed by our own standards, and commissions do not affect the order or content of reviews.

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove your humanity: 10   +   8   =