Hostxpeed
Login Get Started →
Getting Started

How to Keep SSH Session Alive

4 min read
26 views
Jun 10, 2026

Prerequisites

Before configuring SSH keep-alive, make sure you have:

  • SSH access to your VPS
  • Root or sudo privileges (for server-side config)

💡 By default, SSH sessions may disconnect after a few minutes of inactivity. These settings keep your session alive.

Method 1: Client-Side Configuration (Local Machine)

Edit your local SSH config file:

nano ~/.ssh/config

Add these lines:

Host *
    ServerAliveInterval 60
    ServerAliveCountMax 10

Or for specific host only:

Host YOUR_SERVER_IP
    ServerAliveInterval 60
    ServerAliveCountMax 10

What these do:

  • ServerAliveInterval 60 - Send keep-alive packet every 60 seconds
  • ServerAliveCountMax 10 - Allow 10 failures before disconnecting

Method 2: Command Line Option

Connect with keep-alive enabled:

ssh -o ServerAliveInterval=60 hxroot@YOUR_SERVER_IP -p 22

Method 3: Server-Side Configuration (All Users)

Edit SSH server config:

sudo nano /etc/ssh/sshd_config

Add or modify:

ClientAliveInterval 60
ClientAliveCountMax 10

Restart SSH service:

sudo systemctl restart sshd

Method 4: Using tmux or screen (Session Persistence)

These tools keep your session running even after disconnect:

tmux new -s mysession

Run your work, then detach:

Ctrl+B, then D

Later reattach:

tmux attach -t mysession

With screen:

screen -S mysession
Ctrl+A, then D
screen -r mysession

Method 5: Using autossh (Reconnects Automatically)

Install autossh:

apt install autossh -y

Use autossh:

autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" hxroot@YOUR_SERVER_IP

Method 6: Configure PuTTY (Windows)

  1. Open PuTTY
  2. Go to Connection
  3. Set Seconds between keepalives to 60
  4. Save the session

Method 7: Add to Bash Profile (Auto Apply)

Add to ~/.bashrc on your local machine:

alias ssh-keep='ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=10'

Then use:

source ~/.bashrc
ssh-keep hxroot@YOUR_SERVER_IP

Check Current TCP Keep-Alive Settings

On your server:

sudo sysctl net.ipv4.tcp_keepalive_time
sudo sysctl net.ipv4.tcp_keepalive_intvl
sudo sysctl net.ipv4.tcp_keepalive_probes

Why SSH Sessions Disconnect

  • Network firewalls dropping idle connections
  • ISP timeout policies
  • NAT table cleanup
  • Router session timeouts
  • Corporate proxy timeouts

Test Your Keep-Alive Settings

Leave SSH session idle for 5-10 minutes. If still connected, it works.

Monitor connection:

watch -n 1 "echo 'Session is alive at $(date)'"

✅ Your SSH session will now stay alive during periods of inactivity.

Was this article helpful?