usermod command in Linux with examples

The usermod command in Linux is used to modify user accounts. In particular, it’s used for changing various attributes for accounts that are already created, since a separate command is used when creating a brand new account on Linux.

Most users, and especially administrators, of a Linux system will eventually run into the need to do some user account management. This may include adding or deleting a user from the system, or adding a user to a group and removing a user from a group.

In this guide, you’ll learn how to use the usermod command in Linux through examples. Follow along below to see learn about the various options that you can use with this command.

In this tutorial you will learn:

  • How to use the usermod command on Linux


Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software usermod
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options

The usermod command allows us to modify certain attributes of user accounts on Linux. Most people get their first introduction to this command whenever they need to add or remove users from groups, but there are a lot more things you can do with the usermod command. See below for some of the most common examples.


usermod command in Linux Basic Examples

  1. If you want to add an existing user account to a group, that can be done with the -a and -G options. This is, for many system administrators, one of the most common uses of the usermod command. Check the following command where we add “linuxconfig” to the “sambashare” group. Note the syntax here, which is a little tricky, because you need to specify the group name and then the user name.
    # usermod -a -G sambashare linuxconfig
  2. To add an existing user to multiple groups at the same time, use the same syntax as above while separating each group name by a comma. Check this example where we add linuxconfig to three more groups, and then verify that it was successful.
    # usermod -a -G adm,dip,lxd linuxconfig
    # groups linuxconfig
    linuxconfig : linuxconfig adm cdrom dip plugdev lxd sambashare
  3. Another common use for the usermod command is to lock a user’s account with the -L option. What this actually does is put a ! in front of their encrypted password inside the /etc/shadow file, effectively disabling the password.
    # usermod -L linuxconfig
  4. To unlock the user account, which would remove the ! character in /etc/shadow, use the -U option.
    # usermod -U linuxconfig
  5. If you want a user account to expire on a certain day, you can use the -e option with usermod command on Linux. You’ll need to specify your date format in YYYY-MM-DD.
    # usermod -e 2023-01-15 linuxconfig
  6. If you need to disable the expiration date for an account, run the same command but use a blank date "" after the -e option.
    # usermod -e "" linuxconfig
  7. You can also use the usermod command to change the username for an account with the -l option. If you decide to do this, it’s also advisable that you consider changing the user’s home directory to correspond to their new username, but we’ll cover that in the next example. This command would change user name linuxconfig to luke.
    # usermod -l luke linuxconfig
  8. Next, let’s change the home directory for luke so that it’s set to /home/luke. You’ll use the -d option to make the change, but supplying the -m option will also create the new directory if it doesn’t already exist, and it’ll move all the user’s content from their old home directory to the new directory.
    # usermod -d /home/luke -m luke

    If you didn’t want to move all the old content, just omit the -m option.

You can always use the man command to read more about the usermod command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Advanced Usage

The usermod command is pretty simple, but it comes packed with a lot of options. Many of these options fly under the radar, and even some seasoned system administrators may not know of them. However, they can definitely come in handy in various situations. In this section of the guide, we’ll show you a few of the lesser known options of the usermod command that we think are useful.

usermod command in Linux Advanced Examples

  1. Use the -s option with usermod in order to specify the default shell for a user. This will be the shell that they are presented with whenever they log into the system. On most Linux systems, this will be the Bash shell by default. So, instead, let’s set the zsh shell as the default shell for user linuxconfig in this example.
    # usermod -s /usr/bin/zsh linuxconfig
  2. The -c option can be used to change the name (not username) for a user. This information is stored inside of the /etc/passwd file, and may be referenced by various programs across the system. In this example, we’ll set Luke Reynolds as the name for account linuxconfig.
    # usermod -c "Luke Reynolds" linuxconfig
  3. Every user on a Linux system is given a unique ID when first created. It’s possible to use the usermod command and the -u option to change this number. For example, we will set the UID of user linuxconfig to 100. The user’s mailbox, and any files which the user owns and which are located in the user’s home directory will have the file user ID changed automatically. The ownership of files outside of the user’s home directory must be fixed manually.
    # usermod -u 100 linuxconfig

Closing Thoughts

In this guide, we learned all about the usermod command on Linux. This is the main command used in order to modify various aspects of user accounts on a Linux system. Some of its most common uses are to add users to a group or lock a user account. As you’ve seen in this guide, it’s packed with useful options that make it an essential command to know when managing the user accounts on Linux.