What are `ServerAliveInterval` and `ClientAliveInterval` in sshd_config?

OpenSSH is a combination of different utilities for secure networking. It is a protocol (SSH) primarily used for remote connections. The file is in the “/etc/ssh/sshd_config” directory. The ssh will terminate the session if it is inactive and disconnects from the remote (client) machine. The duration of inactivity before the connection breaks are done by increasing the session duration. It is managed through “ServerAliveInterval` and `ClientAliveInterval,” but how do they work?

The ssh works on the base of the client/server model. The client initiates the connection while the ssh server listens for incoming requests.

This guide explores the working of ServerAliveInterval and ClientAliveInterval and how to increase the session duration.

  • ClientAliveInterval
  • ServerAliveInterval

What is ClientAliveInterval in sshd_config File?

A specific time (in seconds) is assigned to the ClientAliveInterval option in the “/etc/ssh/sshd_config” file to keep the connection alive. Here is how the file can be viewed and edited using the nano editor:

$ sudo nano /etc/ssh/sshd_config

Scroll down and find ClientAliveInterval and the ClientAliveCountMax:

Here, we’ve set the customized values, and you can use any value based on the requirements. It is set in seconds.

To calculate the total time before disconnection due to inactivity, this formula is used:

Timeout value = ClientAliveInterval * ClientAliveCountMax

The explanation of the above formula is as follows:

  • ClientAliveInterval is the timeout interval after which the connection is closed if no data is received from the client. This is measured in seconds, and the default value is set to 0, which means no timeout period is defined
  • ClientAliveCountMax is the counter (default value is 3) is the number of ClientAliveMessage or null packets. It continues to do that until a message is received back from the client or the threshold is reached. 

As you see from the above image, we’ve set the value of ClientAliveInterval to 1200 and 4 for ClientAliveCountMax. It will wait 1200 seconds before sending a ClientAliveMessage or null packet. This process will continue for 4800 seconds in total.

What is ServerAliveInterval in sshd_config File?

A specific time (in seconds) is assigned to the ServerAliveInterval option in the “/etc/ssh/sshd_config” file to keep the connection alive. Here is how the file can be viewed and edited using the nano editor:

$ sudo nano /etc/ssh/sshd_config

Scroll down to find  ServerAliveCountMax, and ServerAliveCountMax set the values and save the file by pressing “CTRL+S” and then “CTRL+X” to exit.

Here, we’ve set customized values, and you can use any value based on the requirements. It is set in seconds.

To calculate the total time before disconnection due to inactivity, this formula is used:

Timeout value = ServerAliveInterval * ServerAliveCountMax

The explanation of the above formula is as follows:

 ServerAliveInterval is the timeout interval after which the connection is closed if no data is received from the client. This is measured in seconds, and the default value is set to 0, which means no timeout period is defined

ServerAliveCountMax is the counter (default value is 3) is the number of ClientAliveMessage or null packets. It continues to do that until a message is received back from the client or the threshold is reached.

The only difference is that this option sets the time interval when the server sends a message or a null packet to the client to keep the connection alive. If no message or null packet is received from the client within the specified time set, the server assumes that the connection is automatically closed.

Conclusion

The options ServerAliveInterval and ClientAliveInterval in sshd_config are used to increase or decrease the SSH connection timeout. It is beneficial when a connection is idle for a long time, especially when one of the systems has an unreliable network. It works by sending a message or null packet at regular intervals, and the connection remains open if there is a response from both sides, i.,e client and server.

This guide explained the importance and brief explanation of the `ServerAliveInterval` and `ClientAliveInterval` in sshd_config.