Linux usermod Command Explained

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
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.
Hands-on Lab
Create a new user. Avoid affecting real user accounts.
sudo adduser testuser
Add Information to a User. Use
usermodwith the-coption to add a piece of information about a user to the/etc/passwdfile. This helps identify the user and provides space for temporary user-related comments.sudo usermod -c "This is a Test User" testuserTo check the user-related entry in the
/etc/passwdfile, use thegetentcommand:getent passwd testuser
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/passwdfile no longer returns data. However, the same data is now available under the new name 'newuser'.Change User’s UID. A UID (user identifier) is the unique number assigned to the user upon account creation with the
useraddcommand. The/etc/login.defsfile defines the range of UID values (1000-60000). Change a user’s UID with theusermod -ucommand:sudo usermode -u new-UID username
💡Ensure that a user whose UID is being changed is not currently executing any processes.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-doption:sudo usermod -d /var/newuser newuser
The
-doption 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-moption to move the content of the user’s home directory:sudo usermod -d /var/newuser -m newuserChange the user's shell to
/bin/zsh:sudo usermod -s shell username
Lock and Unlock a User. Lock a user account by using the
-Loption.sudo usermod -L username
To unlock the account previously locked, type
usermod -Ufollowed by the account name:sudo usermod -U usernameChange the user password.
sudo usermod -p password username
Change User’s Primary Group:
sudo usermod -g groupname usernameAdd a User to a Supplementary Group:
sudo usermod -G groupname username
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
usermodcommand 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





