Best Linux Usermod Command With Examples

In this article, we are going to discuss how to use the Linux usermod commands. usermod command is used to Modify the already created user’s settings like change primary group of the user, add secondary groups to the user, change UID and GID of the user, and so on. usermod is another tool like useradd to manage user accounts in Linux.

Best Linux Usermod Command With Examples

Best Linux Usermod Commands With Examples

Follow the below Article for Linux usermod commands :

Set Comment for a User Account

To set a comment for a User Account in Linux we can use Linux usermod commands with the argument -c. The comment is useful for mention some important information about the user like Contact Information or some personal information or something else. refer to the command below.

[[email protected] ~]# usermod -c "my name is john" john   # Set comment for a User Account

# Confirm the Setting

[[email protected] ~]# cat /etc/passwd | grep john
john:x:502:502:my name is john:/home/john:/bin/bash

Lock a User Account

You can use Linux usermod command with argument -L to lock a User Account. Here I am locking the user account john. Refer to the command below.

To confirm if the user account is locked or not you can check in the /etc/shadow file. Locked Account will have an exclamation mark (!) at the beginning of the encrypted password (Highlighted in Red color).

[[email protected] ~]# usermod -L john   # Lock a User Account

# Confirm the Setting

[[email protected] ~]# cat /etc/shadow | grep john
john:!$1$Rr8aOLUq$txkhPu.DqYCSG7FDEc4O71:17312:0:99999:7:::

Unlock a User Account

To Unlock a user account you can use usermod command with argument -U. Refer to the command below.

[[email protected] ~]# usermod -U john   # Unlock a User Account

# Confirm the Setting

[[email protected] ~]# cat /etc/shadow | grep john
john:$1$Rr8aOLUq$txkhPu.DqYCSG7FDEc4O71:17312:0:99999:7:::

Change UID (User ID)

You can change the UID (User ID) of a user account using Linux usermod command with argument -u.  Here I am changing the UID of user john whose current UID is 502 and I am changing it to 555. Refer to the command below.

[[email protected] ~]# id john
uid=502(john) gid=502(john) groups=502(john)

# Confirm the Setting

[[email protected] ~]# usermod -u 555 john   # Change UID of a User Account
[[email protected] ~]# id john
uid=555(john) gid=502(john) groups=502(john)

Rename a Login Name

To rename a login name we can use usermod command with argument -l. Here I am renaming the login name from john to Ricky.

Syntax : usermod -l [New_Name] [Old Name]

[[email protected] ~]# usermod -l ricky john  # Raname Login Name

After renaming the Login Name when you check the User Information with the old username you will get the below error as the Login name is no more exist.

[[email protected] ~]# id john
id: john: No such user

Now check the user Information with New Login Name.

[[email protected] ~]# id ricky   # Check the User Information with New Login Name
uid=555(ricky) gid=502(john) groups=502(john)

Set Expiry Date for a User Account using Linux Usermod Command

You can set an expiry date for a user account by using Linux usermod command with the argument -e.

[[email protected] ~]# usermod -e 2017-06-15 ricky  # Set Expiry date for a User Account

# Confirm the Setting

[[email protected] ~]# chage -l ricky
Last password change                                    : May 26, 2017
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : Jun 15, 2017
Minimum number of days between password change          : 0
Maximum number of days between password change          : 99999
Number of days of warning before password expires       : 7

Change Primary Group of a User Account

You can change the Primary group of a User Account by using usermod command with argument -g. Here I have a User Account named tony whose current Primary GID is 556 and I am changing the GID to 503. refer to the command below.

[[email protected] ~]# id tony
uid=556(tony) gid=556(tony) groups=556(tony)

# Confirm the Setting

[[email protected] ~]# usermod -g admins tony   # Change the Primary Group
[[email protected] ~]# id tony
uid=556(tony) gid=503(admins) groups=503(admins)

Add Multiple Supplementary/Secondary Groups using Linux usermod command

You can add multiple Supplementary groups OR Secondary groups to a user account. You can do so by using usermod command with argument -G. Here we are using an extra argument i.e. -a which is also referred to as append.

Note: If you don’t use the argument -a with -G then Linux usermod will remove all previously added Secondary groups and only add the current one which is you are adding.

[[email protected] ~]# usermod -a -G developers,workers ricky  # Add Supplementary/ Secondary to a User Account

# Confirm the Setting

[[email protected] ~]# id ricky
uid=555(ricky) gid=502(john) groups=502(john),557(developers),558(workers)

Change Home Directory

To change the Home directory of a User Account use Linux usermod command with argument -d. Refer to the command below.

[[email protected] ~]# usermod -d /users/itsmarttricks itsmarttricks # Change the Home Directory

# Confirm the Setting

[[email protected] ~]# cat /etc/passwd | grep itsmarttricks
itsmarttricks:x:557:559::/users/itsmarttricks:/bin/bash

Set Password Inactive for a User Account

Password Inactive is a password aging policy by which users can use the same password even after expiring the password till allowed days. Here I am allowing 10 days of password inactive for user itsmarttricks. To set Password Inactive you can use Linux usermod command with argument -f. Refer to the command below.

[[email protected] ~]# usermod -f 10 itsmarttricks # Set Password Inactive for a User Account

# Confirm the Setting

[[email protected] ~]# cat /etc/shadow | grep itsmarttricks
itsmarttricks:$1$HQEiPMJa$A1IXvuj8Ys08eLolPz6fP1:17312:0:99999:7:10::

Move Home Directory of a User Account

Linux usermod command also allowing us to move the home directory of a user account with its data. We can do so by using Linux usermod command with arguments -m (for moving the home directory) and -d (to change the directory).

Let’s do the task more briefly :

Here I am creating a user account named Shree and set a password for that.

[[email protected] ~]# useradd shree   # Creating a New User Account
[[email protected] ~]# passwd shree   # Set Password for User Account
Changing password for user shree.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

Now create some files in the home directory of the user account.

[[email protected] ~]# cd /home/shree/   # Changing to User's Home Directory
[[email protected] shree]# ls
[[email protected] shree]# touch file{1,2,3,4,5}.txt   # Creating some files
[[email protected] shree]# ls
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt

So let’s go ahead and move the home directory of the user account shree. Refer to the below command.

[[email protected] ~]# usermod -m -d /users/shree shree   # Move the Home Directory of a User Account
# Confirm the Setting
[[email protected] ~]# cat /etc/passwd | grep shree
shree:x:558:560::/users/shree:/bin/bash

Confirm if data moved or not.

[[email protected] ~]# cd /users/shree/
[[email protected] shree]# ls
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt

Also, you can notice that the Home Directory is not available on its previous location.

[[email protected] shree]# cd /home/
[[email protected] home]# ls
itsmarttricks  john  tony

Change Shell Script of User Account

To change the Shell Script of a User Account you can use usermod command with argument -s. Refer to the command below.

[[email protected] ~]# usermod -s /etc/test itsmarttricks   # Change Shell Script of a User Account
[[email protected] ~]# cat /etc/passwd | grep itsmarttricks
itsmarttricks:x:557:503::/users/itsmarttricks:/etc/test

For more Help on usermod Commands and Arguments

For help on Linux usermod command and its arguments you can refer to the below.

[[email protected] ~]# usermod --help  # For more Help on usermod command
Usage: usermod [options] LOGIN

Options:
  -c, --comment COMMENT         new value of the GECOS field
  -d, --home HOME_DIR           new home directory for the user account
  -e, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE
  -f, --inactive INACTIVE       set password inactive after expiration
                                to INACTIVE
  -g, --gid GROUP               force use GROUP as new primary group
  -G, --groups GROUPS           new list of supplementary GROUPS
  -a, --append                  append the user to the supplemental GROUPS
                                mentioned by the -G option without removing
                                him/her from other groups
  -h, --help                    display this help message and exit
  -l, --login NEW_LOGIN         new value of the login name
  -L, --lock                    lock the user account
  -m, --move-home               move contents of the home directory to the
                                new location (use only with -d)
  -o, --non-unique              allow using duplicate (non-unique) UID
  -p, --password PASSWORD       use encrypted password for the new password
  -s, --shell SHELL             new login shell for the user account
  -u, --uid UID                 new UID for the user account
  -U, --unlock                  unlock the user account
  -Z, --sitsmarttricks-user            new Sitsmarttricks user mapping for the user account

Manual Page of usermod commands

You can refer to the manual page of the Linux usermod command

[[email protected] ~]# man usermod   # Refer the Manual Page of Linux usermod command

Also Read – Managing Users And Groups In Linux – A Complete Guide For Beginners

That’s all, In this article, we have explained Best Linux Usermod Commands With Examples. I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.

Share this:

ITSmarttricks Team

This is Mangesh Dhulap the Founder and Editor of IT SMART TRICKS have 6+ years of Industrial Experience. We expect from our visitors to like, share, and comment on our posts.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.