Skip to content

How to Stop SSH Session Timeouts (ServerAliveInterval, ClientAliveInterval) and Exit, Kill or Stop SSH

Updated 6 min read

Updated: 9 October 2026 · Applies to: OpenSSH 10.2p1 (Ubuntu 26.04), 10.0p2 (Debian 13), 9.9p1 (AlmaLinux 10.2 and Rocky Linux 9.8) and 9.6p1 (Ubuntu 24.04)

OpenSSH does not close an idle session by itself: ClientAliveInterval is 0 and there is no idle timeout by default. When a quiet session drops, a router, NAT gateway or firewall between you and the server has usually forgotten the connection, or the shell has a TMOUT set. Make the client send a small keepalive through the encrypted connection every minute and the session stays open:

# on your own computer, in ~/.ssh/config
Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3

For a single connection: ssh -o ServerAliveInterval=60 user@server. Below: the server-side setting, idle logouts you want on purpose, and how to exit, kill or stop SSH.

1. Keep the session alive from your computer

  1. Add the lines above to ~/.ssh/config (all servers) or under a Host myserver block (one server). On Windows 10 and 11 the built-in OpenSSH client reads %userprofile%\.ssh\config, according to Microsoft.
  2. In PuTTY, set Connection > Seconds between keepalives to 60 and save the session, according to the PuTTY documentation.
  3. Check what the client will use:
    $ ssh -G -o ServerAliveInterval=60 example.com | grep serveralive
    serveralivecountmax 3
    serveraliveinterval 60

The same setting also detects a dead server: after ServerAliveInterval x ServerAliveCountMax seconds without an answer, ssh prints Timeout, server NAME not responding. and exits with code 255. In our test (2 seconds x 2) that took 6 seconds.

2. Keep it alive from the server (ClientAliveInterval)

If you manage the server, set it once for every user. All the tested systems read /etc/ssh/sshd_config.d/*.conf, and sshd uses the first value it finds, so a file with a low number wins:

sudo tee /etc/ssh/sshd_config.d/10-keepalive.conf <<'EOF'
ClientAliveInterval 60
ClientAliveCountMax 3
EOF

Test the file and see the values sshd will use:

$ sudo sshd -t && sudo sshd -T | grep -i clientalive
clientaliveinterval 60
clientalivecountmax 3

Apply it with sudo systemctl reload ssh (Debian, Ubuntu) or sudo systemctl reload sshd (AlmaLinux, Rocky Linux, RHEL). Reload signals only the listening sshd process, so open sessions stay up; new logins get the setting. A client that stops answering is now dropped after about 3 minutes (60 x 3), and the server log shows a line starting with Timeout, client not responding.

3. What TCPKeepAlive does

TCPKeepAlive yes is already the default on both sides (sshd -T and ssh -G show it on every tested system). It uses kernel TCP keepalives, and by default Linux sends the first probe only after net.ipv4.tcp_keepalive_time = 7200 seconds of silence, far too late for a firewall that forgets idle connections after a few minutes. The OpenSSH manual also notes that TCP keepalives can be spoofed, while the ServerAlive and ClientAlive messages travel inside the encrypted channel. Leave TCPKeepAlive on and use the alive messages to keep sessions open.

4. Log idle users out on purpose (SSH idle timeout)

  • Not ClientAliveCountMax 0: many older guides suggest it, but the current manual says a zero value "disables connection termination". In our test, a session with ClientAliveInterval 2 and ClientAliveCountMax 0 stayed connected.
  • Shell timeout: create /etc/profile.d/tmout.sh with these lines. Bash then logs out a prompt that gets no input for 15 minutes and prints timed out waiting for input: auto-logout. It does not interrupt a program that is running.
    TMOUT=900
    readonly TMOUT
    export TMOUT
  • sshd idle timeout (OpenSSH 9.2 and later): add to your drop-in file and reload:
    ChannelTimeout session:*=15m
    UnusedConnectionTimeout 15m
    Use session:*. The manual's example says session=, but on OpenSSH 9.6, 10.0 and 10.2 that form did not close idle sessions in our tests, while session:* did (sshd names the channels session:shell, session:command and so on). This timeout counts traffic, so it also ends a long command that prints nothing: an idle sleep 15 was cut off after 5 seconds with a 5-second timeout.

5. How to exit or leave an SSH session

  • Type exit or logout, or press Ctrl+D at an empty prompt.
  • If the session is frozen, press Enter, then ~, then . (the ~. escape disconnects). It works in normal interactive logins, which have a terminal; with ssh -T or a remote command it was ignored in our test. ~? lists all escapes, and ~~. disconnects the inner session of a nested ssh.
  • For a shared (multiplexed) connection, ssh -O exit server closes the master. See SSH multiplexing and control commands.

6. Find and kill an SSH session on the server

Each login has its own process. On OpenSSH 9.8 and later (Debian 13, Ubuntu 26.04, AlmaLinux 10, Rocky Linux 9.8) it is called sshd-session: USER@pts/N; on older versions such as Ubuntu 24.04 it is sshd: USER@pts/N. Sessions without a terminal show @notty. Run tty first so you do not kill your own session, then:

ps -eo pid,user,tty,args | grep '[s]shd.*@'
sudo kill PID

The user sees Connection to HOST closed by remote host. To end everything on one terminal, use sudo pkill -HUP -t pts/N. A plain pkill -t pts/N sends SIGTERM, which an interactive bash ignores: in our test the shell survived it and only exited on SIGHUP. For more on finding processes, see how to find and kill running processes.

7. Stop ssh-agent

$ eval "$(ssh-agent -k)"
Agent pid 1254 killed

Using eval also clears SSH_AUTH_SOCK and SSH_AGENT_PID in your shell. Without those variables you get SSH_AGENT_PID not set, cannot kill agent. To keep the agent but remove its keys, run ssh-add -D.

8. Stop the SSH server

  • AlmaLinux, Rocky Linux, RHEL: sudo systemctl stop sshd
  • Debian: sudo systemctl stop ssh
  • Ubuntu 24.04 and 26.04 start SSH through ssh.socket, so stop both: sudo systemctl stop ssh.socket ssh.service

Use disable --now instead of stop to keep it off after a reboot. The service units use KillMode=process, so stopping SSH ends only the listener: open sessions keep running, but nobody can log in again until it is started. Do this only with another way in, such as the IPMI KVM console of a dedicated server. To change the port safely instead, see the default SSH port and how to change it.

9. Connection timeout (ConnectTimeout)

If a connection attempt hangs, limit the wait with ConnectTimeout (default: none, so the system's TCP timeout applies):

$ ssh -o ConnectTimeout=5 root@192.0.2.10
ssh: connect to host 192.0.2.10 port 22: Connection timed out

It gave up after 5 seconds with exit code 255. On the server, LoginGraceTime (default 120 seconds) closes connections that do not finish logging in.

To keep a long job running even if the connection does drop, run it inside tmux or screen: see ways to keep SSH sessions and processes running after disconnection.

Official documentation: sshd_config, ssh_config and ssh manual pages.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Linux server management

hardening, patching, monitoring and automation for Ubuntu, Debian, RHEL and AlmaLinux.

Was this answer helpful?

Still stuck? Open a ticket