Home » Questions » Computers [ Ask a new question ]

Way to avoid ssh connection timeout & freezing of GNOME Terminal

Way to avoid ssh connection timeout & freezing of GNOME Terminal

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).

Asked by: Guest | Views: 65
Total answers/comments: 5
Guest [Entry]

"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."
Guest [Entry]

"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."
Guest [Entry]

"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"
Guest [Entry]

"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"
Guest [Entry]

"Not sure if this works for gnome terminal too but you should be able to use the terminal again after you kill the ssh client process.
pgrep -a ssh

gives you the process ID which you can then kill
kill <pID>"