Table of contents
Introduction
In the Linux operating system, user accounts play a crucial role in controlling access and ensuring system security. To achieve this, administrators can use the 'usermod' command to modify existing user accounts effortlessly. In this blog, we will explore various 'usermod' options and learn how to customize user accounts with simple examples.
- Changing the User's UID (User ID):
UID is a unique number assigned to each user in Linux. To modify a user's UID, use the '-u' option with 'usermod':
Let's change the UID of the user 'alice' to 5220:
[root@192 Desktop]# usermod -u 5220 alice
- Updating the Primary Group (GID) of a User:
The primary group determines a user's initial group ownership. To modify the GID of a user, use the '-g' option:
Suppose we want to change 'bob's primary group to 'developers' (GID 5222):
[root@192 Desktop]# usermod -g developers bob
- Adding a Comment (Full Name) to a User:
A comment associated with a user is typically the user's full name or any additional information. Use the '-c' option to add or change the comment:
Let's add the full name "Jane Smith" to the user 'jane':
[root@192 Desktop]# usermod -c "Jane Smiths" jane
- Modifying the Home Directory Path:
A user's home directory is their personal workspace in Linux. To change the home directory path, use the '-d' option:
Suppose we want to move 'john' to a new home directory '/home/johnsmith':
[root@192 Desktop]# usermod -d /home/johnsmith john
- Setting the Login Shell:
The login shell is the program that runs when a user logs in. To change a user's login shell, use the '-s' option:
Let's change 'mary's login shell to '/bin/sh':
[root@192 Desktop]# usermod -s /bin/sh mary
- Assigning Additional Groups to a User:
Users can belong to multiple groups, not just their primary group. To assign additional groups to a user, use the '-G' option:
Suppose we want to add 'accounting' and 'sales' groups to 'jack':
[root@192 Desktop]# usermod -G accounting,sales jack
- Setting an Inactive Password:
Sometimes, you might need to disable a user's password temporarily. Use the '-f' option to set an inactive password:
Let's disable password login for 'guest' for 30 days:
[root@192 Desktop]# usermod -f 30 guest
- Account Expiry:
You can set an expiration date for a user's account using the '-e' option:
Let's set 'peter's account to expire on December 31, 2023:
[root@192 Desktop]# usermod -e '2023-12-31' peter
- Handling Duplicate Usernames:
If you want to create a new user with an existing username, use the '-o' option with 'useradd':
Creating a new user 'mike' with UID 5225, even if a user with UID 5225 already exists:
[root@192 Desktop]# usermod -ou 5225 mike
- Locking and Unlocking User Accounts:
To temporarily prevent a user from logging in, use the '-L' option with 'usermod':
[root@192 Desktop]# usermod -L mike
To unlock the account, use the '-u' option:
[root@192 Desktop]# usermod -u mike
11 . Changing the Login Name (Username):
To change a user's login name (username), use the '-l' option with 'usermod'
Let's change the login name of the user 'old_name' to 'new_name':
[root@192 Desktop]# usermod -l newuser olduser
Conclusion
The 'usermod' command in Linux provides an array of options to customize user accounts. By utilizing these options, administrators can easily manage users' details, enhance security, and efficiently control access to the system. With the examples provided in this blog, you should now feel more confident in managing user accounts like a pro!