useradd (create a new user)
The useradd command is a low-level utility for creating new user accounts in RHEL and other Linux distributions. It allows system administrators to define various properties for the new user, such as their home directory, login shell, and group memberships. Proper use of useradd is fundamental to multi-user system administration.
Example 1: Create a basic user account
# Create a new user named 'jdoe' with default settings
sudo useradd jdoe
Explanation
This is the simplest form of the command. It creates a new user jdoe with a home directory in /home/jdoe, a default shell, and a primary group with the same name as the user. The sudo command is used to execute the command with root privileges.
Example 2: Create a user with a specific home directory
# Create a user 'msmith' with a custom home directory
sudo useradd -d /srv/webapps/msmith msmith
Explanation
The -d option allows you to specify a custom path for the user's home directory. This is useful when you need to store user data in a non-standard location, such as a separate partition for web application files.
Example 3: Create a user and assign a primary group
# Create user 'asmith' and assign them to the 'developers' primary group
sudo useradd -g developers asmith
Explanation
With the -g option, you can set the user's primary group. The user's initial login group will be developers. This is essential for managing file permissions based on group ownership.
Example 4: Create a user with multiple supplementary groups
# Create user 'rjohnson' and add them to the 'ftp' and 'web' groups
sudo useradd -G ftp,web rjohnson
Explanation
The -G option assigns supplementary groups to the user. This grants the user permissions associated with those groups, in addition to their primary group. Multiple groups are separated by commas without spaces.
Example 5: Create a user with a specific login shell
# Create user 'guest' with the nologin shell to prevent interactive login
sudo useradd -s /sbin/nologin guest
Explanation
The -s option sets the user's login shell. Assigning /sbin/nologin is a common security practice for service accounts or users who should not have shell access, as it prevents them from logging into the system interactively.
Example 6: Create a user with a comment (GECOS)
# Create a user 'llee' and add a descriptive comment
sudo useradd -c "Laura Lee, Marketing Department" llee
Explanation
The -c option adds a comment to the user's account, typically their full name or department. This information is stored in the GECOS field of the /etc/passwd file and can be useful for identifying users.
Example 7: Create a system user
# Create a system user 'apache' without a home directory
sudo useradd -r apache
Explanation
The -r flag creates a system user. These users are intended for running services and daemons, and they typically have UIDs below 1000 and do not have a home directory created by default.
Example 8: Create a user with a specific UID
# Create a user 'testuser' with a specific User ID (UID) of 1010
sudo useradd -u 1010 testuser
Explanation
The -u option allows you to specify a unique User ID (UID) for the new user. This is useful in environments where UIDs need to be consistent across multiple systems for things like NFS shares.
UNIX Variants
Solaris: The command is the same: useradd.
FreeBSD: The command is adduser, which is an interactive script, or pw useradd for a direct, non-interactive command similar to RHEL's useradd.