In windows
PS C:\Users\rakes> Get-ChildItem -Path $env:USERPROFILE -Recurse -Directory -Filter ".ssh"
Host *
ServerAliveInterval 60
ServerAliveCountMax 5
C:\Users\rajes\.ssh
inside config.txt
Host *
ServerAliveInterval 60
ServerAliveCountMax 5
SSH (Secure Shell) is an essential tool for remote server management, but sometimes, idle SSH connections can be disconnected automatically. To prevent this, you can configure SSH keep-alive settings, ensuring that your SSH sessions remain active even during periods of inactivity.
In this blog post, I'll walk you through the steps to configure SSH keep-alive settings in Ubuntu/Linux, specifically focusing on editing the SSH configuration file for the root user. This process can also be applied to other users if needed.
Why Use SSH Keep-Alive Settings?
By default, SSH sessions can disconnect after a certain period of inactivity due to network issues, server settings, or firewall configurations. To avoid these unexpected disconnections, SSH keep-alive settings send periodic signals to the server, keeping the connection alive.
Two Important SSH Parameters:
ServerAliveInterval
: Specifies the interval (in seconds) at which your SSH client sends a signal to the server to keep the connection alive.
ServerAliveCountMax
: Sets the maximum number of times the server can fail to respond before the SSH client terminates the connection.
How to Configure SSH Keep-Alive Settings
Step 1: Access the SSH Configuration File
For root user configuration, you'll be editing the /root/.ssh/config file. This file might not exist yet, but we can create it if needed.
Using find Command
You can use the find command to locate the .ssh directory:
find ~ -type d -name ".ssh"
This command searches for a directory named .ssh within your home directory (~).
Using ls and grep Command
You can combine ls with grep to check if the .ssh directory exists in your home directory:
ls -a ~ | grep .ssh
Open the terminal and use the vi editor to access the file:
sudo vi /root/.ssh/config
If you’re configuring this for a non-root user, the file would be located at ~/.ssh/config.
Step 2: Edit the Configuration File
Once you've opened the file with vi, follow these steps:
Press i to enter Insert Mode in vi.
Add the following lines to set the SSH keep-alive interval and maximum count:
Host *
ServerAliveInterval 60
ServerAliveCountMax 5
ServerAliveInterval 60: This will send a keep-alive signal to the server every 60 seconds.
ServerAliveCountMax 5: The connection will close if there is no response after 5 attempts (i.e., 5 intervals).
================or=================
Host *
ServerAliveInterval 3600
ServerAliveCountMax 5
Step 3: Save and Exit the Configuration File
To save your changes and exit vi:
Press Esc to exit Insert Mode.
Type :wq and press Enter to save and quit.
Step 4: Test Your SSH Connection
To verify your changes, start a new SSH session:
ssh root@your-server-ip
Keep this session idle and monitor if it disconnects after a certain period. With the new configuration, your SSH connection should remain active as long as your ServerAliveInterval and ServerAliveCountMax settings are met.
Applying the Configuration for All Users
If you want to apply these settings for all users on the system, you need to edit the global SSH client configuration file:
Open the global configuration file:
sudo vi /etc/ssh/ssh_config
Add the same lines:
Host *
ServerAliveInterval 60
ServerAliveCountMax 5
Save and exit (:wq).
Why These Settings Are Important
Prevent Disconnections: Prevents the SSH session from being terminated due to inactivity, making it useful for long-running processes or monitoring tasks.
Improved User Experience: Helps maintain a stable connection when working on remote servers, reducing frustration from unexpected disconnections.
Tips for Using SSH Keep-Alive Settings
Adjust the Interval and Count: The ServerAliveInterval and ServerAliveCountMax values can be customized based on your requirements. For example, you might set a lower interval (e.g., 30 seconds) for a more active connection or a higher count (e.g., 10) if you have occasional network issues.
Use Carefully on Public Networks: While keep-alive settings help maintain connections, they may slightly increase network traffic. Be cautious when using them on metered or unstable public networks.
Top comments (0)