How to Increase & Prevent SSH Connection Timeout

SSH Connection Timeout

You face SSH connection timeout due to inactivity and you need to start all over again. Here you will see how to increase & prevent SSH connection timeout.
First, let’s see how you can increase the SSH timeout limit and keep your SSH session alive even if it is inactive.

Increase SSH Connection Timeout:

Increase SSH Connection Timeout

You need to head over to the configuration file.

/etc/ssh/sshd_config

Now locate the below-given parameters

#ClientAliveInterval
#ClientAliveCountMax

Where the ClientAliveInterval indicates the time in seconds while the server was waiting before it sends a null packet to the client system to keep the connection alive.

The ClientAliveCountMax indicates the number of client messages that are alive, which was sent from the client without receiving any messages. When this limit is reached the SSH session terminates.

The time out value will be;

Timeout value = ClientAliveInterval * ClientAliveCountMax

Suppose if you have defined your parameters as ClientAliveInterval  1200 and ClientAliveCountMax 4, then your connection disconnects after (1200*3)seconds if the client sends no response.
That is ClientAliveInterval  3600

Now reload your OpenSSH daemon to see the changes.

$ sudo systemctl reload sshd

Note: Don’t set the SSH timeout value too high. Otherwise, anyone can access your session when you are away.

Prevent SSH from timing out

If you spend a lot of time at the command line, you may have noticed that your session runs out after a relatively short amount of inactivity. This is useful from a security point of view, it might pose issues when performing a long-running operation. Here’s how to prevent SSH from timing out temporarily.

When you’ve been inactive for a long, your connection to the server is usually reset, resulting in the error: Connection reset by peer.

Let us see the solution to avoid this issue by, configuring a Keep-Alive option on either the client or the server.

Solution 1 – Keep Alive on Server Side

This technique is less secure than the Client-Side option since you must take this operation as root, and it applies to all client connections rather than just yours. As a result, we recommend you to adopt the Client-Side method whenever feasible, or once after the work is completed remove this option from the server.

Follow the steps to enable the SSH Keep Alive option on the server:

  •  First login as root.
  •  Next, Edit the file at /etc/ssh/sshd_config
  • Insert this line into the file: ClientAliveInterval 60
  • Then you can save the file and Restart sshd on the server

Solution 2 – Keep-Alive on Client Side

This technique is configured on the client machine that you are using to connect to the server. If you’re running Linux, the process is similar to the Server Side procedures, with slight differences.

Follow the steps to enable the SSH Keep Alive option on a Linux client:    

  • First login as root.
  • Next, Edit the file at /etc/ssh/sshd_config
  • Then, Insert this line into the file: ServerAliveInterval 60
  • Now save the file.

You can also check: Installing OpenSSH on Windows Servers

To get more updates you can follow us on Facebook, Twitter, LinkedIn

Subscribe to get free blog content to your Inbox
Loading

Written by actsupp-r0cks