How to Find UID of a User in Linux [3 Best Methods]

Written by

Reviewed by

Last updated: July 12, 2024

Expert verified

SVG Image

TL;DR

To find UID of a user in Linux, follow these steps:

  1. Open your Terminal with Ctrl+Alt+T.
  2. Type id to find your current user’s UID.
  3. Type id -u username (replace username with the actual username) to find a specific user’s UID.

Dealing with user permissions on Linux can be tricky, especially when you need to identify users correctly. Knowing a user’s UID (User Identifier) is crucial for this task. In this post, I’ll show you easy ways to find UID and GID of a user in Linux, helping you manage your system better. You’ll get clear steps, helpful tips, and best practices for managing UIDs and GIDs. By the end, you’ll understand how to handle user and group identifiers, making your Linux administration tasks smoother and more secure.

What is the UID of a User on Linux

In Linux, the UID (User Identifier) is a unique number assigned to each user account. It is used by the system to identify and manage user permissions and access rights.

Here are some key points about UIDs in Linux:

  1. UID range: UIDs are non-negative integers. Typically, regular user accounts have UIDs starting from 1000 or higher, while system and reserved accounts have UIDs below 1000.
  2. Root user: The root user, which has full administrative privileges, has a UID of 0.
  3. /etc/passwd: UIDs are stored in the /etc/passwd file along with other user account information. Each line in this file represents a user account and includes fields separated by colons, with the UID being the third field.
  4. getuid(): Programs can retrieve the UID of the current user using the getuid() system call.
  5. File ownership: UIDs are used to determine file ownership. Each file in Linux is associated with a user ID, indicating the owner of the file.
  6. Permissions: UIDs play a crucial role in file and directory permissions. The owner of a file or directory has specific permissions (read, write, execute) associated with their UID.
  7. Changing UID: The UID of a user can be changed using the usermod command with the -u option, but it is generally not recommended unless necessary, as it can have implications for file ownership and permissions.

To find UID Linux, you can use the id command followed by the username. For example:

$ id john
uid=1000(john) gid=1000(john) groups=1000(john)

This output shows that the user “john” has a UID of 1000.

Explore this guide to learn how to get the uuid in Linux to get information about your system’s devices and files.

How to Find UID of a User in Linux?

To find the UID of a user on Linux, you can use the id command followed by the username. For example, to find the UID for a user named “john,” you would use the command id -u john. This command outputs the numerical UID assigned to the user “john” without additional information, making it straightforward for scripts and command-line usage.

That was the quick answer, to get more details to find uid of user Linux and to learn more ways, read below:

3 Ways to Find the UID of a User in Linux

1. id Command

The id command in Linux is a quick and effective tool to fetch user and group information. It’s commonly used to find out a user’s UID (User Identifier), which is crucial for managing permissions and access control. Follow these steps to Linux get user UID using the id command:

  1. Open your Terminal by pressing Ctrl+Alt+T.
open terminal
  1. To get UID of user that is currently logged in, simply type the following command and press Enter:
id

This command will display several pieces of information including the UID.

displaying UID of user using id command
  1. If you need the UID of a specific user, type: 
id -u username

Replace the username with the actual username.

displaying UID of a specific user

2. /etc/passwd File

The /etc/passwd file is a traditional database that contains essential information about system accounts and users. It’s structured in a way that allows you to view a user’s UID directly. Here is the step-by-step guide to Linux get user id:

  1. Access your command window and execute the following command:
cat /etc/passwd

This command will list the contents of the /etc/passwd file.

listing content of passwd file
  1. To find a specific user’s information, use the grep command: 
grep username /etc/passwd

Replace the username with the user’s name.

The output will be a line that includes the username, UID, and other details, separated by colons.

fetching information of a user from passwd file

3. getent Command

The getent command fetches entries from databases configured in /etc/nsswitch.conf, including user information from the passwd database. It’s particularly useful in environments with multiple data sources for user information. Here is how to Linux find UID:

  1. Launch your command prompt and type the following command to Linux get UID about a specific user: 
getent passwd username

Replace the username with the actual user name.

The format will be similar to the /etc/passwd output, with fields separated by colons.

viewing information of a specific user using getent

How to Change the UID of a User in Linux?

Changing the UID (User Identifier) of a user in Linux is something system administrators might do to keep UIDs the same across different systems, to fix issues with overlapping UIDs, or to follow security rules. Here’s a simple guide on how to change a user’s UID in Linux:

  1. First, it’s good to confirm the current UID of the user. You can do this with the id command:
id username

Replace the username with the actual user’s name. This command will show the UID among other details.

checking current uid of user
  1. Check if there are any running processes for the user:
pgrep -u username

If any processes are running, you should stop them or ensure they are not critical before proceeding.

check for any running processes of user
  1. Use the usermod command to change the UID. You need superuser privileges for this:
sudo usermod -u newUID username

Replace newUID with the desired UID number and username with the actual user’s name.

changing the UID of a user
  1. After changing the UID, the files previously owned by the user will retain the old UID as their owner attribute. To update the ownership of all files in the filesystem to reflect the new UID, use the find command:
sudo find / -user oldUID -exec chown -h newUID {} \;

Replace oldUID with the old UID number and newUID with the new UID number. This command searches the entire filesystem (/) for files owned by oldUID and changes their ownership to newUID. Use caution with this command, as it affects the entire file system.

changing ownership of files to new UID
  1. Check that the UID has been changed successfully and that files have the correct ownership:
id username

The output will be:

verifying the changed UID

What is the GID of a User and How to Find It?

In Linux, GID stands for Group Identifier. It’s a unique number assigned to each group on the system. Groups help manage and organize users by giving them shared access to files and directories. Each user belongs to at least one group, and the GID identifies which group that is.

Why is the GID Important?

  • Permissions: The GID helps control access to files and directories. If a file belongs to a group, only users in that group can access it.
  • Organization: Grouping users with similar roles or permissions makes managing a system easier.

How to Find the GID of a User?

1. Using the id Command

  1. Open a terminal.
  2. Type id username and press Enter. Replace “username” with the actual username.
id username

Look for the gid value in the output. It shows the user’s primary group ID.

viewing gid of a user using id command

2. Checking the /etc/passwd File

  1. Access your command window and type the following command and press Enter. Replace username with the actual username.
cat /etc/passwd | grep username

Find the line that starts with the username. The fourth field on this line is the GID.

accessing gid of a user from etc passwd file

5 Best Practices for Managing UIDs in Linux

Managing User Identifiers (UIDs) effectively is crucial for maintaining secure and orderly Linux environments. Proper UID management helps ensure that permissions and resources are appropriately allocated and maintained. Here are five best practices for managing UIDs:

  1. 🔒 Maintain a UID Allocation Scheme: Establish and follow a consistent UID allocation scheme to avoid conflicts. Assign ranges for different types of accounts (system, regular users, services) to simplify management and enhance security.
  2. 🔄 Regularly Audit UIDs: Periodically review and audit UIDs and associated permissions to ensure they still align with current roles and responsibilities. This practice helps identify and rectify any discrepancies or unauthorized changes.
  3. 🛡️ Use System UIDs for System Accounts: Reserve lower UID numbers (usually below 1000) exclusively for system and service accounts. This separation prevents ordinary users from accessing critical system files and processes.
  4. 🔧 Standardize UIDs Across Systems: For environments with multiple systems, standardize UIDs across all devices. This uniformity prevents issues with permissions and resource access when users or processes operate across different systems.
  5. 📖 Document UID Changes and Policies: Keep detailed records of UID assignments, changes, and the rationale behind these decisions. Documentation provides clarity and aids in troubleshooting, compliance, and audits.

Linux UID: Final Thoughts

To find the UID of a user, you can use methods like the id command, checking the /etc/passwd file, or using the getent command. These methods help you manage user permissions effectively.

If you’re looking to explore more, check out articles on:

Frequently Asked Questions

What Is id Command?

The id command in Linux is used to display the user identity information. It provides the user ID (UID), group ID (GID), and groups associated with the current user or a specified user. This command is useful for checking a user’s permissions, verifying their groups, and for scripting and system administration tasks to ensure correct user settings are applied.

What Is getent Command?

The getent command in Linux is used to fetch entries from databases supported by the Name Service Switch libraries, which include passwd, group, and others. It allows users to get entries in a manner similar to how the system would retrieve them, useful for querying user information, group memberships, and more. This command is particularly helpful for system administrators to verify user and group configurations and to ensure that authentication mechanisms are functioning properly.

How do I resolve conflicts with duplicate UIDs on a system?

To resolve conflicts with duplicate UIDs, you need to change the UID of one of the users. This can be done using the usermod command, followed by adjusting file ownership with chown to ensure all user files reflect the new UID, maintaining file access and system security.

How can changes to UIDs affect file ownership and permissions?

Changing a user’s UID can disrupt file ownership because files are associated with UIDs, not usernames. If you change a UID, files previously owned by the user will still be linked to the old UID. You must use chown to reassign the ownership of the files to the new UID.

How can I list all UIDs currently assigned on my Linux system?

To list all UIDs currently assigned on your system, you can use the command cut -d: -f3 /etc/passwd | sort -n. This command extracts the third field from the /etc/passwd file, where UIDs are stored, sorts them numerically, and displays them.

What is the range of valid UIDs available in Linux?

The range of valid UIDs in Linux typically starts from 0 (assigned to the root user) and can go up to 65535 for standard configurations. However, modern systems support larger numbers, allowing for more extensive UID ranges, which is configurable based on system settings and requirements.

Ojash

Author

Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

Akshat

Reviewer

Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

Share this article
Shareable URL
Prev Post

How to Linux Format Disk Partitions? [3 Easy Methods]

Next Post

7 Easy Methods to Rename Multiple Files Linux

Leave a Reply

Your email address will not be published. Required fields are marked *

Read next