A “UID(User Identifier)” is a unique numeric value assigned by each Linux user in a system. This UID is mainly designed to identify the user and associated system resources. In Linux, the “0” zero UID is set by default for the root user. The UID also plays an important role in getting the logged-in user’s details, accessing and managing the shared system resources, and many other tasks.
This guide enlists possible ways to find my user id from the terminal.
- Method 1: Using the “id” Command
- Method 2: Using the “echo” Command
- Method 3: Using the “lslogin” Command
- Method 4: Using the “grep” Command
- Method 5: Using the “getent” Command
Method 1: Using the “id” Command
Linux offers a built-in “id” command to check the current or specific user and groups numeric ids, names, security context and other related pieces of information.
For Current User
In this scenario, the currently logged-in user is “itslinuxfoss”. Execute the “id” command in the terminal and check its User ID:
The “itslinuxfoss” User ID is “1000”
For Specific User
Specify the particular user name with the “id” command and its associated flag “u(shows user)” to get its user id on the terminal:
$ id -u anna
Another user of the current system “anna” has a “1001” User ID.
Method 2: Using the “echo” Command
The “echo” command prints the passing string as an argument in the terminal. It is also beneficial to get the environment variable’s key values.
The “$UID” is the default environment variable that stores the system user’s id. Run the “echo” command with the “$UID” to find the currently logged-in User ID:
$ echo $UID
Now the logged-in user is “johnson” and its associated User ID is “1002”.
Method 3: Using the “lslogin” Command
The “lslogin” command provides the system user account and the login activity with a few important details. It prints the “UID” column in the output that shows all system Users’ IDs.
Type the “lslogin” command followed by the “u(represents user)” flag to find out the system User IDs in the terminal:
$ lslogins -u
The first “UID” column shows the system Users’ IDs.
Method 4: Using the “grep” Command
All the UIDs are stored in the “/etc/passwd” file that can be easily accessed through the “cat” command. But the problem is that finding the IDs from the whole content is difficult. For this purpose, the “grep” command is used to filter out the targeted user id from the “etc/passwd” file:
Execute the “grep” command with the user name whose id needs to find in the “/etc/passwd” file:
$ grep anna /etc/passwd
The specified user “anna” is assigned “1001” User ID and “1001” group id(gid).
Method 5: Using the “getent” Command
The “getent” is a built-in Linux command to show the details of the database having entries “passwd”, “uid”, “gid”, “shell”, and “protocols”. It is known as the common method to get user details in Linux.
Execute the “getent” command with the specified user group to view its User ID:
$ getent passwd itslinuxfoss
The first “1000” value is the User Id, and the second specifies the group id.
Linux and its distributions offer the pre-installed “id”, “echo”, “lslogin”, and “getent” command line utilities for finding User ID from the terminal. In addition, this operation can also be done by accessing the “/etc/passwd” file using the “grep” command. This guide provides a detailed view of finding my user id from the terminal.