# Linux usermod Command Explained

The [`usermod`](https://phoenixnap.com/kb/usermod-linux) (short for *user modification*) is a powerful tool that allows system administrators to make various changes to user accounts, such as updating user information, adding the user to new groups, changing the user's login name or home directory, and more. It ***modifies configuration files containing user account information***. Those files are:

* `/etc/passwd` – information regarding users’ accounts
    
* `/etc/shadow` – user security-related information
    
* `/etc/group` – information about groups
    
* `/etc/gshadow` – group security-related information
    
* `/etc/login.defs` – shadow password suite configuration
    

It is possible to edit the above-mentioned files directly, using a text editor such as [`nano`](https://phoenixnap.com/kb/use-nano-text-editor-commands-linux) or [`vim`](https://www.vim.org/download.php). However, `usermod` makes the process faster and more straightforward.

Because `usermod` modifies system configuration files, it typically requires superuser (root) privileges to run.

## Hands-on Lab Overview

This hands-on explains the use of the `usermod` command, along with its numerous options. It was done in the Ubuntu 22.04 virtual environment.

<div data-node-type="callout">
<div data-node-type="callout-emoji">❗</div>
<div data-node-type="callout-text">Be cautious of doing this on your system, as incorrect usage can lead to system issues or lock you out of accounts. </div>
</div>

 <div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">To create virtual machine you can use the following guide: <a target="_blank" rel="noopener noreferrer nofollow" href="https://karlygash-yakiyayeva.dev/how-to-create-an-ec2-instance-with-aws-sdk" style="pointer-events: none">How to Create an EC2 Instance with AWS SDK</a></div>
</div>

## Hands-on Lab

1. **Create a new user.** Avoid affecting real user accounts.
    
    ```bash
    sudo adduser testuser
    ```
    
    ![](https://i.imgur.com/71s4OzR.png align="center")
    
2. **Add Information to a User.** Use `usermod` with the `-c` option to add a piece of information about a user to the `/etc/passwd` file. This helps identify the user and provides space for temporary user-related comments.
    
    ```bash
    sudo usermod -c "This is a Test User" testuser
    ```
    
    To check the user-related entry in the `/etc/passwd` file, use the `getent` command:
    
    ```bash
    getent passwd testuser
    ```
    
    ![](https://i.imgur.com/u9HxHdh.png align="center")
    
3. **Change the login name** of the user:
    
    ```bash
    sudo usermod -l newname oldname
    ```
    
    ![](https://i.imgur.com/q8JWMDH.png align="center")
    
    As the example above shows, using the old username to check the `/etc/passwd` file no longer returns data. However, the same data is now available under the new name 'newuser'.
    
4. **Change User’s UID.** A UID (user identifier) is the unique number assigned to the user upon account creation with the `useradd` command. The `/etc/login.defs` file defines the range of UID values (1000-60000). Change a user’s UID with the `usermod -u` command:
    
    ```bash
    sudo usermode -u new-UID username
    ```
    
    ![](https://i.imgur.com/C5hGtH4.png align="center")
    
    <div data-node-type="callout">
    <div data-node-type="callout-emoji">💡</div>
    <div data-node-type="callout-text">Ensure that a user whose UID is being changed is not currently executing any processes.</div>
    </div>
    
5. **Set User’s Home Directory.** When a new user is created in Linux, the system automatically creates a home folder for them in `/home/[username]`. To change the location of the user’s home folder, use the `-d` option:
    
    ```bash
    sudo usermod -d /var/newuser newuser
    ```
    
    ![](https://i.imgur.com/ThFOxmN.png align="center")
    
    The `-d` option does not move the home folder’s content to the new location. If the user has previously utilized the home folder to store their files, add the `-m` option to move the content of the user’s home directory:
    
    ```bash
    sudo usermod -d /var/newuser -m newuser
    ```
    
6. **Change the user's shell** to `/bin/zsh`:
    
    ```bash
    sudo usermod -s shell username
    ```
    
    ![](https://i.imgur.com/va9mjDB.png align="center")
    
7. **Lock and Unlock a User.** Lock a user account by using the `-L` option.
    
    ```bash
    sudo usermod -L username
    ```
    
    ![](https://i.imgur.com/fYvinJp.png align="center")
    
    To **unlock** the account previously locked, type `usermod -U` followed by the account name:
    
    ```bash
    sudo usermod -U username
    ```
    
8. **Change the user password**.
    
    ```bash
    sudo usermod -p password username
    ```
    
    ![](https://i.imgur.com/dPq2e1n.png align="center")
    
9. **Change User’s Primary Group:**
    
    ```bash
    sudo usermod -g groupname username
    ```
    
10. **Add a User to a Supplementary Group:**
    
    ```bash
    sudo usermod -G groupname username
    ```
    
    ![](https://i.imgur.com/Lu0PHlX.png align="center")
    
11. **Change User’s Account Using Multiple Options:**
    
    Use multiple options in one command for a more convenient way to edit a user. The example below shows a `usermod` command that:
    
    * changes the home folder and the shell,
        
    * adds a comment,
        
    * changes the login name,
        
    * changes the UID, and
        
    * adds the user to a supplementary group.
        
    
    ```bash
    sudo usermod -d home-folder -s shell -c "comment" -u UID 
    -aG group username
    ```
    
    ![](https://i.imgur.com/yvxoHdb.png align="center")
    

## References

1. [usermod Command](https://www.ibm.com/docs/en/aix/7.1?topic=u-usermod-command)
    
2. [How to Use the usermod Command in Linux](https://phoenixnap.com/kb/usermod-linux)
