Modify User in Linux

Modify User in Linux

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

usermod [OPTIONS] username

Common usermod Options

OptionDescription
-l newnameChange the username
-d /new/homeChange the home directory
-mMove home directory contents to the new location
-g groupnameChange the primary group
-G group1,group2Add the user to additional groups
-aG groupnameAppend a user to a group (without removing existing groups)
-LLock the user account
-UUnlock the user account
-s /bin/shellChange the default shell
-e YYYY-MM-DDSet account expiration date

Examples

1. Change Username

sudo usermod -l newuser olduser

Example:

sudo usermod -l john_doe john

This renames john to john_doe.

2. Change User Home Directory

sudo usermod -d /new/home/directory -m username

Example:

sudo usermod -d /home/johndoe -m john

This moves john’s home directory to /home/johndoe.

3. Change Primary Group

sudo usermod -g newgroup username

Example:

sudo usermod -g developers alice

This sets developers as Alice’s primary group.

4. Add User to a Secondary Group

sudo usermod -aG groupname username

Example:

sudo usermod -aG sudo alice

Now, Alice is part of the sudo group.

5. Change User Login Shell

sudo usermod -s /bin/zsh alice

This sets Alice’s shell to zsh.

6. Lock a User Account

sudo usermod -L alice

This locks alice, preventing login.

To unlock:

sudo usermod -U alice

7. Set an Account Expiration Date

sudo usermod -e 2025-12-31 alice

Alice’s account will expire on December 31, 2025.

Check User Information

To verify user details:

id username

Example:

id alice

Or check full user details:

getent passwd alice

Difference Between usermod, passwd, and chage

CommandPurpose
usermodModify user account details.
passwdChange user password.
chageManage 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? 🚀

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close