When you connect to a remote Linux server via SSH, the connection establishes a quiet, encrypted tunnel. However, if you stop typing and leave the terminal idle while you read documentation or drink coffee, you will often return to find the terminal completely frozen. Eventually, it will spit out a frustrating error message: Write failed: Broken pipe or Connection reset by peer.
This happens because modern firewalls and enterprise routers aggressively terminate “idle” network connections to conserve resources. If no data travels through the SSH tunnel for a few minutes, the router assumes the connection is dead and silently severs it.
To solve this, you must configure your local SSH client to automatically send microscopic, invisible heartbeat signals to the server to keep the connection alive.
How to Configure ServerAliveInterval
You can apply this fix globally so that it affects every SSH connection you ever make from your local Linux (or macOS) machine.
- Open the terminal on your local computer (not the remote server).
- You need to edit your personal SSH configuration file. Use a text editor like nano:
nano ~/.ssh/config
- (Note: If the file does not exist, nano will simply create a blank file for you, which is perfectly fine).
- Type or paste the following three lines exactly as they appear:
Host * ServerAliveInterval 60 ServerAliveCountMax 3 - Press Ctrl + O, then Enter to save the file.
- Press Ctrl + X to exit nano.
How the Configuration Works
By adding these lines to your configuration file, you have fundamentally changed how your SSH client behaves.
- Host *: This wildcard tells the SSH client to apply these rules to every single server you connect to.
- ServerAliveInterval 60: This is the critical heartbeat. If you do not touch your keyboard for 60 seconds, your local computer will silently send a tiny, encrypted ping to the remote server requesting a response. This microscopic burst of data tricks any firewalls sitting in the middle of the connection into believing you are actively typing, preventing them from closing the tunnel.
- ServerAliveCountMax 3: If your local internet connection actually goes down, your computer will send three heartbeats (waiting 60 seconds between each). If the server fails to reply to all three, your client will realize the internet is truly broken and gracefully close the connection, rather than leaving a frozen terminal window open indefinitely.