LinuxCommandLibrary

usermod

Modify user account properties

TLDR

Change a username

$ sudo usermod [[-l|--login]] [new_username] [username]
copy

Change a user ID
$ sudo usermod [[-u|--uid]] [id] [username]
copy

Change a user shell
$ sudo usermod [[-s|--shell]] [path/to/shell] [username]
copy

Add a user to supplementary groups (mind the lack of whitespace)
$ sudo usermod [[-aG|--append --groups]] [group1,group2,...] [username]
copy

Remove a user from specific groups
$ sudo usermod [[-rG|--remove --groups]] [group1,group2,...] [username]
copy

Change a user home directory
$ sudo usermod [[-m|--move-home]] [[-d|--home]] [path/to/new_home] [username]
copy

Lock an account
$ sudo usermod [[-L|--lock]] [username]
copy

Unlock an account
$ sudo usermod [[-U|--unlock]] [username]
copy

SYNOPSIS

usermod [options] LOGIN
Example: usermod -g developers -a -G testers newuser

PARAMETERS

-c, --comment COMMENT
    Sets a new value for the user's GECOS field (e.g., full name, contact information).

-d, --home HOME_DIR
    Assigns a new home directory. Use with -m to move the old directory's contents.

-e, --expiredate EXPIRE_DATE
    Sets the account expiration date in YYYY-MM-DD format.

-f, --inactive INACTIVE_DAYS
    Sets the number of days after a password expires until the account is permanently disabled.

-g, --gid GROUP
    Changes the user's primary group to the specified GROUP (by name or GID).

-G, --groups GROUP1[,GROUP2,...]
    Sets new supplementary groups for the user. Use with -a to append to existing groups.

-a, --append
    Used with -G to add the user to new supplementary groups without removing existing ones.

-l, --login NEW_LOGIN
    Changes the user's login name. The home directory must be manually updated or moved with -d and -m.

-L, --lock
    Locks a user's password, effectively disabling the account.

-U, --unlock
    Unlocks a user's password, re-enabling the account.

-m, --move-home
    Moves the content of the current home directory to the new location specified by -d.

-s, --shell SHELL
    Sets the user's new login shell (e.g., /bin/bash, /bin/sh).

-u, --uid UID
    Assigns a new unique user ID (UID) to the user. Exercise caution, as this does not update file ownership outside the home directory.

DESCRIPTION

The usermod command is a fundamental administrative utility on Linux systems, used to modify various attributes of an existing user account. It provides a flexible way to manage user environments and permissions after an account has been created. Administrators can use usermod to change a user's login name, unique user ID (UID), primary group, supplementary group memberships, home directory location, login shell, password expiration date, and more. This command directly interacts with crucial system configuration files like /etc/passwd, /etc/shadow, and /etc/group. It is generally recommended that the target user not be logged in while performing modifications to prevent potential issues or data inconsistencies.

CAVEATS

When using usermod, it's crucial to ensure the target user is not logged in to avoid potential system instability or data corruption. Changing a user's UID (-u) does not automatically update the ownership of files or directories outside their home directory; these often require manual updates using commands like chown. Changing the login name (-l) does not automatically rename the home directory unless used in conjunction with -d and -m. Avoid using the -p option to set a password directly with a plain-text value in scripts due to security risks; prefer interactive tools like passwd.

REQUIRED PRIVILEGES

usermod requires root privileges to execute, as it modifies critical system-wide account information. Users typically invoke it using the sudo command.

SYSTEM FILES MODIFIED

The command primarily modifies entries in /etc/passwd (user account details), /etc/shadow (encrypted password and expiry information), and /etc/group (user-to-group mappings). Understanding these files is key to advanced user management.

HISTORY

The usermod command is an integral part of the shadow-utils package, which provides a suite of tools for managing user and group accounts on Linux and Unix-like systems. Its core functionality has remained consistent for decades, reflecting its foundational role in system administration for modifying user properties.

SEE ALSO

useradd(8), userdel(8), groupadd(8), groupdel(8), groupmod(8), passwd(1), chown(1)

Copied to clipboard