Enforcing Password Policies
Linux provides several tools to enforce password policies, which help maintain system security by ensuring users select strong passwords. In this step, you will learn how to implement basic password policies.
Setting Password Expiration
The chage
command allows administrators to set password expiration policies. Navigate to your project directory:
cd ~/project
Let's set the maximum password age for our cyberuser
account to 90 days:
sudo chage -M 90 cyberuser
This command sets the maximum number of days that a password remains valid before the user is required to change it.
You can also set a minimum password age, which prevents users from changing their passwords too frequently:
sudo chage -m 7 cyberuser
This command requires the user to wait at least 7 days before changing their password again.
Verify the changes you made:
sudo chage -l cyberuser
You should see the updated password policy information:
Last password change : Jul 15, 2023
Password expires : Oct 13, 2023
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 : 7
Creating a Password Policy Script
Now, let's create a script that will implement more advanced password policies. Create a new file named cyberpolicy.sh
:
nano ~/project/cyberpolicy.sh
Add the following content to the file:
#!/bin/bash
## Enforce password complexity
echo "password requisite pam_pwquality.so retry=3 minlen=8 difok=3" | sudo tee -a /etc/pam.d/common-password
## Set password expiration policy for all new users
echo "PASS_MAX_DAYS 90" | sudo tee -a /etc/login.defs
echo "PASS_MIN_DAYS 7" | sudo tee -a /etc/login.defs
echo "PASS_WARN_AGE 7" | sudo tee -a /etc/login.defs
echo "Password policy has been updated successfully."
Save the file by pressing Ctrl+O
, then Enter
, and exit nano with Ctrl+X
.
Make the script executable:
chmod +x ~/project/cyberpolicy.sh
This script sets the following password policies:
- Password complexity: Minimum 8 characters, at least 3 different characters from the previous password, with 3 retries allowed
- Password expiration: 90-day maximum password age
- Password change restriction: 7-day minimum password age
- Password warning: 7 days before password expires
In a production environment, you would execute this script to apply these policies. However, for this lab, we'll just examine the content to understand how password policies can be implemented.