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.

 

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.