Linux - How to lock or unlock user account, Disable or Enable a user account

How to lock or unlock user account

The passwd command is used to create a password or to change password of a user account, The same password command is used for locking or unlocking a user account. The passwd command can also used by any user to change his or her username or password.

How to lock an user account?
 # passwd -l <username>
Above is the syntax to lock an user. The -l option along with the passwd command will lock an user account. This option disables an useraccount by changing the password to a value which matches no possible encrypted value.

How to unlock an user account?
 # passwd -u <username>
Above is the syntax to unlock an user. The -u option along with the passwd command will unlock an useraccount. This option re-enables an account by changing the password back to its previous value i.e. to value before using -l option.

Examples:
How to lock and unlock a user account named myname?
For locking and unlocking a useraccount login as a root user and then type passwd command with -l and -u option.
 # passwd -l myname
 
 # passwd -u myname


The other option to lock and unlock a user account is to use usermod command. This command is used to modify the user home directory, users shell and all other options of a user account. The same command is used for locking or unlocking a user. Passwd -l does not disable an account, just makes the password unusable, but the user could still login using an ssh key or other auth method.

Disable an user account
 # usermod -L <username>

-L --lock
Lock a user's password. This puts a '!' in front of the encrypted password, effectively disabling the password. You can't use this option with -p or -U.

Enable a user account
 # usermod -U <username>

-U --unlock
Unlock a user's password. This removes the '!' in front of the encrypted password. You can't use this option with -p or -L

The topic on Linux - How to lock or unlock user account is posted by - Math

Hope you have enjoyed, Linux - How to lock or unlock user accountThanks for your time

Tech Bluff