What is UID in Linux? How to Find UID of a User and Change it

Learn via video courses
Topics Covered

What is UID in Linux?

In the Linux operating system, UID stands for User Identifier. It is a unique numerical value assigned to each user account. The UID is used by the system to identify and differentiate between different users. The uses of UID are,

  • Determining user permissions and access levels for files and resources on a Linux system.
  • Process management to associate running processes with the user who initiated them.
  • When a user logs in to a Linux system, the UID associated with their account is used to verify their identity.
  • Each file and directory in Linux has an associated UID that identifies its owner.
  • UID is interconnected with Group Identifier (GID), which represents a specific user group. By using UID and GID together, Linux systems can assign permissions and access levels to both individual users and user groups.

Some other configurations regarding UID are,

  • Linux systems reserve a range of UIDs for user accounts. The range is usually defined in the /etc/login.defs or /etc/default/useradd configuration file. The range typically starts from a minimum UID, such as 1000, and extends up to a maximum UID.
  • Within the UID range, certain UIDs may be reserved for system accounts or predefined users. System accounts are used by the operating system and various processes and typically have UIDs below the user UID range.
  • The user management system ensures that each UID assigned to a user account is unique and does not conflict with existing UIDs on the system. This is achieved by checking the /etc/passwd file.

Where to Find Stored UID?

The /etc/passwd file stores user account information, including the UID.

  • This file contains a list of all user accounts on the system, along with their corresponding UIDs, group IDs (GIDs), home directories, and login shells.
  • Each line in the file consists of several fields separated by colons (":").
  • The cat command can be used to show the content of the file.

Similar to the /etc/passwd, the /etc/group file in Linux is a system file that contains information about user groups on the system. It stores a list of group names, group identifiers (GIDs), and a list of user accounts that belong to each group.

Identify the UID

The output for the command in the last section is,

identify uid code output

Let us explore the first entry of the output and understand the representation,

  • The value root represents the username.
  • The x indicates that the password for this account is stored in the /etc/shadow file.
  • The value 0 is the UID (User Identifier) assigned to the root user.
  • The value 0 is the GID (Group Identifier) assigned to the root user.
  • The /root is the home directory for the root user.
  • The /usr/bin/zsh is the login shell for the root user. The value /usr/sbin/nologin specifies that the user cannot log in to the system. This can be seen in processes and daemons.

UID is also associated with files and processes. There are three types of UID associated with processes and they are,

  • The real UID(RUID) represents the user account that owns or initiates a process. It corresponds to the actual user account under which the process is running. The real UID is set when the process is created and remains constant throughout its lifetime, reflecting the process's ownership.
  • The effective UID(euid) is used to determine the permissions and access rights that a process has when interacting with system resources. The effective UID can change during the execution of a process, allowing temporary elevation or reduction of privileges for specific operations.
  • The saved set-user-ID(suid), or saved UID, is used in special cases when a process temporarily changes its effective UID. The saved UID is a backup of the effective UID before any changes occur. It allows a process to revert to its original effective UID when needed, ensuring that it can regain its original privileges after performing privileged operations.

How to Find the UID of a User, Group, or an Account

There are several ways to identify the UID of a user or group or account. The methods are,

  • The id command allows you to retrieve information about a specific user. The -u option can be specified to display only the UID. The syntax of the command is,

For example, to get the UID of user hari,

output id command

  • The grep command can be used to search for a specific username and extract its corresponding UID from the /etc/passwd file.

For example,

This command fetches the data from /etc/passwd from which we can identify the UID. output grep command

  • The getent command retrieves entries from the system databases, including user account information. To retrieve the UID of a specific user, we can use the following command

For example,

output getent command

This command provides the same output as the previous command. We must have an understanding of the structure of /etc/passwd file to get the UID from the command.

  • You can also use the awk command-line tool which is a tool for text processing and data extraction to extract the UID from the /etc/passwd file. Execute the following command:
  • The -F option is used to set the field separator. In this case, : is specified as the field separator.
  • The next part of the command is the pattern and it defines the condition as /^username:/ to search for a line that starts with the specified username, followed by a colon.

output awk f command

How to Change the UID of a User in Linux?

To change the UID of a user in Linux, you need to use the usermod command. This command allows you to modify various attributes of a user account, including the UID. Here's the syntax for changing the UID using the usermod command,

The -u option is used to specify the new user ID. We need to replace new_uid as the new UID for the user with the user name as username.

For example, to change the UID of the user 'linux' to '1123', we can use the following command

After executing this command, the UID of the 'linux' user will be updated to '1001'.

change uid of user in linux

Create a New User With a Specific UID

To create a new user with a specific UID in Linux, you can use the useradd command with the '-u' option. The syntax is,

For example, to create a new user named linux with the UID 1234, we can use the following command,

This command will create a new user account with the specified UID.

  • We can set a password to the created user using the passwd command along with the name of the user.
  • The id command can be used to verify the existence of a user and check the UID of the created user.
  • Execute the following command to create the home directory for the user linux.
  • We can also switch to the newly created user using the su or switch user command.

create new user with specific uid

Conclusion

  • UID (User Identifier) in Linux is a unique numerical value assigned to each user account, helping identify and differentiate users.
  • The UID is stored in the system's user database, usually in the /etc/passwd file, along with other user details.
  • To find the stored UID, you can use commands like id, grep, or getent passwd to retrieve information from the '/etc/passwd' file.
  • To change the UID of an existing user, the usermod command can be used with the -u option.
  • To create a new user with a specific UID, the useradd command is used.
  • UID plays a vital role in determining user permissions and access levels for files, directories, and resources on a Linux system.
  • UID is used for user authentication, process management, file ownership, and system logging.
  • There are three types of UIDs associated with a process: real UID (ruid), effective UID (euid), and saved set-user-ID (suid).