How to use usermod command on Ubuntu Linux with examples

The usermod command is used by system administrators to change users’ account settings similar to the passwd command.

For new users and students looking for ways to learn how to use and manage Linux systems, the easiest place to start might be on Ubuntu Linux OS.

Ubuntu is an open-source Linux operating system that runs on desktops, laptops, servers, and other devices.

When you’re ready to learn how to use the usermod commands, follow the guide below:

About usermod command:

The usermod command is used by system administrators to change users’ account settings similar to the passwd command. Like using your mouse and keyboard to change account settings in the GUI. the usermod is the way to do it on the command line.

Syntax:

The syntax is the rule and format of how the usermod command can be used. the syntax’s options can be reordered. but a straight format must be followed.

Below is an example syntax of how to use the usermod command.

usermod [OPTION]. LOGIN.

Options:

The command line options are switches or flags that determined how the commands are executed or controlled. they modify the behavior of the command. they are separated by spaces and followed after the command’s options.

Below are some options for the usermod command:

LOGIN.Replace LOGIN .. with the name of the account, if you want to change its settings. If the account doesn’t already exist, the command will fail and not execute.
-c, –comment COMMENT
Use the -c or –command COMMENT option to set or change the user’s account comment field.
-d, –home HOME_DIRUse the -d or –home HOME_DIR option to change or set a new home directory for the user account.
-f, –inactive INACTIVEUse the -f or –inactive INACTIVE option to set password inactive after expiration to INACTIVE
 -g, –gid GROUPUse the -g or –gid GROUP option to force use GROUP as new primary group
-G, –groups GROUPSUse the -G or –groups GROUP option to set new list of supplementary GROUPS
-a, –append  Use the -a or –append option to append the user to the supplemental GROUPS
-l, –login NEW_LOGIN Use the -l or –login NEW_LOGIN option to create new value of the login name
-h, –help Display help message and exit.
-L, –lock Use the -L or –lock option to lock the user account

Examples:

Below are some examples of how to run and use the usermod on Ubuntu Linux.

If you want to add an account name John to the sudo group, run the commands below.

usermod -a -G sudo john

If you wish to change John’s account Full Name or comment field, run the usermod command and specify John’s account name. in this case, it’s called john.

usermod -c "John Doe" john

If you’re not logged in as a root account, you may have to use the sudo command it it.

sudo usermod -c "John Doe" john

To change John’s account login name to paul, run the commands below.

sudo usermod -l paul john

To unlock John’s account, run the command below

sudo usermod -U john

To lock John’s account, run the commands below.

sudo usermod -L john

To change John’s home directory, run the commands below

sudo usermod -d /home/newHomeDir -m johnNewHome

When you run usermod with the –help option, you’ll see the help text below:

Usage: usermod [options] LOGIN

Options:
  -c, --comment COMMENT         new value of the GECOS field
  -d, --home HOME_DIR           new home directory for the user account
  -e, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE
  -f, --inactive INACTIVE       set password inactive after expiration
                                to INACTIVE
  -g, --gid GROUP               force use GROUP as new primary group
  -G, --groups GROUPS           new list of supplementary GROUPS

That’s it!

Hope you like it and please come back soon for more Ubuntu Linux commands!