The “usermod” Command in Linux [14+ Practical Examples]

Sometimes, you need to change or modify the details of an existing user in the system. At that time, you can use the usermod command to modify those details. Here, In this article, I will describe all the features and uses of the usermod command in Linux with multiple examples.

A. Description

The usermod command is a useful tool to modify the attributes of an existing user. This command allows the root user or superuser to modify the user name, user ID, groups, home directory, password, user shell, expiry date, and other user details of an existing user. Generally, the usermod command provides the opportunity to modify the file like /etc/group, /etc/shadow, /etc/gshadow, /etc/login.def & /etc/passwd.

Note: You must be logged in as the root user or you must use the “sudo” command to execute the “usermod” command with root privileges.

B. Syntax

The syntax of the usermod command in Linux is pretty simple, as shown below.

usermod [OPTION]... USER

Note: In the syntax above, the OPTION enclosed by the square bracket means it is not mandatory and three dots after the square bracket mean multiple OPTIONS can be used after the usermod command.

C. Options

There are numerous options available for the usermod command. In the section below, I have listed a few of them. You can learn more about the usermod command, its options and their uses by checking the man page.

man usermod

Useful Options

  • -a, –append: Use toadd the user to the one or more supplementary group. You can Use it only with the -G
  • -b, –badnames: Permit non-compliant names.
  • -c, –comment:Use to add comment field for the user account.
  • -d, –home:Use to modify the login directory for any existing user account.
  • -e, –expiredate:Use to set account expiry date. The date is specified in the format YYYY-MM-DD.
  • -f, –inactive:The number of days after a password expires until the account is permanently disabled.
  • -g, –gid:Change the primary group for a User.
  • -G, –groups:Use To add supplementary groups.
  • -l, –login:To change the login name from user_name to new_user_name.
  • -L, –lock:To lock the user’s password.
  • -m, –move-home:moving the contents of the home directory from the existing home directory to the new directory.
  • -o, –non-unique:This option allows to change the user ID to a non-unique value.
  • -p, –password: Allows to specify new un-encrypted password.
  • -R, –root CHROOT_DIR: Apply changes in the CHROOT_DIR directory and use the configuration files from the CHROOT_DIR directory.
  • -s, –shell:Specify the user’s new login shell.
  • -u, –uid: Specify the new numerical value of the user’s ID.
  • -U, –unlock:Unlock a user’s password.

Note: Commands and their options are case-sensitive in Linux. So, be careful while using them.

Practical Examples of the “usermod” Command in Linux

In Linux, the usermod command is a helpful tool that allows superuser or root user to modify the user name, user ID, groups, home directory, password, user shell, expiry date, and other user details of an existing user. In the section below, I will show you some of the most useful applications for the usermod command in Linux.

Example 1: Rename the User With the “usermod” Command in Linux

The usermod command in Linux allows you to modify an existing user name by simply running the usermod command followed by the option -l. Here, I will modify a user name from “mary” having a UID 2031 to “lisa” with the usermod command. You can do the same by following the steps below.

Steps to follow >

➊ At first, open the Ubuntu Terminal.

 Run the command below:

id mary

➌ Now, press the ENTER key.

You will receive Mary’s UID, GID and a list of the groups she is a member of, as shown in the image below.Showing Mary's UID, GID and a list of the groups she belongs to.➍ Execute the command below to rename.

sudo usermod -l lisa mary

Alternatively,

sudo usermod --login lisa mary

➎ Type your password.

➏ Tap ENTER again.

➐ Now, type the following command into the command prompt:

id lisa mary

➑ Hit ENTER button.

Output >

In the following image, you can see that user mary has been renamed to lisa with the previous UID 2031.Showing that the user mary is renamed to lisa.


Similar Readings


Example 2: Modify the User UID With the “usermod” Command in Linux

You can modify the UID of any user by running the usermod command with the option –uid or -u. In this case, I’ll modify lisa‘s UID (currently 2031) and assign UID to 2040 using the usermod command. By using the steps listed below, you can achieve the same results.

Steps to follow >

➊ Open the Ubuntu Terminal first.

 Type the following command into the command prompt

id lisa

In the following image, you can see the IDs of the user “lisa” including UID which is 2031.Showing the IDs of the user “lisa” including UID which is 2031.➌ Now execute the command below to alter its UID.

sudo usermod --uid 2040 lisa

Alternatively,

sudo usermod -u 2040 lisa

➍ Tap the ENTER button.

➎ Type your Password.

➏ Hit ENTER again.

➐ You can check the UID by typing the following command.

id lisa

➑ Now, press ENTER key

Output >

You can see in the image below that I have modified the UID of lisa with the UID 2040“.Showing that the UID of lisa is replaced with the UID "2040".

Example 3: Modify the User UID With a Non-Unique Value

The UID needs to be an individual, non-negative number. However, you can create users with duplicate (non-unique) UIDs by using the -o or –non-unique option. Here, I will replace the UID of “lisa with the same UID as “john” using the usermod command. You can do the same by following the steps below.

Steps to Follow >

➊ Launch the Ubuntu Terminal first.

 Type the following command into the command prompt:

id lisa john

➌ Press the ENTER button.

In the following image, you can see that the users named lisa and john have UID values of 2040 and 2028, respectively.Showing the IDs of users named lisa and john.➍ Now, enter the following command into the command prompt:

sudo usermod -o -u 2028 lisa

Alternatively,

sudo usermod --non-unique --uid 2028 lisa

➎ Type your Password.

➏ Press ENTER again.

➐ You can check the GID by typing the following command.

id lisa john

➑ Press the ENTER key.

Output >

In the image below, you can see that I have changed the UID of lisa with the same UID as john.Showing that the UID of user Lisa has been changed with the same UID as john using usermod command in linux.

Note: As the user with UID 2028 is a member of group john with GID 2028. So, the group of lisa also turned into jhon while I changed the UID.

Example 4: Modify the User GID With the “usermod” Command in Linux

You can modify the GID of any user by running the usermod command with the option –gid or -g. In this case, I’ll modify john‘s GID (currently 2028) and assign GID to 2040 using the usermod command. By using the steps listed below, you can achieve the same results.

Steps to follow >

➊ Open the Ubuntu Terminal first.

 Type the following command into the command prompt

id jhon

In the following image, you can see the IDs of john including GID which is 2028.Showing the IDs of john.➌ Now execute the command below to alter its GID.

sudo usermod --gid 2040 john

Alternatively,

sudo usermod -g 2040 john

➍ Tap the ENTER button.

➎ Type your Password.

➏ Hit ENTER again.

➐ You can check the UID by typing the following command.

id john

➑ Now, press ENTER key

Output >

You can see in the image below that I have modified the GID of john with the GID 2040“.Showing that the GID of john has been changed using usermod command in linux.

Example 5: Add a User to Secondary Groups Using “usermod” Command in Linux

The usermod command allows you to add a user to one or more secondary groups. For adding a user to secondary groups you have to use option -G after the usermod command. Now, I will add the user named john to multiple groups named group4(GID 2029), group5(GID 2030) and group6(GID 2031). You will able to do this if you follow the steps below.

Steps to Follow >

➊ Launch the Ubuntu Terminal.

 Type the following command into the command prompt:

sudo usermod -G 2029,2030,2031 john

Alternatively,

sudo usermod --groups 2029,2030,2031 john

➌ Now, tap the ENTER button.

➍ Type your Password.

➎ Tap ENTER again.

➏ Now type the following command into the command prompt:

id john

➐ Press ENTER key.

Output >

You can see in the image below that I have added user john to group4, group5, group6.Showing that the user jhon is added to some secondary group using usermod command in linux.

Note: A primary group in Linux is the group to which a user belongs initially, and it is used to specify the group ownership of files and directories that the user creates. In addition to their primary group, users can also be a part of other groups, which are referred to as supplementary groups or secondary groups. These groups are used to grant additional permissions or access to resources.

Example 6: Append a User to Secondary Groups Along With the Existing Groups

In the previous example, I have shown you the process to add a user to secondary groups. Now, If I try to add user jhon to other secondary groups only with option -G then the previous secondary groups will disappear. So, to add john to other secondary groups without overwriting others, you have to use option -a or –append along with the option -G or –groups. In this section, I will show the process to add john to a new group named group8(GID 2033) without losing the membership of group4, group5 and group6. You can do the same by following the steps below.

Steps to Follow >

➊ Firstly, open the Ubuntu Terminal.

 Execute the following command

sudo usermod -a -G 2033 john

Alternatively,

sudo usermod --append --groups 2033 john

➌ Now, tap the ENTER button.

➍ Give your Password.

➎ Press ENTER again.

➏ Now type the following command into the command prompt:

id john

➐ Hit ENTER key.

Output >

In the following image, you can see that the user john has become a member of group8 without losing the membership of previous secondary groups.Showing that the user john is append to a new group without loosing the membership of previous groups using usermod command in linux.

Example 7: Adding Information to User Account With “usermood” Command in Linux

The usermod command allows you to add information to a user account in the form of a comment. For adding information to a user account option -c or –comment after the usermod command. At this point, I will add a comment to the user named john. You will able to do the same if you follow the steps below.

Steps to Follow >

➊ Launch the Ubuntu Terminal.

 Type the following command into the command prompt:

sudo usermod -c "this is john" john

Alternatively,

sudo usermod --comment "this is john" john

➌ Now, tap the ENTER button.

➍ Type your Password.

➎ Tap ENTER key again.

➏ Now type the following command into the command prompt:

grep john /etc/passwd

➐ Press ENTER key.

Output >

In the following image, you can see that I have added information about the user john in the form of a comment.Showing that a information is added to user john as a comment.

Example 8: Change the User Home Directory With the “usermod” Command in Linux

You can use the usermod command in Linux to change the current home directory of a user. To change the current home directory, -d or –home is used after the usermod command. In this example, I will change the home directory of the user james(/home/james) and use the home directory of user david(/home/david) as james’s home directory. You can check the home directory of users david and james by executing the command below.

getent passwd david james

Showing the user details of user david and james.You can also change the home directory of any user by following the steps below.

Steps to Follow >

➊ Open the Ubuntu Terminal.

 Execute the following command

sudo usermod -d /home/david james

Alternatively,

sudo usermod --home /home/david james

➌ Now, tap the ENTER button.

➍ Type your Password.

➎ Press ENTER again.

➏ Now type the following command into the command prompt:

getent passwd james

➐ Tap ENTER key.

Output >

You can see in the image that the home directory of user james has changed to /home/david from /home/james.The home directory of user james has been changed using usermod command in linux.

Example 9: Move the Content of the User’s Home Directory Using “usermod” Command in Linux

When you change the home directory of any user, the content of the previous home directory does not shift to the new home directory. So, if you want to shift a user’s home directory along with the content of the home directory, then you have to use the -m or –move-home option after the usermod command. In this example, I will move the home directory of user Lisa, whose current home directory is at /home/Lisa, to the directory /home/amdadul/Public along with the existing content of /home/Lisa. To check the home directory of user Lisa run the command below.

getent passwd Lisa

Then you can see the terminal as the image shown below.Showing the user details of user Lisa.If you want to move any user’s home directory along with the contents, then follow the steps listed below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

 Run the command below:

sudo usermod -d /home/amdadul/Public -m Lisa

Alternatively,

sudo usermod --home /home/amdadul/Public --move-home Lisa

➌ Now, press the ENTER key.

➍ Type your password.

➎ Tap ENTER again.

➏ Now, type the following command into the command prompt:

getent passwd Lisa

➐ Hit ENTER button.

Output >

In the following image, you can see that I have moved the home directory of the user Lisa from /home/Lisa to /home/amdadul/Public.Showing that the home directory of user Lisa has been moved using usermod command in linux.

Example 10: Change User Login Shell With the “usermod” Command in Linux

The usermod command allows you to change the login shell of any user. To change the login shell of any user using the usermod command, you have to use the option -s or –shell. In this section, I will change the login shell of user Lisa, Whose current login shell is /bin/sh shown in the image below.Showing the user details of user Lisa.To perform the same thing follow the steps below.

Steps to Follow >

➊ Open the Ubuntu Terminal.

 Type the following command into the command prompt:

sudo usermod -s /bin/bash Lisa

Alternatively,

sudo usermod --shell /bin/bash Lisa

➌ Now, tap the ENTER button.

➍ Type your Password.

➎ Tap ENTER key again.

➏ Now type the following command into the command prompt:

getent passwd Lisa

➐ Press ENTER key.

Output >

In the image below, you can see that I have changed Lisa’s login shell from /bin/sh to /bin/bash.Showing that the user login shell for Lisa has been changed using usermod command in linux.

Example 11: Modify User Account Expiry Date With the “usermod” Command in Linux

The usermod command allows you to modify the expiry date of a user account. In this section, I will modify the expiry date of user Lisa whose user details I obtained by running the command below.

sudo chage -l Lisa

The result I got by running the command above is shown below.Showing the account details of user Lisa.To modify the expiration date of a user account you can follow the steps below.

Steps to Follow >

➊ Launch the Ubuntu Terminal.

 Type the following command into the command prompt:

sudo usermod -e 2023-02-22 Lisa

Alternatively,

sudo usermod --expiredate 2023-02-22 Lisa

➌ Now, tap the ENTER button.

➍ Provie your Password.

➎ Tap ENTER key again.

➏ Now type the following command into the command prompt:

sudo chage -l Lisa

➐ Press ENTER key.

Output >

In the following image, you can see that I have modified the expiration date of the user account ‘Lisa’ from having no expiration date to Jan 22, 2023.Showing that the expire date is modified with usermod command in linux.

Example 12: Update the User Password Using the “usermod” Command in Linux

The usermod command provides modification permission for a user’s password. That means you can set a new password for a user or change the existing password of a user by using the usermod command. In the section below, I will discuss both cases.

Case A: Set Password of a User Without Having One

In this section, I will show you the process of setting a password for user Lisa, who has no password. You can see the current password status in the image below by running the command sudo grep Lisa /etc/shadow.Showing that the user Lisa has no password.I am providing the steps to set a new password for Lisa in the section below and you can do the same by following the steps below.

Steps to Follow >

➊ First, open the Ubuntu Terminal.

 Execute the following command

sudo usermod -p abcdefg123 Lisa

Alternatively,

sudo usermod --password abcdefg123 Lisa

➌ Now, tap the ENTER button.

➍ Input your Password.

➎ Press ENTER again.

➏ Now type the following command into the command prompt:

sudo grep Lisa /etc/shadow

➐ Tap ENTER key.

Output >

You can see in the image that I have set up a password for user Lisa.Showing that a password has been set for User Lisa using usermod command in linux.

Case B: Change the Existing Password of a User

Setting up a new password and changing process of the existing password is almost the same. In this section, I will show you the process of changing a password for user Lisa, whose current password is abcdefg123. You can see the current password status in the image below by running the command sudo grep Lisa /etc/shadow.Showing user Lisa's current password.I am providing the steps to change an existing password for Lisa in the section below and you can do the same by following the steps below.

Steps to Follow >

➊ Open the Ubuntu Terminal, first.

 Run the following command

sudo usermod -p hijklm123 Lisa

Alternatively,

sudo usermod --password hijklm123 Lisa

➌ Now, hit the ENTER button.

➍ Type your Password.

➎ Press ENTER again.

➏ Now type the following command into the command prompt:

sudo grep Lisa /etc/shadow

➐ Tap ENTER key.

Output >

You can see in the image that I have changed the existing password abcdefg123 to hijklm123  for user Lisa.Showing that the previous password of user Lisa has been changed using usermod command in linux.

Example 13: Lock User Account Using the “usermod” Command in Linux

The usermod command allows you to lock an account. You can lock an account with the usermod command followed by the option -L or –lock. Now, I will show you the process of locking a user account. You can do the same by following the steps below.

Steps to Follow >

➊ Launch the Ubuntu Terminal.

 Execute the following command

sudo usermod -L Lisa

Alternatively,

sudo usermod --lock Lisa

➌ Now, tap the ENTER button.

➍ Input your Password.

➎ Press ENTER again.

➏ Now type the following command into the command prompt:

sudo grep Lisa /etc/shadow

➐ Tap ENTER key.

Output >

In the image, the “!” sign after the name of the account Lisa illustrates that this account is locked.Showing that the account of user Lisa is locked.

Example 14: Unlock User Account Using the “usermod” Command in Linux

In the previous section, I have shown you the process of locking the account of user Lisa. In this section, I will unlock that account with the usermod command follower by the option -U or –unlock. Now, I will show you the process of unlocking the user account ‘Lisa’. You can do the same by following the steps below.

Steps to Follow >

➊ Open the Ubuntu Terminal.

 Run the following command

sudo usermod -U Lisa

Alternatively,

sudo usermod --unlock Lisa

➌ Now, tap the ENTER button.

➍ Provide your Password.

➎ Press ENTER again.

➏ Now type the following command into the command prompt:

sudo grep Lisa /etc/shadow

➐ Tap ENTER key.

Output >

In the following image, You can see that there is no “!” sign after the name of the account Lisa which illustrates that this account is being unlocked.Showing that the user account of user Lisa is unlocked using usermod command in linux.

Conclusion

In this article, I’ve discussed the usermod command which is useful to modify user name, user ID, groups, home directory, password, user shell, expiry date, and other user details of an existing user. At the same time, I’ve discussed its options and provided some relevant examples. Undoubtedly, this article will help you better understand the usermod command and its use. Consequently, it will make your journey easier to become a power user of the Linux command line.


Similar Readings

Rate this post
S. M. Amdadul Islam

Hello everyone. I am S. M. Amdadul Islam, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Naval Architecture and Marine Engineering (NAME) graduate from the Bangladesh University of Engineering and Technology. In addition to my regular work, I enjoy watching Youtube, discovering new things, gossiping with friends, visiting new places, and taking photos of landscapes. Read Full Bio

Leave a Comment