Usermod Command in Linux Explained with Examples

The usermod command allows us to manage user accounts. We can use it to add a user to the group, change the username, remove a user from the group, change a user's default shell, lock or unlock a user account, change a user's primary group, and change a user's home directory.

The usermod command syntax

The usermod command uses the following syntax.

#usermod [option] [argument] username

The usermod command options and arguments

Short option Long option Description
-g --gid Used to change the user’s primary group
-G --groups Used to change the user’s secondary group or groups.
-a --append Used with the –G option to add the user to the supplied group or groups.
-c --comment Used to update the user description.
-d --home Used to change the home directory.
-m --move-home Used to create a new home directory and move all the contents from the current directory to it.
-s --shell Used to update the login shell
-l --login Used to change the login name
-L --lock Used to lock the account
-U --unlock Used to unlock the account

The usermod command example

Login from the root account or switch to the super user account, access a terminal and run the following commands.

Command Description
#useradd testuser Add the testuser account
#passwd testuser Set the password
#grep testuser /etc/passwd Show the testuser user's account entry in the /etc/passwd file. The /etc/passwd file saves the user account details.
#grep testuser /etc/shadow Show the testuser user's account entry in the /etc/shadow file. The /etc/shadow file saves the user account's password and related information.
#grep testuser /etc/group Show the testuser user's account entry in the /etc/group file. The /etc/group file saves the user's group name and related information.
#grep testuser /etc/gshadow Show the testuser user's account entry in the /etc/gshadow file. The /etc/gshadow file saves the user's group password and related information.
#ls –dl /home/testuser Display the user's home directory

The above commands adds and verifies a new user account with default settings.

usermod command example

Adding or updating user description

By default, the useradd command does not add a description or comment to the user account. It adds only the username. A username provides only generic information about the account. For example, the username name testuser does not give us any details about the owner. A description or comment allows us to identify the user account. We can save the user's full name in this field. The following command adds the full name 'Johnny Depp' to the user account testuser.

#usermod -c 'Johnny Depp' testuser

We use the same command to update or modify the existing description. For example, the following command changes the full name to 'Johnny Carson'.

#usermod -c 'Johnny Carson' testuser

adding user description

Changing the default user shell

The /bin/bash is the default shell. The -s option allows us to change the default shell. For example, the following command changes the default shell to the /bin/sh for testuser.

#usermod -s /bin/sh testuser
changing default shell

Locking a user account

The -L option locks the user account. It adds an exclamation sign before the user’s encrypted password stored in the /etc/shadow file. The following command locks the testuser account.

#usermod -L testuser

locking a user account

Unlocking a user account

The -U option locks the user account. We can use this option to unlock a user account. This option removes the exclamation sign added by the -L option. The following command unlocks the testuser account.

#usermod -U testuser

unlock a user account

Changing home directory

The -d option allows us to change the user's home directory. This option only updates the /etc/passwd file entry. It does not create the specified directory. If we use this option, we must make the specified directory first.

#mkdir /testuser
#usermod -d /testuser testuser

changing home directory

The user's home directory contains profile files. These files control the shell environment and run scripts just after the login and before the logout. If we use the above option to change a user's home directory, these files will not copy into the new directory. The -m option creates the specified directory and moves all files from the current directory to the new directory. However, this option does not update the /etc/passwd file entry. For this, we can combine this option with the -d option. The following command restores the original home directory.

#usermod -d /home/testuser testuser

The following command creates the new home directory and moves all files from the current home directory to it.

#usermod -md /testuser2 testuser

options m and d with the usermod command

Changing primary group

The -g option changes the user's primary group. Similar to the -d option, this option only updates the /etc/group file. It does not create the specified group. If we want to use this option, we must add that group first.

#groupadd testgroup
#usermod -g testgroup testuser

changing primary group

Changing login name

The -l option allows us to change the username or login name. The following command changes the login name to demouser.

 #usermod -l demouser testuser

changing login name

Instead of the usernames, Linux uses user IDs to track and monitor the user accounts. Changing the login name of a user account does not change its user ID.

ComputerNetworkingNotes Linux Tutorials Usermod Command in Linux Explained with Examples

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com