Linux provides usermod
command by default for the most of the distributions. This command is used to user modification. After user creation the user related information, values and policies can be changed with usermod command. In this tutorial we will look various features of usermod command with examples.
Syntax
Syntax of the usermod command is like below.
usermod [options] LOGIN
Help
$ usermod -h

Add Comment or Information
User information is stored in a file named passwd
. This file is located as /etc/passwd
. In the ancient days of Linux and Linux this file is also used to store users password. But storing user information and user credential created security problems and passwords are stored in a separate file named shadown
in the same directory etc
.
There is a columns used to store information about the user. This information can be anything like First name, surname, the birthday etc.
$ usermod -c "İsmail Baydan " ismail

We have set the comment for the user ismail as İsmail Baydan
. We can check the modification with grep command by filtering line contains the user name ismail.
$ grep ismail /etc/passwd
Change Home Directory
Human users generally have home directory for storing data, file, media etc for their personal or corporate usage. This home directory is generally reside in /home
in Linux root file system.
For example user ismail
will have the /home/ismail
directory as his own home directory for the most the Linux systems. This value can be changed after creation for some reasons like we need to set and application folder for an application user like apache
.
In this example we will change the home directory of user nick
. We will set /mnt
as nick’s home directory like below.
$ sudo usermod -d "/mnt " nick

As we can see from grep command output column number 6 is /mnt
which is nick’s new home directory.
Set User Account Expiry Date
Some users are generally created in a hurry for temporary usage and than they forgotten and resides in the system as an active account. This is an security vulnerability and should be avoided with practical solution. One of the action to take to prevent this security issue is setting account expiry date for required accounts. Expire date will be specified in YYYY-MM-DD
format as 2017-02-28
in this example for the user nick
$ sudo usermod -e 2017-02-28 nick

And we will check the account expire date with the following command for the user nick.
$ sudo chage -l nick
Show User’s Groups
As like in the Windows operating systems in Linux operating systems user accounts are associated with groups. There are two type of user group. First type is primary group and each user account have single primary user group. Other type of group is secondary groups where each user may have more than one group. Primary groups of existing users can be changed with usermod command easily.
$ id nick

Change User Primary Group
Previously we have printed the user group. Now we will change the user primary group more detailed information about primary group can be found previous example.
In this example we will add user nick to the root group.
$ sudo usermod -g root nick

Adding Group To Existing User
There is two type of supplementary or secondary group addition. First we will specify the supplementary groups for the user and previous supplementary group memberships of the user will be removed if old groups do not presented. Which simply means the user will have only specified groups and all old groups will be reset.
$ usermod -G avahi,test nick

And the second way we will add new supplementary groups to the user but old groups will be preserved. For his operations we will use extra option -a
like below.
$ usermod -a -G games nick

Change User Login Name
User accounts have login names to authentication themselves. In Linux operations systems users are identified by their user ID’s. User names are used an easy way to remember and use. So A user account user ID can not be changed after creation but the user name can be changed easily. In this example we will change user name nick
to the nick1
with option -l
.
$ sudo usermod -l nick1 nick

Lock User Account
Some times we may need to lock some users for various reasons like security, holiday, test etc. User locking means the account will be inactive up to unlock operations and will be no user login for this period. In this example we will lock user nick1
with the -L
option.
$ sudo usermod -L nick1

As we can see from screenshot in the shadow file a !
is added which simply symbolize the account is locked.
Unlock User Account
Now in the previous example we have locked the account and we want to unlock the account named nick1. Simply provide -U
options to the usermod command like below.
$ sudo usermod -U nick1
Change User Shell
In Linux user accounts are associated with shells. Shell is the application which interface with user and operating system. There are different type of shells used in Linux community. But the most popular shell is bash
and generally used for most Linux distributions. This default shell for the user account can be changed with -s
options.
In this example we will change the shell of user account nick1 with sh
shell.
$ sudo usermod -s "/bin/sh" nick1

Change User ID
User id of and account can be changed too. This change operation is done with -u
option like below. In this example we will change the user nick1 user ID to the 1234
.
$ sudo usermod -u 1234 nick1

good explanations! thank you!