Linux usermod Command Explained

The usermod (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 or vim. 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.

Be cautious of doing this on your system, as incorrect usage can lead to system issues or lock you out of accounts.
💡
To create virtual machine you can use the following guide: How to Create an EC2 Instance with AWS SDK

Hands-on Lab

  1. Create a new user. Avoid affecting real user accounts.

     sudo adduser testuser
    

  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.

     sudo usermod -c "This is a Test User" testuser
    

    To check the user-related entry in the /etc/passwd file, use the getent command:

     getent passwd testuser
    

  3. Change the login name of the user:

     sudo usermod -l newname oldname
    

    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:

     sudo usermode -u new-UID username
    

    💡
    Ensure that a user whose UID is being changed is not currently executing any processes.
  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:

     sudo usermod -d /var/newuser newuser
    

    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:

     sudo usermod -d /var/newuser -m newuser
    
  6. Change the user's shell to /bin/zsh:

     sudo usermod -s shell username
    

  7. Lock and Unlock a User. Lock a user account by using the -L option.

     sudo usermod -L username
    

    To unlock the account previously locked, type usermod -U followed by the account name:

     sudo usermod -U username
    
  8. Change the user password.

     sudo usermod -p password username
    

  9. Change User’s Primary Group:

     sudo usermod -g groupname username
    
  10. Add a User to a Supplementary Group:

    sudo usermod -G groupname username
    

  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.

    sudo usermod -d home-folder -s shell -c "comment" -u UID 
    -aG group username

References

  1. usermod Command

  2. How to Use the usermod Command in Linux