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
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – 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
- If you want to add an existing user account to a group, that can be done with the
-Goptions. 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
- 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
- Another common use for the usermod command is to lock a user’s account with the
-Loption. What this actually does is put a
!in front of their encrypted password inside the
/etc/shadowfile, effectively disabling the password.
# usermod -L linuxconfig
- To unlock the user account, which would remove the
/etc/shadow, use the
# usermod -U linuxconfig
- If you want a user account to expire on a certain day, you can use the
-eoption with usermod command on Linux. You’ll need to specify your date format in YYYY-MM-DD.
# usermod -e 2023-01-15 linuxconfig
- If you need to disable the expiration date for an account, run the same command but use a blank date
# usermod -e "" linuxconfig
- You can also use the usermod command to change the username for an account with the
-loption. 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
# usermod -l luke linuxconfig
- Next, let’s change the home directory for
lukeso that it’s set to
/home/luke. You’ll use the
-doption to make the change, but supplying the
-moption 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
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.
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
- Use the
-soption 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
linuxconfigin this example.
# usermod -s /usr/bin/zsh linuxconfig
-coption can be used to change the name (not username) for a user. This information is stored inside of the
/etc/passwdfile, and may be referenced by various programs across the system. In this example, we’ll set Luke Reynolds as the name for account
# usermod -c "Luke Reynolds" linuxconfig
- Every user on a Linux system is given a unique ID when first created. It’s possible to use the usermod command and the
-uoption to change this number. For example, we will set the UID of user
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
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.