Keep Alive SSH Sessions
To keep an SSH connection alive, you can modify your SSH configuration to send periodic keepalive messages. Here are a few methods to achieve this:
Method 1: Modify the SSH Client Configuration
- Edit the SSH client configuration file (
/etc/ssh/ssh_configor~/.ssh/config):nano ~/.ssh/config
- Add the following lines to send keepalive messages every 60 seconds:
Host * ServerAliveInterval 60 ServerAliveCountMax 3ServerAliveIntervalspecifies the interval in seconds between keepalive messages.ServerAliveCountMaxspecifies the number of keepalive messages that can be sent without receiving a response from the server before the connection is terminated.
Method 2: Modify the SSH Server Configuration
- Edit the SSH server configuration file (
/etc/ssh/sshd_config):sudo nano /etc/ssh/sshd_config
- Add or modify the following lines to keep the server alive:
ClientAliveInterval 60 ClientAliveCountMax 3
ClientAliveIntervalspecifies the interval in seconds that the server will wait before sending a null packet to the client to keep the connection alive.ClientAliveCountMaxspecifies the number of client alive messages which may be sent without receiving any messages back from the client.
- Restart the SSH service to apply the changes:
sudo systemctl restart sshd