How to Prevent SSH Session Timeout on macOS and Linux

SSH (Secure Shell) is an essential tool for managing remote servers, allowing secure communication and control over various systems. However, sessions can sometimes time out due to inactivity, which can be particularly inconvenient when working on long tasks or running scripts. This automatic disconnection can interrupt workflows, causing users to lose unsaved progress or encounter delays while reconnecting. Fortunately, there are configurations you can implement on both macOS and Linux systems to keep your SSH sessions alive, preventing these interruptions and enhancing productivity.

In this guide, we’ll demonstrate how to configure your SSH client and server settings to maintain persistent connections. By adjusting parameters such as ServerAliveInterval and ClientAliveInterval, you can effectively manage how frequently keepalive messages are sent between the client and server. This ensures that connections remain active even during periods of inactivity. Following these steps not only enhances your workflow but also reduces frustration when accessing remote servers for system management, development tasks, or file transfers.

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


Table of Contents


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:
vim ~/.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:
vim ~/.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 Comment