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.

[root@localhost ~]# usermod -c "my name is john" john   # Set comment for a User Account

# Confirm the Setting

[root@localhost ~]# 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).

[root@localhost ~]# usermod -L john   # Lock a User Account

# Confirm the Setting

[root@localhost ~]# 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.

[root@localhost ~]# usermod -U john   # Unlock a User Account

# Confirm the Setting

[root@localhost ~]# 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.

[root@localhost ~]# id john
uid=502(john) gid=502(john) groups=502(john)

# Confirm the Setting

[root@localhost ~]# usermod -u 555 john   # Change UID of a User Account
[root@localhost ~]# 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]

[root@localhost ~]# 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.

[root@localhost ~]# id john
id: john: No such user

Now check the user Information with New Login Name.

[root@localhost ~]# 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.

[root@localhost ~]# usermod -e 2017-06-15 ricky  # Set Expiry date for a User Account

# Confirm the Setting

[root@localhost ~]# 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.

[root@localhost ~]# id tony
uid=556(tony) gid=556(tony) groups=556(tony)

# Confirm the Setting

[root@localhost ~]# usermod -g admins tony   # Change the Primary Group
[root@localhost ~]# 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.

[root@localhost ~]# usermod -a -G developers,workers ricky  # Add Supplementary/ Secondary to a User Account

# Confirm the Setting

[root@localhost ~]# 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.

[root@localhost ~]# usermod -d /users/itsmarttricks itsmarttricks # Change the Home Directory

# Confirm the Setting

[root@localhost ~]# 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.

[root@localhost ~]# usermod -f 10 itsmarttricks # Set Password Inactive for a User Account

# Confirm the Setting

[root@localhost ~]# 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.

[root@localhost ~]# useradd shree   # Creating a New User Account
[root@localhost ~]# 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.

[root@localhost ~]# cd /home/shree/   # Changing to User's Home Directory
[root@localhost shree]# ls
[root@localhost shree]# touch file{1,2,3,4,5}.txt   # Creating some files
[root@localhost 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.

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

Confirm if data moved or not.

[root@localhost ~]# cd /users/shree/
[root@localhost 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.

[root@localhost shree]# cd /home/
[root@localhost 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.

[root@localhost ~]# usermod -s /etc/test itsmarttricks   # Change Shell Script of a User Account
[root@localhost ~]# 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.

[root@localhost ~]# 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

[root@localhost ~]# 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:
WhatsApp Channel Join Now
Telegram Channel Join Now
Instagram Channel Join Now

Leave a Comment

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