The useradd command in Red Hat-based Linux distributions is used to create new user accounts on the system. It allows you to specify various parameters while creating a user account. Here’s a table with information and examples related to the useradd command:
| Parameter | Description | Example |
|---|---|---|
-c, --comment <comment> | Provide a comment or description for the user | useradd -c "John Doe" john |
-d, --home <directory> | Set the home directory for the user | useradd -d /home/john john |
-g, --gid <group> | Set the primary group for the user | useradd -g users john |
-G, --groups <groups> | Specify additional groups for the user (comma-separated list) | useradd -G developers,admins john |
-m, --create-home | Create the user’s home directory if it doesn’t exist | useradd -m john |
-s, --shell <shell> | Set the login shell for the user | useradd -s /bin/bash john |
-u, --uid <UID> | Set the user ID (UID) for the user | useradd -u 1001 john |
-p, --password <password> | Set the encrypted password for the user | useradd -p 'encrypted_password' john |
username | The username of the new user | useradd newuser |
Examples:
- Create a user named “john” with a comment:
Bash
useradd -c "John Doe" john- Create a user named “john” with a specific home directory:
Bash
useradd -d /home/john john- Create a user named “john” and assign them to the “users” primary group:
Bash
useradd -g users john- Create a user named “john” and add them to the “developers” and “admins” groups:
Bash
useradd -G developers,admins john- Create a user named “john” with a home directory and a default login shell:
Bash
useradd -m -s /bin/bash john- Create a user named “john” with a specific UID (user ID):
Bash
useradd -u 1001 john- Create a user named “john” with a pre-encrypted password:
Bash
useradd -p 'encrypted_password' john- Create a user named “newuser”:
Bash
useradd newuserPlease note that managing user accounts might require administrative privileges (sudo). Additionally, it’s a good practice to use the passwd command after creating a user to set their password interactively, as storing passwords in plain text or with reversible encryption is not recommended for security reasons.
| Command | Description | Example |
|---|---|---|
useradd | Create a new user account with specified parameters | useradd -c "John Doe" -m -s /bin/bash john |
usermod | Modify existing user account attributes | usermod -aG developers john |
userdel | Delete a user account and related files | userdel -r john |
Examples:
- useradd: Create a user named “john” with a comment, home directory, and default shell:
Bash
useradd -c "John Doe" -m -s /bin/bash john- usermod: Add user “john” to the “developers” group:
Bash
usermod -aG developers john- userdel: Delete the user “john” and remove their home directory and files:
Bash
userdel -r john