Introduction
Before we begin talking about how to use usermod
command in Linux, let's briefly understand - What is usermod
?
usermod
is a command-line tool that lets you change a user's login credentials. It allows administrators to make changes to user accounts, such as modifying the username, user ID, home directory, login shell, group membership, and more. usermod
provides a convenient way to manage user settings and permissions on a system without having to manually edit configuration files.
In this tutorial, you will use usermod
command in Linux. We will also address a few FAQs on how to use usermod
command in Linux.
usermod
Command
The usermod
command is written in the following format:
usermod [options] USER
usermod
can only be used by root or users with the sudo ability to alter a user account. The command does not provide any output if it succeeds.
Add a User to a Group
Adding a user to a group is the most common use case of the usermod
.
Use the -a -G
arguments followed by the group's name and the username to add an existing user to a secondary group:
usermod -a -G GROUP USER
If you want to add the user to multiple groups at the same time, supply the groups following the -G
option, separated by, (commas), with no whitespace between them.
For example, you could use the following command to add the user vegauser
to the games group:
sudo usermod -a -G games vegauser
When adding a user to a new group, always use the -a
(append) option. If the -a
option is not used, the user will be removed from all groups that are not included after the -G
option.
The command will alert you if the user or group does not exist.
Change User Primary Group
Invoke the usermod
command with the -g
option followed by the group's name and the username to modify a user's primary group:
sudo usermod -g GROUP USER
We'll change the primary group of the user vegauser to developers in the following example:
sudo usermod -g developers vegauser
A user can only be a member of one primary group and one or more secondary groups.
Changing the User Information
Run the command with the -c
option followed by the new comment and username to alter the GECOS (complete name of the user) information:
sudo usermod -c "GECOS Comment" USER
An example of how to provide additional information to the user vegauser is as follows:
sudo usermod -c "Test User" vegauser
The /etc/passwd
file contains this information.
Changing a User Home Directory
User home directories are established inside the /home directory on most Linux systems and are called after the user's name.
If you need to modify the user's home directory for any reason, use the usermod command with the -d option, followed by the absolute path to the new home directory and the user's name:
usermod -d HOME_DIR USER
The content of the user's home directory is not moved to the new one by default. Use the -m
option to relocate the material. If the New directory does not already exist, it will be automatically created.
usermod -d HOME_DIR -m USER
Here's how you can change www-data
data's directory to /var/www
:
sudo usermod -d /var/www www-data
Changing a User Default Shell
The default shell is the one that is launched after you log in. The default shell on most Linux systems is Bash Shell by default.
To change the user's default shell, run the following command with the -s
option, followed by the shell's absolute path and the user's name:
usermod -s SHELL USER
We'll change the user shell to Zsh in the example below:
sudo usermod -s /usr/bin/zsh vegauser
Displaying the contents of the /etc/shells
file will tell you what shells are accessible on your system.
Changing a User UID
Each user is assigned a unique UID (user identification). The operating system uses it to refer to a user.
To update the user's UID, use the -u
option, followed by the new UID and the user's name:
usermod -u UID USER
The following is an example of how to modify the "UID" number to "2361":
sudo usermod -u 2361 vegauser
The user's mailbox file's UID, as well as the UID of the files owned by the user and located in the user's home directory, will be immediately modified. All other files require manual ownership changes.
Changing a Username
Although it is not particularly common, you may need to change the name of an existing user on occasion. To change the username, use the -l
option:
usermod -l NEW_USER USER
We'll rename the user vegauser to vegastack in the below example:
sudo usermod -l vegastack vegauser
You may also wish to update the user's home directory to reflect the new username when updating the username.
Setting a User Expiry Date
The user account's expiry date is the day on which it will be disabled. Use the -e
option to set the user's expiration date:
sudo usermod -e DATE USER
The expiration date must be entered in the YYYY-MM-DD format.
You would use the following command to disable the user vegauser on 2022-12-11, for example:
sudo usermod -e "2022-12-11" vegauser
To disable the expiration of an account, set an empty expiry date:
sudo usermod -e "" vegauser
To see the user's expiry date, use the chage -l
command:
sudo chage -l vegauser
Output
Last password change : Sept 24, 2019
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
The /etc/shadow
file stores the expiration date.
Locking and Unlocking a User Account
You can lock a user account with the -L option:
usermod -L USER
In front of the encrypted password, the commands will place an exclamation point (!) symbol. The user will not be able to log in to the system using password authentication if the password field in the /etc/shadow
file contains an exclamation point. Other means of authentication, such as key-based authentication or switching to the user, are still permitted. You must also set the expiration date to 1 if you wish to lock the account and disable all login methods.
The following examples demonstrate how to lock the vegauser user:
sudo usermod -L vegauser
sudo usermod -L -e 1 vegauser
Run usermod
with the -u
option to unlock a user:
sudo usermod -U USER
FAQs to use Usermod command in Linux
How can I change a user's username using usermod?
To change a user's username, run the command: sudo usermod -l new_username old_username
Can I change a user's home directory using usermod?
Yes, you can change a user's home directory by executing: sudo usermod -d /new/home/directory username
How can I add a user to a supplementary group using usermod?
Use the command: sudo usermod -a -G groupname username
Can I change a user's primary group with usermod?
Yes, you can change a user's primary group by running: sudo usermod -g new_primary_group username
Can I set an expiration date for a user account using usermod?
Yes, execute: sudo usermod -e YYYY-MM-DD username
to set an expiration date for the user account.
How can I change a user's login shell with usermod?
Use the command: sudo usermod -s /path/to/new/shell username
How can I lock or unlock a user account using usermod?
To lock a user account, run: sudo usermod -L username
. To unlock, use: sudo usermod -U username
Conclusion
We hope this detailed guide helped you understand how to use usermod
command in Linux.
If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.