Who am I? The id and whoami commands

Video 111 of 140 in Virtualization Foundations, in the unit “Users, groups, permissions and sudo”. 2:47, with English and Hindi audio.

Every command you type runs as some account, and that account decides what the command may read and change. Before you change anything on a server, you should know who you are. Two small commands answer that question: whoami prints your account name, and id prints your numbers and every group you belong to. This article explains why the kernel compares numbers rather than names, and how to read the output of id.

What you will learn

The kernel checks numbers, not names

Each time a program opens a file, the kernel checks a few numbers. Who is asking, and which groups does that account belong to? The names you see are labels for people. The kernel works with the user ID and the group IDs, which are numbers.

Most of the time the name is all you need. The number matters when files travel between machines, as the trap below shows.

Asking for one piece at a time

Print the name of the account your shell runs as
whoami
Print only the user number
id -u
Print the names of all your groups, separated by spaces
id -Gn

Your shell takes these numbers at login. A group change made later shows up only in a new login.

Reading the full id line

Show the user, the main group and all groups at once
id

Note: In the example output, uid is the user, gid is the main group, and groups lists them all. Your names and numbers will differ.

A user who belongs to many groups gets all of their rights at once. That is why a long groups list can grant more than you expect. To look at another account, add its name, as in id bob. Reading that information needs no special rights.

The trap: same name, different number

Imagine two machines. On machine A, the account reports has user ID 1001. On machine B, reports was created later and got ID 1005. A file stores the number, not the name. When the file moves to machine B, its owner may show up as a stranger, or as a bare number.

When an owner looks wrong, check the numbers with id, and compare the passwd file on both machines.

Try it yourself

You need a Linux terminal, logged in as any account. Run four commands, from the shortest answer to the full picture, and compare the names with the numbers.

  1. Print the name of the account your shell runs as.

    whoami

    What you should see: The shell prints your account name, such as alice.

  2. Print only your user number, which is what the kernel compares.

    id -u

    What you should see: The shell prints one number, your user ID.

  3. Print the names of all the groups you belong to.

    id -Gn

    What you should see: The shell prints the group names, separated by spaces.

  4. Print the full picture: user, main group and every group, with their numbers.

    id

    What you should see: An example: uid=1001(alice) gid=1001(alice) groups=1001(alice),27(sudo). Your names and numbers will differ.

Terms

User ID
The number the kernel uses for an account, shown by id as uid, when it decides what a process may do.
Group ID
The number the kernel uses for a group, shown by id as gid for the main group and in groups for the others.

Check yourself

Why does the kernel check numbers rather than names?

The kernel works with user and group IDs, which are numbers. Names are labels for people, so compare numbers when names disagree.

Which command prints only your user number?

id -u. Plain id shows the user, the main group and all the groups together.

Why might a new group membership not apply in an open session?

The membership is a change to the group file, and it counts only when that person logs in again.

Previous: Users and groups: the files behind them. Next: File modes: read, write and execute.