Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

id Command Guide for Checking Your User and Group Identities

id prints who you are — your user ID, your groups, and their IDs — the identity the kernel actually uses for permission checks.


What is id, and Why Use It?

Every process on Linux runs as a user and a set of groups, identified by numbers, not names. id shows you exactly those numbers and names for the current user — or for any user you name. It’s the fastest way to answer “who am I, really?” when permissions decide whether something works or fails with “Permission denied”.

When you run id with no arguments, it shows your own identity:

id
uid=1000(alice) gid=1000(alice) groups=1000(alice),27(sudo),44(video)

Why Learn id?

  • Permissions are enforced by UID and GID numbers — names are just labels; id shows the numbers that actually matter.
  • It explains group membership — which groups you belong to determines what you can read, write, and execute.
  • It’s the first diagnostic for permission errors — before changing anything with chmod, check who the files and commands belong to.
  • Scripts and servers rely on it — “is this user in the right group?” is a question id answers in one line.

If chmod and chown are the verbs of permissions, id is the noun: it tells you who you and your files actually are. Check identity first, change permissions second.


1. Show Your Own Identity

id

Example:

$ id
uid=1000(alice) gid=1000(alice) groups=1000(alice),27(sudo),999(docker)

Reading the output:

  • uid=1000(alice) — your user ID is 1000, and its name is alice.
  • gid=1000(alice) — your primary group is 1000.
  • groups=... — all your supplementary groups: sudo, docker, and so on.

The names in parentheses are looked up from /etc/passwd and /etc/group.


2. Look Up Another User

Name a user to inspect their identity:

id username

Example:

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

$ id postgres
uid=113(postgres) gid=118(postgres) groups=118(postgres)

$ id www-data
uid=33(www-data) gid=33(www-data) groups=33(www-data)
  • Useful when debugging a service: “what user is this running as?”
  • The UID tells you privilege level at a glance: 0 is always root.
  • If the user doesn’t exist, id says so and exits with an error.

3. Show Only the Numbers

Skip name resolution for scripts and raw inspection:

id -u    # user ID only
id -g    # primary group ID only
id -G    # all group IDs

Example:

$ id -u
1000

$ id -g
1000

$ id -G
1000 27 999

$ id -un    # username only
alice
  • id -u is a common test in shell scripts: if [ "$(id -u)" -eq 0 ]; then ... checks “am I root?”
  • Numeric output is stable even if name lookups fail (e.g., on a machine where /etc/passwd is unavailable).

4. Check a File’s Ownership in ID Terms

The ls -l output pairs with id to make ownership concrete:

ls -l secret.txt
id -nG

Example:

$ ls -l secret.txt
-rw-r----- 1 alice devs 512 Dec  4 10:00 secret.txt

$ id -nG
alice sudo docker

$ id -gn
alice
  • The file belongs to alice:devs; the permission for group members applies only if you’re in devs.
  • You’re not in devs, so the group read bit does nothing for you — which is why the fix is chgrp or chown, not chmod.

5. Why UID Zero Is Special

Linux treats UID 0 as root, regardless of the account’s name:

# An account named "notroot" with uid 0 is still fully root
$ id notroot
uid=0(notroot) gid=0(root) groups=0(root)
  • Checks like if [ $(id -u) -eq 0 ] are how scripts detect root.
  • Some container setups exploit this — a container process can be UID 0 inside while mapping to an unprivileged host user.
  • This is also why chmod 4755 (setuid root) binaries are powerful — and why you should audit them carefully.

Common Pitfalls

  • Relying on names when numbers matter. Permission checks use UIDs/GIDs; if id shows a name you don’t recognize, check the numeric ID against /etc/group.
  • Forgetting that group membership changes at login. Adding yourself to a group with usermod doesn’t apply to running shells — log out and back in, then confirm with id.
  • Using id to check file ownership. id shows your identity; for files use ls -l (or stat).
  • Assuming the primary group. gid= is your primary group, but groups= is the full list — permission checks consider the full list.
  • Running id inside a container and expecting host answers. A container has its own users and groups; id shows the container’s view, which may differ from the host’s.

PageWhy it’s related
whoamiThe shorter “what’s my username?” check
chmodPermission bits only make sense once you know who you are
chownChanging ownership, verified with id
chgrpJoining a file’s group — or finding out you haven’t
/etc/passwdWhere id gets the names for its numbers
sudoRunning as root when your id says you’re not

id Cheatsheet

CommandMeaning
idShow your full identity (user + groups)
id usernameShow another user’s identity
id -uPrint only your numeric user ID
id -gPrint only your primary group ID
id -GPrint all your group IDs
id -unPrint only your username
id -gnPrint only your primary group name
id -nGPrint all your group names
[ $(id -u) -eq 0 ]The classic “am I root?” shell test