≡ Menu

Linux Failed Login Control: Lock and Unlock User Accounts Using PAM

Under CentOS Linux it is possible to lock out a user login after failed login attempts. This is a security feature. You can also automatically unlock account after some time.

pam_tally – login counter (tallying) module

This module maintains a count of attempted accesses, can reset count on success, can deny access if too many attempts fail.


Use /etc/pam.d/system-auth configuration file to configure attempted login accesses and other related activities. Append following AUTH configuration to /etc/pam.d/system-auth file:
auth required pam_tally.so onerr=fail deny=5 unlock_time=21600
(a)deny=5 – Deny access if tally for this user exceeds 5 times.

(b) unlock_time=21600 – Allow access after 21600 seconds (6 hours) after failed attempt. If this option is used the user will be locked out for the specified amount of time after he exceeded his maximum allowed attempts. Otherwise the account is locked until the lock is removed by a manual intervention of the system administrator.

(c) onerr=fail – If something weird happens (like unable to open the file), return with PAM_SUCESS if onerr=succeed is given, else with the corresponding PAM error code.

Default file /var/log/faillog is used to keep login counts.

The above PAM module is part of all Linux distribution and configuration should work with any Linux distribution.

See also:

  1. man pages faillog, pam.conf, pam.d, pam, and pam_tally
  2. pam_tally – login counter (tallying) module documentation.
  3. CentOS Linux project
Share this on:
{ 8 comments… add one }
  • Aaron C December 8, 2011, 8:59 pm

    From the top of /etc/pam.d/system-auth:

    # This file is auto-generated.
    # User changes will be destroyed the next time authconfig is run.

    Is there another place these options should be set so that authconfig does not clobber them? Right now, as a work around, I am going to `chattr +i /etc/pam.d/system-auth`.


  • Yongzhi Pan January 8, 2012, 5:39 am

    Aaron C, Just edit this file and do not use authconfig.

    It should be noted this will not effect for ssh remote login. You should put it in `sshd’ pam config file for this.

  • Yongzhi Pan January 8, 2012, 6:23 am

    “Append following AUTH configuration to /etc/pam.d/system-auth file”

    This does not work. I have tested it. Even if the fails are recorded, login is not denied at all. The modules are tried in the order list. You have to prepend it before any ‘auth’ to use it.

    Another note, we should be using the newer pam_tally2.so module.

  • tally June 7, 2012, 5:41 am

    pam_tally tool shows number of bad attempts by a user by using /var/log/faillog database. And after lockout time expires, with a correct login attempt count gets cleared. can someone tell me is there any way I can clear tally account automatically after the lockout time expires for a user.
    I want to clear tally automatically once lockout time expires, don’t want to wait for user to login again with correct credentials.
    Any help is highly appreciated.

  • David March 5, 2013, 9:54 am

    Before doing this have a quick look in /lib64/security and check that there is a pam_tally.so file in there as you may need to use pam_tally2.so instead.
    If in doubt add the line:
    auth sufficient pam_tally.so onerr=fail deny=5 unlock_time=21600
    to the system-auth file instead, then su or login in and check /var/log/secure for error messages.
    You can change the sufficient to required if pam isn’t reporting errors.
    That should save you getting locked out of the root account…

  • Ravinder July 6, 2013, 8:16 am

    I tried the same but didn’t work for me. Can you tell me what shall i do. I am doing in centos 6.

  • Aaron C July 8, 2013, 2:30 pm

    Under CentOS 6 you should use pam_tally2.so.

    You will want to modify both /etc/pam.d/system-auth-ac and /etc/pam.d/password-auth-ac.

    In the auth section, under pam_env.so insert the following:

    auth        required      pam_tally2.so deny=3 onerr=fail unlock_time=900

    AND, in the account section, under pam_unix.so, insert the following:

    account     required      pam_tally2.so

    Make these edits in both files to cover remote and locally authenticated services. (Gnome, SSH)

    • Dan P August 12, 2015, 3:54 am

      Aaron C, didn’t work for me, got it working using this configurations:

      If your concern is for remote login, you can modify /etc/pam.d/password-auth for ssh auth connection.

      Insert the following under “auth required pam_env.so” :

      auth        required      pam_tally2.so deny=3 unlock_time=900

      AND under “account required pam_unix.so” :

      account     required      pam_tally2.so reset

      Hope this can help someone struggling out there!

Security: Are you a robot or human?

Leave a Comment

   Tagged with: , , , , , , , , , ,