Way to avoid ssh connection timeout & freezing of GNOME Terminal

236

147

When I connect via ssh to certain servers, it timeouts and "freezes" the terminal (doesn't accept input, doesn't disconnect, can't Ctrl-C to kill the ssh process or anything).

This is in Ubuntu's gnome-terminal though it seems to be pausing the terminal input/output, and doesn't affect the operation of the GNOME Terminal software itself. So less a bug with gnome-terminal than an annoying inconsistency with ssh.

So, is there a way to prevent/regain the terminal from ssh connections that have timed out?

Kzqai

Posted 2010-01-20T23:33:01.830

Reputation: 2 698

Possible duplicate of How to reliably keep an SSH tunnel open?

– Murmel – 2018-01-15T22:33:59.710

Answers

259

sshd (the server) closes the connection if it doesn't hear anything from the client for a while. You can tell your client to send a sign-of-life signal to the server once in a while.

The configuration for this is in the file ~/.ssh/config. To send the signal every four minutes to remotehost, put the following in your ~/.ssh/config.

Host remotehost
  HostName remotehost.com
  ServerAliveInterval 240

This is what I have in my ~/.ssh/config.

To enable it for all hosts use:

Host *
  ServerAliveInterval 240

Also make sure to run chmod 600 ~/.ssh/config, because the config file must not be world-readable.

Ludwig Weinzierl

Posted 2010-01-20T23:33:01.830

Reputation: 7 695

1This is not what the OP asked. He's not getting kicked off due to inactivity. He's attempting to connect and his terminal is freezing. – Cerin – 2014-07-02T16:43:06.797

Where is this ~/.ssh/config? – User – 2014-09-26T21:48:58.487

1@User '~/' represents your home folder. '.ssh' is a folder within your home folder. – wkm – 2015-01-28T12:44:10.880

6this is useless if your connection is actually being lost... – so12311 – 2015-02-04T19:25:12.213

1Thanks sblair for helping me with the wording, it's much appreciated. I changed "should not" back to "must not" because ssh checks the permissions of the file and if it is world or group readable it fails. – Ludwig Weinzierl – 2011-10-01T20:26:16.097

250

Press Enter, ~, . one after the other to disconnect from a frozen session.

The section "ESCAPE CHARACTERS" in the ssh man page explains the underlying details.

Peter Eisentraut

Posted 2010-01-20T23:33:01.830

Reputation: 6 330

@CoatedMoose The question title asks for ways to avoid ssh connection timeout; this is a way to terminate the connection after it has frozen. It's a very useful trick, but not a more accurate answer to the question. – Mark – 2015-04-05T12:57:56.087

2@Mark the question asks "is there a way to prevent regain the terminal from ssh connections that have timed out?" Emphasis mine. – CoatedMoose – 2015-04-07T18:55:16.270

2Eventhough it was incorrect in the context of the question, this is exactly what I wanted. – Subin Sebastian – 2016-04-28T12:37:26.440

This is one of the most useful commands for anyone accessing remote machines from a laptop/wifi! – James McCormac – 2017-04-04T21:14:47.440

Is there a way to make this sequence into a short-cut, so I just press something like ctrl-alt-c or something? – gone – 2017-08-03T23:59:47.313

40This answer appears to me to more accurately answer the question, and in any case, was the answer I was looking for. – CoatedMoose – 2012-12-02T05:06:51.320

Note that you need to uncomment the line EscapeChar ~ in /etc/ssh/ssh_config (or ~/.ssh/ssh_config if you prefer). – Aditya M P – 2013-08-03T06:19:08.120

@adityamenon No, EscapeChar ~ is already the built-in default. – Peter Eisentraut – 2013-08-04T20:48:50.283

Hmm, it did not work for me in Ubuntu 12.04 till I found that line and uncommented it... – Aditya M P – 2013-08-04T20:55:29.067

39

Even tho this is not a direct answer to your question, it is highly related to the problem you have. Instead of trying to keep the connection alive (all connections eventually die) you can use terminal multiplexors, like screen and tmux that keep the session alive in the background even if your terminal gets disconnected.

Essentially when you login in to the SSH server you immediately run screen which will create and attach a new session:

$ screen

Then you go ahead and do your work with the shell as you would normally do. Now if the connection gets dropped, when you can get back online and reconnect to the server over SSH, you get a list the current sessions with:

$ screen -ls

To reattach to a session:

$ screen -r <session>

where <session> is the PID or a session name. You will be reconnected to your session and you can continue from where you left off!

You can even detach the session and reconnect from home to pick up from the exact point where you left off. To detach the session you use C-a followed by C-d (thats Control + A and then Control + D).

There is simple online tutorial as well.

Using screen and tmux on remote servers is considered a best practice and is highly recommended. Some people go as far as to have screen as their default login shell, so when they connect they immediately start a new screen session.

fotos

Posted 2010-01-20T23:33:01.830

Reputation: 511

2

another alternative is to use mosh: https://mosh.mit.edu/

– Zombies – 2015-12-25T19:13:02.243

This is especially useful if you are using a hotspot or wifi that occasionally drops out. – Randall – 2016-07-13T17:27:23.393

12

Try appending -o ServerAliveInterval=30 to your connection string (30 means 30 seconds and can of course be adjusted)

Fergie

Posted 2010-01-20T23:33:01.830

Reputation: 251

6

You can also set an idle timeout interval from SSH server side:

File: /etc/ssh/ssh_config

Content:

ClientAliveInterval XX
ClientAliveCountMax YY

This works in the exact same way as the client setting, but null packets are sent from from the server, rather than the client.

Extracted from:

http://www.sysadmit.com/2016/02/linux-y-vmware-ssh-evitar-desconexion.html

Ladinfremes

Posted 2010-01-20T23:33:01.830

Reputation: 61

2

For people who want to prevent the client from timing out in the first place.

You could try to set ConnectTimeout 0 in the configuration file. The value 0 means the connection will be kept alive Indefinitely unless closed.

your config (or ssh_config) file might look like this:

Host *
   ConnectTimeout 0

Yohannes Libanos

Posted 2010-01-20T23:33:01.830

Reputation: 29

0

In my case problem was in large MTU size. You can change MTU on router if you using NAT, but I change MTU on server:

sudo /sbin/ifconfig eth0 mtu 1036
sudo /etc/init.d/networking restart

On Windows you can also increase this key:

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters]
"TcpMaxDataRetransmissions"=dword:00000010

Vasin Yuriy

Posted 2010-01-20T23:33:01.830

Reputation: 101