If you want to prevent a
Secure Shell (SSH)
connection being dropped when it has been inactive for several minutes, you can
specify a keepalive value.
To specify a keepalive value directly from the command line, use
the -o flag followed by the
ServerAliveIntervaloption
Command Line Syntax
Run the following command to send a keepalive packet every 60 seconds:
ssh -o ServerAliveInterval=60 user@hostname
Advanced Keepalive Control
For more robust protection against drops, combine the interval with ServerAliveCountMax. This defines how many consecutive keepalive messages can go unanswered before the client disconnects.
ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 user@hostname
ServerAliveInterval=60: Sends a packet every 60 seconds of inactivity.ServerAliveCountMax=3: Drops the dead connection if the server fails to respond 3 times in a row.
Making It Permanent (Optional)
If you get tired of typing this out every time, you can save it to your local configuration:
- Open your configuration file in a text editor,
such as nano:
nano ~/.ssh/config - Add these lines to apply it to all hosts:
Host * ServerAliveInterval 60 ServerAliveCountMax 3
