Stop ssh Terminal Sessions Hanging

On some Linuxen, when you ssh to another server, after a timeout it drops the connection.  That terminal session is now frozen.  There are at least three solutions to this:
  1. When your terminal freezes, you can get out of it by typing, one by one: [Enter] then [ ~ ] then [ . ].
    This will bring you back to the terminal session where you executed the initial ssh.
  2. Create or open ~/.ssh/config.  For the host(s) your dealing with, in their sections, add a line ServerAliveInterval 240 indented by two spaces (or inline with the othe entries) something like:
    Host remotehost
      HostName remotehost.com
      ServerAliveInterval 240
    or, if you wish this to apply to all hosts:
    Host *
      ServerAliveInterval 240
    If it's not the default, make sure ~/.ssh/config is readable by all by doing
    $ chmod a+r ~/.ssh/config
  3. Instead of trying to keep the connection alive you can use terminal multiplexors, like screen (or tmux) that keep the session alive in the background even if your terminal gets disconnected.
    Essentially when you login in with ssh you immediately run screen which will create and attach a new session:
    $ screen
    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
    Re-attach with:
    $ screen -r <session>
    ("session" is the process id or the session name taken from screen -ls)

    You can also deliberately detach with [Ctrl]-a and then re-attach later (from anywhere) with [Ctrl]-d.

Comments

Popular posts from this blog

TEX on Linux

Freeze Changing HTML Element in Chrome Developer Tools

Change Unix terminal Title