Open In App

Get Username by User ID in Linux

Last Updated : 24 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Using the ‘getent’ command.
  2. Using the ‘id’ command.
  3. 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:

  1. getent: Retrieves information based on the Name Service Switch (NSS) libraries, which handle user and group data, among other things.
  2. passwd: Specifies that you want to retrieve information from the password database.
  3. UID: You provide the User ID for which you want to find the username.

Example:

Retrieving Username with getent command.

Screenshot-2023-09-29-091020-min

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:

  1. id: Print real and effective user and group IDs.
  2. -u: Print only the effective user ID.
  3. -n: Print a name instead of a number
  4. UID: You provide the User ID for which you want to find the username.

Example:

Obtaining Username with id command.
Screenshot-2023-09-29-192041-min

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:

  1. awk: This is a powerful text processing tool in Linux that can be used for pattern scanning and processing.
  2. -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. $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).
  4. /etc/passwd: This is a path that store the user information.

Example:

Parsing /etc/passwd to Find Username.

Screenshot-2023-09-29-200549-min

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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads