How to prevent SSH from timing out

Keeping connections to an SSH server alive

If you spend a lot of time at the command line you may have run into an annoying issue where your session times out after a relatively brief period of inactivity. While this is desirable from a security perspective, it can cause problems when you’re trying to perform a long running operation. Here’s how to temporarily prevent SSH from timing out.

Usually what happens is that your connection to the server is reset when you’ve been idle for a while, typically producing the error: Connection reset by peer. To circumvent this, you need to set a Keep Alive option on either the client or the server.

Option 1) Server Side Keep Alive

This method is less secure than the Client Side alternative because A) you need to perform this action as root, and B) because it will apply to all client connections instead of just yours. For this reason, I recommend using the Client Side approach when possible, or at least removing this option from the server when your work is complete.

To set the SSH keep alive option on the server:

  • Log in as root

  • Edit the file at /etc/ssh/sshd_config

  • Add this line to the file: ClientAliveInterval 60

  • Save the file

  • Restart sshd on the server

Option 2) Client Side Keep Alive

This method is set on your client machine which you’re using to connect to the server. If you’re using Linux, the method is similar to the Server Side steps with a couple of minor differences.

To set the SSH keep alive option on a Linux client:

  • Log in as root

  • Edit the file at /etc/ssh/ssh_config

  • Add this line to the file: ServerAliveInterval 60

  • Save the file

To set the SSH keep alive option on Windows (with PuTTY):

  • Open PuTTY

  • Click on the Connection Category in the left menu

  • Check the box for Enable TCP keepalives (SO_KEEPALIVE option)

1_16.png
What’s wrong? The new clean desk test
Join the discussion
Be the first to comment on this article. Our Commenting Policies