The most direct way to get a user ID (UID) in Linux is to use the id command, which prints the UID, group ID (GID), and group memberships for the current user or a specified username. For example, running id -u username returns just that user’s numeric UID. This article explains every common method—from simple commands to reading system files—and how to interpret the results.
What Is a User ID in Linux?
Every user account on a Linux system has a unique numeric identifier called the user ID (UID). The kernel uses UIDs, not usernames, to track ownership of files, processes, and permissions. UID 0 is reserved for root, the superuser with full system access. Regular user accounts typically start at UID 1000 on most distributions (like Ubuntu, Debian, Fedora), while system accounts (daemons, services) often occupy UIDs 1–999. The mapping between usernames and UIDs is stored in the /etc/passwd file, which is readable by all users.
Using the id Command (Quickest Method)
The id command is the standard tool for displaying user and group identity information. Without arguments, it shows the current user’s UID, GID, and supplementary groups:
$ id
uid=1000(alice) gid=1000(alice) groups=1000(alice),4(adm),27(sudo)
To get only the numeric UID of a specific user, use the -u option:
$ id -u alice
1000
You can also use id -u without a username to get your own UID. The -g option gives the primary group ID, and -G lists all group IDs. This command works on virtually all Linux distributions and is part of the GNU coreutils package.
Reading /etc/passwd Directly
The /etc/passwd file contains one line per user, with fields separated by colons. The third field is the UID. For example:
alice:x:1000:1000:Alice Smith:/home/alice:/bin/bash
Here, 1000 is Alice’s UID. You can view a specific user’s entry with:
grep alice /etc/passwd
Or use cut to extract just the UID field:
cut -d: -f3 /etc/passwd
This lists all UIDs on the system. Note that the password field is an x because actual hashes are stored in the shadow file (/etc/shadow), which is only readable by root.
Using getent for Network-Aware Lookups
The getent command queries the system’s name service switch (NSS) configuration, which may include local files, LDAP, or NIS. It is the recommended way to look up user information because it works with any backend, not just local files. To get the UID for a user:
getent passwd alice
This outputs the same format as an /etc/passwd line. To extract just the UID:
getent passwd alice | cut -d: -f3
If the user is stored in LDAP, getent will still find them, whereas grepping /etc/passwd would fail. This makes getent the more robust choice in enterprise environments.
Finding Your Own UID in Shell Scripts
In shell scripts, you often need the current user’s UID to make decisions. The $UID environment variable holds the numeric UID of the current process owner. For example:
echo $UID
However, $UID is a Bash built-in variable and may not be available in all shells (like sh or dash). A portable alternative is to use id -u inside command substitution:
myuid=$(id -u)
This works in any POSIX-compliant shell. To check if the script is running as root, you can test [ "$EUID" -eq 0 ] in Bash, but again id -u is more portable.
Using the who and ls Commands
Sometimes you need the UID of a user who is currently logged in. The who command with -u shows the login name and the UID (as a number) for each active session:
who -u
Output includes the user’s UID in the second column. Alternatively, the ls command with the -n option displays numeric UIDs and GIDs instead of names when listing files:
ls -ln
This is useful when you want to see file ownership without name resolution, which can be slow on large NFS mounts or when a user has been deleted.
Common Problems and Misconceptions
One common mistake is confusing the UID with the effective user ID (EUID). The id command shows the real UID by default, but if a program runs with setuid (like sudo), the effective UID may differ. Use id -u for the real UID and id -ru to force the real UID, or id -u with the --real option in newer coreutils. Another issue: UIDs are not necessarily unique across different systems, so a UID on one machine may refer to a different user on another. This is why NFS uses UID mapping. Also, the root user always has UID 0, but there can be other users with UID 0 if the system administrator creates them (which is a security risk).
Frequently Asked Questions
How do I find the UID of a user if I only know the username?
Use id -u username. This works for local users and, if NSS is configured, for LDAP users as well. For example, id -u john prints 1001.
What is the difference between UID and GID?
UID is the user ID, unique to each user. GID is the group ID, identifying a group of users. A user has one primary group (shown in /etc/passwd) and can belong to additional supplementary groups (listed in /etc/group).
Can two users have the same UID?
Technically yes, if you edit /etc/passwd manually, but it is discouraged because the system treats them as the same user for ownership and permissions. Files owned by one will appear owned by both, and they can access each other’s files.
How do I find my UID in a script without using id?
You can use getent passwd $(whoami) or read $UID in Bash. For maximum portability, id -u is the safest because it is defined by POSIX.
Why does my UID start at 1000?
Most modern distributions reserve UIDs 0–999 for system accounts and start regular users at 1000. This is a convention, not a rule; you can change it in /etc/login.defs.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Optimize Wi-Fi on Windows 11 with Preferred Band Settings
- Secure Boot Folder: What It Does & Why It’s Hidden
- iPhone Alarm Volume Low or Loud? Fix It
- How to Access iMessage Apps in iOS 17 on iPhone
- Android Messages App Not Working? 9 Fixes That Work
- iPhone Hotspot Not Working? Here’s How to Fix It