To lock a user account using the passwd command in Linux, you can use the -l option. This effectively disables the user's ability to log in by placing a '!' in front of the user's password hash in the /etc/shadow file. Here’s how to do it:
-
Open a terminal.
-
Run the following command, replacing
usernamewith the name of the user account you want to lock:
sudo passwd -l username
For example, to lock a user account named john, you would use:
sudo passwd -l john
- To verify that the account has been locked, you can check the
/etc/shadowfile:
sudo grep username /etc/shadow
You should see an entry for the user with a '!' at the beginning of the password field, indicating that the account is locked.
To unlock the account later, you can use the -u option with the passwd command:
sudo passwd -u username
This will remove the '!' and restore the user's ability to log in.
