Linux usermod Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to modify user account properties, change a user's primary group, and disable user account expiration using the usermod command in Linux. The usermod command is a powerful tool for managing user accounts and their associated settings. Through practical examples, you will gain hands-on experience in performing these user management tasks, which are essential for system administration and user access control.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/UserandGroupManagementGroup -.-> linux/groupadd("`Group Adding`") linux/UserandGroupManagementGroup -.-> linux/id("`User/Group ID Displaying`") linux/UserandGroupManagementGroup -.-> linux/useradd("`User Adding`") linux/UserandGroupManagementGroup -.-> linux/usermod("`User Modifying`") linux/UserandGroupManagementGroup -.-> linux/passwd("`Password Changing`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") subgraph Lab Skills linux/groupadd -.-> lab-422987{{"`Linux usermod Command with Practical Examples`"}} linux/id -.-> lab-422987{{"`Linux usermod Command with Practical Examples`"}} linux/useradd -.-> lab-422987{{"`Linux usermod Command with Practical Examples`"}} linux/usermod -.-> lab-422987{{"`Linux usermod Command with Practical Examples`"}} linux/passwd -.-> lab-422987{{"`Linux usermod Command with Practical Examples`"}} linux/touch -.-> lab-422987{{"`Linux usermod Command with Practical Examples`"}} end

Modify User Account Properties

In this step, we will learn how to modify user account properties using the usermod command in Linux.

The usermod command is used to modify an existing user account. We can use it to change the user's login name, home directory, shell, password expiration date, and other properties.

Let's start by modifying the user's full name and shell:

sudo usermod -c "John Doe" -s /bin/zsh labex

Example output:

No changes

The -c option sets the user's full name, and the -s option sets the user's login shell to /bin/zsh.

Next, let's change the user's home directory:

sudo usermod -d /home/newuser labex

Example output:

usermod: user 'labex' does not exist

Oops, the user labex doesn't exist. Let's create the user first:

sudo useradd -m -s /bin/zsh labex

Now, let's try changing the home directory again:

sudo usermod -d /home/newuser labex

Example output:

No changes

The -d option sets the user's home directory to /home/newuser.

Finally, let's disable the user account by locking the password:

sudo usermod -L labex

Example output:

No changes

The -L option locks the user's password, effectively disabling the account.

Change User's Primary Group

In this step, we will learn how to change a user's primary group using the usermod command.

The primary group is the default group that a user belongs to. When a user creates a new file or directory, the file/directory will be owned by the user and the user's primary group.

Let's start by creating a new group called "developers":

sudo groupadd developers

Example output:

No output

Now, let's add the labex user to the "developers" group as the primary group:

sudo usermod -g developers labex

Example output:

No changes

The -g option sets the user's primary group to "developers".

To verify the change, we can check the user's group membership:

id labex

Example output:

uid=1000(labex) gid=1001(developers) groups=1001(developers)

As you can see, the user's primary group is now "developers".

Next, let's create a new file and check the ownership:

touch ~/project/test.txt
ls -l ~/project/test.txt

Example output:

-rw-r--r-- 1 labex developers 0 May 15 12:34 /home/labex/project/test.txt

The file is owned by the labex user and the "developers" group, as expected.

Disable User Account Expiration

In this step, we will learn how to disable the expiration date for a user account using the usermod command.

By default, user accounts in Linux do not have an expiration date. However, you can set an expiration date for a user account, after which the account will be disabled.

To disable the expiration date for the labex user, we can use the usermod command with the -e option:

sudo usermod -e "" labex

Example output:

No changes

The -e option sets the expiration date for the user account. By setting it to an empty string "", we effectively disable the expiration date.

To verify that the expiration date has been disabled, we can use the chage command to display the user's account information:

sudo chage -l labex

Example output:

Last password change                                    : May 15, 2023
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : never
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

As you can see, the "Account expires" field is set to "never", indicating that the user account expiration has been disabled.

Now, let's create a file and check the ownership:

touch ~/project/test2.txt
ls -l ~/project/test2.txt

Example output:

-rw-r--r-- 1 labex developers 0 May 15 12:34 /home/labex/project/test2.txt

The file is owned by the labex user and the "developers" group, as expected.

Summary

In this lab, you learned how to modify user account properties using the usermod command, including changing the user's full name, login shell, home directory, and locking the user's password to disable the account. You also learned how to change a user's primary group, creating a new group and setting it as the primary group for a user. These skills are useful for managing user accounts and permissions in a Linux environment.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like