Modify User Password Aging and Group Membership with chage and usermod
In this step, you will manage more advanced user properties. You'll learn how to enforce password security policies using chage to control password expiration, and how to manage a user's group memberships using usermod to control their permissions and access rights.
First, let's examine the password aging information for the student1 account. The chage (change age) command with the -l (list) flag displays these details.
sudo chage -l student1
The output will show the default settings for the account. The dates will correspond to when you created the user.
Last password change : Dec 08, 2024
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
For security, it's a good practice to require users to change their passwords periodically. Let's set a policy where the password must be changed every 90 days (-M 90), can only be changed once every 7 days (-m 7), and the user gets a warning 14 days before it expires (-W 14).
sudo chage -M 90 -m 7 -W 14 student1
Now, view the settings again to confirm your changes:
sudo chage -l student1
The output will reflect the new policy:
Last password change : Dec 08, 2024
Password expires : Mar 08, 2025
Password inactive : never
Account expires : never
Minimum number of days between password change : 7
Maximum number of days between password change : 90
Number of days of warning before password expires : 14
Next, let's modify group memberships. A user belongs to a primary group and can belong to multiple secondary (or supplementary) groups. You can view a user's groups with the id command.
id student1
uid=5001(student1) gid=5001(student1) groups=5001(student1)
This shows that student1's primary group is student1 (gid=5001) and they are not yet in any secondary groups.
Let's create a new group called developers and add student1 to it. First, create the group:
sudo groupadd developers
Now, add student1 to this new group using usermod. The -aG flags are crucial: -G specifies the secondary groups, and -a appends the user to the group without removing them from other groups.
sudo usermod -aG developers student1
Check the user's groups again:
id student1
The output now includes the developers group:
uid=5001(student1) gid=5001(student1) groups=5001(student1),1002(developers)
What happens if you forget the -a flag? Let's create another group, testers, and add student1 to it using only -G.
sudo groupadd testers
sudo usermod -G testers student1
Now check the groups one more time:
id student1
uid=5001(student1) gid=5001(student1) groups=5001(student1),1003(testers)
Notice that student1 is no longer in the developers group. Using usermod -G without -a replaces all existing secondary groups with the new list. To have the user in both groups, you must either list all groups (-G developers,testers) or use the append flag. Let's fix this by re-adding student1 to the developers group correctly.
sudo usermod -aG developers student1
Verify the final state. The user should now be a member of both groups.
id student1
uid=5001(student1) gid=5001(student1) groups=5001(student1),1003(testers),1002(developers)