How to Lock An Account on Linux

See all GNU/Linux related FAQ
How do I lock an account (user login id) under Linux operating system? How can I disable a user’s login without disabling the account on a Linux based server?

You can use the passwd command to change user or group accounts password. A normal user may only change the password for his/her own account, the super user (root) may change the password for any account. You can use the passwd command for locking or unlocking an account on a Linux operating systesm.
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux terminal
Category User Management
OS compatibility AlmaLinux Alpine Amazon Linux Arch CentOS Debian Fedora Linux Mint openSUSE Pop!_OS RHEL Rocky Stream SUSE Ubuntu WSL
Est. reading time 3 minutes

Task: Linux locking an account

The syntax is as follows for locking down the account. It is performed by rendering the encrypted password into an invalid string by prefixing the encrypted string with an !. The -l option is available to root user only:
# passwd -l {username}
The -l option disables an account by changing the password to a value which matches no possible encrypted value. In this example, lock user account named vivek. First, login as a root user and type the following command:

# passwd -l vivek

OR use the sudo command:

$ sudo passwd -l vivek

Sample outputs:

Locking password for user vivek.
passwd: Success

Task: Linux Unlocking an Account

The syntax is as follows and the -u option is available to root user only:
# passwd -u {username}
The -u option re-enables an account by changing the password back to its previous value i.e. to value before using -l option. To unlock user account named vivek. Login as a root user and type following command:

# passwd -u vivek

Sample outputs:

Unlocking password for user vivek.
passwd: Success

Task: Root can access any account

The syntax is:
$ su - {username}
$ su - vivek

Sample session: Disable a user’s login without disabling the account

Fig.01: How to Linux disable a user's login without disabling  account

Fig.01: How to Linux disable a user’s login without disabling account

Please note that user account information is stored in the /etc/passwd file and hashed passwords in encrypted format in the /etc/shadow file.

A note about the ssh public key based authentication

User account locked with the -l option can still log in by other methods such as the ssh public key authentication. Use the following command to for full account locking:
# chage -E 0 {username}
## full lockdown for user named vivek ##
# chage -E 0 vivek

Fig.02: Linux chage command set and unset expire date for given user account

Fig.02: Linux chage command set and unset expire date for given user account

How can I remove an account expiration date?

The syntax is as follows for the chage command:
# chage -E -1 vivek
# chage -l vivek
## optional: assign a new password for vivek ##
# passwd vivek

Sample outputs:

Last password change					: Feb 15, 2015
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

User can now login using ssh public key or password. Try the ssh command as follows:
$ ssh vivek@nas01
$ ssh -Y vivek@nas01

Sample outputs:

Linux nas01 3.2.0-4-amd64 #1 SMP Debian 3.2.65-1+deb7u1 x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
No mail.
Last login: Sun Feb 15 18:13:45 2015 from

See also

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

21 comments… add one
  • umesh Sep 26, 2007 @ 11:54

    how to lock the file or directory so that others cant even open it

    • JTJ Apr 26, 2011 @ 10:40

      change the file permissions….
      #chmod 777
      for full permissions rwx
      #chmod 700
      so that no can able to access in your files or dir. then user means u have the full permissions but others do not…

  • jamie Oct 15, 2008 @ 14:07

    once you have locked an account, is there any way to view a comfirmation of some sort of that locked account? Or even better print an account showing its locked somehow?

    say for audit purposes?

  • Nathan Collins Nov 11, 2008 @ 7:02

    jamie, you can get a list of locked accounts with the passwd command:

    $ sudo passwd -Sa | awk ‘($2 == “L”)’

    • Michael Mar 10, 2011 @ 16:21

      I tried your command and I got an invalid option

      sudo passwd -Sa | awk ‘($2 == “L”)’

      option requires a username and unknow option with -S

      Just wondering if there was something I am missing. We are on Red Hat Enterprise servers.

      • Nathan Collins Mar 11, 2011 @ 7:37

        Still works for me. I’m on Ubuntu 10.04. Maybe you have a different version of the passwd command?

      • Anthony Sep 28, 2015 @ 18:48

        -a is not an option for CentOS 6 version of passwd.


  • karthik rajan Sep 11, 2009 @ 9:42

    IS any way can display the message if the “Account is Locked Plsease Contact Systems Dept” when account is locked by the pam_d module in the Linux

    • enoksrd Apr 8, 2011 @ 23:27

      @Karthik: setting the expiration date (chage -E 1), but NOT locking the password (i.e. don’t use usermod -L) gives the behavior you want: when a user attempts to login, and provides their password or ssh-key, they get the message (on Ubuntu10.10 anyway):

      Your account has expired; please contact your system administrator

  • suranga Oct 13, 2009 @ 14:16

    also using
    usermod -L username

  • Bhagesh P Jun 26, 2010 @ 9:59


    Is any way to lock the account automatically which is not used for more than 20 days.

    • enoksrd Jun 26, 2010 @ 22:18

      Bhagesh P,

      here’s a first try:

      lastlog -b 20 | tail -n +2 | cut -d ‘ ‘ -f 1 | xargs -n1 echo usermod -L

      That locks any account not logged into for 20 or more days. Note that “logged into” seems to mean pseudo terminal and ssh logins, but not GUI logins (for Gnome/GDM in Ubuntu anyways).

  • Rinkal Oct 19, 2010 @ 10:07

    Is it possible to have an account as non login account on Linux, Solaris or HPUx.
    As on Solaris10, we can have an account with non login or no passwd.
    # passwd -N testuser
    passwd: password information changed for testuser

    –> Show password attributes
    # passwd -s testuser
    gmb NL

    –> Shadow file entry
    # grep “^testuser:” /etc/shadow

    You will notice that the user’s original password has been removed and replaced with the string “NP”. This account is now a non-login account and the original password has been discarded. You will not be able to login to this account, but the account will be able to make use of delayed execution facilities. To re-enable an account for interactive logins, simply reassign a password to the account using the passwd(1) command.

  • Anonymous Apr 8, 2011 @ 10:13

    Nopes The above wont lock a account, It i will lock the password only.

    If the user has ssh-keys set.. he is still able to log into the account.

    • enoksrd Apr 8, 2011 @ 23:22

      @Arpit: thanks!

      Arpit is correct: usermod -L <user> only prevents password logins. I
      looked into this and found another easy solution, but there are some

      Looking at the man page for usermod, the -L entry says:

      Note: if you wish to lock the account (not only access with a password),
      you should also set the EXPIRE_DATE to 1.

      But BE CAREFUL: moduser can change the expire date with -e, but that
      options expects a YYYY-MM-DD formatted date, and if you do

      usermod -e 1 <user>,

      the confusing documentation, this seems like a major bug to me …

      Now, there are at least two ways to set the expire date to the “1”
      that the usermod man page suggests. But first, what does “1” mean?
      According to the shadow and chage man pages, it’s the number of days
      since 1 January 1970 (the unix epoch). So, the point is that (1+1)
      January 1970 is in the past, and actually any VALID date in the past
      would effectively lock the account.

      Another WARNING: but don’t think you can simply set the date
      arbitrarily: dates before 1 Jan 1970 are not valid, and passing such a
      date to usermod appears to succeed, but then /etc/shadow is corrupted
      and subsequent commands, INCLUDING usermod, can not edit
      <user>’s entry (e.g. chage will add a second entry for that user
      and pwck will suggest you delete the entry with the bad date!).

      OK, so how to set the expire date correctly? Two easy ways:

      1. with chage: chage -E 1 <user>. then use chage -l <user> to see that
      the expiration date is in the past.

      2. with usermod: usermod -e 1970-01-02 <user>.

      If you screw something up (e.g. by passing usermod a date before 1
      January 1970), you can edit /etc/shadow manually with vipw -s.

      Thanks again to Arpit for pointing out that usermod -L was not really
      locking the accounts.

      • enoksrd Apr 8, 2011 @ 23:42

        NB: the above may be Debian (and derivatives, e.g. Ubuntu) specific.

        • no Sep 8, 2011 @ 17:37

          usermod -e 1 foouser
          works just fine in Ubuntu 11.04
          check with
          chage -l foouser

      • Anonymous May 10, 2011 @ 0:08

        @ enoksrd

        No need for thanks, Sorry I wasnt online for long.

        Will the solution work for LDAP Users also which are on linux clients configured using authconfig
        I have a scenerio where LDAP users gets locked after giving three wrong password attempts but they can login using ssh. how can i block them.

  • tharnge Feb 20, 2012 @ 14:24

    why is my account locked?please open theaccount

    • Drew Jan 25, 2013 @ 20:32

      ^ lol

  • r00000t Mar 5, 2013 @ 7:57

    ^ Die laughing

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.