idShow user and group identity
id prints a user's UID, primary GID and every group they belong to, as numbers and names. Without a name it shows the identity of your current process, which is what the kernel actually uses for permission checks.
id [-u | -g | -G] [-n] [-r] [USER]You've just logged in to devops-lab and want to know your UID and which groups you're in.
idPrints identity information (coreutils).
Without a user name, id asks the kernel for the credentials of its own process with getuid, geteuid, getgid, getegid and getgroups. Those values were inherited from your shell, which got them at login, so they show what this session can really do. With a user name, id looks the account up through NSS (getpwnam, then getgrouplist), reading /etc/passwd and /etc/group or LDAP/SSSD. That is the database view: it changes the moment someone runs usermod, while your running shell does not. When the effective IDs differ from the real ones (inside a setuid program) id prints euid= and egid= as well, and on SELinux systems it adds context=.
-uPrint only the effective UID-gPrint only the effective primary GID-GPrint all group IDs-nNames instead of numbers (with -u, -g or -G)-rReal ID instead of effective (with -u, -g or -G)-ZSELinux security context only-zSeparate entries with NUL instead of spaces (scripts)Running id username to debug 'why can't my shell do X'id username shows the account database, not your session. Your shell may have an older group list.
Run plain id (or cat /proc/$$/status) in the failing sessionParsing the full id output with cut or sed in scriptsThe format changes with SELinux contexts, euid fields and locales.
id -u, id -g, id -Gn or id -un, which print one clean valueChecking [ "$USER" = root ] to detect root$USER is just an environment variable: it can be wrong under sudo, cron or su without -.
[ "$(id -u)" -eq 0 ]Try these on a Linux machine of your own. Each task has a hint and a solution.
Print your UID, your primary group name and all your group names, each with one command.
Show the groups of the root user and of devops.
Write a one-liner that prints 'root' if you are root and 'not root' otherwise. Try it with and without sudo.
idShow user and group identity
id prints a user's UID, primary GID and every group they belong to, as numbers and names. Without a name it shows the identity of your current process, which is what the kernel actually uses for permission checks.
id [-u | -g | -G] [-n] [-r] [USER]You've just logged in to devops-lab and want to know your UID and which groups you're in.
idPrints identity information (coreutils).
Without a user name, id asks the kernel for the credentials of its own process with getuid, geteuid, getgid, getegid and getgroups. Those values were inherited from your shell, which got them at login, so they show what this session can really do. With a user name, id looks the account up through NSS (getpwnam, then getgrouplist), reading /etc/passwd and /etc/group or LDAP/SSSD. That is the database view: it changes the moment someone runs usermod, while your running shell does not. When the effective IDs differ from the real ones (inside a setuid program) id prints euid= and egid= as well, and on SELinux systems it adds context=.
-uPrint only the effective UID-gPrint only the effective primary GID-GPrint all group IDs-nNames instead of numbers (with -u, -g or -G)-rReal ID instead of effective (with -u, -g or -G)-ZSELinux security context only-zSeparate entries with NUL instead of spaces (scripts)Running id username to debug 'why can't my shell do X'id username shows the account database, not your session. Your shell may have an older group list.
Run plain id (or cat /proc/$$/status) in the failing sessionParsing the full id output with cut or sed in scriptsThe format changes with SELinux contexts, euid fields and locales.
id -u, id -g, id -Gn or id -un, which print one clean valueChecking [ "$USER" = root ] to detect root$USER is just an environment variable: it can be wrong under sudo, cron or su without -.
[ "$(id -u)" -eq 0 ]Try these on a Linux machine of your own. Each task has a hint and a solution.
Print your UID, your primary group name and all your group names, each with one command.
Show the groups of the root user and of devops.
Write a one-liner that prints 'root' if you are root and 'not root' otherwise. Try it with and without sudo.