If you have interacted with Linux systems, there is a probability you have come across these two words, UID and GID. If you have no clear idea about them, then follow along with this article.
What is UID in Linux?
A UID is an abbreviation of the word User Identifier, while GID is an abbreviation of the word Group Identifier. In this particular article, we will focus on the User Identifier (UID).
A UID is a unique identification number assigned to every user present in a Linux system. The primary role of the UID number is to identify the user to the Linux kernel.
It is used to manage the system resources that a user has access to in the system. It is one of the reasons for using a unique UID for every user available. Otherwise, if we have two users listed under one UID, then they could both have access to resources meant for the other.
Where to find stored UID?
You can find the UID in the /etc/passwd file, which is the file that also stores all users registered in the system. To view the /etc/passwd file contents, run the cat command on the file, as shown below on the terminal.
The /etc/passwd file holds all necessary attributes or basic information about every single user in the system. The data is displayed in seven columns, as listed below. These fields are separated by colons (:). This file also contains system-defined accounts and groups required for proper installation, running, and update of the system.
Column 1 – Name
Column 2 – Password – If the user has set a password on this field, then it is indicated with the letter (x).
Column 3 – UID (User ID)
Column 4 – GID (Group ID)
Column 5 – Gecos – Contain general information about the user and can be empty.
Column 6 – Home directory
Column 7 – Shell – The path to the default shell for the user.
Identify the UID
From the image above, the first user listed on the file is root. Root has overall control over every aspect of the system. The root user is assigned UID Zero (O) and GID (0). The other that follow are the system-defined accounts and groups.
One more thing to note is that UID = 0 and GID = 0 are what give the root user all the powers in the system. If you wish to prove so, rename the root to something else like Example_User and create a new root user with a new UID and GID. You will realize that the Example_User will still have elevated privileges despite not having the username root.
As you will also notice from the image, the system-defined accounts and groups that follow the root user have the UID 1,2,3,4,… and so on. It is because most Linux systems reserve the first 500 UID for system users. Other users added with the useradd command are assigned UID from 500. In Ubuntu and Fedora systems, a new user, even one created during the installation process, is given the UID from 1000 and above.
You can see this in the image below, where we have two users. Fosslinux_admin and Tuts.
The user “tuts” was created during the installation process and is assigned the UID 1000. The other user, “fosslinux,” was added later and was given the UID 1001.
How to find the UID of a User, Group or an account
We have discussed how we can find a UID by displaying the contents of /etc/passwd file. There is a faster and easier way. We will use the id command.
For example, to find the UID of user Fosslinux_admin and Tuts, execute the command below. You might be required to enter the root password.
id fosslinux_admin id tuts
We can also run the id command on other groups, as shown below. By executing the id command alone in the terminal, it will display the UID of the current logged in user. See the image below.
How to change UID’s
Suppose you are managing a system with a lot of users, say an organization or institution. If a user left the company, you would probably need to assign the new user the UID of the employee who quit.
Let’s first create a temporary user for this example. We will use the useradd command. You will need to have root privileges. See the command below.
useradd example_user
By running the command id on user example_user, we can see that this new user has the UID 1003
Now, let’s delete the Fosslinux_admin user who had the UID = 1001 and assign it to our new user. We will use the userdel command to remove the user.
sudo userdel -r fosslinux_admin
Once done that, we will assign our new user – example_user, the UID that belonged to fosslinux_admin. That is UID = 1001. We will do this using the usermod command.
usermod -u 1001 exmple_user
By running the id command on the user, for example, _user, we see that the user now has the UID = 1001.
Now once you have the new user the UID of the old user, you will need to sync these with all other files that belonged to the old user. You can do this by executing the command below.
find / -user [UID_of_old_user] -exec chown -h [new_user] {} \; e.g sudo find / -user 1001 -exec chown -h user_2 {} \;
Create a New User With a Specific UID
Alternatively, we can create a new user with useradd command and assign the user a specific UID. See the syntax below.
sudo useradd -u 1111 user_2
By running the id command on user_2, we see that the user’s UID = 1111.
Conclusion
That’s it! Everything you need to know about UID’s in Linux systems. If you think we missed a meaningful concept or need any clarification, don’t hesitate to let us know in the comment section below.
1 comment
Much useful, Thanks !