usermod Command in Linux
Build an AI-First Career, Master the Complete Skillset
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
AI Forward Deployed Engineer Program
Full-stack engineering, production AI and client-facing consulting
+1000 moreOverview
The usermod command in Linux is a crucial tool for system administrators. It allows them to modify existing user accounts, altering properties such as home directories, shell types, and group affiliations. Understanding how to use the usermod command in Linux can greatly enhance your ability to manage users effectively.
Syntax of usermod Command in Linux
The basic syntax of the usermod command in Linux is as follows:
Where:
- usermod: This is the command itself, telling Linux to modify a user.
- [options]: These are the different flags or parameters that you can use with the command to specify what modifications you want to make.
- LOGIN: This is the username of the account you wish to modify.
Options in usermod Command in Linux
-
-d, --home: This option changes the user's home directory.
For example -
Output:
This command changes the home directory of 'username' to '/new/home/directory'.
-
-s, --shell: This option changes the user's login shell.
For example -
Output:
This command changes the login shell of 'username' to '/bin/ksh'.
-
-a, --append: This option is used with -G and adds the user to the supplementary group(s).
For example -
Output:
This command adds 'username' to 'group1' and 'group2' without removing them from any other groups.
How Scaler Transformed Careers in Different Fields
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Example Usages
-
Changing a user's home directory:
Output:
Explanation: This usage of the usermod command in Linux alters the home directory of the user.
-
Adding a user to additional groups:
Output:
Explanation: This usage of the usermod command in Linux adds the user to additional groups without removing them from their current groups.
Tips
-
Always make sure you have administrative rights or root access before attempting to modify user accounts with the usermod command in Linux. Without these rights, the command may not execute successfully.
-
When changing a user's home directory, it's a good practice to also move the contents of the old directory to the new one using the -m or --move-home option.
-
Be cautious when changing the login shell. Make sure the new shell is installed and configured correctly. A wrong shell can prevent the user from logging in.
Turn Learning into Career Growth
Advanced Use Cases of usermod Command in Linux
-
Locking a user's password:
Output:
Explanation: This command locks the password of 'username', making it impossible for them to login until the password is unlocked.
-
Expire a user's password:
Output:
Explanation: This command sets the expiry date of 'username''s password to 1 day after the account was created, effectively expiring the password immediately.
-
Change user's primary group:
Output:
Explanation: This command changes 'username''s primary group to 'new_group'.
Conclusion
-
The usermod command in Linux is a powerful tool for managing user accounts.
-
It can modify various properties of a user account including the home directory, login shell, and group affiliations.
-
Advanced uses of the command include locking and expiring passwords, and changing the primary group of a user.
-
Remember to use the usermod command with caution, as incorrect usage can lead to user login issues.