Keep SSH session alive

LinuxSshTerminal

Linux Problem Overview


I use ssh -p8520 username@remote_host to login remote server.

Issue:

It is always connected and works properly when I am in the work place. Unfortunately, terminal freezes in 10 - 15 minutes after I connected with the remote server from home.

There's no error/timeout report on the console but the cursor cannot move any more.

When enter w to check the login users, some zombies login users are there, and I have to kill them manually.

This is quite annoying. Can anyone help me?

Linux Solutions


Solution 1 - Linux

The ssh daemon (sshd), which runs server-side, closes the connection from the server-side if the client goes silent (i.e., does not send information). To prevent connection loss, instruct the ssh client to send a sign-of-life signal to the server once in a while.

The configuration for this is in the file $HOME/.ssh/config, create the file if it does not exist (the config file must not be world-readable, so run chmod 600 ~/.ssh/config after creating the file). To send the signal every e.g. four minutes (240 seconds) to the remote host, put the following in that configuration file:

Host remotehost
    HostName remotehost.com
    ServerAliveInterval 240

To enable sending a keep-alive signal for all hosts, place the following contents in the configuration file:

Host *
    ServerAliveInterval 240

Solution 2 - Linux

I wanted a one-time solution:

ssh -o ServerAliveInterval=60 [email protected]

Stored it in an alias:

alias sshprod='ssh -v -o ServerAliveInterval=60 [email protected]'

Now can connect like this:

me@MyMachine:~$ sshprod

Solution 3 - Linux

For those wondering, @edward-coast

If you want to set the keep alive for the server, add this to /etc/ssh/sshd_config:

ClientAliveInterval 60
ClientAliveCountMax 2

> ClientAliveInterval: Sets a timeout interval in seconds after which if no data has been received from the client, sshd(8) will send a message through the encrypted channel to request a response from the client.

> ClientAliveCountMax: Sets the number of client alive messages (see below) which may be sent without sshd(8) receiving any messages back from the client. If this threshold is reached while client alive messages are being sent, sshd will disconnect the client, terminating the session.

Solution 4 - Linux

putty settings

FYI Putty Users can set the options here

Solution 5 - Linux

We can keep our ssh connection alive by having following Global configurations

Add the following line to the /etc/ssh/ssh_config file:

ServerAliveInterval 60

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHaifeng ZhangView Question on Stackoverflow
Solution 1 - Linuxrockymonkey555View Answer on Stackoverflow
Solution 2 - LinuxRyanView Answer on Stackoverflow
Solution 3 - LinuxJeff DavenportView Answer on Stackoverflow
Solution 4 - LinuxRuben BenjaminView Answer on Stackoverflow
Solution 5 - Linuxminhas23View Answer on Stackoverflow