There are several potential reasons for these disconnects, including idle timeouts, network instability, or server settings. This guide will cover some common solutions to prevent frequent SSH and SCP disconnects.
1. Adjust Client-Side Keep-Alive Settings #
On the client side (like your laptop), you can configure your SSH client to send periodic keepalive packets to prevent disconnections.
Step 1: Modify the Client-Side SSH Configuration
- Using a text editor like vim or nano, edit your local SSH configuration file (
ssh_config
). - This file is typically located in your home directory. If the file doesn’t exist, you can create it.
vi ~/.ssh/config

- Add the following lines to the file. You may adjust the values 60 and 3 as you desire.
Host *
ServerAliveInterval 60
ServerAliveCountMax 3

- In the above
- ServerAliveInterval: Sends a keepalive packet every 60 seconds.
- ServerAliveCountMax: Limits the number of keepalive messages sent without receiving a response from the server. After 3 attempts, the connection will be closed.
Step 2: Save the file
- Once the configuration is updated, save and close the file.
2. Adjust Server-Side Timeout Settings #
Another way could be to adjust the server-side settings. Consider this, only if the client side settings above do not work.
On the On the server, SSH connections can be configured to stay alive even during periods of inactivity. You
Step 1: Modify the Client-Side SSH Configuration
- Using a text editor like vim or nano, edit your local SSH configuration file (
ssh_config
). - This file is typically located in your home directory. If the file doesn’t exist, you can create it.
sudo vi /etc/ssh/sshd_config

- Look for these options or add them if they don’t exist:
ClientAliveInterval 60
ClientAliveCountMax 3

- In the above
- ClientAliveInterval: This sets the number of seconds that the server will wait before sending a null packet to the client to keep the connection alive. Setting this to 60 seconds will send a keepalive packet every minute.
- ClientAliveCountMax: The maximum number of client keepalive messages the server sends without receiving any response before disconnecting the client. Setting this to 3 means that if the server doesn’t receive any response after 3 minutes, it will disconnect.
Step 2: Save the file and Restart SSH Service
- Once the configuration is updated, save and close the file.
- Also restart SSH service using the command below
sudo systemctl restart sshd
3. Increase the SCP Timeout Limit #
- This is implemented in the scp command.
- If SCP connections are timing out frequently, especially during long file transfers, you can adjust the timeout limit by adding the
-o
flag to your SCP command. - This command below forces the client to send keepalive messages every 60 seconds, preventing the SCP connection from timing out.
scp -o ServerAliveInterval=60 source_file user@remote_host:/path/to/destination
If you use a custom port, then you will have the command syntax below
scp -P port -o ServerAliveInterval=60 source_file user@remote_host:/path/to/destination
