What is UID in Linux? How to find and change the UID

If you've ever interacted with Linux systems, chances are you've come across these two words, UID and GID. If you are not familiar with them, let's dig deeper through the following article.

If you've ever interacted with Linux systems, chances are you've come across these two words, UID and GID. If you are not familiar with them, let's dig deeper through the following article.

What is UID in Linux?

UID stands for User Identifier, while GID stands for Group Identifier. In this specific article, TipsMake.com will focus on User Identifier (UID).

The UID is a unique identifier assigned to every user on the Linux system. The main role of UID is to identify users with the Linux kernel.

It is used to manage resources that users have access to in the system. That is one of the reasons to use unique UID for every available user. On the other hand, if there are two users listed with the same UID, both may have access to resources for the other.

Where to find stored UID?

You can find the UID in the / etc / passwd file, which is also the file that stores all registered users in the system. To view the contents of the / etc / passwd file, run the command caton the file, as shown below on the Terminal.

What is UID in Linux? How to find and change the UID Picture 1What is UID in Linux? How to find and change the UID Picture 1 View the contents of the file / etc / passwd

The / etc / passwd file contains all the necessary attributes or basic information about every user on the system. Data is displayed in 7 columns, as listed below. These fields are separated by colons (:). This file also contains the system-defined accounts and groups needed to install, run, and update the appropriate system.

  1. Column 1 - Name
  2. Column 2 - Password - If the user has set a password on this field, then it is indicated by the letter (x).
  3. Column 3 - UID (User ID)
  4. Column 4 - GID (Group ID)
  5. Column 5 - Gecos - Contains general information about the user and can be left blank.
  6. Column 6 - Home directory
  7. Column 7 - Shell - The path to the default shell for the user.

Determine the UID

From the image above, the first user listed on the file is root. The root has complete control over every aspect of the system. The root user is assigned UID Zero (O) and GID (0). Followed by system-defined accounts and groups.

What is UID in Linux? How to find and change the UID Picture 2What is UID in Linux? How to find and change the UID Picture 2 The root has complete control over every aspect of the system

One more thing to note is that UID = 0 and GID = 0 are what give root users all the permissions in the system. If you want to prove it, change the root name to something else like example_User and create a new root user with the new UID and GID.

Also, system-defined accounts and groups by root user have UID 1,2,3,4, etc. That's because most Linux systems give the first 500 UIDs to system users. . Other users add with a command useraddassigned with a UID of 500 or more. In Ubuntu and Fedora systems, a new user, or even a user created during the installation process, is granted a UID of 1,000 or more.

You can see this in the image below, where there are 2 users Fosslinux_admin and Tuts.

What is UID in Linux? How to find and change the UID Picture 3What is UID in Linux? How to find and change the UID Picture 3 There are 2 users Fosslinux_admin and Tuts

The tuts user was created during the installation and assignment of UID 1000. The remaining user, fosslinux, was added later and issued UID 1001.

How to find the user UID, group or account

The article discussed how to find a UID by displaying the contents of the / etc / passwd file. There is a quicker and easier way using commands id.

For example, to find the UID of Fosslinux_admin and Tuts users, execute the command below. You may be asked to enter the root password.

id fosslinux_admin id tuts

You can also run commands idon other groups. By executing the command alone idin the Terminal, it will display the UID of the currently logged in user.

What is UID in Linux? How to find and change the UID Picture 4What is UID in Linux? How to find and change the UID Picture 4 You can also run the id command on other groups

How to change the UID

Suppose you are managing a system with many users. If a user leaves the company, you'll probably need to assign the new user the UID of the employee who quit.

First, create a temporary user for this example. Posts will use the command useradd. You will need to have root access. 

useradd example_user

By running the command idon user example_user , you can see that this new user has UID 1003.

What is UID in Linux? How to find and change the UID Picture 5What is UID in Linux? How to find and change the UID Picture 5 This new user has UID 1003

Now, delete user Fosslinux_admin with UID = 1001 and assign it to new user. Posts will use the command userdelto remove users.

sudo userdel -r fosslinux_admin

After doing that, the UID will be assigned a new user - example_user - UID belongs to Fosslinux_admin. That is UID = 1001. The article will do this by command usermod.

usermod -u 1001 exmple_user

By running the id command on the user, for example, _user, we see that the user currently has UID = 1001.

What is UID in Linux? How to find and change the UID Picture 6What is UID in Linux? How to find and change the UID Picture 6 Existing user UID = 1001

Now, when you assign an old user's UID to a new user, you'll need to sync these files with all other files that belong 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 new users with specific UIDs

Alternatively, you can create a new user with the command useraddand assign the user a specific UID. See the syntax below.

sudo useradd -u 1111 user_2
What is UID in Linux? How to find and change the UID Picture 7What is UID in Linux? How to find and change the UID Picture 7 Create new users with specific UIDs

By running the command idon user_2 , we see that the user UID = 1111.

4.3 ★ | 15 Vote