How to lock user account in Linux?
Some times it’s required to lock user account so that he can not login to the machine. This is done for security reasons so that unauthorized users are not allowed to login. Locking user account can be done in many ways. Below are some ways you can disable an user in Linux.
1)Disable the login with passwd command
2)Set the user account expiry time with usermod command or chage command.
3)Set the nologin shell to user.
and many more.
Example1: Disable or lock user account using passwd command. Use passwd command with -l option to lock the user account, as the passwd is disabled for the user.
passwd -l username
Example:
passwd -l surendra
Locking password for user surendra.
passwd: Success
Example2: Disable user account by setting expiry date with usermod command
usermod -e yyyy-mm-dd username
Example:
usermod -e 2012-01-10 surendra
ssh surendra@192.168.100.166
surendra@192.168.100.166’s password:
Your account has expired; please contact your system administrator
Example3: Disable user account by setting expiry date with chage command
chage -E yyyy-mm-dd username
Example:
chage -E 2012-01-10 surendra
ssh surendra@192.168.100.166
surendra@192.168.100.166’s password:
Your account has expired; please contact your system administrator
Example4: Setting user shell to /sbin/nologin so that he can not login to the machine
usermod -s /sbin/nologin username
Example:
usermod -s /sbin/nologin surendra
This account is currently not available.
Share your thoughts if you have any on other ways to disable user accounts in Linux.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018