Overview of usermod
`usermod` command modifies user account information created with `useradd` in system files such as `/etc/passwd`, `/etc/shadow`, `/etc/group`, `/etc/gshadow`. Changing account information while a user is logged in can cause issues, so ensure that the user you want to modify is logged out or proceed with caution. All these processes require administrative privileges (`sudo`).
Key Roles of usermod
`usermod` command is primarily used for the following purposes:
Major Use Cases
- Modify User Information: Changes the login name, home directory, shell, etc., of an existing user account.
- Manage Group Membership: Adds users to new groups or removes them from existing groups.
- Adjust Account Status: Sets the expiration date of an account or locks and unlocks the account.
- Change UID/GID: Changes the user's unique ID (UID) or primary group ID (GID). (Requires great caution)
useradd vs usermod
`useradd` is used to 'create' a new account, while `usermod` is used to 'modify' the properties of an already existing account.
Key usermod Command Options
`usermod` command offers a wide range of options to change various account properties of users.
1. Change Account Name and ID
2. Change Group Membership
3. Change Home Directory and Shell
4. Account Status and Others
Generated command:
Try combining the commands.
Description:
`usermod` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Learn how to efficiently change and manage the properties of existing user accounts through various examples of the `usermod` command.
Change Username
sudo usermod -l newuser -m -d /home/newuser olduser
Changes the login name of `olduser` to `newuser`. The account information and home directory name will be changed together.
Add User to 'sudo' Group
sudo usermod -aG sudo devuser
Adds the `devuser` user to the `sudo` group to enable administrative privileges. Existing group memberships are retained.
Change User's Default Shell
sudo usermod -s /bin/zsh testuser
Changes the default login shell of `testuser` to `/bin/zsh`.
Lock User Account
sudo usermod -L guestuser
Locks the `guestuser` account to prevent further logins. This is useful when you want to disable an account without deleting it.
Unlock User Account
sudo usermod -U guestuser
Reactivates the previously locked `guestuser` account to allow logins.
Set User Account Expiration Date
sudo usermod -e 2025-12-31 tempuser
Sets the `tempuser` account to automatically expire on December 31, 2025.
Change User's All Supplementary Groups (Remove Existing Groups)
sudo usermod -G developers john
Ensures that `john` belongs only to the `developers` group, removing all other previously associated supplementary groups.