Linux Tutorial
Linux File/Directory Management
Linux Packaging And Compression
Vim Text Editor
Linux Text Processing
Linux Software Installation
Linux User/User Group Management
Linux Permission Management
Linux Filesystem Management
Linux Advanced Filesystem Management
Linux System Management
Linux Backup and Recovery
Linux System Service Management
Linux System Log Management
Linux Boot Management
LAMP/LNMP Environment
SELinux Management
The usermod
command in Linux is used to modify user accounts. It allows administrators to change various user settings, such as home directories, default shells, and primary groups. In this tutorial, we'll cover how to use the usermod
command and its options.
Changing a user's home directory
To change a user's home directory, use the -d
flag followed by the new home directory path:
sudo usermod -d /new/home/directory username
You can also use the -m
flag in conjunction with the -d
flag to move the contents of the current home directory to the new one:
sudo usermod -m -d /new/home/directory username
Changing a user's default shell
To change a user's default shell, use the -s
flag followed by the path to the new shell:
sudo usermod -s /bin/tcsh username
Adding a user to a group
To add a user to one or more secondary groups, use the -a
and -G
flags followed by a comma-separated list of group names:
sudo usermod -a -G group1,group2 username
Changing a user's primary group
To change a user's primary group, use the -g
flag followed by the new primary group:
sudo usermod -g new_primary_group username
Changing a user's login name
To change a user's login name, use the -l
flag followed by the new login name:
sudo usermod -l new_login_name old_login_name
Locking a user account
To lock a user account and prevent the user from logging in, use the -L
flag:
sudo usermod -L username
Unlocking a user account
To unlock a previously locked user account, use the -U
flag:
sudo usermod -U username
Modifying a user account with multiple options
You can combine multiple options to modify a user account with specific settings:
sudo usermod -d /new/home/directory -s /bin/tcsh -a -G group1,group2 -g new_primary_group username
In conclusion, the usermod
command is a versatile tool for managing user accounts in Linux. By understanding its various options, you can modify user accounts to suit your system's requirements and maintain a secure and organized user environment.
How to use the Linux usermod
command:
usermod
command is used to modify user account attributes in Linux.# Example: Modifying user attributes sudo usermod options username
Modifying user attributes in Linux with usermod
:
usermod
allows for the modification of various user attributes, such as username, home directory, login shell, etc.# Example: Modifying user attributes sudo usermod -l newname -d /newhome -s /bin/bash oldname
Changing user details like username and home directory:
-l
option changes the username, and the -d
option changes the home directory.# Example: Changing username and home directory sudo usermod -l newname -d /newhome oldname
Adding and removing users from groups using usermod
:
-G
option adds the user to specified groups, and -g
sets the primary group.# Example: Adding and removing users from groups sudo usermod -G group1,group2 -g newprimarygroup username
Setting expiration date for a user with usermod
:
-e
option sets the expiration date for the user account.# Example: Setting expiration date for a user sudo usermod -e 2023-12-31 username
Adjusting login shell and password aging with usermod
:
-s
option sets the login shell, and -e
and -I
options adjust password aging policies.# Example: Adjusting login shell and password aging sudo usermod -s /bin/zsh -e 2024-12-31 -I 30 username
Modifying UID and GID of a user in Linux:
-u
option sets the UID, and -g
sets the GID of the user.# Example: Modifying UID and GID sudo usermod -u 1001 -g 1001 username
Batch modifying user attributes with usermod
:
usermod
command for efficiency.# Example: Batch modifying user attributes sudo usermod -l newname -d /newhome -s /bin/bash -G group1,group2 -e 2023-12-31 username
usermod
examples for different scenarios in Linux:
usermod
can be adapted for various scenarios, such as changing multiple attributes simultaneously.# Example: Adapting usermod for different scenarios sudo usermod -l newname -G group1,group2 -s /bin/bash username
Troubleshooting common issues with usermod
: