Troubleshooting
SSH tunnel keeps disconnecting
Most SSH tunnels that drop were idle and a firewall or NAT device forgot them, or the computer slept or changed network. Keepalives fix the first cause. Only something that restarts the tunnel fixes the second.
- Level
- Intermediate
- Reading time
- 10 min
- Applies to
- Windows, macOS, Linux, OpenSSH command line
- Transputty steps checked against
- Transputty 0.1 (development build, not released)
Before you start
- The ssh command or PuTTY session you use for the tunnel
- The message ssh printed when the tunnel dropped, if any
- A rough idea of when it drops: after idle time, after sleep, or at random
Short answer
An SSH tunnel that keeps disconnecting is usually idle long enough for a firewall or NAT router to drop it. Add ServerAliveInterval 30 and ServerAliveCountMax 3 to the host's entry in ~/.ssh/config (in PuTTY, set "Seconds between keepalives" to 30). Keepalives do not reconnect after sleep or a network change; for that, run the tunnel under autossh, systemd or a tunnel manager.
The last section describes how Transputty, an SSH tunnel manager for Windows 11, handles drops. Transputty is in development with no public build yet; see release status. The fixes before that section need only OpenSSH or PuTTY.
The messages
What ssh prints when the connection dies depends on the system and the OpenSSH version:
client_loop: send disconnect: Broken pipe
client_loop: send disconnect: Connection reset
Timeout, server bastion.example.com not responding.
Connection to bastion.example.com closed by remote host.
Read from remote host bastion.example.com: Connection reset by peer
packet_write_wait: Connection to 203.0.113.10 port 22: Broken pipe
"Broken pipe" is the Linux and macOS wording; Windows' built-in ssh prints "Connection reset" for the same event. packet_write_wait comes from older OpenSSH releases. Often there is no message at all, because the window was closed or the computer slept, and the first sign is the application:
psql: error: connection to server at "127.0.0.1", port 5433 failed: Connection refused
server closed the connection unexpectedly
Quick diagnosis
| Symptom | Likely cause | Check |
|---|---|---|
| Drops after a quiet period, works while busy | Idle timeout on a NAT router, firewall or load balancer | Does it stop with ServerAliveInterval 30? |
| Dies whenever the laptop sleeps, changes Wi-Fi or reconnects a VPN | The local address or route changed; the old TCP connection cannot continue | Does it match sleep or network events? |
Timeout, server … not responding. |
Keepalives are on and the path went dead | Is the network or the server down? |
closed by remote host at regular intervals |
A server-side timeout or a scheduled restart | sshd settings and the server's log |
| Drops at random under load | Packet loss or an unstable link | ping to the server while it runs |
| ssh is still running but applications are refused | The forward was never bound, or you are on the wrong port | Connection refused |
Cause 1: an idle timeout on the path
Home routers, corporate firewalls, cloud NAT gateways and load balancers track each TCP connection and forget one that carries no packets for a while. Timeouts of a few minutes are common. A -N tunnel with no traffic sends nothing, so the entry expires. Neither end is told. The next packet is dropped or answered with a reset, and ssh reports a broken pipe.
Confirm it by noting when the tunnel was last used. If it fails only after idle time, this is your cause.
The fix is a keepalive inside the SSH connection. In ~/.ssh/config (on Windows, C:\Users\you\.ssh\config):
Host bastion.example.com
ServerAliveInterval 30
ServerAliveCountMax 3
Or for one command:
ssh -N -o ServerAliveInterval=30 -o ServerAliveCountMax=3 -L 5433:db.internal:5432 deploy@bastion.example.com
After 30 seconds without data from the server, ssh sends a request through the encrypted channel and expects a reply. That traffic keeps the path's entries fresh. If three in a row go unanswered, ssh prints Timeout, server … not responding. and exits, so a dead tunnel is noticed in about 90 seconds instead of hanging. Both options default to off (0) and 3. TCPKeepAlive, which is on by default, is a different mechanism and too slow to help here.
In PuTTY, open Connection and set Seconds between keepalives (0 to turn off) to 30, then save the session.
Cause 2: sleep, Wi-Fi roaming and VPN reconnects
A TCP connection is tied to its two addresses. When a laptop sleeps past the idle timeout, moves to another network or reconnects a VPN, the old connection is gone and no keepalive can save it. The only fix is to start the tunnel again, by hand or automatically (see below).
Cause 3: the server ends the session
Check these with the server's administrator, or read /etc/ssh/sshd_config and the sshd log:
ClientAliveIntervalandClientAliveCountMax. The server-side mirror of the client options: sshd probes the client and disconnects one that stops answering. A responsive client is not disconnected by these settings just for being idle.ChannelTimeoutandUnusedConnectionTimeout(OpenSSH 9.2 and later). These do close idle things on purpose.UnusedConnectionTimeoutends a connection that has no open channels, which is exactly what a-Ntunnel looks like while no application is connected through it.ChannelTimeoutcan close idle forwarded connections.- Shell timeouts such as
TMOUT. These end an idle interactive shell. They do not affect a tunnel started with-N, which has no shell, but they do kill a tunnel you keep open through a normal login session. - Restarts and maintenance. A reboot or a network change on the server drops every session.
MaxSessions and MaxStartups are often blamed and rarely relevant. MaxSessions limits shell and subsystem sessions per connection, not forwarded ports. MaxStartups throttles connections that have not logged in yet, so it can slow down many simultaneous reconnects but does not end an established tunnel.
Cause 4: an unstable link
On a lossy connection, keepalive replies can be lost and ssh gives up even though the network recovers moments later. Raise the tolerance instead of the frequency, for example ServerAliveCountMax 6, and investigate the link itself.
Reading ssh -vvv
Run the tunnel with -vvv and leave it until it drops. With keepalives on, each probe appears as a pair of lines: debug3: send packet: type 80 followed by debug3: receive packet: type 82. A healthy idle tunnel shows that pair every interval. If the sends continue and the receives stop, the path died silently, and Timeout, server … not responding. follows. If the log instead ends abruptly with a Connection reset or closed by remote host line, something actively ended the connection: look at the server's log for the same moment.
Make the tunnel come back by itself
Keepalives detect a dead tunnel; they never restart it. On Linux and macOS, autossh reruns ssh whenever it exits:
autossh -M 0 -N -o ServerAliveInterval=30 -o ServerAliveCountMax=3 -o ExitOnForwardFailure=yes -L 5433:db.internal:5432 deploy@bastion.example.com
-M 0 turns off autossh's own monitoring port and relies on the ServerAlive options. On a Linux machine that should always hold the tunnel, a systemd service does the same job:
[Service]
ExecStart=/usr/bin/ssh -N -o ServerAliveInterval=30 -o ServerAliveCountMax=3 -o ExitOnForwardFailure=yes -L 5433:db.internal:5432 deploy@bastion.example.com
Restart=always
RestartSec=5
Both need a login that works without typing, which means a key (see SSH key authentication), because nobody is there to enter a password. ExitOnForwardFailure=yes matters: without it, a restarted ssh that cannot bind its local port stays connected and useless. Windows has no built-in equivalent for the ssh client; people use a PowerShell loop or a scheduled task.
What happens to applications using the tunnel
Every forwarded connection travels inside the one SSH connection, so when it drops they all break at once. In our test, a psql session running a query reported server closed the connection unexpectedly, and new connections got Connection refused because plain ssh removes the local port when it exits. Open transactions are rolled back by the database. Connection pools may hand out dead connections until they validate them. No tunnel tool can preserve those inner sessions; what a tool can do is bring the tunnel back quickly and keep the address stable. The automatic reconnection guide covers that background in full.
How Transputty handles a dropped tunnel
In version 0.1, each connection has Keepalive (s) in the editor's Behaviour section. The default is 30, with three missed replies before the connection is treated as dead; 0 turns keepalives off. Reconnect automatically is on by default and retries with a backoff that starts at 1 second and doubles up to 60 seconds.
While Transputty reconnects:
- The connection shows the pill "Reconnecting" with a countdown to the next attempt, and each local or SOCKS rule shows "Port held". The local port stays open, so applications keep the same address, and a client that connects during a reconnect is held for up to 10 seconds instead of being refused. If the tunnel is not back by then, the client's connection is reset.
- A callout titled "Server stopped responding" (error code
KEEPALIVE_TIMEOUT) or "Connection lost" (TRANSPORT_CLOSED) says the SSH connection dropped and that it is retrying automatically, with Retry now and Stop buttons. - When Windows reports no network, the pill reads "Waiting for network" and Transputty resumes when the network returns.
- A rejected login is never retried in a loop.
Three limits are worth knowing before you rely on this:
- A held port looks open. While a rule shows "Port held", the local port accepts TCP connections although the tunnel is down. A plain port check such as
Test-NetConnection 127.0.0.1 -Port 5433succeeds, and a client waits up to 10 seconds before it is cut off, where plainsshwould have refused it at once. Health checks should make a real request through the tunnel. In 0.1 the hold is fixed at 10 seconds and cannot be switched off. - One-time codes need you again. A password or key passphrase you typed is kept in memory while the connection stays open in the app and is reused for reconnects. Answers to an Interactive (keyboard-interactive) login, such as a one-time code, are never kept, so every reconnect asks again. A prompt nobody answers times out after 5 minutes; the connection then shows "Failed" with the tag "Prompt not answered" and stays that way until you click Retry. A tunnel behind a one-time-code login does not come back unattended.
- Remote (-R) rules can stay failed. After a reconnect Transputty asks the server for the listener again. If the server still holds the port from the dead session, the rule shows "Server refused to listen" and is not retried automatically; click Retry on the rule. The remote port forwarding tutorial explains why.
Connections that were already open through the tunnel still break, as described above. Afterwards the inspector lists each outage under "Reconnect history" on the Overview tab and "Reconnect log" on the Statistics tab, next to "Availability · 24 h".
A silently dead path is detected by keepalive, which with the defaults takes about 90 to 120 seconds. To run several tunnels this way, see managing multiple SSH tunnels on Windows.
Common questions
What is the best ServerAliveInterval for an SSH tunnel?
30 seconds with ServerAliveCountMax 3 suits most networks. ssh then sends a small encrypted probe after 30 seconds of silence, which keeps firewall and NAT entries alive, and gives up after about 90 seconds without an answer. Go lower, for example 15, only if a device on the path forgets idle connections faster than that.
What does client_loop: send disconnect: Broken pipe mean?
ssh tried to send data and the operating system reported that the TCP connection no longer exists. Something on the path dropped it earlier, usually an idle timeout on a firewall or NAT router, a network change or a sleep. On Windows the same event reads client_loop: send disconnect: Connection reset. Keepalives prevent the idle case.
Does ServerAliveInterval reconnect a dropped tunnel?
No. ServerAliveInterval keeps an idle connection from being forgotten and detects a dead one so that ssh exits. It never reconnects. To bring a tunnel back you need something that runs ssh again, such as autossh, a systemd service with Restart=always, a loop in a script or a tunnel manager.
What happens to my database connection when the SSH tunnel drops?
It breaks. Every connection through the tunnel lives inside the one SSH connection, so open sessions, transactions and idle pool connections are lost when it goes. After the tunnel returns, applications must connect again. Reconnecting the tunnel, by any tool, cannot restore the sessions that were inside it.
Is TCPKeepAlive the same as ServerAliveInterval?
No. TCPKeepAlive uses the operating system's TCP keepalive probes, which by default start only after about two hours of silence, far too late for most firewalls. ServerAliveInterval sends messages inside the encrypted SSH connection at the interval you choose. For tunnels that drop when idle, ServerAliveInterval is the setting that matters.
How this tutorial was checked
2026-11-26: Reproduced on Windows 11 with the built-in ssh client (OpenSSH_for_Windows 9.5p2) against a real OpenSSH server (OpenSSH for Windows 10.4p1) on the same PC, with psql 18 running a query through the tunnel: the 'Timeout, server not responding' message after freezing the server-side session (with ServerAliveInterval=2 and ServerAliveCountMax=2); 'client_loop: send disconnect: Connection reset' after killing it; psql's 'server closed the connection unexpectedly' in both cases; and 'Connection refused' on the local port afterwards. Not reproduced: the Linux and macOS wordings (Broken pipe, Connection reset by peer, packet_write_wait), which are quoted as commonly reported. Not run: the ssh -vvv packet lines, PuTTY, autossh, the systemd unit, a real idle NAT timeout, sleep and wake, network changes and the server-side timeout settings, which are described from the OpenSSH manual. The Transputty behaviour is described from the 0.1 source code (engine and app) and was not observed in the app.
Sources and further reading
- ssh_config(5) manual page, OpenBSD (ServerAliveInterval, ServerAliveCountMax, TCPKeepAlive, ExitOnForwardFailure)
- sshd_config(5) manual page, OpenBSD (ClientAliveInterval, ClientAliveCountMax, ChannelTimeout, UnusedConnectionTimeout, MaxSessions, MaxStartups)
- PuTTY manual, chapter 4: the Connection panel (Seconds between keepalives)
- autossh home page
- systemd.service manual (Restart=, RestartSec=)
Transputty keeps SSH tunnels running on Windows
Local, remote and SOCKS5 forwards in one window, automatic reconnection with ports that stay bound, and a clear reason when something fails. In development; no public build yet.