If you see “ssh: write failed: Connection reset by peer,” it means the SSH client lost the TCP connection while sending data, often due to network instability, a timeout, or a server-side drop. The fix is to identify the cause and adjust SSH keep-alive settings, network conditions, or server limits. This article explains why the error occurs and provides step-by-step fixes you can try.
What Does “Connection Reset by Peer” Mean?
In SSH, the “connection reset by peer” message appears when the remote server (the “peer”) sends a TCP RST packet, abruptly closing the connection. This happens after the SSH client has already started writing data, so the error is “write failed” rather than a failure to connect. The reset can be triggered by:
- Network interruptions (Wi-Fi drops, VPN changes, firewall resets)
- Idle timeout on the server or client (no data sent for a long time)
- Server resource limits (max sessions, memory pressure)
- MTU or packet fragmentation issues
- Aggressive firewall or NAT timeouts
Unlike a clean SSH session close (where you see “Connection to host closed”), a reset is abrupt and often leaves your terminal in a broken state.
Why Does SSH Drop Idle Connections?
SSH connections are designed to stay open, but network devices (routers, firewalls, NAT gateways) often drop idle TCP sessions after a timeout—commonly 5 to 30 minutes. When the SSH client finally sends data (e.g., you type a command), the connection is already gone, and the server (or an intermediate device) sends a RST, causing the “write failed” error.
To prevent this, SSH has a built-in keep-alive mechanism: the client can send a null packet at a set interval to keep the connection alive. You can enable this in your SSH client configuration (~/.ssh/config) or on the command line.
How to Fix the Error (Step-by-Step)
Try these fixes in order; the first one usually resolves the issue for most users.
- Enable SSH keep-alive on the client. Edit or create
~/.ssh/configand add:
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
This sends a keep-alive packet every 60 seconds; if no response after 3 tries, the client gives up. You can also add it per-host.
- Enable keep-alive on the server. Edit
/etc/ssh/sshd_configon the server and add:
ClientAliveInterval 60 ClientAliveCountMax 3
Then restart sshd (e.g., sudo systemctl restart sshd). This is useful if you control the server.
- Check network stability. If you’re on Wi-Fi, try a wired connection. If you use a VPN, try disconnecting it. Run a continuous ping to the server to see if packets drop.
- Adjust MTU size. Large packets can be dropped by some networks. You can test with a smaller MTU using
ping -M do -s 1400 server(Linux) orping -D -s 1400 server(macOS). If that works, you can set a lower MTU on your interface, but this is rarely the cause. - Check server resource limits. On the server, look at
dmesgor/var/log/auth.logfor messages about sshd being killed or out of memory. If the server is under heavy load, it may drop connections. - Use a connection multiplexer. Tools like
mosh(mobile shell) handle network changes better and don’t drop on idle. Mosh is a good alternative if you frequently have this issue.
Common Misconception: It’s a Server Ban or Security Issue
Many users assume “connection reset” means the server blocked them (e.g., fail2ban). While that can happen, the error message is different: you’d typically see “Connection closed by remote host” or “Permission denied.” A reset during an established session is almost always a network or timeout issue, not a security block. If you suspect a block, try connecting from a different IP or after a few minutes.
Server-Side Configuration That Can Cause Resets
If you control the server, check these settings:
- MaxSessions and MaxStartups in sshd_config: if these are too low, new connections may be dropped, but existing sessions are usually not reset.
- LoginGraceTime: if you don’t authenticate within the timeout, the connection is dropped, but that’s before the session starts.
- TCPKeepAlive: this is a lower-level keep-alive that may not be enough; the SSH-level keep-alive is more reliable.
- Firewall rules: some firewalls (like iptables) have connection tracking timeouts that can be adjusted, but that’s advanced.
When to Use a Different Tool
If you regularly work over unreliable networks (e.g., mobile hotspots, long-running tasks), consider these alternatives:
- mosh: maintains the session across IP changes and doesn’t drop on idle. It requires installing mosh on both client and server.
- tmux or screen: run your work inside a terminal multiplexer on the server. If the SSH connection drops, you can reconnect and resume your session.
- autossh: automatically restarts SSH sessions if they drop. Useful for port forwarding.
Using tmux is a good practice even without the error, as it protects against any network hiccup.
Frequently Asked Questions
Why does my SSH connection reset when I’m idle for 10 minutes?
This is almost certainly an idle timeout on a network device (router, firewall, or NAT). The SSH keep-alive settings described above (ServerAliveInterval) will prevent this by sending a small packet every 60 seconds.
Can I fix this without editing server config?
Yes. The client-side keep-alive (ServerAliveInterval) works without any server changes. It sends a null packet that keeps the connection active. If the server has a strict idle timeout (e.g., 5 minutes), set your interval to 30 seconds.
Does this error corrupt my files or data?
No. The error only happens when the connection is lost; any data already transferred is intact. If you were in the middle of a file transfer (like scp), the transfer will fail and you’ll need to restart it. For interactive sessions, your terminal state is lost, but no data is corrupted.
What’s the difference between “Connection reset by peer” and “Connection timed out”?
A reset means the peer actively closed the connection (sent RST), so you get an immediate error. A timeout means no response was received within a certain time, so the client gives up. Both indicate a network issue, but a reset often means a firewall or server actively dropped the connection.
Is it safe to use keep-alive packets?
Yes. SSH keep-alive packets are encrypted and contain no data; they just keep the connection alive. They are a standard feature and don’t affect security.
My SSH connection resets only when I run a long command. Why?
This can happen if the command produces no output for a long time, and the server or network times out the idle connection. The keep-alive settings will help. Also, ensure your client doesn’t have a timeout configured (e.g., ServerAliveInterval is not set to 0).
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Games Like Assassin’s Creed: 12 Open-World Picks
- KCD2 Blacksmith’s Son Quest Guide
- Omega ORU52X Specs: What They Mean for You42.00 cu ft
- AI Agent State Safety During Context Compaction
- Snapdragon 6s Gen 3 vs 665: Which Is Faster?
- Delete Stations Pandora? How to Remove Them