CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Users & Groups / id

id

Show user and group identity

Beginnerusersgroupsdebugging

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.

Syntaxid [-u | -g | -G] [-n] [-r] [USER]

Watch it run

You've just logged in to devops-lab and want to know your UID and which groups you're in.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
id

uid is you, gid is your primary group (new files get it), groups lists every group whose permissions apply to this shell.

Anatomy of a command

$
commandid

Prints identity information (coreutils).

How it works

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=.

Options you'll actually use

-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)

Common mistakes

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 session
Parsing the full id output with cut or sed in scripts

The format changes with SELinux contexts, euid fields and locales.

id -u, id -g, id -Gn or id -un, which print one clean value
Checking [ "$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 ]

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print your UID, your primary group name and all your group names, each with one command.

  2. 2

    Show the groups of the root user and of devops.

  3. 3

    Write a one-liner that prints 'root' if you are root and 'not root' otherwise. Try it with and without sudo.

Related commands

groupsList the groups a user belongs tousermodChange an existing user accountsuSwitch to another usersudoRun a command as root or another user
PreviousgroupaddNextwho
Linux / Users & Groups / id

id

Show user and group identity

Beginnerusersgroupsdebugging

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.

Syntaxid [-u | -g | -G] [-n] [-r] [USER]

Watch it run

You've just logged in to devops-lab and want to know your UID and which groups you're in.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
id

uid is you, gid is your primary group (new files get it), groups lists every group whose permissions apply to this shell.

Anatomy of a command

$
commandid

Prints identity information (coreutils).

How it works

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=.

Options you'll actually use

-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)

Common mistakes

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 session
Parsing the full id output with cut or sed in scripts

The format changes with SELinux contexts, euid fields and locales.

id -u, id -g, id -Gn or id -un, which print one clean value
Checking [ "$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 ]

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print your UID, your primary group name and all your group names, each with one command.

  2. 2

    Show the groups of the root user and of devops.

  3. 3

    Write a one-liner that prints 'root' if you are root and 'not root' otherwise. Try it with and without sudo.

Related commands

groupsList the groups a user belongs tousermodChange an existing user accountsuSwitch to another usersudoRun a command as root or another user
PreviousgroupaddNextwho