34

when I ssh to a server over the internet in a gnome terminal tab, if I lost internet connection, the terminal tab will be hang and not accept any input. Why is it hang?

Is there some way to activate the terminal tab, i.e. make it continue running the local shell process?

Is closing the terminal tab the only way?

0

1 Answer 1

39

SSH connection goes down automatically after a specified period of time set by ClientAliveInterval and ClientAliveCountMax parameters and their client-side equivalents. If these timeouts are quite high you will experience a frozen shell. However, if you use OpenSSH you don't have to wait for a timeout and can force closing a connection using escape characters:

ESCAPE CHARACTERS
When a pseudo-terminal has been requested, ssh supports a number
of functions through the use of an escape character.  A single
tilde character can be sent as ~~ or by following the tilde by a
character other than those described below. The escape character
must always follow a newline to be interpreted as special. The
escape character can be changed in configuration files using the
EscapeChar configuration directive or on the command line by the
-e option.
The supported escapes (assuming the default ‘~’) are:

~.
    Disconnect.
(...)

When connection freezes press ~ (that means Shift+` keys together), release it and press .. Alternatively, if you work with an unstable connection or need to be connected to the remote server all the time you can use autossh to automatically renew lost connection, it's very convenient.

EDIT:

However, if both ClientAliveInterval and ServerAliveInterval are set to 0 explicitly or are not set explicitly and are then set to 0 by default according to sshd_config and ssh_config manpages, timeout settings are set in the following files (from http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/usingkeepalive.html):

  # cat /proc/sys/net/ipv4/tcp_keepalive_time
  7200

  # cat /proc/sys/net/ipv4/tcp_keepalive_intvl
  75

  # cat /proc/sys/net/ipv4/tcp_keepalive_probes
  9

  The first two parameters are expressed in seconds, and the last is
  the pure number. This means that the keepalive routines wait for
  two hours (7200 secs) before sending the first keepalive probe,
  and then resend it every 75 seconds. If no ACK response is
  received for nine consecutive times, the connection is marked as
  broken.

You can modify these 3 files just by using echo and see yourself that a frozen SSH session is disconnected according to these values.

10
  • 18
    I'd like to add that ssh only recognizes its escape character initially, or after an Enter. When an ssh connection gets stuck, Enter is often but not always the last character that you pressed previously. So it's probably better to get used to Enter ~ . as the sequence that terminates the connection.
    – egmont
    Apr 30, 2015 at 14:22
  • Thanks. I am still not clear how /proc/sys/net/ipv4/tcp_keepalive* files and ClientAliveInterval and ServerAliveInterval work together? Are they all for the same settings i.e. to keep ssh connection alive? Are the former files not just for ssh connections but also other TCP connections?
    – Tim
    Oct 29, 2015 at 15:09
  • Another question: why "If these timeouts are quite high you will experience a frozen shell." What do you mean by "time outs are quite high"? Isn't the shell always frozen, if lack of activity for the specified period of time? or do you mean under some cases of lack of activity, the shell isn't frozen but exit automatically?
    – Tim
    Oct 29, 2015 at 15:36
  • @Tim: 1. /proc/sys/net/ipv4/tcp_keepalive* are not only for ssh(d) but as it says in the document I linked to: Remember that keepalive support, even if configured in the kernel, is not the default behavior in Linux. Programs must request keepalive control for their sockets using the setsockopt interface. There are relatively few programs implementing keepalive, but you can easily add keepalive support for most of them following the instructions explained later in this document. Oct 29, 2015 at 16:10
  • 1
    I'd like to add that Enter ~ ? prints help about the supported escape sequences
    – Ves
    May 23, 2019 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.