Get Username by User ID in Linux
Last Updated :
24 Oct, 2023
The need to get user names by User ID is frequently encountered by system administrators in the Linux world. This task is necessary to manage permissions, diagnose, or simply identify users of the Linux system for different management purposes. Fortunately, Linux provides several methods to achieve this objective, each with its different advantages and uses.
In Linux, we can get the Username by User ID using the various methods:
- Using the 'getent' command.
- Using the 'id' command.
- Parsing the '/etc/passwd' File.
1. Using the 'getent' command.
The getent command is a versatile tool in Linux that allows you to access entries from various databases, as configured in the /etc/nsswitch.conf file.
Syntax of the the `getnet` command in Linux
getent passwd <UID>
When you use getent passwd <UID>, you are specifically querying the password database for a user with the given User ID.
Here:
- getent: Retrieves information based on the Name Service Switch (NSS) libraries, which handle user and group data, among other things.
- passwd: Specifies that you want to retrieve information from the password database.
- UID: You provide the User ID for which you want to find the username.
Example:
Retrieving Username with getent command.

2. Using the 'id' command.
The id command prints the information of the user and group for each specified USER or the current process.
Syntax of `id` command in Linux:
id -u -n <UID>
When used with the -u -n options, it prints the username associated with a given UID.
Here:
- id: Print real and effective user and group IDs.
- -u: Print only the effective user ID.
- -n: Print a name instead of a number
- UID: You provide the User ID for which you want to find the username.
Example:
Obtaining Username with id command.

3. Parsing the 'etc/passwd' File.
The /etc/passwd file is a text file that contains essential user information, including usernames and UIDs. You can manually parse this file using tools like awk to retrieve a username based on a given UID.
Syntax of the `etc/passwd` in Linux:
awk -F: '$3 == <UID> {print $1}' /etc/passwd
Here:
- awk: This is a powerful text processing tool in Linux that can be used for pattern scanning and processing.
- -F: The -F option specifies the field separator, which in this case is a colon (":"). In the /etc/passwd file, each line is structured with fields separated by colons.
- $3==<UID>{print $1}: This is an awk script that checks if the third field (UID) in each line matches the provided UID and then prints the first field (username).
- /etc/passwd: This is a path that store the user information.
Example:
Parsing /etc/passwd to Find Username.

Conclusion:
To retrieve a user's username based on their user ID, Linux provides several methods. You can choose from using the 'getent' command to access Name Service Switch libraries, the 'id' command for quick information retrieval, or parsing the '/etc/passwd' file for a more manual approach. The efficient management and access of user data is facilitated by this wide range of options available to Linux users.
Similar Reads
How to Delete User in Linux | userdel Command Managing user accounts is an essential aspect of Linux system administration. Understanding how to delete a user in Linux is crucial, whether you need to remove an unused account, revoke access for a departing employee, or clean up your system for security reasons. Here, we will explore the 'userdel
5 min read
How to Change the username or userID in Kali Linux? Kali Linux, a popular Linux distribution for penetration testing and ethical hacking, allows users to create a username during installation, automatically assigning a unique User ID (UID) to each user for identification. However, there are situations where you might need to change the username or us
4 min read
How to add User in Linux | useradd Command useradd is a command in Linux that is used to add user accounts to your system. It is just a symbolic link to adduser command in Linux and the difference between both of them is that useradd is a native binary compiled with the system whereas adduser is a Perl script that uses useradd binary in the
5 min read
How to get the current username in Python When building interacting Python programs you might need to get the current username for features like personalizing user experience, providing user-specific resources, Implementing robust logging and debugging, strengthening security, etc. We will use different Python modules and functions like os.
4 min read
Real, Effective and Saved UserID in Linux Every user in Unix like operating system is identified by a different integer number, this unique number is called as UserID. There are three types of UID defined for a process, which can be dynamically changed as per the privilege of task. The three different types of UIDs defined are : 1. Real Use
3 min read
Deleting a User in Linux using Python Script Deleting a user from your system or server via a python script is a very easy task. You just need to pass the username of the user and the script will remove the details and all the files of that user.This python script uses userdel Linux command to delete the user.You can directly use userdel comma
2 min read