usermod
is a command-line utility that allows you to modify a user’s login information.
This article covers how to use the usermod
command to add a user to a group, change a user shell, login name, home directory, and more.
usermod
CommandThe syntax of the usermod
command takes the following form:
usermod [options] USER
Copy
Only root or users with [sudo](https://linuxize.com/post/sudo-command-in-linux/)
access can invoke usermod
and modify a user account. On success, the command does not display any output.
The most typical use case of the usermod
is adding a user to a group.
To add an existing user to a secondary group, use the -a -G
options followed the group’s name and the username:
usermod -a -G GROUP USER
If you want to add the user to multiple groups at once, specify the groups after the -G
option separated with ,
(commas) with no intervening whitespace.
For example, to add the user linuxize
to the games
group, you would run the following command:
sudo usermod -a -G games linuxize
Always use the -a
(append) option when adding a user to a new group. If you omit the -a
option, the user will be removed from the groups not listed after the -G
option.
If the user or group doesn’t exist, the command will warn you.
To change a user’s primary group, invoke the usermod
command with by the -g
option followed the group’s name and the username:
sudo usermod -g GROUP USER
In the following example, we are changing the primary group of the user linuxize
to developers
:
usermod -g developers linuxize
Each user can belong to exactly one primary group and zero or more secondary groups.
To change the GECOS (the full name of the user) information, run the command with the -c
option followed by the new comment and username:
usermod -c "GECOS Comment" USER
Here is an example showing how to add additional information to the user linuxize:
usermod -c "Test User" linuxize
This information is stored in the [/etc/passwd](https://linuxize.com/post/etc-passwd-file/)
file.
#linux