How to Modify a User in Linux – usermod
Command
In Linux, user accounts can be modified using the usermod
command. This is useful for updating user details like usernames, home directories, group memberships, and login settings.
Syntax of usermod
Common usermod
Options
Option | Description |
---|---|
-l newname | Change the username |
-d /new/home | Change the home directory |
-m | Move home directory contents to the new location |
-g groupname | Change the primary group |
-G group1,group2 | Add the user to additional groups |
-aG groupname | Append a user to a group (without removing existing groups) |
-L | Lock the user account |
-U | Unlock the user account |
-s /bin/shell | Change the default shell |
-e YYYY-MM-DD | Set account expiration date |
Examples
1. Change Username
Example:
This renames john
to john_doe
.
2. Change User Home Directory
Example:
This moves john
’s home directory to /home/johndoe
.
3. Change Primary Group
Example:
This sets developers
as Alice’s primary group.
4. Add User to a Secondary Group
Example:
Now, Alice is part of the sudo
group.
5. Change User Login Shell
This sets Alice’s shell to zsh
.
6. Lock a User Account
This locks alice
, preventing login.
To unlock:
7. Set an Account Expiration Date
Alice’s account will expire on December 31, 2025.
Check User Information
To verify user details:
Example:
Or check full user details:
Difference Between usermod
, passwd
, and chage
Command | Purpose |
---|---|
usermod | Modify user account details. |
passwd | Change user password. |
chage | Manage password expiration policies. |
Conclusion
The usermod
command is essential for managing users in Linux, allowing administrators to modify account settings efficiently.
Would you like additional details or SEO optimization? 🚀