Apple - How can I prevent an SSH session from hanging in OS X Terminal?

For keeping the connection alive, you can check in /etc/ssh/ssh_config the line where it says ServerAliveInterval, that tells you how often (in seconds) your computer is gonna send a null packet to keep the connection alive. If you have a 0 in there that indicates that your computer is not trying to keep the connection alive (it is disabled), otherwise it tells you how often (in seconds) it is sending the aforementioned packet. Try putting in 120 or 240, if it is still killing your connection, you can go lower, maybe to 5, if with that number it doesn't happen, maybe it is your router who is dumping the connection to free memory.

For killing it when it gets hang up, you can use the ssh escape character:

~.

That is, press the tilde and then the period, if it doesn't work, press Enter before you press that, that will kill the connection immediately.


You can:

Configure your system, system-wide, for all connections By editing: /etc/ssh/ssh_config And add the line:

ServerAliveInterval 10

Or, per-server ~/.ssh/config

Host keepsdroppingme.com
   ServerAliveInterval 10

What this basically does it sends keep alive packet, every 10 seconds...


You can also try using the awesome mosh project. It will gracefully handle network losses, machines going to sleep, etc and happily restore the connection when you have a working network again.

Another more time-honored solution is to use tmux on the remote machine. In that case you may still get a broken pipe, but if you reconnect your shell and applications will be waiting just as you left them.