424

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?

Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
  • 1
    I have had this too, I started to use `screen`. Maybe this is some kind of a timeout issue. – martin Aug 01 '14 at 16:12
  • 1
    @martin `screen` helps to keep the program running. Unfortunately sometimes i have to work on the remote server:( – Haifeng Zhang Aug 01 '14 at 16:14
  • If you start it without parameters, this gives you a shell. – martin Aug 01 '14 at 16:15
  • Are your saying `screen` only? It can solve the lost connection issue? No `screen -S screenName`? I always use screen with `-S` and `-r` – Haifeng Zhang Aug 01 '14 at 16:16
  • 1
    No, this does not solve the issue, you only avoid loosing your work in the terminal. If you don't do anything, it will still freeze, you just can pick up where you started. I meant that you can create a terminal which you can always resume by using `screen`. You seem to already know that ;) – martin Aug 01 '14 at 16:20
  • @martin pls check the answer. it works for me, hope it can help you as well – Haifeng Zhang Aug 03 '14 at 13:57

5 Answers5

743

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
t0r0X
  • 4,212
  • 1
  • 38
  • 34
rockymonkey555
  • 7,520
  • 1
  • 13
  • 12
  • The config must only be user-writable. readability is not a concern. Using 600 as your permission level is fine since that is only user-writable. – jbruni Sep 04 '18 at 17:31
  • 2
    just in case it's not obvious this config file is on your PC/linux box – zzapper Oct 23 '18 at 15:00
  • 33
    I always get confused about the config name: the client uses `ServerAliveInterval ` while the server uses `ClientAliveInterval `. What a mess. – youkaichao Feb 11 '19 at 13:42
  • 3
    @youkaichao There is often this confusion. As an API designer, there are reasons that I might put it in both ways. Ultimately, what's most critical, is good documentation of the chosen standard. – Cameron Tacklind Oct 17 '19 at 01:33
  • 2
    `c:/users/youruser/.ssh/config` for Windows – Michael Zelensky May 22 '20 at 11:36
  • 5
    @youkaichao It's like if you went to Carl's house and you saw "Mike's address:
    " on a slip of paper on the fridge. It's not confused that Mike's name is written in Carl's house; Carl is the only name that _doesn't_ need to appear at Carl's house.
    – Luke Griffiths Mar 05 '21 at 19:49
  • I need this whenever using an AT&T cellular hotpot... they like to kill TCP connections every 5 minutes for fun. – Ray Foss May 23 '21 at 20:29
  • why "must" we use chmod 600? what is so bad with default 644 permissions? – Bilal Siddiqui Nov 25 '21 at 20:51
  • 1
    @BilalSiddiqui 644 allow group and public to read your config file, possibly leaking info. The manpage for ssh_config specify "read/write for the user, and not writable by others", which imply 644 is fine. But generally its safer to use 600 since 644 can trigger error in some circumstances. – bangbambang Jul 18 '23 at 01:56
298

I wanted a one-time solution:

ssh -o ServerAliveInterval=60 myname@myhost.com

Stored it in an alias:

alias sshprod='ssh -v -o ServerAliveInterval=60 myname@myhost.com'

Now can connect like this:

me@MyMachine:~$ sshprod
Ryan
  • 5,959
  • 2
  • 25
  • 24
  • 17
    That's cool, but not exactly a "one-time solution". If you're going to connect to the server more than once, why not do `Host *` & `ServerAliveInterval 240` (or specify the hostname, if you only want it for `myname@myhost.com` as in rockymonkey555's answer? Setting an alias doesn't seem easier in any way. – Lambart Jul 16 '15 at 01:00
  • 10
    Or, just create an alias in your ~/.bashrc: `alias ssh='ssh -o ServerAliveInterval=60'` – Jabba Mar 17 '16 at 05:34
  • 29
    He means "one time" as in "it only works one time", not "I only have to write it one time". – Jonathan Hartley Nov 30 '17 at 22:12
  • 3
    Yes, if you read "one time" as "single use", this does the job perfectly. – philraj Aug 21 '19 at 19:45
  • 7
    If you want a "one-liner" that sets `ServerAliveInterval` to `60`, great. However using a bash alias when you could be using `.ssh/config` is just silly. – Cameron Tacklind Oct 17 '19 at 01:35
  • This works great when connecting to dev / production servers that have non-standard ports, to prevent having to type the port out every time -- Just make a separated alias per server/port EG `alias sshweb='ssh -o ServerAliveInterval=60 zak@example.com -p 9999'` OR `alias sshdev='ssh -o ServerAliveInterval=60 zak@dev.example.com -p 7777'` – Zak Jan 04 '21 at 23:38
  • @CameronTacklind it's really not. Being an admin of a mixed estate means you have new IPs getting introduced every x minutes and old ones removed at a similar rate. Updating config each time - THAT is just silly. Especially when the domain names are nto guaranteed and IPs have no pattern suggesting which connections might be flaky (and/or likely to recover) and which ones will be reliable. I like to choose on-the-go when I want to try the conn with KA enabled and when not. So an alias is a perfectly useful option. – netikras Mar 13 '21 at 06:29
  • 1
    @netikras I don't see how aliases help with IP addresses changing. If you want to use an alias, go for it. I'd personally rather just use the tool's built-in configuration options instead of re-inventing my own. – Cameron Tacklind Mar 13 '21 at 22:06
82

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.

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
Jeff Davenport
  • 2,624
  • 2
  • 13
  • 19
  • It seems that there is no such "parameter" as ClientAliveInterval, only ServerAliveInterval, you can check in the manual page "man ssh_config" – dtj Jul 28 '16 at 19:16
  • 11
    That is the wrong one, look in "man sshd_config" for the server portion running the ssh daemon, not the client config. – Jeff Davenport Jul 29 '16 at 22:35
  • Should I use `ClientAliveInterval` to let the server check for client alive, or should i let the client "ping" the server with `ServerAliveInterval` repeatedly? Both seems not to make sense – droid192 Jun 02 '17 at 14:08
  • 3
    Only set the `ClientAliveInterval` on the server if you want the server to disconnect on dead connections that do not respond, and you can customize how often and when that happens. – Jeff Davenport Jul 25 '17 at 20:22
  • 1
    In my case, server side set `ClientAliveInterval 15` and `ClientAliveCountMax 2` , and client side set `-o reconnect` , works well when transferring large file through sshfs without stuck. – allenyllee Jun 23 '21 at 06:29
36

putty settings

FYI Putty Users can set the options here

Community
  • 1
  • 1
Ruben Benjamin
  • 707
  • 9
  • 9
17

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
Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
minhas23
  • 9,291
  • 3
  • 58
  • 40