The usermod command in Linux: modify user accounts

Introduction

In any multi-user Linux system, account management is a key task for the administrator. The commandusermodallows to modify virtually any attribute of an existing account without the need to delete it and create it again. From changing the login name to adjusting the home directory, going through the allocation of additional groups or blocking the account,usermodbecomes an essential tool to keep the system orderly and safe. In this article we will explore your syntax, the most used options and some practical examples you can apply on your day to day.

What is usermod?

Usermod is part of the set of account management utilities provided by the Shadow-utils package. Unlike useradd, which creates new accounts, usermod works on existing accounts, modifying the fields stored in / etc / passd, / etc / shadow and / etc / group files. Each time you run usermod, the program updates these files in an atomic way, reducing the risk of leaving the system in an inconsistent state. It is important to remember that, to use usermod with full privileges, you need to be root or have sudo permits.

Basic syntax

The general form of the command is:

usermod [options]

Whereoptionsrepresents one or more modifiers that indicate which attribute you want to change; andNAME _ OF _ USERIt's the account you're gonna change. You can combine several options into one invocation, as long as they do not conflict. For example, you can simultaneously change the login name and home directory as long as you also indicate that the content is moved with the -m option.

Most commonly used options

  • -l NEW _ NAME: change the login name. The home directory is not automatically renomerated; for this it is combined with -d and -m.
  • -c COMMENTARY: updates the GECOS field, usually used to store the full name or contact information.
  • -d NEW _ HOME: establishes a new home directory. Without -m, the current content does not move.
  • -m: move the content of the current home directory to the new one specified with -d. It should be used next to -d.
  • -g GID: changes the main group of the user. The GID can be a number or group name.
  • - G GRUPO1, GRUPO2,...: defines the list of additional groups. Each time it is used, replace the above list; to add without deletion, it is used -aG.
  • - L: blocks the account by placing a! in front of the password in / etc / shadow.
  • - U: unlock the account by eliminating that!.
  • -s SHELL: change the user login shell.

Practical examples

Imagine you need to rename the account ofjuanajjuanand move your home directory from/home/juana/home/jjuan. The command would be:

sudo usermod -l jjuan -d / home / jjuan -m juan

In this case, -l changes the login, -d defines the new home and -m ensures that all files are transferred.

Another typical case is to add the useranaGroupdockerwithout taking away your current groups:

sudo usermod -aG docker ana

If you want to update the full name ofluisa «Luis González»You'd use:

sudo usermod -c "Luis González" luis

To temporarily block thetempuserfor safety reasons:

sudo usermod -L tempuser

And to reenable it:

sudo usermod -U tempuser

Precautions and good practices

Before running usermod, check that there are no critical processes associated with the user you are going to modify, especially if you change your shell or home directory, as it could cause failures in running services. It is always recommended to back up the / etc / passd, / etc / shadow and / etc / group files, or at least use the commandvipwto edit them safely. When you change the UID or the GID, remember to update the property of the files belonging to the user withchownandchgrpto avoid orphans. Finally, it documents each change in an administration register to facilitate future audits.

Conclusion

The commandusermodis a powerful and flexible tool that allows Linux administrators to adjust user accounts without the need to recreate them. Knowing your options and applying them with caution will save you time and reduce the risk of errors in system configuration. Whether you need to rename a login, move a home, modify groups or block an account, usermod has the right choice for the task. Practice in a test environment before applying changes in production and keep your systems safe and well organized.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish