Linux Commands to manage Local Accounts – useradd, usermod, chage & passwd

User administration is one of the important task of Linux system administrator. Local accounts or users in Linux like operating system is managed by useradd, usermod, userdel, chage and passwd commands.

  • useradd command is used to create new accounts in Linux
  • usermod command used to modify the existing accounts in linux
  • userdel command is used to delete local account in linux
  • passwd command used assign password to local accounts or users.
  • chage comamnd is used to view & modify users password expiry information

Syntax of ‘useradd’ command

# useradd <options> <username_or_login>

Options used in useradd command :


Syntax of usermod command :

# usermod <options> <username_or_login>

Options used in usermod command.


Syntax of userdel command:

# userdel <options> <username_or_login>

Options used in userdel command :


Syntax of chage :

# chage <options> <username_or_login>

Options used in chage command :


Syntax of passwd Command :

# passwd <username_or_login>

For more details on passwd command please refer ‘10 passwd command examples in Linux

In this article we will discuss different examples of user administration on CentOS 7 & RHEL 7.

Example:1 Create a local account & assign password.

User the below syntax to create and assign to the username.

# useradd <username> ; echo -e "<newpassword>\n<newpassword>" | passwd username

Let’s create a username ‘harry’ and assign password.

[[email protected] ~]# useradd harry ; echo -e "[email protected]#\[email protected]#" | passwd harry
Changing password for user harry.
New password: Retype new password: passwd: all authentication tokens updated successfully.
[[email protected] ~]#

Note : When a user is created in Linux followings are updated:

  • A home directory is created under ‘/home/<username>’
  • User info is updated in ‘/etc/passwd’ file
  • Group Information is stored in ‘/etc/group’
  • password info is updated in ‘/etc/shadow’ file.
  • File for user’s email is created under ‘/var/spool/mail/<username>’

Example:2 Create a user with customize settings

Let’s create a user with following options :

UID = 2000
GID = 5000
Comments = ‘Admin Account of SAP’
Home Directory = /opt/sap
Shell = /bin/ksh
Username = john
password = xxxxxx

[[email protected] ~]# useradd -u 2000 -g 5000 -c "Admin Account of SAP" -d /opt/sap -s /bin/ksh john
[[email protected] ~]#
[[email protected] ~]# echo -e "[email protected]#\[email protected]#" | passwd john
Changing password for user john.
New password: Retype new password: passwd: all authentication tokens updated successfully.
[[email protected] ~]#

Verify the above settings from /etc/passwd file.

[[email protected] ~]# grep john /etc/passwd
john:x:2000:5000:Admin Account of SAP:/opt/sap:/bin/ksh
[[email protected] ~]#

Example:3 Modify the Existing User

usermod command is used to modify the existing local accounts in Linux.

Let’s make the existing user “harry” part of Secondary group “sap” and change its home directory from ‘/home/harry’ to ‘/opt/sap’ and login shell from ‘/bin/bash’ to ‘/bin/sh’

[[email protected] ~]# usermod -G sap -d /opt/sap -s /bin/sh harry
[[email protected] ~]#
[[email protected] ~]# grep harry /etc/passwd
[[email protected] ~]#

Example:4 Create a user and force to change the password at first login.

Let’s create a user ‘mark’ with secondary group ‘sap’, home directory as ‘/opt/sap’ and force him to change his password at the first login.

We can force users to change its password at first login by using command ‘chage -d 0 <username>‘.

[[email protected] ~]# useradd -c "sap user" -G sap -d /opt/data mark
[[email protected] ~]# echo -e "[email protected]#\[email protected]#" | passwd mark ; chage -d 0 mark
Changing password for user mark.
New password: Retype new password: passwd: all authentication tokens updated successfully.
[[email protected] ~]#

Now try to login as mark and see whether user is getting a prompt to change password or not.


Note : Use ‘chage -l <username>‘ command to view the user’s password expiry info.

Example:5 Delete a User along with its home directory

userdel command is used to delete local accounts or users in Linux. Let’s delete a user linuxtechi along with its related its files (home directory).

[[email protected] ~]# userdel -r linuxtechi
[[email protected] ~]# grep linuxtechi /etc/passwd
[[email protected] ~]#

Read Also : 16 Useful ‘cp’ Command Examples for Linux Beginners

1 Response

  1. nerdtron says:

    instead of using the echo -e command to set the password, the passwd command has an option to accept input from the Standard input. This is helpful for script and batch changing passwords for users:

    echo “newpass” | passwd –stdin username

Leave a Reply

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

Pin It on Pinterest