Linux usermod Command Explained

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
Search for a command to run...

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
Any commands that require root privileges, which are the commands that generally make changes to the system are going to need access to the root or sudo. Sudo can be used in place of root, which allows particular users to run various commands as the ...
The git log command is used to view the commit history of a Git repository. You can customize the output format to show specific details in a more readable or structured way. Below are some useful variations and formatting options for git log: To sh...

Overview This article will teach us how to: Install Ubuntu Server 24.04 on a laptop from scratch Install Ubuntu server by using Vagrant tool with ISO image Prerequisites: A laptop without any OS installed; A virtualization software Virtualbox t...

The /etc/passwd and /etc/shadow files are the backbone of Linux user management. Together, they store user account information and handle authentication securely. This article provides a hands-on guide to understanding these files, their structure, a...

In Linux, links are powerful tools that allow you to create references to files and directories. There are two main types of links: hard links and soft links (also known as symbolic links or symlinks). Understanding the differences between these two ...

A hostname is a human-friendly name given to a computer. It is a unique identifier that allows us to identify the machine in various network communications, making it easier to locate and manage devices. Type: hostname To see where it is stored, typ...

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.
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.
Create a new user. Avoid affecting real user accounts.
sudo adduser testuser

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

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'.
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

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
Change the user's shell to /bin/zsh:
sudo usermod -s shell username

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
Change the user password.
sudo usermod -p password username

Change User’s Primary Group:
sudo usermod -g groupname username
Add 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 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
