Ubuntu 24.04: systemd-logind sessions remaining in closing state after their owning process has disappeared?

Ubuntu Version

Ubuntu 24.04 LTS

Desktop Environment (if applicable)

Other

Problem Description

OS: Ubuntu 24.04
Component: systemd-logind
Service: SSH/SFTP (OpenSSH)
Issue observed through PAM/systemd session management

Commands used:

  • loginctl
  • systemd-logind
  • systemctl
  • sshd

Users are intermittently unable to establish new SSH/SFTP sessions.

Observed error:

pam_systemd(sshd:session): Failed to create session:
Maximum number of sessions (8192) reached, refusing further sessions.

The server is not under CPU, memory, or storage pressure at the time of the issue.

Investigation found:

  • 8,192 total sessions registered in loginctl
  • 7,858 sessions in “closing” state
  • 334 sessions in “active” state

Several sampled sessions remain visible in loginctl despite:

  • Session leader PID no longer existing
  • Corresponding session-.scope no longer existing

Example:

  • Session state: closing
  • Leader PID: no longer exists
  • systemctl status session-.scope = Unit could not be found

This suggests stale/orphaned session records are accumulating until the logind session limit is reached, preventing new SSH/SFTP logins.

STEPS To REPRODUCE

  1. Verified issue reported by users:

    • Unable to establish new SSH/SFTP sessions.
  2. Confirmed PAM error:

    • “Maximum number of sessions (8192) reached”
  3. Verified system health:

    • CPU utilization normal
    • Memory utilization normal
    • No evidence of storage bottleneck
  4. Collected session statistics:

    • loginctl list-sessions
    • loginctl show-session <session_id>
  5. Identified a large number of sessions in “closing” state.

  6. Investigated sample sessions:

    • Retrieved Leader PID
    • Verified PID no longer exists (ps -fp )
    • Verified corresponding session scope no longer exists
      (systemctl status session-.scope)
  7. Reloaded systemd-logind:

    • systemctl reload systemd-logind
  8. Result after reload:

    • Service remained healthy
    • Session counts unchanged
    • Stale sessions remained present
  9. Reviewed systemd-logind logs:

    • No obvious errors observed.
  10. Please help determine the root cause of stale/orphaned session entries remaining in loginctl despite:

    • Leader process no longer existing
    • Session scope no longer existing
  11. Is this a known issue with systemd-logind, PAM, OpenSSH, or session cleanup?

  12. Is there a supported method to purge or rebuild stale logind session records without rebooting the server?

  13. What is the recommended recovery action?

  14. What is the expected impact of restarting systemd-logind in a production SFTP environment?

Error Messages / Logs

sshd[3157537]: pam_systemd(sshd:session): Failed to create session: Maximum number of sessions (8192) reached, refusing further sessions.
sshd[3157537]: pam_systemd(sshd:session): Failed to create session: Maximum number of sessions (8192) reached, refusing further sessions.

Run loginctl terminate-session <id> on one of the closing sessions, then check whether logind released it:

loginctl show-session <id> -p State

If it’s gone, loop over the closing sessions to free slots without a reboot, but this also kills any process still left in those sessions:

for s in $(loginctl list-sessions --no-legend | awk '{print $1}'); do [ "$(loginctl show-session "$s" -p State --value)" = closing ] && loginctl terminate-session "$s"; done

If they don’t clear, raise SessionsMax= in /etc/systemd/logind.conf and run sudo systemctl restart systemd-logind. Existing SSH and SFTP sessions should stay connected through that restart, but I’d still do it in a quiet window.

The same symptom on 20.04 is in Launchpad bug 2020177 ([Bug 2020177] Re: Leaking sessions in pam_systemd), where a reboot was the only fix found.

Drafted with AI.

sir . is there permanent solution for this issue?

Not one I can point to for 24.04 yet.

If loginctl terminate-session cleared the closing sessions, run that loop from a daily cron job or systemd timer so the count stays under 8,192 until an update fixes the leak.

If they stayed in closing, report it with ubuntu-bug systemd and attach loginctl show-session <id> for one of them.

Did terminate-session clear them?