Keep your SSH session alive and save your keyboard

Permalink · By · ·

For the last couple years I’ve been setting up and maintaining the web server we use for our websites at work.

It’s been a long time since the FTP-based workflow we were forced into when we used regular shared servers. I now find myself doing stuff over SSH every day, so I have a session open in a terminal window at almost all times.

There was a problem that used to annoy me very much: after a few minutes without any activity, the SSH connection was dead and I couldn’t input any text. Then I had to reopen the terminal and log-in to the server again. Repeat this a million times during the day and you’ll soon start smashing your keyboard in frustration.

If you, like me, are a newbie in this subject and want to keep your keyboard safe, I have the solution for this specific annoyance (if you’re on a mac).


In a terminal, edit your /etc/ssh/ssh_config file

sudo nano /etc/ssh/ssh_config

Then add these lines at the end of the file

Host *
  ServerAliveInterval 60

This makes your machine send a “ping” to the server every 60 seconds, so the connection doesn’t die.

Save the file and exit the editor (ctrl + o, then ctrl + x) and you’re ready to go. Restart your terminal and go on your SSH session. Now you can keep it open forever!