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:
- 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. - 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:
or, if you wish this to apply to all hosts:Host remotehost HostName remotehost.com ServerAliveInterval 240
If it's not the default, make sure ~/.ssh/config is readable by all by doingHost * ServerAliveInterval 240$ chmod a+r ~/.ssh/config - 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:
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
Re-attach with:$ screen -ls
("session" is the process id or the session name taken from screen -ls)$ screen -r<session>
You can also deliberately detach with [Ctrl]-a and then re-attach later (from anywhere) with [Ctrl]-d.
Comments
Post a Comment