Linux Get User by ID

In Linux, everything is assigned a unique identification number which is used to store its information, like, each user has a separate user ID “UID”, a group has a unique Group ID “GID,” and a process has its unique ID “PID”. The user ID UID can be used to get any particular information about that user. In this article, we will use the UID to get the user information with different methods, which are as follows:

Let’s start.

Pre-Requisite

The UID will be used to get the user’s information. Let’s find the user’s ID for the “itslinuxfoss” using this command:

$ id itslinuxfoss

Note: The user has UID “1000”, which will be used through this guide to get the information about the user. Let’s get user details using UID.

How to Get User by ID in Linux?

User details can be obtained via the unique ID (UID) assigned to the user. Let’s use the UID to find the user details.

Method 1: Get the User by ID Using the id Command

The id command is utilized to find all the user’s information. To get the information for a specific UID “1000”, the below command is used:

$ id 1000

The above output shows the details like user ID (UID), Group ID (GID), and groups for users with “1000” UID.

If you want to get all the group IDs (GID) for a specific UID (1000), use the following command:

$ id -G 1000

The groups for UID “1000” are displayed in the output.

Method 2: Get the User by ID Using the cat Command

The “/etc/passwd” file contains all the information about the user. To get the information for a specific user with UID “1000”, using the “cat” command, use the below-mentioned command:

$ cat /etc/passwd | grep 1000

The output shows the User Name, Encrypted Password, User ID (UID), Group ID (GID) and other details.

If you want to find the user name only for the UID (1000), using the /etc/passwd file and cat command, execute the following command:

$ cat /etc/passwd | grep 1000 | cut -d':' -f1

The user name “itslinuxfoss” is displayed on the output for UDI (1000).

Method 3: Get the User’s Groups by ID Using getent Command

The “getent” command stores the user information, which can be accessed using the UID. To get the details about the user with its UID (1000), use the following command:

$ getent passwd 1000

The output shows all the details about the UID “1000”.

If you want to get the name of the user only using the “getent” command for a specific UID (1000), use this command:

$ getent passwd 1000 | cut -d':' -f1

The user is logged in as “itslinuxfoss” user name.

Method 4: Get User’s Groups by ID Using awk Command

The “awk” command is also used to find the user details about a particular UID. For instance, to get the user name for UID “1000”, use this command:

$ awk -F':' -v uid=1000 '$3 == uid { print $1 }' /etc/passwd

Similarly, we can use the “awk” command with the getent command to find the user name with the help of its UID (1000):

$ getent passwd 1000 | awk -F':' '{ print $1 }'

The user name is displayed in the output.

That’s it from this article.

Conclusion

In Linux, the User ID is a unique identification number that stores information about the user. The User ID “UID” can be used with different commands to get specific information about the user. Moreover, this guide has explained different methods of using the UID to find all the information or specific information about the user.