To keep an SSH connection alive and prevent it from timing out, you can use the ServerAliveInterval
option in your SSH client configuration or command line. This option sets the interval at which the client sends keepalive messages to the server. Additionally, the ServerAliveCountMax
option controls how many unanswered keepalive messages trigger a connection termination.
To set the heartbeat interval in your SSH client configuration:
- Locate the SSH configuration file: The file is typically located at
~/.ssh/config
for user-specific settings, or/etc/ssh/ssh_config
for system-wide settings. - Add or modify the following lines:
Code
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
Host *
applies the settings to all hosts. You can specify a particular host instead, likeHost example.com
.ServerAliveInterval 60
sets the interval to 60 seconds.ServerAliveCountMax 3
closes the connection if 3 consecutive keepalive messages are not acknowledged.
To set the heartbeat interval directly in the SSH command:
Code
ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 user@hostname
Explanation:
ServerAliveInterval
: Specifies the interval (in seconds) at which the client sends keepalive messages (also known as “hellos”) to the server. If the server doesn’t respond within this interval, the client will send another keepalive. If the server fails to respond afterServerAliveCountMax
intervals, the connection is dropped.ServerAliveCountMax
: Defines the number of unanswered keepalive messages that will trigger a disconnection.
Leave a Reply