How to make your SSH Terminal shell sessions last longer on macOS

If you find that your SSH shell sessions to remote computers are timing out too quickly with the error “Write failed: Broken pipe”, you can make a simple configuration to keep these sessions going as long as you have your terminal shell window running: 

Move into your SSH settings folder

cd ~/.ssh

Create a local SSH config file

nano config

Add in a session variable

ServerAliveInterval 60

Write out and save the file.

So the next time you initiate a SSH session with a remote computer, after 60 seconds of inactivity a ‘no-op’ (no operation) message is sent to keep the session alive and it just keeps going and going.

2 Comments

  1. Brian on October 13, 2022 at 6:09 pm

    I purchased a new MBP 16″ about two weeks ago. I have tried everything I know and could find, including your tip above, to keep my ssh session alive. NOTHING seems to work. It’s truly maddening. Contacting Apple Support has gone nowhere as well. The only thing their support seem to understand are the MacOS app toys. :(

    • emma on October 17, 2022 at 5:54 pm

      Have you tried this?
      ServerAliveInterval 120
      ServerAliveCountMax 20
      TCPKeepAlive yes
      StrictHostKeyChecking no

      ControlMaster auto
      ControlPath ~/.ssh/cntlsession/%C
      ControlPersist yes

      What exactly is you issue? After how long your connection is interrupted?

Leave all Comment