How to Prevent SSH Session Timeout on macOS and Linux

Two penguins kissing.

SSH (Secure Shell) is an essential tool for managing remote servers, but sessions can sometimes time out due to inactivity. This can be particularly inconvenient if you’re working on long tasks or running scripts. In this guide, we’ll show you how to keep SSH sessions alive on both macOS and Linux, ensuring your work isn’t interrupted by automatic disconnections.

Related: How to Set Up SSH Key-Based Authentication for Remote Login Without a Password

penguin holding key guarding a door. angsy. grungy gritt. textured

Why SSH Sessions Timeout

SSH sessions may close automatically due to inactivity to prevent security risks and free up server resources. To maintain an active session, you can adjust settings on both your SSH client and the remote server.

1. Adjust SSH Client Settings on macOS and Linux

To prevent your SSH client from timing out, you can configure it to send periodic keep-alive messages to the server. Follow these steps:

On macOS:

Open the SSH Configuration File:

  • Open Terminal on your Mac. Edit the SSH configuration file by typing:
$ nano ~/.ssh/config

Add Keep-Alive Settings:

  • If the file is empty, add the following lines:
Host * ServerAliveInterval 60

ServerAliveInterval 60 tells your SSH client to send a keep-alive message every 60 seconds.

Save and Exit:

  • Press Ctrl + X, then Y to confirm changes, and Enter to save and exit.

Connect to the Remote Host:

  • Use SSH as usual. The new settings will apply automatically.

On Linux:

Open the SSH Configuration File:

  • Open Terminal on your Linux machine.
  • Edit the SSH configuration file by typing:
nano ~/.ssh/config

Add Keep-Alive Settings:

  • If the file is empty, add the following lines:
Host * 
ServerAliveInterval 60
  • ServerAliveInterval 60 tells your SSH client to send a keep-alive message every 60 seconds.

Save and Exit:

  • Press Ctrl + X, then Y to confirm changes, and Enter to save and exit.

Connect to the Remote Host:

  • Use SSH as usual. The new settings will apply automatically.

2. Configure SSH Server Settings

If you have control over the remote server, you can adjust its settings to prevent it from closing idle connections.

On macOS:

Access the Server:

  • Connect to your macOS server via SSH.

Edit the SSH Server Configuration File:

Open the server configuration file with:

$ sudo nano /etc/ssh/sshd_config

Update Timeout Settings:

Add or modify the following lines:

ClientAliveInterval 60 
ClientAliveCountMax 3
  • ClientAliveInterval 60 makes the server send keep-alive messages every 60 seconds.
  • ClientAliveCountMax 3 allows for 3 missed messages before disconnecting.

Restart the SSH Service:

Apply the changes by restarting the SSH service with:bashCopy code

$ sudo systemctl restart ssh

On Linux:

Access the Server:

  • Connect to your Linux server via SSH.

Edit the SSH Server Configuration File:

Open the server configuration file with:

$ sudo nano /etc/ssh/sshd_config

Update Timeout Settings:

Add or modify the following lines:

ClientAliveInterval 60 
ClientAliveCountMax 3
  • ClientAliveInterval 60 makes the server send keep-alive messages every 60 seconds.
  • ClientAliveCountMax 3 allows for 3 missed messages before disconnecting.

Restart the SSH Service:

Apply the changes by restarting the SSH service with:

$ sudo systemctl restart ssh

Conclusion

By configuring both your SSH client and the remote server, you can prevent SSH sessions from timing out due to inactivity. This ensures a smoother workflow, particularly for long-running tasks. Adjusting these settings is straightforward and can greatly enhance your remote working experience.

Leave a Reply

Your email address will not be published. Required fields are marked *