How to limit SSH LoginGraceTime

Unauthenticated SSH connections should not sit open for minutes while clients stall, scanners pause, or password prompts are abandoned. Limiting LoginGraceTime controls how long sshd waits for authentication before closing a connection that has not logged in.

The LoginGraceTime directive belongs to the OpenSSH daemon configuration, normally /etc/ssh/sshd_config plus any included drop-in files. OpenSSH defaults to 120 seconds, and a value of 0 removes the pre-authentication time limit rather than tightening it.

Shorter grace periods reduce the time that unauthenticated sessions can consume server slots, but very small values can interrupt users on high-latency links or systems that require multi-factor prompts. Values such as 30 or 60 seconds are common starting points for hardening, and the change should be tested before the current administrative session is closed.

Steps to limit SSH LoginGraceTime:

  1. Open a terminal on the SSH server with sudo privileges.
    $ whoami
    user

    Keep an existing SSH session or console path open until a new login succeeds after the reload.

  2. Check the current effective LoginGraceTime value.
    $ sudo sshd -T
    port 22
    addressfamily any
    listenaddress [::]:22
    listenaddress 0.0.0.0:22
    usepam yes
    pamservicename sshd
    logingracetime 120
    ##### snipped #####

    sshd -T validates the configuration and prints the effective daemon settings after defaults and included files are applied.

  3. Check whether the main configuration loads local drop-in files.
    $ grep -i '^Include' /etc/ssh/sshd_config
    Include /etc/ssh/sshd_config.d/*.conf

    If no Include line appears, place the same LoginGraceTime directive in the global section of /etc/ssh/sshd_config instead.

  4. Back up the main SSH daemon configuration file.
    $ sudo cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config.backup

    A syntax error in the SSH daemon configuration can block new remote logins until fixed from an existing session or console.

  5. Open a local SSH daemon drop-in file.
    $ sudoedit /etc/ssh/sshd_config.d/90-login-grace-time.conf
  6. Set the grace period to the chosen number of seconds.
    LoginGraceTime 30

    Do not use LoginGraceTime 0 for hardening; zero disables the authentication grace timeout.

  7. Test the SSH daemon configuration.
    $ sudo sshd -t

    No output means the configuration parsed successfully.

  8. Reload the SSH service.
    $ sudo systemctl reload ssh

    Use sudo systemctl reload sshd on distributions where the daemon unit is named sshd.

  9. Verify the effective LoginGraceTime value after the reload.
    $ sudo sshd -T
    port 22
    addressfamily any
    listenaddress [::]:22
    listenaddress 0.0.0.0:22
    usepam yes
    pamservicename sshd
    logingracetime 30
    ##### snipped #####
  10. Leave a test authentication prompt idle from a separate client session.
    $ ssh user@host.example.net
    user@host.example.net's password:
    Connection closed by host.example.net port 22

    The server should close the unauthenticated connection near the configured grace period while successful logins continue to work normally.