97

When I close my MacBook with an active SSH session in Terminal and then wake it up, the prompt goes unresponsive.

In about five minutes it says

Write failed: Broken pipe

and terminates.

This also happens when I don't type anything in the Terminal in about fifteen minutes.

Can I force OS X to:

  1. Keep SSH connection alive for longest time possible
  2. Kill it immediately once it becomes unresponsive

?

117

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.

  • 3
    Ok, you can kill the connection with ~., great tip! But what does it actually do? – CousinCocaine Apr 17 '14 at 12:21
  • The only thing I was confused about was wether or not I had to restart sshd, but it seems as though just exiting the session and reconnecting did the trick – dgig Jun 20 '14 at 18:02
  • Often the ~. doesn't work for me for several minutes. Why is that? – Sridhar Sarnobat Mar 25 '17 at 18:38
  • 2
    The location of ssh_config is /etc/ssh/ssh_config on Mac OS X. I would edit the answer myself, but I don't have enough rep on this site to do so. – Erik B Aug 18 '18 at 23:52
  • should I put this into client or the server? I think client, but can be clearer. – EralpB Sep 6 '18 at 9:59
21

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...

8

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.

  • Actually, that's exactly what I'm doing now. – Dan Nov 8 '13 at 19:20
5

The various answers here are in conflict regarding the exact path to the config files. Maybe it is different in different unixes? I suggest reading the man pages on your system!

In man ssh(1), search for text: config. On my system, I have a choice of either system-wide or this-user config files (and the paths are oddly different).

In man ssh_config(5), search for text: alive. On my system, it looks like I need to set both ServerAliveInterval and ServerAliveCountMax.

What worked for me, on macOS 10.12 Sierra, is to put the following two lines in ~/.ssh/config:

ServerAliveInterval 20
ServerAliveCountMax 180

Now I can ssh to my Ubuntu 16.04 server with no disconnect for 20*180 seconds = 1 hour. Made my day!

2

Automatically reconnect ssh after sleep or wifi interruption

  1. Install autossh from MacPorts or Homebrew: brew install autossh

  2. Open Terminal and run autossh -M 20000 -f [SSH ARGUMENTS GO HERE]

  3. Disconnect your wifi or put your Mac to sleep.

  4. When you reconnect to wifi, the ssh session will automatically reconnect.


Automatically ssh after login

Jon Stacey wrote a blog post about automatically activating an ssh session when you login.

  1. Install autossh from MacPorts or Homebrew: brew install autossh

  2. Create an Automator workflow with a bash script.

Automator shell script

  1. Go to login items in System Preferences.

System Preferences > Login items

  1. Add your Automator workflow to Login items.

Login items

1

Add these two lines to /etc/ssh/ssh_config:

ServerAliveInterval 60
TCPKeepAlive yes
0

Just resolved this issue add these following lines in $HOME/.ssh/config.

Host *
  ServerAliveInterval 120
  TCPKeepAlive no

Setting TCPKeepAlive no tells the client to just assume the connection is still good until proven otherwise by a user request, meaning that temporary connection breakages while your ssh term is sitting idle in the background won't kill the connection.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .