Applying Password Policies to Existing Users
In this final step, we will learn how to apply our new password policies to existing users and manage user password information.
Understanding Password Expiration Management
When you change password policies in /etc/login.defs
, the new settings only apply to newly created users or when you manually update existing users. Let's see how to manage existing users:
-
First, check the current status of a user account:
sudo chage -l labex
This displays the password aging information for the current user.
-
To force a user to change their password at next login:
sudo chage -d 0 testuser
This sets the last password change date to 0, forcing a password change at next login.
-
To manually set the password expiration date:
sudo chage -E $(date -d "90 days" +%Y-%m-%d) testuser
This sets the account expiration date to 90 days from today.
Setting Maximum Password Age for Existing Users
To apply the maximum password age policy to an existing user:
sudo chage -M 90 testuser
This sets the maximum password age to 90 days for the user.
Setting Minimum Password Age for Existing Users
To apply the minimum password age policy to an existing user:
sudo chage -m 7 testuser
This sets the minimum password age to 7 days for the user.
Setting Password Expiration Warning for Existing Users
To apply the password expiration warning policy to an existing user:
sudo chage -W 14 testuser
This sets the password expiration warning period to 14 days for the user.
You can check a user's password information using several commands:
-
View all local user accounts:
cat /etc/passwd
-
Check password status for a specific user:
sudo passwd -S testuser
-
List users with a specific password setting:
awk -F: '($3 >= 1000) {print $1}' /etc/passwd
This lists all regular users on the system (UID >= 1000).
Applying Policies to All Users
To apply the new password policies to all regular users on the system, you could use a simple script. Here's an example:
for user in $(awk -F: '($3 >= 1000) && ($3 < 60000) {print $1}' /etc/passwd); do
echo "Updating password policy for user: $user"
sudo chage -M 90 -m 7 -W 14 $user
done
This script updates all regular users with our new password aging policies.
By applying these policies to existing users, you ensure consistent password security across your entire system.