UIDs (User Identifiers)
In RHEL (Red Hat Enterprise Linux), a UID is a unique integer assigned to each user account. The kernel uses this number to identify the user and enforce permissions for files and processes. UIDs from 0 to 999 are typically reserved for system accounts, while UIDs for regular users start from 1000.
Example 1: Creating a user and automatically assigning a UID
# Add a new user named 'jdoe'
# The system will automatically assign the next available UID from the range defined in /etc/login.defs
sudo useradd jdoe
Explanation The useradd command is the standard utility for creating a new user account. When invoked without the -u option, it automatically selects the next available UID greater than or equal to UID_MIN (typically 1000) as specified in the /etc/login.defs configuration file, ensuring a unique identifier for the new user.
Example 2: Creating a user with a specific UID
# Add a new user 'msmith' and assign a specific UID of 1050
# The -u or --uid option is used to specify the user ID
sudo useradd -u 1050 msmith
Explanation This command creates a new user named 'msmith' but explicitly sets the UID to 1050 using the -u flag. This is useful in environments where UIDs need to be synchronized across multiple systems, such as with NFS (Network File System) shares.
Example 3: Modifying an existing user's UID
# Modify the UID for user 'jdoe' to 1055
# The -u or --uid option of the usermod command changes the user's ID
sudo usermod -u 1055 jdoe
Explanation The usermod command alters existing user account attributes. Here, the -u option changes the UID for the user 'jdoe' to 1055. The system will also need to update the ownership of the user's files in their home directory to reflect the new UID.
Example 4: Finding a user's UID
# Display the UID for the user 'msmith'
# The id command provides user identity information
id -u msmith
Explanation The id command is a versatile tool for displaying user and group information. Using the -u flag isolates the output to show only the numerical UID of the specified user, which in this case is 'msmith'.
Example 5: Locating all files owned by a specific UID
# Search the entire filesystem ('/') for files owned by UID 1050
# The -uid option in the find command specifies the user ID to search for
sudo find / -uid 1050
Explanation The find command is a powerful utility for searching for files and directories. This example uses the -uid predicate to search the entire filesystem starting from the root directory (/) for any file or directory owned by the user with UID 1050.
Example 6: Command variations for creating a user with a specific UID
# RHEL Linux
# Create a user 'testuser' with UID 2001
sudo useradd -u 2001 testuser
# Solaris
# The -u option serves the same purpose in Solaris' useradd command
useradd -u 2001 testuser
# AIX
# The mkuser command is used, and attributes are set directly
mkuser id=2001 pgrp=staff home=/home/testuser testuser
Explanation While RHEL and Solaris share a similar useradd syntax for assigning a specific UID, IBM AIX uses a different command, mkuser. In AIX, attributes like the UID are specified as key=value pairs directly in the command.
GIDs (Group Identifiers)
A GID is a unique numerical identifier for a user group in RHEL. Every user has a primary group, and can also be a member of multiple supplementary groups. GIDs are fundamental to the Linux security model, allowing access permissions to be managed for a collection of users at once.
Example 1: Creating a group with an automatic GID
# Add a new group named 'developers'
# The system automatically assigns the next available GID
sudo groupadd developers
Explanation The groupadd command creates a new user group. Without any special options, it assigns the next available GID from the range defined for system or user groups, ensuring the new group has a unique identifier.
Example 2: Creating a group with a specific GID
# Add a new group 'admins' with a specific GID of 2000
# The -g or --gid option is used to set the group ID
sudo groupadd -g 2000 admins
Explanation This command establishes a new group named 'admins' and uses the -g flag to assign it the specific GID of 2000. This practice is common in environments requiring consistent GIDs across different servers for shared resource access control.
Example 3: Changing a user's primary group
# Change the primary group for user 'jdoe' to 'developers'
# The -g or --gid option in usermod sets the primary group
sudo usermod -g developers jdoe
Explanation The usermod command with the -g flag modifies a user's primary group. In this instance, the primary group for the user 'jdoe' is changed to the 'developers' group. Any new files created by 'jdoe' will now be associated with the 'developers' GID by default.
Example 4: Adding a user to a supplementary group
# Add the user 'msmith' to the 'admins' and 'developers' groups
# -a (append) is used with -G (supplementary groups) to add without removing existing groups
sudo usermod -aG admins,developers msmith
Explanation This command adds the user 'msmith' to one or more supplementary groups without altering their primary group. The -aG flags are crucial; -a appends the user to the groups listed with -G, preventing them from being removed from other supplementary groups they already belong to.
Example 5: Finding a group's GID
# Display the GID for the 'developers' group
# The getent command retrieves entries from administrative databases
getent group developers | cut -d: -f3
Explanation The getent group command looks up the specified group in the system's group database (typically /etc/group). The output is then piped to the cut command, which uses a colon (:) as a delimiter to extract the third field, which is the numerical GID.
Example 6: Command variations for creating a group with a specific GID
# RHEL Linux
# Create a group 'data' with GID 3005
sudo groupadd -g 3005 data
# Solaris
# The syntax is identical to RHEL for this operation
groupadd -g 3005 data
# AIX
# AIX uses the mkgroup command and specifies the GID with the 'id' attribute
mkgroup id=3005 data
Explanation RHEL and Solaris use the same groupadd -g command structure to create a group with a designated GID. In contrast, AIX employs the mkgroup command, where the GID is set using the id attribute within the command itself.