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

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.

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

usermod [OPTION]... USER

Options of “usermod” Command

There are numerous options available for the usermod command. You can learn more about the usermod command, its options and their uses by checking the man page.

man usermod
Options Use
-a, –append Add 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 Add a comment field for the user account.
-d, –home Modify the login directory for any existing user account.
-e, –expiredate Set account expiry date. The date is specified in the format YYYY-MM-DD.
-f, –inactive Permanently disable the number of days after a password expires until the account.
-g, –gid Change the primary group for a User.
-G, –groups Add supplementary groups.
-L, –lock Lock the user’s password.
-m, –move-home Move the contents of the home directory from the existing home directory to the new directory.
-o, –non-unique Change the user ID to a non-unique value.
-l, –login Change the login name from user_name to new_user_name.
-p, –password Specify new unencrypted password.
-R 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.

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

The usermod command in Linux allows you to modify an existing user name by simply running the usermod command followed by the option -l. Follow the below example:

  1. At first, open the Ubuntu Terminal.
  2.  Run the command below:
    id mary

    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.

  3. Execute the command below to rename.
    sudo usermod -l lisa mary

    Alternatively,

    sudo usermod --login lisa mary
  4. Type your password.
  5. Tap ENTER again.
  6. Now, run the following command into the command prompt:
    id lisa mary

    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

To modify the UID of any user by running the usermod command with the option –uid or -u. Follow the below example:

  1. Open the Ubuntu Terminal first.
  2.  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.

  3. Now execute the command below to alter its UID.
    sudo usermod --uid 2040 lisa

    Alternatively,

    sudo usermod -u 2040 lisa
  4. Tap the ENTER button.
  5. Type your Password.
  6. You can check the UID by typing the following command.
    id lisa

    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 createusers with duplicate (non-unique) UIDs by using the-o or --non-unique option. Let’s see how:

  1. Launch the Ubuntu Terminal first.
  2.  Execute the following command into the command prompt:
    id lisa john

    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.

  3. Now, enter the following command into the command prompt:
    sudo usermod -o -u 2028 lisa

    Alternatively,

    sudo usermod --non-unique --uid 2028 lisa
  4. Type your Password.
  5. You can check the GID by typing the following command.
    id lisa john

    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

You can modify the GID of any user by running the usermod command with the option --gid or -g. Here’s how:

  1. Open the Ubuntu Terminal first.
  2.  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.

  3. Now execute the command below to alter its GID.
    sudo usermod --gid 2040 john

    Alternatively,

    sudo usermod -g 2040 john
  4. You can check the UID by typing the following command.
    id john

    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

To add a user to secondary groups you have to use option usermode -G command. Follow the steps below.

  1. Launch the Ubuntu Terminal.
  2.  Run the following command into the command prompt:
    sudo usermod -G 2029,2030,2031 john

    Alternatively,

    sudo usermod --groups 2029,2030,2031 john
  3. Now type the following command into the command prompt:
    id john

    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

To add a user to other secondary groups without overwriting others, you have to use option -a or --append along with the option -G or --groups. Here’s how:

  1. Firstly, open the Ubuntu Terminal.
  2.  Execute the following command
    sudo usermod -a -G 2033 john

    Alternatively,

    sudo usermod --append --groups 2033 john
  3. Now use the following command:
    id john

    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

To add information to a user account option -c or --comment after the “usermod” command. Follow the steps below.

  1. Launch the Ubuntu Terminal.
  2.  Type the following command into the command prompt:
    sudo usermod -c "this is john" john

    Alternatively,

    sudo usermod --comment "this is john" john
  3. Now type the following command into the command prompt:
    grep john /etc/passwd

    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

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. Here’s how:

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.

  1. Open the Ubuntu Terminal.
  2.  Execute the following command
    sudo usermod -d /home/david james

    Alternatively,

    sudo usermod --home /home/david james
  3. Now type the following command into the command prompt:
    getent passwd james

    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

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. Here’s how:

getent passwd Lisa

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

  1. At first, open the Ubuntu Terminal.
  2.  Run the command below:
    sudo usermod -d /home/amdadul/Public -m Lisa

    Alternatively,

    sudo usermod --home /home/amdadul/Public --move-home Lisa
  3. Now, type the following command into the command prompt:
    getent passwd Lisa

    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

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 operation, follow the steps below.

  1. Open the Ubuntu Terminal.
  2.  Type the following command into the command prompt:
    sudo usermod -s /bin/bash Lisa

    Alternatively,

    sudo usermod --shell /bin/bash Lisa
  3. Now type the following command into the command prompt:
    getent passwd Lisa

    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.

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.

  1. Launch the Ubuntu Terminal.
  2.  Type the following command into the command prompt:
    sudo usermod -e 2023-02-22 Lisa

    Alternatively,

    sudo usermod --expiredate 2023-02-22 Lisa
  3. Now type the following command into the command prompt:
    sudo chage -l Lisa

    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

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

Let’s sees the process of setting a password for user:Showing that the user Lisa has no password.

  1. First, open the Ubuntu Terminal.
  2.  Execute the following command
    sudo usermod -p abcdefg123 Lisa

    Alternatively,

    sudo usermod --password abcdefg123 Lisa
  3. Now type the following command into the command prompt:
    sudo grep Lisa /etc/shadow

    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

Follow the steps to change an existing password for a user in the section below:

  1. Open the Ubuntu Terminal, first.
  2.  Run the following command
    sudo usermod -p hijklm123 Lisa

    Alternatively,

    sudo usermod --password hijklm123 Lisa
  3. Now type the following command into the command prompt:
    sudo grep Lisa /etc/shadow

    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

The usermod command allows you to lock an account by the option-L or --lock:

  1. Launch the Ubuntu Terminal.
  2.  Execute the following command
    sudo usermod -L Lisa

    Alternatively,

    sudo usermod --lock Lisa
  3. Now type the following command into the command prompt:
    sudo grep Lisa /etc/shadow

    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

To unlock a user account, use the usermod command follower by the option -U or --unlock. Here’s how:

  1. Open the Ubuntu Terminal.
  2.  Run the following command
    sudo usermod -U Lisa

    Alternatively,

    sudo usermod --unlock Lisa
  3. Now type the following command into the command prompt:
    sudo grep Lisa /etc/shadow

    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