usermod command in Linux with Examples

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 infomration 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 temrinal 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 usermode -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

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.