usermod


usermod (modify user account)

The usermod command is used to modify the properties of an existing user account. This includes changing the username, home directory, shell, and group memberships. It is an essential tool for ongoing user management in a RHEL environment.


Example 1: Change a user's primary group

# Change the primary group of user 'jdoe' to 'staff'
sudo usermod -g staff jdoe

Explanation

The -g option changes the user's primary group. All new files created by jdoe will now be owned by the staff group by default.


Example 2: Add a user to supplementary groups

# Add the user 'msmith' to the 'sysadmin' and 'security' groups
sudo usermod -aG sysadmin,security msmith

Explanation

The -aG option appends the user to supplementary groups. The -a (append) is crucial; without it, the user would be removed from any other supplementary groups they were already in.


Example 3: Change a user's home directory

# Change the home directory for user 'asmith' and move their files
sudo usermod -d /newhome/asmith -m asmith

Explanation

The -d option specifies the new home directory path, and the -m option moves the contents of the old home directory to the new one. This ensures the user doesn't lose their data when their home directory is relocated.


Example 4: Change a user's login name

# Change the username of 'rjohnson' to 'rjay'
sudo usermod -l rjay rjohnson

Explanation

The -l option changes the user's login name. You will also need to manually rename the user's home directory if you want it to match the new login name.


Example 5: Lock a user's account

# Lock the account of user 'llee' to prevent them from logging in
sudo usermod -L llee

Explanation

The -L option locks a user's account by prepending a '!' to their encrypted password in the /etc/shadow file. This is a non-destructive way to temporarily disable an account.


Example 6: Unlock a user's account

# Unlock the previously locked account of user 'llee'
sudo usermod -U llee

Explanation

The -U option unlocks a user's account by removing the '!' from the beginning of their encrypted password. This restores their ability to log in.


Example 7: Set an expiration date for an account

# Set the user account 'guest' to expire on December 31, 2025
sudo usermod -e 2025-12-31 guest

Explanation

The -e option sets an expiration date for the user account in YYYY-MM-DD format. After this date, the account will be disabled. This is useful for temporary accounts.


Example 8: Change a user's shell

# Change the login shell for user 'testuser' to Bash
sudo usermod -s /bin/bash testuser

Explanation

The -s option changes the user's default login shell. This is useful if a user was created with a restricted shell and now needs full shell access, or vice-versa.

UNIX Variants

Solaris: The command is the same: usermod.

FreeBSD: The command is pw usermod.