Sunteți pe pagina 1din 14

A Complete Guide to Usage of usermod command

with 15 Practical Examples


The command useradd or adduser is used for creating user accounts in Linux systems. To know
more about on how to create system users, read our complete guide at:
1.

A Complete Guide to useradd Command in Linux

After creating user accounts, in some scenarios where we need to change the attributes of an existing
user such as, change users home directory, login name, login shell, password expiry date, etc,
where in such case usermod command is used.
When we execute usermod command in terminal, the following files are used and affected.
1.
2.
3.
4.
5.

/etc/passwd User account information.


/etc/shadow Secure account information.
/etc/group Group account information.
/etc/gshadow Secure group account information.
/etc/login.defs Shadow password suite configuration..

Basic syntax of command is:


usermod [options] username

Requirements
1.
2.
3.
4.

We must have existing user accounts to execute usermod command.


Only superuser (root) is allowed to execute usermod command.
The usermod command can be executed on any Linux distribution.
Must have basic knowledge of usermod command with options

Options of Usermod
The usermod command is simple to use with lots of options to make changes to an existing user.
Let us see how to use usermod command by modifying some existing users in Linux box with the
help of following options.
1. -c = We can add comment field for the useraccount.
2. -d = To modify the directory for any existing user account.
3. -e = Using this option we can make the account expiry in specific period.
4. -g = Change the primary group for a User.
5. -G = To add a supplementary groups.
6. -a = To add anyone of the group to a secondary group.
7. -l = To change the login name from tecmint to tecmint_admin.
8. -L = To lock the user account. This will lock the password so we cant use the account.
9. -m = moving the contents of the home directory from existing home dir to new dir.
10. -p = To Use un-encrypted password for the new password. (NOT Secured).
11. -s = Create a Specified shell for new accounts.
12. -u = Used to Assigned UID for the user account between 0 to 999.

Page |2
13. -U = To unlock the user accounts. This will remove the password lock and allow us to use
the user account.
In this article we will see 15 usermod commands with their practical examples and usage in
Linux, which will help you to learn and enhance your command-line skills using these options.

1. Adding Information to User Account


The -c option is used to set a brief comment (information) about the user account. For example,
lets add information on tecmint user, using the following command.
# usermod -c "This is Tecmint" tecmint

After adding information on user, the same comment can be viewed in /etc/passwd file.
# grep -E --color 'tecmint' /etc/passwd
tecmint:x:500:500:This is Tecmint:/home/tecmint:/bin/sh

Add Information to User


2. Change User Home Directory
In the above step we can see that our home directory is under /home/tecmint/, If we need to change
it to some other directory we can change it using -d option with usermod command.
For example, I want to change our home directory to /var/www/, but before changing, lets check
the current home directory of a user, using the following command.
# grep -E --color '/home/tecmint' /etc/passwd
tecmint:x:500:500:This is Tecmint:/home/tecmint:/bin/sh

Now, change home directory from /home/tecmint to /var/www/ and confirm the home director
after changing.
# usermod -d /var/www/ tecmint
# grep -E --color '/var/www/' /etc/passwd
tecmint:x:500:500:This is Tecmint:/var/www:/bin/sh

Change User Home Directory


3. Set User Account Expiry Date
The option -e is used to set expiry date on a user account with the date format YYYY-MM-DD.
Before, setting up an expiry date on a user, lets first check the current account expiry status using
the chage (change user password expiry information) command.
# chage -l tecmint
Last password change
Password expires

: Nov 02, 2014


: never

Page |3
Password inactive
Account expires
Minimum number of days between password change
Maximum number of days between password change
Number of days of warning before password expires

:
:
:
:
:

never
Dec 01, 2014
0
99999
7

The expiry status of a tecmint user is Dec 1 2014, lets change it to Nov 1 2014 using usermod e option and confirm the expiry date with chage command.
# usermod -e 2014-11-01 tecmint
# chage -l tecmint
Last password change
Password expires
Password inactive
Account expires
Minimum number of days between password change
Maximum number of days between password change
Number of days of warning before password expires

:
:
:
:
:
:
:

Nov 02, 2014


never
never
Nov 01, 2014
0
99999
7

4. Change User Primary Group


To set or change a user primary group, we use option -g with usermod command. Before, changing
user primary group, first make sure to check the current group for the user tecmint_test.
# id tecmint_test
uid=501(tecmint_test) gid=502(tecmint_test) groups=502(tecmint_test)

Now, set the babin group as a primary group to user tecmint_test and confirm the changes.
# usermod -g babin tecmint_test
# id tecmint_test
uid=501(tecmint_test) gid=502(babin) groups=502(tecmint_test)

5. Adding Group to an Existing User


If you want to add a new group called tecmint_test0 to tecmint user, you can use option -G
with usermod command as shown below.
# usermod -G tecmint_test0 tecmint
# id tecmint

Note: Be careful, while adding a new groups to an existing user with -G option alone, will remove
all existing groups that user belongs. So, always add the -a (append) with -G option to add or
append new groups.
6. Adding Supplementary and Primary Group to User
If you need to add a user to any one of the supplementary group, you can use the options -a and G. For example, here we going to add a user account tecmint_test0 with the group wheel. This is
to add multiple groups for a user account.

Page |4
# usermod -a -G wheel tecmint_test0
# id tecmint_test0

So, user tecmint_test0 remains in its primary group and also in secondary group (wheel). This will
make my normal user account to execute any root privileged commands in Linux box.
eg : sudo service httpd restart

7. Change User Login Name


To change any existing user login name, we can use -l (new login) option. In the example below,
we changing login name tecmint to tecmint_admin. So the username tecmint has been renamed
with the new name tecmint_admin.
# usermod -l tecmint_admin tecmint

Now check for the tecmint user, It will not be present because we have changed it to
tecmint_admin.
# id tecmint

Check for the tecmint_admin account it will be there with same UID and with existing group what
we have added before.
# id tecmint_admin

8. Lock User Account


To Lock any system user account, we can use -L (lock) option, after the account is locked we cant
login by using the password and you will see a ! added before the encrypted password in
/etc/shadow file, means passwrd disabled. Here Im going to lock (disable) a user named: Babin
# usermod -L babin

Check for the locked account.


# grep -E --color 'babin' cat /etc/shadow

9. Unlock User Account


The -U option is used to unlock any locked user, this will remove the ! before the encrypted
password.
# grep -E --color 'babin' /etc/shadow
# usermod -U babin

Verify the user after unlock.


# grep -E --color 'babin' /etc/shadow

Page |5

Unlock User Account


10. Move User Home Directory to New location
Lets say youve a user account as pinky with home directory /home/pinky, you want to move
to new location say /var/pinky. You can use the options -d and -m to move the existing user
files from current home directory to a new home directory.
Check for the account and its current home directory.
# grep -E --color 'pinky' /etc/passwd

Then list the files which is owned by user pinky.


# ls -l /home/pinky/

Now we have to move the home directory from /home/pinky to /var/pinky.


# usermod -d /var/pinky/ -m pinky

Next, verify the directory change.


# grep -E --color 'pinky' /etc/passwd

Check for the files under /home/pinky. Here we have moved the files using -m option so there
will be no files. The pinky user files will be now under /var/pinky.
# ls -l /home/pinky/
# ls -l /var/pinky/

11. Create Un-encrypted Password for User


To create an un-encrypted password, we use option -p (password). For demonstration purpose, Im
setting a new password say redhat on a user pinky.

Page |6
# usermod -p redhat pinky

After setting password, now check the shadow file to see whether its in encrypted format or unencrypted.
# grep -E --color 'pinky' /etc/shadow

Create Unencrypted User Password


Note: Did you see in the above image, the password is clearly visible to everyone. So, this option is
not recommended to use, because the password will be visible to all users.
12. Change User Shell
The user login shell can be changed or defined during user creation with useradd command or
changed with usermod command using option -s (shell). For example, the user babin has the
/bin/bash shell by default, now I want to change it to /bin/sh.
# grep -E --color 'babin' /etc/passwd
# usermod -s /bin/sh babin

After changing user shell, verify the user shell using the following command.
# grep -E --color 'babin' /etc/passwd

13. Change User ID (UID)


In the example below, you can see that my user account babin holds the UID of 502, now I want
to change it to 888 as my UID. We can assign UID between 0 to 999.

Page |7
# grep -E --color 'babin' /etc/passwd
OR
# id babin

Now, lets change the UID for user babin using -u (uid) option and verify the changes.
# usermod -u 888 babin
# id babin

14. Modifying User Account with Multiple Options


Here we have a user jack and now I want to modify his home directory, shell, expiry date, label,
UID and group at once using one single command with all options as we discussed above.
The user Jack has the default home directory /home/jack, Now I want to change it to
/var/www/html and assign his shell as bash, set expiry date as December 10th 2014, add new label
as This is jack, change UID to 555 and he will be member of apple group.
Let we see how to modify the jack account using multiple option now.
# usermod -d /var/www/html/ -s /bin/bash -e 2014-12-10 -c "This is Jack"

-u 555 -aG apple jack

Then check for the UID & home directory changes.


# grep -E --color 'jack' /etc/passwd

Account expire check.


# chage -l jack

Check for the group which all jack have been member.
# grep -E --color 'jack' /etc/group

15. Change UID and GID of a User


We can change UID (user id) and GID (group id) of a current user. For changing to a New GID we
need an existing group. Here already there is an account named as orange with GID of 777.
Now my jack user account want to be assigned with UID of 666 and GID of Orange (777).
Check for the current UID and GID before modifying.
# id jack

Modify the UID and GID.


# usermod -u 666 -g 777 jack

Check for the changes.

Page |8
# id jack

Change User UID and GID

Conclusion
Here we have seen how to use usermod command with its options in very detailed fashion, Before
knowing about usermod command, one should must know useradd command and its options to use
the usermod. If Ive missed any point in the article do let me know via comments and dont forget to
add your valuable comments.
Source: http://www.tecmint.com/usermod-command-examples/#
Editor :
Hamami InkaZo
Instructor and CEO of TKJ Department
SMK Informatika Wonosobo
www.facebook.com/hammamy

Page |9
We all are aware about the most popular command called useradd or adduser in Linux. There
are times when a Linux System Administrator asked to create user accounts on Linux with some
specific properties, limitations or comments.
In Linux, a useradd command is a low-level utility that is used for adding/creating user accounts
in Linux and other Unix-like operating systems. The adduser is much similar to useradd
command, because it is just a symbolic link to it.
In some other Linux distributions, useradd command may comes with lightly difference version. I
suggest you to read your documentation, before using our instructions to create new user accounts in
Linux.
When we run useradd command in Linux terminal, it performs following major things:
1. It edits /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow files for the newly created
User account.
2. Creates and populate a home directory for the new user.
3. Sets permissions and ownerships to home directory.
Basic syntax of command is:
useradd [options] username

In this article we will show you the most used 15 useradd commands with their practical examples
in Linux. We have divided the section into two parts from Basic to Advance usage of command.
1. Part I: Basic usage with 10 examples
2. Part II: Advance usage with 5 examples

Part I 10 Basic Usage of useradd Commands


1. How to Add a New User in Linux
To add/create a new user, all youve to follow the command useradd or adduser with
username. The username is a user login name, that is used by user to login into the system.
Only one user can be added and that username must be unique (different from other username
already exists on the system).
For example, to add a new user called tecmint, use the following command.
[root@tecmint ~]# useradd tecmint

When we add a new user in Linux with useradd command it gets created in locked state and to
unlock that user account, we need to set a password for that account with passwd command.
[root@tecmint ~]# passwd tecmint
Changing password for user tecmint.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

P a g e | 10
Once a new user created, its entry automatically added to the /etc/passwd file. The file is used to
store users information and the entry should be.
tecmint:x:504:504:tecmint:/home/tecmint:/bin/bash

The above entry contains a set of seven colon-separated fields, each field has its own meaning.
Lets see what are these fields:
1. Username: User login name used to login into system. It should be between 1 to 32
charcters long.
2. Password: User password (or x character) stored in /etc/shadow file in encrypted format.
3. User ID (UID): Every user must have a User ID (UID) User Identification Number. By
default UID 0 is reserved for root user and UIDs ranging from 1-99 are reserved for other
predefined accounts. Further UIDs ranging from 100-999 are reserved for system accounts
and groups.
4. Group ID (GID): The primary Group ID (GID) Group Identification Number stored in
/etc/group file.
5. User Info: This field is optional and allow you to define extra information about the user.
For example, user full name. This field is filled by finger command.
6. Home Directory: The absolute location of users home directory.
7. Shell: The absolute location of a users shell i.e. /bin/bash.
2. Create a User with Different Home Directory
By default useradd command creates a users home directory under /home directory with
username. Thus, for example, weve seen above the default home directory for the user tecmint is
/home/tecmint.
However, this action can be changed by using -d option along with the location of new home
directory (i.e. /data/projects). For example, the following command will create a user anusha
with a home directory /data/projects.
[root@tecmint ~]# useradd -d /data/projects anusha

You can see the user home directory and other user related information like user id, group id, shell
and comments.
[root@tecmint ~]# cat /etc/passwd | grep anusha
anusha:x:505:505::/data/projects:/bin/bash

3. Create a User with Specific User ID


In Linux, every user has its own UID (Unique Identification Number). By default, whenever we
create a new user accounts in Linux, it assigns userid 500, 501, 502 and so on
But, we can create users with custom userid with -u option. For example, the following command
will create a user navin with custom userid 999.
[root@tecmint ~]# useradd -u 999 navin

Now, lets verify that the user created with a defined userid (999) using following command.

P a g e | 11
[root@tecmint ~]# cat /etc/passwd | grep tecmint
navin:x:999:999::/home/navin:/bin/bash

NOTE: Make sure the value of a user ID must be unique from any other already created users on the
system.
4. Create a User with Specific Group ID
Similarly, every user has its own GID (Group Identification Number). We can create users with
specific group IDs as well with -g option.
Here in this example, we will add a user tarunika with a specific UID and GID simultaneously
with the help of -u and -g options.
[root@tecmint ~]# useradd -u 1000 -g 500 tarunika

Now, see the assigned user id and group id in /etc/passwd file.


[root@tecmint ~]# cat /etc/passwd | grep tarunika
tarunika:x:1000:500::/home/tarunika:/bin/bash

5. Add a User to Multiple Groups


The -G option is used to add a user to additional groups. Each group name is separated by a
comma, with no intervening spaces.
Here in this example, we are adding a user tecmint into multiple groups like admins, webadmin
and developer.
[root@tecmint ~]# useradd -G admins,webadmin,developers tecmint

Next, verify that the multiple groups assigned to the user with id command.
[root@tecmint ~]# id tecmint
uid=1001(tecmint) gid=1001(tecmint)
groups=1001(tecmint),500(admins),501(webadmin),502(developers)
context=root:system_r:unconfined_t:SystemLow-SystemHigh

6. Add a User without Home Directory


In some situations, where we dont want to assign a home directories for a users, due to some
security reasons. In such situation, when a user logs into a system that has just restarted, its home
directory will be root. When such user uses su command, its login directory will be the previous user
home directory.
To create users without their home directories, -M is used. For example, the following command
will create a user shilpi without a home directory.
[root@tecmint ~]# useradd -M shilpi

P a g e | 12
Now, lets verify that the user is created without home directory, using ls command.
[root@tecmint ~]# ls -l /home/shilpi
ls: cannot access /home/shilpi: No such file or directory

7. Create a User with Account Expiry Date


By default, when we add users with useradd command user account never get expires i.e their
expiry date is set to 0 (means never expired).
However, we can set the expiry date using -e option, that sets date in YYYY-MM-DD format.
This is helpful for creating temporary accounts for a specific period of time.
Here in this example, we create a user aparna with account expiry date i.e. 27th April 2014 in
YYYY-MM-DD format.
[root@tecmint ~]# useradd -e 2014-03-27 aparna

Next, verify the age of account and password with chage command for user aparna after setting
account expiry date.
[root@tecmint ~]# chage -l aparna
Last password change
Password expires
Password inactive
Account expires
Minimum number of days between password change
Maximum number of days between password change
Number of days of warning before password expires

:
:
:
:

Mar 28,
never
never
Mar 27,
:
:
: 7

2014
2014
0
99999

8. Create a User with Password Expiry Date


The -f argument is used to define the number of days after a password expires. A value of 0
inactive the user account as soon as the password has expired. By default, the password expiry value
set to -1 means never expire.
Here in this example, we will set a account password expiry date i.e. 45 days on a user tecmint
using -e and -f options.
[root@tecmint ~]# useradd -e 2014-04-27 -f 45 tecmint

9. Add a User with Custom Comments


The -c option allows you to add custom comments, such as users full name, phone number, etc
to /etc/passwd file. The comment can be added as a single line without any spaces.
For example, the following command will add a user mansi and would insert that users full name,
Manis Khurana, into the comment field.
[root@tecmint ~]# useradd -c "Manis Khurana" mansi

P a g e | 13
You can see your comments in /etc/passwd file in comments section.
[root@tecmint ~]# tail -1 /etc/passwd
mansi:x:1006:1008:Manis Khurana:/home/mansi:/bin/sh

10. Change User Login Shell:


Sometimes, we add users which has nothing to do with login shell or sometimes we require to assign
different shells to our users. We can assign different login shells to a each user with -s option.
Here in this example, will add a user tecmint without login shell i.e. /sbin/nologin shell.
[root@tecmint ~]# useradd -s /sbin/nologin tecmint

You can check assigned shell to the user in /etc/passwd file.


[root@tecmint ~]# tail -1 /etc/passwd
tecmint:x:1002:1002::/home/tecmint:/sbin/nologin

Part II 5 Advance Usage of useradd Commands


11. Add a User with Specific Home Directory, Default Shell and Custom Comment
The following command will create a user ravi with home directory /var/www/tecmint, default
shell /bin/bash and adds extra information about user.
[root@tecmint ~]# useradd -m -d /var/www/ravi -s /bin/bash -c "TecMint

Owner" -U ravi

In the above command -m -d option creates a user with specified home directory and the -s
option set the users default shell i.e. /bin/bash. The -c option adds the extra information about
user and -U argument create/adds a group with the same name as the user.
12. Add a User with Home Directory, Custom Shell, Custom Comment and UID/GID
The command is very similar to above, but here we defining shell as /bin/zsh and custom UID and
GID to a user tarunika. Where -u defines new users UID (i.e. 1000) and whereas -g defines
GID (i.e. 1000).
[root@tecmint ~]# useradd -m -d /var/www/tarunika -s /bin/zsh -c "TecMint

Technical Writer" -u 1000 -g 1000 tarunika

13. Add a User with Home Directory, No Shell, Custom Comment and User ID
The following command is very much similar to above two commands, the only difference is here,
that we disabling login shell to a user called avishek with custom User ID (i.e. 1019).
Here -s option adds the default shell /bin/bash, but in this case we set login to /usr/sbin/nologin.
That means user avishek will not able to login into the system.

P a g e | 14
[root@tecmint ~]# useradd -m -d /var/www/avishek -s /usr/sbin/nologin -c

"TecMint Sr. Technical Writer" -u 1019 avishek

14. Add a User with Home Directory, Shell, Custom Skell/Comment and User ID
The only change in this command is, we used -k option to set custom skeleton directory i.e.
/etc/custom.skell, not the default one /etc/skel. We also used -s option to define different shell i.e.
/bin/tcsh to user navin.
[root@tecmint ~]# useradd -m -d /var/www/navin -k /etc/custom.skell -s

/bin/tcsh -c "No Active Member of TecMint" -u 1027 navin

15. Add a User without Home Directory, No Shell, No Group and Custom Comment
This following command is very different than the other commands explained above. Here we used
-M option to create user without users home directory and -N argument is used that tells the
system to only create username (without group). The -r arguments is for creating a system user.
[root@tecmint ~]# useradd -M -N -r -s /bin/false -c "Disabled TecMint

Member" clayton

For more information and options about useradd, run useradd command on the terminal to see
available options.
Source: http://www.tecmint.com/add-users-in-linux/

S-ar putea să vă placă și