brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: loginctl terminate-session closes a stale SSH login for good

Network and access
~/tips/tip-linux-loginctl-terminate-session

loginctl list-sessions

Ends a hanging login session and every process that belongs to it.

A laptop that went to sleep mid-session can leave an SSH login hanging on a server for a long time. who still lists it, and so does everything that counts users. I’d rather clean that up properly than hunt for the right PID.

systemd-logind tracks every login as a session with its own ID. List them first:

loginctl list-sessions
loginctl session-status 42

The list shows the session ID, user, TTY and more. session-status gives the details of one session, including its processes and the latest log lines, so you can check that 42 really is the stale one. Then end it:

sudo loginctl terminate-session 42

That kills all processes of the session and frees everything attached to it. To clear out every session of one account in one go, there is sudo loginctl terminate-user alice.

Two caveats. First, it really is all processes: a long job or a tmux server that was started from that login lives in the same session and goes down with it. If something important might still be running there, look at session-status before you press enter. Second, don’t end your own session by accident. Run loginctl session-status without an ID to see which one you are in, and keep in mind that an empty string as the ID means the session that runs the command.

For a gentler approach, loginctl kill-session sends a signal of your choice with --signal= instead of tearing the whole session down.

Further reading: loginctl(1) manual page

More Network and access tips scp -3 alice@server-a:/var/backups/db.sql.gz bob@server-b:/srv/restore/ tailscale exit-node suggest tailscale lock sign nodekey:1abddef1 tlpub:abcdef12 wg genpsk > peer1.psk Table = off All Network and access tips (163) All topics

Link