This page covers commands for managing users and groups on your Rocky Linux system.
31. useradd
- Create a New User Account
Purpose: The useradd
command is used to create a new user account on the system. It creates an entry in the system account files and optionally creates a home directory and copies skeleton files.
Syntax: sudo useradd [OPTIONS] USERNAME
Key Options:
-m
: (create home directory) Creates the user's home directory if it doesn't exist.-s SHELL
: Specifies the user's login shell (e.g.,/bin/bash
,/bin/sh
).-g GROUP
: Specifies the primary group for the new user.-G GROUP1,GROUP2
: Adds the user to supplementary groups.-c "COMMENT"
: Adds a comment for the user (e.g., full name).
Examples:
sudo useradd -m newuser sudo useradd -m -s /bin/bash -c "John Doe" johndoe sudo useradd -m -g developers -G sudo,webdev jane
32. passwd
- Change User Password
Purpose: The passwd
command is used to change a user's password. Users can change their own password, while the root user can change any user's password.
Syntax: passwd [OPTIONS] [USERNAME]
Examples:
passwd # Change your own password sudo passwd root # Change the root user's password sudo passwd newuser # Set or change password for 'newuser'
33. usermod
- Modify a User Account
Purpose: The usermod
command is used to modify an existing user account. You can change a user's home directory, shell, group memberships, and more.
Syntax: sudo usermod [OPTIONS] USERNAME
Key Options:
-l NEW_LOGIN
: Change the user's login name.-d HOME_DIR
: Change the user's home directory.-m
: (move home directory) Moves the content of the current home directory to the new location. Used with-d
.-s SHELL
: Change the user's login shell.-aG GROUP
: Add the user to a supplementary group (-a
appends,-G
specifies groups).
Examples:
sudo usermod -l newname oldname sudo usermod -d /home/newhome -m username sudo usermod -s /bin/zsh myuser sudo usermod -aG admin,developers alice
34. userdel
- Delete a User Account
Purpose: The userdel
command is used to delete a user account from the system.
Syntax: sudo userdel [OPTIONS] USERNAME
Key Options:
-r
: (remove home directory) Removes the user's home directory and mail spool along with the account.
Examples:
sudo userdel olduser sudo userdel -r unusedaccount
35. groupadd
- Create a New Group
Purpose: The groupadd
command is used to create a new group on the system.
Syntax: sudo groupadd [OPTIONS] GROUPNAME
Examples:
sudo groupadd developers sudo groupadd web_admins