Every user account in Linux has a unique UID, and the `id` command reveals yours immediately. If you’ve ever wondered how to get UID in Linux, you’re in the right place. This guide walks you through every method, from simple commands to scripting tricks, so you can always find the numeric identifier behind any username.
What Is A UID In Linux
A UID, or user identifier, is a number assigned to each user account on a Linux system. The kernel uses UIDs to track who owns files, processes, and other resources. Think of it as the system’s internal ID card—your username is just a friendly label for that number.
Most Linux systems reserve UID 0 for the root user. Regular users typically get UIDs starting from 1000 or 500, depending on the distribution. System accounts like daemons often have UIDs below 1000.
How To Get Uid In Linux
This is the core section you came for. Below are all the reliable ways to find a user’s UID, from the simplest one-liner to more advanced methods.
Using The Id Command
The `id` command is the fastest way to see your own UID. Open a terminal and type:
id
You’ll see output like this:
uid=1000(john) gid=1000(john) groups=1000(john),4(adm),27(sudo)
The number after `uid=` is your UID. To check another user’s UID, just add their username:
id alice
Checking The /Etc/passwd File
Every user account is stored in the `/etc/passwd` file. You can view it with `cat` or `less`:
cat /etc/passwd
Each line has seven fields separated by colons. The third field is the UID. For example:
john:x:1000:1000:John Doe:/home/john:/bin/bash
Here, the UID is 1000. You can filter for a specific user with `grep`:
grep '^john:' /etc/passwd
Using The Getent Command
The `getent` command queries system databases, including the password database. It works even if user data comes from LDAP or other sources:
getent passwd john
This prints the same format as `/etc/passwd`. The UID is still the third field. This method is more portable across different system configurations.
Extracting UID With Cut Or Awk
If you want just the number without extra text, use `cut` or `awk`. For your own UID:
id -u
For another user:
id -u alice
You can also parse `/etc/passwd`:
grep '^john:' /etc/passwd | cut -d: -f3
Or with `awk`:
awk -F: '/^john:/ {print $3}' /etc/passwd
Finding UID For The Current Shell User
Sometimes you need the UID in a script. The `$UID` environment variable holds it:
echo $UID
This is a read-only variable set by the shell. It’s perfect for conditional checks in bash scripts.
Using The Who And Users Commands
If you want UIDs for currently logged-in users, combine `who` with `id`:
who | awk '{print $1}' | xargs -I {} id -u {}
This lists usernames from `who`, then feeds each to `id -u`. You’ll get a list of UIDs for active sessions.
Why UID Matters In Linux
Understanding UIDs helps you manage permissions, troubleshoot file ownership issues, and write secure scripts. For example, if you see a file owned by UID 1001 but no user with that UID exists, you know the account was deleted.
UIDs also determine what a process can do. A process running with UID 0 (root) has full system access. Regular users are confined to their own resources.
UID Ranges And Conventions
Different Linux distributions use different UID ranges. Here’s a quick breakdown:
- UID 0: Always root
- UID 1-99: System accounts (often reserved)
- UID 100-999: More system accounts, sometimes used for daemons
- UID 1000+: Regular user accounts (on most modern distros)
- UID 65534: Often used for the nobody user
Some older systems start regular users at UID 500. You can check your system’s convention by looking at `/etc/login.defs`:
grep -E '^UID_MIN|^UID_MAX' /etc/login.defs
How To Get UID For Multiple Users At Once
When you need UIDs for all users on the system, use a loop or a single command:
cut -d: -f1,3 /etc/passwd
This prints username and UID for every account. For a cleaner list excluding system accounts:
awk -F: '$3 >= 1000 {print $1, $3}' /etc/passwd
Adjust the number (1000) based on your system’s UID_MIN setting.
Using A Script To Find UID By Name
Here’s a simple bash function you can add to your `.bashrc`:
getuid() {
if [ -z "$1" ]; then
id -u
else
id -u "$1" 2>/dev/null || echo "User $1 not found"
fi
}
Then just type `getuid alice` to get her UID. This is handy for daily use.
Common Errors When Getting UID
Sometimes things go wrong. Here are typical issues and fixes:
User Does Not Exist
If you try `id nonexistentuser`, you’ll get:
id: 'nonexistentuser': no such user
Double-check the spelling. Use `getent passwd` to see if the user is defined in a remote system like LDAP.
Permission Denied
Reading `/etc/passwd` usually works for everyone, but some systems restrict it. Use `getent` instead—it’s more reliable across different setups.
UID Not Found In /Etc/passwd
If a user exists but isn’t in `/etc/passwd`, they might be from NIS, LDAP, or a container. The `getent` command handles these cases.
How To Get UID In Linux Scripts
Automation often requires UID checks. Here are practical script examples:
Check If Running As Root
if [ "$UID" -ne 0 ]; then
echo "This script must be run as root"
exit 1
fi
This is a common pattern in installation scripts.
Find Files Owned By A Specific UID
find /home -uid 1001 -type f
This lists all files owned by UID 1001 under /home. Replace the path and UID as needed.
Create A User With A Specific UID
If you need to assign a particular UID when creating a user:
sudo useradd -u 1500 newuser
Make sure the UID isn’t already taken. You can check with `id 1500` or `getent passwd 1500`.
How To Get UID In Linux Without Commands
You can also find UIDs using graphical tools. Most file managers show the UID in file properties. For example, in Nautilus (GNOME), right-click a file, go to Properties, then the Permissions tab. The owner is shown by name, but you can see the numeric UID in the terminal.
System monitoring tools like `htop` also display UIDs for processes. Press F2 to configure columns and add the UID field.
How To Get UID In Linux For Docker Containers
Inside a Docker container, the same commands work. But containers often have limited users. To check the UID of the container’s default user:
docker run --rm alpine id
You can also check the UID of a mounted volume’s owner from the host:
ls -n /path/to/volume
The `-n` flag shows numeric UIDs instead of usernames.
How To Get UID In Linux For Remote Users
If you manage multiple servers, you can get UIDs remotely with SSH:
ssh user@server 'id -u username'
Or for all users:
ssh user@server 'cut -d: -f1,3 /etc/passwd'
This is useful for inventory scripts or auditing user accounts across a fleet.
How To Get UID In Linux For System Accounts
System accounts like `www-data` or `nobody` have low UIDs. To list them:
awk -F: '$3 < 1000 {print $1, $3}' /etc/passwd
This shows all accounts with UID below 1000. Adjust the threshold based on your system.
How To Get UID In Linux Using Python
If you prefer scripting in Python, here's how:
import os
import pwd
# Get current user's UID
print(os.getuid())
# Get UID for a specific username
print(pwd.getpwnam('john').pw_uid)
This is useful for cross-platform scripts or when you need more complex logic.
How To Get UID In Linux For Files And Directories
To see the UID of a file's owner, use `stat`:
stat -c '%u' filename
The `-c '%u'` format prints only the numeric UID. For a human-readable version:
stat -c '%U' filename
This shows the username instead.
How To Get UID In Linux For Processes
Each process has a real UID (RUID) and effective UID (EUID). To see them:
ps -eo pid,uid,euid,cmd
Or for a specific process ID:
ps -p 1234 -o uid,euid
The UID column shows the real user ID of the process owner.
How To Get UID In Linux For Groups
Groups also have numeric IDs (GIDs). The same commands work with `group` instead of `passwd`:
getent group www-data
The third field is the GID. For your own groups:
id -g
This prints the primary group's GID.
How To Get UID In Linux When Username Has Spaces
Usernames with spaces are rare but possible. Use quotes:
id "john doe"
Or escape the space:
id john\ doe
In scripts, always quote variables to handle spaces safely.
How To Get UID In Linux For Deleted Users
If a user was deleted but their files remain, you'll see a numeric UID instead of a name. Use `find` to locate such files:
find / -nouser -ls
This finds files with no corresponding user. The UID is shown in the listing.
How To Get UID In Linux For LDAP Users
If your system uses LDAP, `getent` still works:
getent passwd ldapuser
But the UID might be managed by the LDAP server. Ensure your client is configured correctly.
How To Get UID In Linux For Samba Users
Samba maps Windows users to Linux UIDs. To check:
pdbedit -L -v | grep 'Unix UID'
This shows the UID for each Samba user.
How To Get UID In Linux For Containers (LXC)
Inside an LXC container, UIDs are isolated. The container's root (UID 0) might be mapped to a high UID on the host. Check with:
cat /proc/self/uid_map
This shows the UID mapping between container and host.
How To Get UID In Linux For Cron Jobs
Cron jobs run with the UID of the user who owns the crontab. To see the UID of a cron job's owner:
ls -n /var/spool/cron/crontabs/
The numeric owner is the UID.
How To Get UID In Linux For NFS Mounts
NFS mounts may show UIDs from the remote server. Use `ls -n` to see numeric UIDs:
ls -n /mnt/nfs_share
If UIDs don't match between client and server, you'll see numeric values instead of names.
How To Get UID In Linux For Systemd Services
Systemd services can run under specific UIDs. Check the service file:
systemctl show myservice | grep UID
Or look at the process:
systemctl status myservice | grep 'Main PID'
Then check that PID's UID with `ps`.
How To Get UID In Linux For Audit Logs
Audit logs record UIDs for security events. Use `ausearch`:
ausearch -ua 1000
This shows events related to UID 1000.
How To Get UID In Linux For Kernel Parameters
Some kernel parameters reference UIDs. Check `/proc/sys/kernel/` for files like `overflowuid`:
cat /proc/sys/kernel/overflowuid
This shows the UID used for overflows (usually 65534).
Frequently Asked Questions
How Do I Find My UID In Linux Quickly?
Type `id -u` in the terminal. It prints your numeric UID immediately.
Can Two Users Have The Same UID?
Technically yes, but it's a bad practice. The system treats them as the same user for permission purposes. Always assign unique UIDs.
What Is The UID Of The Root User?
Root always has UID 0. This is hardcoded in the Linux kernel.
How Do I Find The UID Of A User Without Logging In?
Use `id username` or `getent passwd username` from any terminal. You don't need to be that user.
What Happens If I Change A User's UID?
Files owned by the old UID become orphaned. You need to use `chown` to reassign them. Use `usermod -u` carefully.
Now you know multiple ways to get a UID in Linux. Whether you're a beginner or a sysadmin