id – print user and group IDs

id command
id command

id displays user and group IDs for the current user or a specified user.

Synopsis

id [OPTIONS] [USER]

Common Options

OptionDescription
-uPrint only effective user ID
-gPrint only effective group ID
-GPrint all group IDs
-nPrint name instead of number
-rPrint real ID instead of effective

Examples

Full information

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

Just username

$ id -un
greys

Just user ID

$ id -u
1000

Just group ID

$ id -g
1000

All groups

$ id -Gn
greys sudo docker www-data

Another user

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

$ id www-data
uid=33(www-data) gid=33(www-data) groups=33(www-data)

Understanding Output

uid=1000(greys) gid=1000(greys) groups=1000(greys),27(sudo),999(docker)
│               │               │
│               │               └─ All group memberships
│               └─ Primary group
└─ User ID and name

Common Patterns

Check if root

if [ "$(id -u)" -eq 0 ]; then
    echo "Running as root"
fi

Check group membership

if id -nG | grep -qw docker; then
    echo "User is in docker group"
fi

Tips

  • id vs whoami: id shows more info (groups, IDs)
  • Effective vs real: After sudo, effective UID changes but real doesn’t
  • Group membership: Shows why you can/can’t access certain files
  • Numeric IDs: Useful in scripts for cross-system compatibility

See Also

  • whoami — Print effective username
  • groups — Print group memberships
  • who — Who is logged in

Tutorials