Over the last 15 years, I have spent more time inside a shell terminal than any other place on Earth. It is by far my favorite place to be. Often times, I am logged into a remote server. And I like staying logged in. There are a couple of ways of keeping that connection alive that in turn help me maintain my happiness.

The easiest is to set configuration on the local machine (i.e., MacBook):

# Edit ssh_config file
sudo vim /etc/ssh/ssh_config

# Add ServerAliveInterval
Host *
  ...
  ServerAliveInterval 30
  ...

The other solution is to set that configuration on the remote server itself. This is ideal if there are either multiple users or multiple workstations that need to remain connected. Of course, we’ll need to take into account the security implications.

# Login to remote server
ssh user@example.com

# Check the value (should say: ClientAliveInterval 180)
sudo grep "ClientAliveInterval" /etc/ssh/sshd_config

# If it does not exist, add it.
sudo vim /etc/ssh/sshd_config

# Restart service
sudo systemctl restart sshd

Resources