How to Force User to Change Password in Linux

How to Force User to Change Password in Linux

Sometimes Linux administrators need to force users to reset password for security reasons. You can easily do this in Linux using passwd or chage commands. They are available by default in almost every Linux distribution. In this article, we will learn how to force user to change password in Linux.

How to Force User to Change Password in Linux

Basically, we will force user password to expire prompting a reset when the user logs in the next time.

1. Using passwd command

Here is the command to expire user password using passwd command in Linux. Mention the username whose password you want to force reset, after -expire or -e option as shown below.

$ sudo passwd --expire ubuntu
OR
$ sudo passwd -e ubuntu

Thereafter, you can use chage command to verify the changes.

$ 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, 2024
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 the expiry date of your Linux user account. So you can use it to forcibly expire a user account. Once a user account is expired, the user will be forced to reset password on next login.

Here is the command to set account expiry. You need to specify the number of days to expire or the date starting 1970-01-01 or the date to expire, followed by username.

# chage --lastday 0 ubuntu 
OR
# chage --lastday 1970-01-01 ubuntu

Afterwards, you can use chage command to verify the changes.

Last password change                                    : password must be changed
Password expires : password must be changed
Password inactive : password must be changed
Account expires : July 13, 2024
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 force reset user password in Linux. If you want to regularly expire a user password, you can easily create a cronjob for this purpose. Open crontab with the following command.

$ sudo crontab -e

Add the following line to run the command on 1st day of every month at 10.a.m. It will force reset password of user ubuntu.

0 10 1 * * sudo passwd -e ubuntu ubuntu >/dev/null 2>&1

Save and close the file to setup the cronjob.

In this article, we have learnt how to force change password in Linux.

Also read:

How to Pause Shell Script
How to Send HTML Mail Using Python
How to Find & Replace String in VI Editor
How to List Installed Packages in Ubuntu
How to Find Parent PID in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *