The Wayback Machine - https://web.archive.org/web/20250108185131/https://www.geeksforgeeks.org/usermod-command-in-linux-with-examples/
Open In App

usermod command in Linux with Examples

Last Updated : 19 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

usermod command or modify user is a command in Linux that is used to change the properties of a user in Linux through the command line. After creating a user we have to sometimes change their attributes like password or login directory etc. so in order to do that we use the Usermod command. The information of a user is stored in the following files: 

  • /etc/passwd
  • /etc/group
  • /etc/shadow
  • /etc/login.defs
  • /etc/gshadow
  • /etc/login.defs

When we execute usermod command in terminal the command make the changes in these files itself. 

Note: usermod command needs to be executed only as a root user. 

Working with usermod command

1. To add a comment for a user 

sudo usermod -c "This is test user" test_user

to-add-a-comment-usermod

This will add a comment about the user or a short description related to the user. 

2. To change the home directory of a user 

sudo usermod -d /home/manav test_user

to-change-home-directory

This will change the home directory of the user to /home/manav. 

3. To change the expiry date of a user 

sudo usermod -e 2020-05-29 test_user

change-expiry-date

This will change the expiration date of account “test_user” 

4. To change the group of a user 

sudo usermod -g manav test_user

change-group-of-user

This command will now change the group of test user from test_user to manav 

5. To change user login name 

sudo usermod -l test_account test_user

to-change-user-login

This will now change the login name of the user “test_user”. 

6. To lock a user 

sudo usermod -L test_user

lock-and-unlock-a-user

This will lock the “test_user” account and will display a! sign in shadow file before the username 

7. To unlock a user 

sudo usermod -U test_user

lock-and-unlock-a-user

This will unlock the “test_user” which was locked by the previous command 

8. To set an unencrypted password for the user 

sudo usermod -p test_password test_user

to-set-unencrypted-password

This will set the password “test_password” in the unencrypted form for the user “test_user” 

9. To create a shell for the user 

sudo usermod -s /bin/sh test_user

create-shell-for-user

This command will now create a shell for the user “test_user” from /bin/sh 

10. To change the user id of a user 

sudo usermod -u 1234 test_user

change-uid-for-user

This command will change the user id of “test_user” to 1234

usermod command in Linux with Examples – FAQs

What is the usermod command used for in Linux?

The usermod command in Linux is a utility for modifying a user’s account details. This versatile command allows system administrators to change settings such as the user’s login name, home directory, and shell, or manage group memberships and other account properties. It’s a vital tool for managing user configurations in a multi-user environment.

How to change a user’s home directory with usermod?

To change a user’s home directory, the usermod command is used with the -d option followed by the new directory path, and typically the -m option, which moves the content from the user’s current directory to the new one. For example:

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

This command sets /new/home/dir as the new home directory for username and moves the existing contents of the user’s previous home directory to the new location.

How to lock or unlock a user account using usermod?

To lock a user account, usermod can be used with the -L option. Locking an account disables a user’s ability to log in but preserves the user’s data and settings:

sudo usermod -L username

To unlock a previously locked account, use the -U option:

sudo usermod -U username

These commands make it easy to enable or disable user access without modifying other account details.

How to add a user to a group with usermod?

To add a user to a new group while preserving their existing group memberships, use the -a (append) and -G options followed by the group name. For example:

sudo usermod -aG groupname username

This command adds the user username to the group groupname without affecting their membership in other groups. If you omit -a, the user will be removed from all groups except the ones specified after -G.

What are some common options for the usermod command?

  • -c, –comment COMMENT: Set the new value of the GECOS field. This field usually contains user information like full name.
  • -l, –login NEW_LOGIN: Change the user login name. This can be critical when the user’s name changes for any reason.
  • -L, –lock: Lock the user account, making the user’s password not valid.
  • -U, –unlock: Unlock the user account, restoring access with the existing password.
  • -s, –shell SHELL: Change the user’s login shell. This is useful if you need to restrict or change the user’s command line interface.
  • -u, –uid UID: Set a new user ID for the user account. This must be used with caution as it affects file ownership.
  • -G, –groups GROUP1,GROUP2,…: Set a new list of supplementary groups for the user, replacing any existing memberships unless -a is also used.


Next Article

Similar Reads

three90RightbarBannerImg