How to use usermod command in Linux?

Use

usermod command in linux is used to modify an existing user’s account. But, only the superuser can use this command.

Syntax

– usermod [options] LOGIN

Options available with usermod command in linux

1. Add Supplementary Groups

-a : works in combination with -G option. Refer -G option for details

2. Add value to the Comment/GECOS field

-c : adds a value to the comment or the so-called GECOS filed in the /etc/passwd file

For Example: The user David has no entry in the GECOS filed as shown in the figure below

Before -c option with usermod
Before using -c option with usermod

Next, we use -c option with usermod and add the full name i.e., “David Warner”. Now, the passwd file shows this updated entry.

After -c option with usermod
After using -c option with usermod
3. Change user’s Home Directory

-d : option changes the home directory of the user.

For Example: Consider any user in your system and login with that account.

As an example we logged in as David user. Using the pwd command it shows that David’s home directory is /home/David (By default the user always logs into his home directory)

Before -d option with usermod
Before using -d option with usermod

Next, we use -d option to change the home directory to /home/Warner. Make sure that the directory exists (hence we created the directory first at the desired location)

Using -d option with usermod command in linux
Using -d option with usermo

Now, when David logs in again the home directory has changed. The same can be checked in the /etc/passwd file also.

After -d option with usermod
After using -d option with usermod
4. Change Expiry Date of Account

-e : sets the date on which the user account will expire. The format of date is YYYY-MM-DD

For Example: First, check the expiry date of any user using the chage command.

Before -e option with usermod
Before using -e option with usermod

The expiry date for David’s account is set to never i.e., the account will never expire. Now, we change the expiry date using -e option with usermod command.

After -e option with usermod command in linux
After using -e option with usermod

Now, David’s account will expiry on 24th Dec, 2020.

5. Setting the Inactivity period

-f : options sets the number of days after the password expires until the account is disabled permanently. For example:

After -f option with usermod command in linux
After -f option with usermod command in linux
6. Changing Group to which the user belongs

-g : option is used to change the group to which the user belongs. It can be used either by giving the GID or Name of the new group. For example:

Before -g option with usermod
Before -g option with usermod command in linux

David belongs to the group having GID 1006. We use -g option to change the group to 1005.

After -g option with usermod command in linux
After -g option with usermod command in linux

As seen above, David’s group is now changed to 1005. Now, both David and u1 are members of the group 1005.

Note: The new group to which the user is assigned must already exist.

7. Assign Supplementary Groups

-G : option assigns a user to new groups, in addition to the primary group. This is required if a user is working on multiple projects and for each project a separate group is created in the system. Remember, a user can have only one primary group but multiple supplementary groups. For example:

After -G option with usermod command in linux
After using -G option with usermod command in linux

The above-shown command adds David to two supplementary groups – 1003 and 1002.

Note: 1. On adding a user to new supplementary group(s), it is deleted from any existing supplementary group but if you still want the user to be part of earlier supplementary groups then use -a option along with -G.

2. The group number (1003,1002) should be separated by comma and without space.

8. Change Login Name

-l : option changes the login name of the user.

For Example: Look at the list of users below. Let’s change the login name of user1 to User

Before -l option with usermod
Before -l option with usermod command in linux

Now, we use -l option to change the login name. The updated content shows the user name has changed. Rest of the information related to the user is unchanged.  

After -l option with usermod command in linux
After -l option with usermod command in linux
9. Lock User Password

-L : This option locks the user password.

For Example: The user David logs into his account, as usual, using his password as shown below

Before -L option with usermod
Before -L option with usermod command in linux

Suppose, the admin decides to lock David’s password. So, he will use -L option as

-L option with usermod command in linux
-L option with usermod command in linux

Now, when David tries to login the next time, he gets an error i.e. Login incorrect.

After -L option with usermod command in linux
After -L option with usermod command in linux
10. Move content from Current Home Directory to New Home Directory

-m : option is used in combination with -d option to shift the content of orginal home directory to the newly allocated home directory.

For Example: Let’s consider a user Suraj whose home directory /home/Suraj contains a file “f” and a directory “D” as shown in the figure below.

Before -m option with usermod
Before -m option with usermod command in linux

Now, we user -m option along with -d option to change the home directory and shifts the content of old home directory to the new one.   

-m option with usermod command in linux
Using -m option with usermod

After the change the new directory shows the transfer of the original file “f” and directory “D”.

After -m option with usermod command in linux
After using -m option with usermod
11. Change user Password

-p : option sets the user password. Try avoiding the use of this option as the password will be visible to users.

For Example: We change the password of user Suraj using –p option.

-p option with usermod command in linux
Using -p option with usermod

Next, if we look at the contents of /etc/shadow file the newly set password is clearly visible.

After -p option with usermod command in linux
After -p option with usermod
12. Change Login Shell

-s : option changes the login shell of the user. The number of available shells in the system can be checked in /etc/shells file.

For Example: The current shell for Suraj is /bin/bash.

Before -s option with usermod
Before -s option with usermod

Next, we change the shell using -s option.

After -s option with usermod command in linux
After -s option with usermod

The updated shell entry can be seen in the above figure.

13. Change UID

-u : option changes the UID assigned to any user.

For Example: The current UID of Suraj is 1008

Before -u option with usermod
Before using -u option with usermod

Now, if as an admin you want to assign him the UID 2020.

-u option with usermod command in linux
-u option with usermod

The updated UID is visible in the /etc/passwd file

After -u option with usermod command in linux
After -u option with usermod
14. Unlock user Password

-U : option unlocks the password locked by -L option. For example:

-U option with usermod command in linux
Using -U option with usermod

Now, the locked user David (see -L option) will be able to login again.

After -U option with usermod command in linux
After using -U option with usermod

PPT on usermod command

Video on usermod command

Other Useful Commands

Leave a Reply

Your email address will not be published. Required fields are marked *