System and users
id
Print the user and group IDs of the current user or of a named user.
id [OPTION...] [USER...]
id shows the identity the kernel uses for permission checks: the user ID (uid), the primary
group ID (gid) and the supplementary groups. With no argument it reports the current process;
with a user name it looks that account up in the user database (/etc/passwd, /etc/group, or
LDAP and friends through NSS).
The single-value options make it the standard tool for scripts: -u for the user ID, -g for
the primary group, -G for all groups, and -n to print names instead of numbers. whoami is
just id -un and groups is just id -Gn.
One subtlety matters in practice: group membership is read once, at login. After someone adds
you to a group, id USER (a database lookup) shows the new group immediately while plain id
(your running session) does not until you log in again.
Examples
Show your user and groups
id
uid=1000(dev) gid=1000(dev) groups=1000(dev)
uid, primary gid, and the supplementary groups, each as number and name. The sandbox
user dev belongs only to its own group.
Print just the numeric user ID
id -u
1000
-u alone prints the effective UID; 1000 is the first regular account on Debian.
Print the user name
id -un
dev
-n turns any of -u, -g or -G from numbers into names. This is exactly what whoami prints.
Print the primary group name
id -gn
dev
The primary group is the group that owns the files you create (unless the directory is setgid).
List all group names, space-separated
id -Gn root
root
-G lists every group ID, primary first; with -n, their names. Given a user name, id
looks the account up instead of describing the current process.
Look up another account
id www-data
uid=33(www-data) gid=33(www-data) groups=33(www-data)
www-data is the Debian account web servers run as, with UID and GID 33. Any user in the
database can be inspected without being that user.
Run as root
id
uid=0(root) gid=0(root) groups=0(root)
Root has UID 0 and GID 0. Permission checks are skipped for UID 0, whatever the name is.
Check whether an account exists
if id deploy >/dev/null 2>&1; then echo "deploy exists"; else echo "no deploy user"; fi
id deploy
no deploy user id: ‘deploy’: no such user
For an unknown name id prints no such user and exits 1, so the exit status doubles as an
existence test. The second line shows the error message the first one hides.
Test whether the current user is in a group
for g in dev sudo; do
if id -nG | grep -qw "$g"; then echo "in $g"; else echo "not in $g"; fi
done
in dev not in sudo
grep -w matches whole words, so being in devops would not count as being in dev. The
check uses the running session's groups, the ones that actually apply to file access.
Use the UID in a root check
[ "$(id -u)" -eq 0 ] && echo "root" || echo "not root (uid $(id -u))"
not root (uid 1000)
Checking for UID 0 is more robust than comparing names. In bash, $EUID holds the same
number without running a command.
Common pitfalls
Expecting a new group to work right after
usermod -aG docker alice. Running processes keep their old group list: log out and back in, or start a shell withnewgrp docker.id aliceshows the new group sooner thaniddoes, which is a common source of confusion.Parsing the default output with
cutorsed. Use the single-value options (-u,-gn,-Gn) instead.id -non its own is an error: it only modifies-u,-gor-G.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
The short options
-u -g -G -n -rwork on GNU and BSD/macOS. GNU adds-z(NUL-separated) and the SELinux context-Z; macOS adds-p(human-readable) and-P(passwd-file line).
Options worth knowing
A selection, described in our own words. For the full list, see id(1) or id --help.
| Option | What it does |
|---|---|
-u, --user | Print only the effective user ID. |
-g, --group | Print only the effective (primary) group ID. |
-G, --groups | Print all group IDs. |
-n, --name | Print names instead of numbers; use with -u, -g or -G. |
-r, --real | Print the real ID instead of the effective one; use with -u, -g or -G. |
-z, --zero | Separate entries with NUL instead of spaces. |
-Z, --context | Print only the SELinux security context. |
-a | Ignored; accepted for compatibility. |
Related
whoamiPrint the user name of the effective user ID the current process runs as.groupsPrint the groups the current user, or each named user, belongs to.chownChange the owner and/or group of files and directories.statShow a file's metadata (size, permissions, owner, inode, timestamps), or print chosen fields in your own format.