Depending on how your SSH server or internal firewalls are configured, your SSH session may automatically disconnect after some time. It's possible to configure SSH to send a special packet every certain number of seconds, to keep the connection from idling and becoming a candidate for disconnection. This is useful if you have a service that utilizes SSH, that you do not want to be disconnected. To employ this tweak, we must configure the ServerAliveInterval
setting.
There are two ways of configuring this, one that affects your user account and another that will deploy the setting system wide. First, let's explore how to configure this for your user account.
Remember the ~/.ssh/config
file that we configured earlier in this chapter? Open it up again in your text editor. Here's a sample of this file for your convenience:
Host icarus Hostname 10.10.10.76 Port 22 User jdoe Host daedalus Hostname 10.10.10.88 Port 65000 User duser Host dragon Hostname 10.10.10.99 Port 22 User jdoe
As before, we have three systems. If we wish to configure a host, for example icarus
, to send an alive packet once every 60 seconds, we can add the following setting to it:
Host icarus ServerAliveInterval 60 Hostname 10.10.10.76 Port 22 User jdoe
If we wish to set the ServerAliveInterval
setting for all hosts we connect to, we could add this option as a wildcard instead by adding the following to the top of the file:
Host * ServerAliveInterval 60
With this, the setting takes effect for all systems we initiate a connection to. Although we haven't discussed them (yet), there are two system-wide (global) configuration files for SSH. We'll discuss these files later in this book, but the subject of this section is an opportunity to give you a quick introduction:
/etc/ssh/ssh_config
: This file will impact all users whom make outbound connections. Think of this as the client configuration file./etc/ssh/sshd_config
: This is the global config file for the server.Anything you configure in one of these two files will impact anyone. The ssh_config
file impacts all outbound connections, and the sshd_config
impacts all the incoming connections. For this section, the file we're interested in is the ssh_config
file, since we can set the ServerAliveInterval
setting for all users by including it there. In fact, regardless of whether we're configuring /etc/ssh/ssh_config
or the local ~/.ssh/config
file, the option is the same. Simply add it to the end of the file:
ServerAliveInterval 60
Of course, we'll explore configuring these options further later on in this book. For now, just remember the purpose of these two files and where they're located.