2 ways to Fix SSH Connection Timeouts Due to Inactivity

SSH sessions that drop after a few idle minutes are frustrating—especially when you step away mid-task and come back to a dead terminal. You’ll often see an error like this:

timed out waiting for input: auto-logout
Connection to host closed.

The good news: this is fixable. But first you need to know that two different things can close an idle SSH session, and each has its own fix:

  1. The network drops the connection. A firewall, router, or NAT device decides your idle connection is dead and cuts it. → Fixed with keepalive settings (ServerAliveInterval / ClientAliveInterval).
  2. The shell logs you out. The server’s shell has an idle timer that logs you out after a set time. → Fixed with the TMOUT variable.

The exact error above (timed out waiting for input: auto-logout) comes from the shell timer (TMOUT), so that’s usually the one to change. Let’s cover both.

Part 1: Keepalives — stop the network from dropping you

Think of a keepalive as a small “are you still there?” message. When SSH sends one every so often, firewalls and routers see activity and won’t treat the connection as idle. No data is lost; it just keeps the line warm.

There are two settings, and the names are confusing, so here’s the simple version:

SettingWho sends the probeWhere you configure it
ServerAliveIntervalThe client probes the serverClient: ~/.ssh/config
ClientAliveIntervalThe server probes the clientServer: /etc/ssh/sshd_config

Both do the same job from opposite ends. You only need one of them, but setting both is fine.

Important: Keepalives keep a connection alive—they do not set a session time limit. If your session is being closed by a shell timeout (Part 2), these settings alone won’t fix it.

Configure ServerAliveInterval on the client

The quickest way is a one-off option on the command line:

ssh -o ServerAliveInterval=20 -o ServerAliveCountMax=100 [email protected]

This tells your client to send a probe every 20 seconds, and to give up only after 100 unanswered probes (20 × 100 = 2000 seconds ≈ 33 minutes of true silence before disconnecting).

To make it permanent, add it to your SSH config file at ~/.ssh/config:

See also: Mastering the Linux Command Line — Your Complete Free Training Guide

Host *
    ServerAliveInterval 20
    ServerAliveCountMax 100
    TCPKeepAlive yes
  • ServerAliveInterval 20 — send a probe every 20 seconds.
  • ServerAliveCountMax 100 — disconnect only after 100 missed replies.
  • TCPKeepAlive yes — also enable TCP-level keepalives.

See the keepalives in action

If you capture traffic with tcpdump, you can watch the client send a probe every 20 seconds (the ServerAliveInterval):

sudo tcpdump -i any port 22
10:37:11.741159 IP 10.79.102.248.61622 > 10.124.202.230.ssh: Flags [P.], length 52
10:37:11.767047 IP 10.124.202.230.ssh > 10.79.102.248.61622: Flags [P.], length 28
10:37:31.769396 IP 10.79.102.248.61622 > 10.124.202.230.ssh: Flags [P.], length 52
10:37:31.789991 IP 10.124.202.230.ssh > 10.79.102.248.61622: Flags [P.], length 28

Notice the 20-second gap between probes (10:37:11 → 10:37:31)—that’s the keepalive working.

Configure ClientAliveInterval on the server

To do the same from the server side, edit /etc/ssh/sshd_config:

ClientAliveInterval 60
ClientAliveCountMax 10

This sends a probe to the client every 60 seconds and disconnects only after 10 missed replies. Restart SSH to apply it (service name is ssh on Ubuntu/Debian, sshd on RHEL/CentOS):

sudo systemctl restart ssh      # Ubuntu / Debian
sudo systemctl restart sshd     # RHEL / CentOS / Fedora

Part 2: TMOUT — the shell auto-logout timer

TMOUT is a Bash variable that automatically logs you out after a period of idle time. This is what usually produces the timed out waiting for input: auto-logout message.

Check the current value

echo $TMOUT
300

A value of 300 means the shell logs you out after 300 seconds (5 minutes) of inactivity. If it prints nothing, TMOUT isn’t set.

Increase the timeout

Set a longer idle window—say 10 minutes (600 seconds):

export TMOUT=600
echo $TMOUT
600

Disable auto-logout entirely

Setting TMOUT to 0 turns the auto-logout off:

export TMOUT=0

Make the change permanent

The commands above only affect your current session. To keep the setting:

  • For just your user, add it to ~/.bashrc:echo 'export TMOUT=600' >> ~/.bashrc
  • For all users, add it to a system-wide file like /etc/profile (requires root). Note that in many hardened environments TMOUT is set read-only here for security, so a user can’t lower it.

Good to know: TMOUT only affects idle shells. If a command is actively running (like tail -f or a long build), the timer doesn’t apply—so a running process won’t be killed by TMOUT.

Combine both fixes for a reliable long-lived session:

  1. On the client, add keepalives to ~/.ssh/config:Host * ServerAliveInterval 20 ServerAliveCountMax 100 or per connection:ssh -o ServerAliveInterval=20 -o ServerAliveCountMax=100 [email protected]
  2. On the server, raise or disable the shell timeout:export TMOUT=600 # 10 minutes, or use 0 to disable

Verify from the server log

After raising TMOUT to 600, the session log shows the login and logout are exactly 10 minutes apart:

Feb 26 02:36:51 hostname sshd[127463]: Accepted keyboard-interactive/pam for test from 10.79.102.248 port 61622 ssh2
Feb 26 02:36:51 hostname sshd[127463]: pam_unix(sshd:session): session opened for user test by (uid=0)
Feb 26 02:46:51 hostname sshd[127463]: pam_unix(sshd:session): session closed for user test

02:36:51 → 02:46:51 is 600 seconds. Increase TMOUT further for an even longer allowed idle time.

Quick summary

ProblemFixWhere
Firewall/NAT drops idle connectionServerAliveIntervalClient ~/.ssh/config
Server drops idle connectionClientAliveIntervalServer /etc/ssh/sshd_config
auto-logout from idle shellTMOUT (raise or set to 0)~/.bashrc or /etc/profile

Keepalives keep the network connection open; TMOUT controls the shell’s idle logout. Knowing which one is closing your session is half the battle.

Avatar photo
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 275

Leave a Reply

Your email address will not be published. Required fields are marked *