In Linux, every user has a unique user id (uid) that is used to identify them. This number is assigned when the user account is created, and it remains the same regardless of the user’s name or location.
If you need to know the uid for a particular user, there are several ways to check it. In this blog post, we will discuss two different methods for checking user id in Linux.
understanding user id in Linux
In Linux, user id for human users is assigned automatically when the user account is created. The range of user id is usually 1000-65535, but can vary depending on the system.
User id 0 is reserved for the root user, and user ids in the range of 0-999 are typically reserved for system users.
User id less than 1000 is considered privileged users and can perform actions that could potentially damage the system. For this reason, it is best to avoid using these user ids unless absolutely necessary.
Check this post to get more about how to create users in Linux.
Procedure to check user id in Linux
- Open the terminal.
- Type id -u and press Enter. The output will show the numeric user ID
- If you want to see more information about your user ID, type id -a and press Enter. This will show you the user ID, as well as the group ID and any supplemental group IDs.
Get user id with id command in Linux
We can use id command to get the user id in Linux. This command will print out a variety of information about the user, including the user id uid. To use this command, simply type “id” followed by the username.
For example, if we wanted to check the uid for the user “john”, we would type “id john”.
If you want to get the id of your own username, you can run id command directly.
The output of id command is as follows:
uid=1000(john) gid=1000(john) groups=1000(john),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),112(lpadmin)
- uid – user ID. A number (1000) is assigned when user is created and it is mapped to the user ID.
- gid – primary group ID. User is assigned a primary group ID (gid) and may belong to additional groups
- groups- Different groups that the user is part of. Example: 27 is the sudo(root) user group
This shows that the user “john” has a uid of 1000 and belongs to the same group as the user.
The id command has a few important options that you should be aware of. The -g option prints the effective group id, and the -u option prints the effective user id. These options can be useful when you need to see which groups or users a particular user is a member of or belongs to.
id -u john
id -g john
The output of these commands would be:
As you can see, the id command is a very versatile and powerful tool that can be used to check user id in Linux. If you need to know the uid for any user on your system, this is the best command to use.
Check this post to learn more about how to add users to group in Linux.
Find user id with /etc/passwd file in Linux
Another way to get the user id in Linux is by viewing the /etc/passwd file. This file contains a list of all users on the system, along with their uid and other information. To view the contents of this file, type “cat /etc/passwd”.
The cat command is used to view the contents of files. It can be used to view the contents of a single file, or multiple files at the same time. To view the contents of a file, simply type “cat filename”. For example, if we wanted to view the contents of the /etc/passwd file, we would type “cat /etc/passwd”.
The output of cat command is as follows:
This shows that the root user has a uid of 0 and belongs to the group root. It also shows that other users on the system have uids in the range of 1000-65535.
The drawback of the cat command is that it shows all users on the system. This can be confusing and overwhelming if you’re trying to find the uid for a specific user.
We can use grep command to search the /etc/passwd file for a specific user. To do this, we will first need to find the line that contains the user’s information. We can do this by using the command “grep username /etc/passwd”.
This will print out the entire line that contains the username. Once we have found the correct line, we can use the cut command to extract just the uid. To do this, we will type “grep username /etc/passwd|cut -d: -f 3”. This will print out only the uid portion of the line.
The “cut -d” option is used to specify the delimiter (character) that is used to separate the fields in a text file. The “-f” option is used to specify the field number that we want to extract. In this example, we are extracting the third field from the line that contains the user’s information.
There are several ways to check user id in Linux. The best way to do this is by using the id command, which will print out a variety of information about the user, including the uid. Another way to get the user id is by viewing the /etc/passwd file. This file contains a list of all users on the system, along with their uid and other information.