Modify users with usermod in Ubuntu

We can create a user account using adduser command-line utility. And, once a user account is created – its attributes can be modified later through various tools. One such tool is usermod. In this article, we cover how to modify users with usermod in Ubuntu.

With the help of usermod tool, we can modify a user attributes like Home directory, group, login name, default shell etc. We will cover these and other useful options next. All these changes which we do here can be seen in (unless otherwise stated) –

cat /etc/passwd

Note: Following operations require superuser privileges. In case you don’t have one then, we advise you to contact your System Administrator for assistance.

Modify users with usermod in Ubuntu

I. Change Home directory –

If we want to change home directory of the user then, use the following syntax –

sudo usermod -d </new/home/directory/path> <user_name>

Here, it will create a new home directory for us even if it doesn’t exist.

II. Change username description –

If we wish to make changes to the usernames’ description then,

sudo usermod -c "<new description>" <username>

For instance,

sudo usermod -c "New Description" abc

III. Change default shell –

To change default shell, use -s option

sudo usermod -s <SHELL> <username>

For instance,

sudo usermod -s /bin/sh abc

IV. To change primary group of the user –

sudo usermod -g <group_name> <username>

V. To make modifications in secondary groups –

sudo usermod -G <group_name1>,<group_name2> <username>

But, this will remove any prior secondary group a user is already part of. Therefore, we need to use -a to keep the existing secondary group as well.

sudo usermod -a -G  <group_name1>,<group_name2> <username>

Note: The groups should already be there. Otherwise, it would throw an error – Group does not exist.

We can use command-line utility groupadd <username> to add groups. To view groups a user is part of, use groups <username> command.

VI. To change login name –

sudo usermod -l <new_login_name> <username>

VII. To Lock and Unlock a user –

sudo usermod -L <username>

where, -L option locks the user. And, when we login – it throws an Authentication failure error. In the /etc/shadow file, it would put a exclamation mark at the start of encrypted password associated with the user.

sudo usermod -U <username>

where, -U unlocks the user. Removes the exclamation mark which was set in /etc/shadow

In conclusion, we have covered how to modify users with usermod in Ubuntu.

Additional Info –

We can also set an expiry date for user accounts using usermod.

Similar Posts