This article is all about how to lock and unlock a user account in Linux operating system by using different methods. In the previous article on Linux operating system, we learn about different types of run levels in Linux and we also describe these run levels in a minor way. In the previous post, we also learn that how to create a user account in Linux other than root and also check for terminals and many more things. By reading the previous article we learn some basic command in Linux operating system. Now in this tutorial, we can also learn about some basic commands of Linux operating system in which we can learn about how to lock and unlock a user account in Linux operating system.
Also Read: How to create a file and folder in Linux
First of all, we use below-given command to saw the status of lock and unlock user account of Linux operating system
[[email protected] ~]# cat /etc/shadow
1. First method to lock a user account which is most usable and simplest method to lock a user account
[[email protected] ~]# useradd rahul
[[email protected] ~]# passwd -l rahul Locking password for user rahul. passwd: Success
Now to unlock user account which is locked by this given above command then simply use given below command to unlock user account in Linux
[[email protected] ~]# passwd -uf rahul Unlocking password for user rahul. passwd: Success.
2. Second method to lock a user account in Linux
[[email protected] ~]# passwd -l rahul Locking password for user rahul. passwd: Success
Now to unlock a user which is locked by the second method then use given below command
[[email protected] ~]# passwd -u rahul Unlocking password for user rahul. passwd: Success.
3. Third method for lock a user account in Linux operating system by usermod command
[[email protected] ~]# usermod -L rahul
Now to unlock user account which is locked by third method use given below command
[[email protected] ~]# usermod -U rahul
4. Fourth method to lock a user account in Linux
[[email protected] ~]# usermod -p"*" rahul
After executing this command we saw the output by using #cat /etc/shadow and your output is something like this then your account will be locked
rahul:*:17186:0:99999:7:::
Now to unlock user account which is locked by the fourth method in Linux use given below command
[[email protected] ~]# passwd rahul Changing password for user rahul. New UNIX password: BAD PASSWORD: it is too short Retype new UNIX password: passwd: all authentication tokens updated successfully.
5. Fifth method to lock a user account in Linux
[[email protected] ~]# usermod -s /bin/nologin rahul
Now to unlock user account which is locked by fifth method use given below command
[[email protected] ~]# usermod -s /bin/bash rahul
How to check the lock and unlock status of any user account
Now use the below-given command to check the status of the user account.
if an output is L then it is locked.
If output is p then account has a usable power
If output is np that means no password means unlock
Now check password status for the first method after lock the user account
[[email protected] ~]# passwd -S rahul rahul LK 2017-01-20 0 99999 7 -1 (Alternate authentication scheme in use.)
After that now remove password from user account by using below-given code then saw the status of user account
To remove password and also check the status after removing password from user account
[[email protected] ~]# passwd -l rahul Removing password for user rahul. passwd: Success [[email protected] ~]# passwd -S rahul rahul NP 2017-01-20 0 99999 7 -1 (Empty password.)
How to check if the user shell is set to a valid shell
[[email protected] ~]# grep 'rahul' /etc/passwd rahul:x:500:500::/home/rahul:/bin/bash
How to check the list of password and all user related details in Linux
[[email protected] ~]# chage -l rahul Last password change : Jan 20, 2017 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
[[email protected] ~]# chage --list rahul Last password change : Jan 20, 2017 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
Now we learn that how to set password expiry date for a user account in Linux operating system. In above output, password will never expire but by using given below command we set it to 30 days means user account password automatically expires after 30 days
[[email protected] ~]# chage -M 30 rahul
Now check user password details and also check expiry date of password
[[email protected] ~]# chage -l rahul Last password change : Jan 20, 2017 Password expires : Feb 19, 2017 Password inactive : never Account expires : never Minimum number of days between password change : 0 Maximum number of days between password change : 30 Number of days of warning before password expires : 7
THAT’S IT
If you have any type of queries then solved out through comment section. If you also know about some more tips and tricks to lock a user account and unlock a user account in Linux then share with us through the comment section.