If you are system administrator, then you may need to force user to change password in Linux occasionally. You can easily do this using passwd or chage commands. They are available in almost every Linux distribution so you don’t need to install them separately. In this article, we will look at how to force user to change password in Linux.
How to Force User to Change Password in Linux
Here are the steps to force user to change password in Linux. Basically, we will expire a user’s password to force them to change it, the next time they login.
1. Using passwd command
You can easily expire user password using passwd command as shown below. Just mention the user (e.g ubuntu) whose password you want to expire after passwd –expire command.
$ sudo passwd --expire ubuntu OR $ sudo passwd -e ubuntu
You can verify the change using chage -l command for that user.
$ sudo chage -l ubuntu Last password change : password must be changed Password expires : password must be changed Password inactive : password must be changed Account expires : July 13, 2021 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
2. Using chage command
chage command allows you to set expiration date of a user account in Linux. You can also use this command to expire an account.
Using -d or –lastday option, you can set number of days since Jan 1, 1970 after which the account will expire.
Here is the command to expire user account ubuntu.
# chage --lastday 0 ubuntu OR # chage --lastday 1970-01-01 ubuntu
Again, you can check details of user using chage -l command.
Last password change : password must be changed Password expires : password must be changed Password inactive : password must be changed Account expires : July 13, 2021 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
In this article, we have learnt a couple of simple ways to quickly force user to change password in Linux. It is very useful to expire user passwords and force them change it. If you want to periodically expire user passwords, you can simply setup any of the above commands as cronjob. Open terminal and run the following command to open crontab.
$ sudo crontab -e
Add the following line to run the following command on 1st day of every month at 10 am.
0 10 1 * * sudo passwd -e ubuntu ubuntu >/dev/null 2>&1
Save and close the file to setup your cron job.
Also read:
Shell Script to Automate SSH Login
How to Pause Shell Script
How to Send HTML Email in Python
How to List All Installed Packages in Ubuntu
How to Find & Replace String in VI Editor