Cheatsheet & Examples: usermod
The usermod command modifies a user account's properties on a Linux system. It allows administrators to change various attributes associated with a user, such as the user's login name, home directory, shell, groups, and password expiry information.
Changing User's Login Name
Example Usage:
usermod -l new_username old_username
What it does:
Changes the user's login name from old_username to new_username.
Command-line Arguments Explained:
-l new_username: Specifies the new login name for the user.old_username: Specifies the existing username whose login name needs to be changed.
Modifying User's Home Directory
Example Usage:
usermod -d /new/home/directory username
What it does: Changes the user's home directory to the specified path.
Command-line Arguments Explained:
-d /new/home/directory: Sets the new home directory path for the user.username: Specifies the username whose home directory is being modified.
Changing User's Primary Group
Example Usage:
usermod -g groupname username
What it does: Changes the user's primary group to the specified group.
Command-line Arguments Explained:
-g groupname: Specifies the new primary group by its group name.username: Specifies the username to modify.
Adding a User to Supplementary Groups
Example Usage:
usermod -a -G group1,group2 username
What it does: Adds the user to the specified supplementary groups without removing them from their existing groups.
Command-line Arguments Explained:
-a: Appends the user to the supplementary group list. Requires the-Goption.-G group1,group2: Specifies a comma-separated list of group names to add the user to.username: Specifies the username to modify.
Changing User's Shell
Example Usage:
usermod -s /bin/bash username
What it does: Changes the user's default shell to the specified shell.
Command-line Arguments Explained:
-s /bin/bash: Sets the user's login shell (e.g.,/bin/bash,/bin/zsh).username: Specifies the username to modify.
Locking and Unlocking a User Account
Example Usage:
usermod -L username (Lock)
usermod -U username (Unlock)
What it does: Locks or unlocks a user account, preventing or allowing login.
Command-line Arguments Explained:
-L: Locks the user's password, effectively disabling login by prefixing the password hash in/etc/shadowwith!.-U: Unlocks the user's password by removing the!from the password hash in/etc/shadow, enabling login.username: Specifies the username to modify.
Setting Password Expiration Information
Example Usage:
usermod -e YYYY-MM-DD username
What it does: Sets the date after which the user's password will expire.
Command-line Arguments Explained:
-e YYYY-MM-DD: Sets the account expiration date. Use YYYY-MM-DD format.username: Specifies the username to modify.
Modifying User's UID and GID
Example Usage:
usermod -u 1001 username
usermod -g 1002 username
What it does: Changes the user's User ID (UID) and Group ID (GID).
Command-line Arguments Explained:
-u 1001: Changes the user's UID to 1001. This needs to be a unique ID.-g 1002: Changes the user's GID to 1002. This should match an existing group ID.username: Specifies the username to modify.

