# id — Show User and Group IDs

> id prints the UID, GID and group memberships of a user – ideal for permission troubleshooting, root checks in scripts and debugging access on Linux.

Source: https://www.jpkc.com/db/en/cheatsheets/shell-system/id/

<!-- PROSE:intro -->
`id` answers "who am I, and what am I allowed to do?" in a single line: it prints the UID (user ID), the primary GID (group ID) and every group membership, plus the SELinux security context where applicable. That makes it the go-to tool when file permissions misbehave, a service runs under the wrong account, or a script needs to know whether it is running as root. This guide walks you through the options for humans and machines alike – from the full overview to machine-readable single values for your scripts.
<!-- PROSE:intro:end -->

## Basic Usage

`id` — Print UID, GID, and all group memberships of the current user.

```bash
id
```

`id <user>` — Print UID, GID, and groups for a specific user.

```bash
id www-data
```

`id root` — Show information for the root user. UID and GID should both be 0.

```bash
id root
```

## Numeric IDs

`id -u` — Print only the UID (user ID) of the current user as a number.

```bash
id -u
```

`id -u <user>` — Print only the UID of a specific user.

```bash
id -u www-data
```

`id -g` — Print only the primary GID (group ID) of the current user.

```bash
id -g
```

`id -g <user>` — Print only the primary GID of a specific user.

```bash
id -g deploy
```

`id -G` — Print all group IDs (primary + supplementary) as space-separated numbers.

```bash
id -G
```

`id -G <user>` — Print all group IDs for a specific user.

```bash
id -G jpk
```

## Names Instead of Numbers

`id -un` — Print only the username (name instead of UID). Equivalent to `whoami`.

```bash
id -un
```

`id -gn` — Print only the primary group name.

```bash
id -gn
```

`id -Gn` — Print all group names (primary + supplementary) as space-separated names.

```bash
id -Gn
```

`id -Gn <user>` — Print all group names for a specific user.

```bash
id -Gn jpk
```

## Practical Examples & Scripts

`[ $(id -u) -eq 0 ] && echo root` — Check if the current user is root (UID 0). Common pattern in install scripts.

```bash
[ $(id -u) -eq 0 ] && echo 'Running as root' || echo 'Not root'
```

`id -nG | grep -qw docker && echo 'in docker group'` — Check if the current user is a member of the docker group.

```bash
id -nG | grep -qw docker && echo 'docker access OK'
```

`id -u $USER` — Print the UID of the user stored in the `$USER` environment variable.

```bash
id -u $USER
```

`stat -c '%u %g' <file>` — Show the UID and GID of a file's owner. Useful to compare with `id` output.

```bash
stat -c '%u %g' /var/www/html/index.php
```

## Related Tools

`whoami` — Print only the current username. Equivalent to `id -un`.

```bash
whoami
```

`groups` — Print all group names the current user belongs to. Equivalent to `id -Gn`.

```bash
groups
```

`groups <user>` — Print group memberships for a specific user.

```bash
groups www-data
```

`getent passwd <user>` — Show the full passwd entry for a user: UID, GID, home, shell.

```bash
getent passwd www-data
```

`getent group <group>` — Show group details and all members of a group.

```bash
getent group sudo
```

`grep <user> /etc/passwd` — Show the raw passwd entry for a user directly from the system file.

```bash
grep www-data /etc/passwd
```

<!-- PROSE:outro -->
## Conclusion

`id` is a read-only command with no destructive risk, making it the fastest answer to any permission question. For humans the bare call gives the full overview; for scripts the machine-readable variants are invaluable: `id -u` for the raw UID, `id -g` for the primary group and `id -nG` for a space-separated list of group names. The classic `[ "$(id -u)" -eq 0 ]` root check belongs in every serious install script. One detail worth knowing: for setuid programs the real and effective UID differ – `id` reports both, so you can see which privileges a process actually runs with.

## Further Reading

- [Wikipedia: User identifier](https://en.wikipedia.org/wiki/User_identifier) – background on UID, GID and real versus effective identifiers
- [Ubuntu docs: Adding and deleting users](https://documentation.ubuntu.com/server/how-to/security/user-management/) – official guide to managing users, groups and IDs
<!-- PROSE:outro:end -->

## Related Commands

- [useradd](https://www.jpkc.com/db/en/cheatsheets/shell-system/useradd/) – create new user accounts with their UID and groups
- [su](https://www.jpkc.com/db/en/cheatsheets/shell-system/su/) – switch to another user or to root
- [sudo](https://www.jpkc.com/db/en/cheatsheets/shell-system/sudo/) – run individual commands with elevated privileges

