id Command: Checking UID and Groups, whoami, groups, and Supplementary Groups
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
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).
iduid=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 user1uid=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 admuid=3(adm) gid=4(adm) groups=4(adm)id operatoruid=11(operator) gid=0(root) groups=0(root)Writing several accounts prints one per line.
id root binuid=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-user9id: 'no-such-user9': no such userecho $?1The 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
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 -u0id -u daemon2id -G user11000 4 190Adding -n prints names instead of numbers. Checking the groups of user1 with [id -Gn] turned the three numbers into three names.
id -Gn user1user1 adm systemd-journalid -gn user1user1-n and -r are used together with one of -u, -g, and -G. Writing -n alone gave the error below.
id -nid: 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.
| Option | Copy | Description |
|---|---|---|
| -u--user | Prints the UID number | |
| -g--group | Prints the primary group number | |
| -G--groups | Prints all the numbers of the groups the account belongs to | |
| -n--name | With -u, -g, or -G, prints the name instead of the number | |
| -r--real | With -u, -g, or -G, prints the real ID instead of the effective ID |
| Option | Copy | Description |
|---|---|---|
| -Z--context | Prints only the security context of the process | |
| -z--zero | Separates output items with NUL characters instead of spaces; not available in the default format | |
| -a | Accepted only for compatibility with other versions of id, and ignored | |
| --help | Prints help | |
| --version | Print 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.
3️⃣ The whoami Command and id -un
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.
whoamirootid -unrootUnder a regular account on the Debian 12 server, the two commands also printed the same account name.
whoamiuser1id -unuser1whoami 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 user1whoami: 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 user1user1Writing -u instead of -un prints the UID number in the same place instead of the account name.
4️⃣ The groups Command and Supplementary Groups
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.
groupsrootWith an account name, it prints the account name and a colon followed by the groups. Writing several accounts printed one per line.
groups user1 daemonuser1 : user1 adm systemd-journal
daemon : daemonThe 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 admadm:x:4:user1getent group user1user1:x:1000:getent passwd daemondaemon:x:2:2:daemon:/sbin:/sbin/nologinThe 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
Creating accounts
Linux: useradd Command & passwd Command
Commands that create a new user account and set the password of that account.
Deleting accounts
[Linux] userdel Command: Deleting User Accounts
A command that deletes an unused user account and cleans up its home folder as well.
Password expiry
Linux chage Command
A command that checks and changes the password validity period and expiry date of a user account.
ℹ️ 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.