I usually have at least one terminal window open with a secure shell connection to my server while I’m working. I’m just that kind of crazy person who likes to do some things on the command line. (Ok, most things, but that’s beside the point.) Still, when I’m working I have many tools open, and lately I have been dealing with a strange lockup when I don’t pay enough attention to the terminal.
Now I know there is a timeout that will disconnect the session when it’s idle too long. But this was something different. The session seems frozen. What has happened is the server has timed out, but the local terminal has not disconnected. The only thing to do seems to be to close the tab/window. But that is because your terminal is sending all of your keystrokes to the remote server (which is ignoring you.) If only there was a way to communicate with your local shell.
A way out
There is, type [return] then tilde-period…
That should immediately disconnect you and return you to your local shell. There are also a variety of other escape sequences, you can see them all with [return] ~?
A real solution
Ok, that’s great to know, but it does not completely solve the problem. What I really want to have happen is to keep the connection alive. Many visual terminal clients I’ve used have a background communication with the server just to keep the connection alive, and so does command line ssh. You just have to configure it. Open or create a file in the .ssh directory in your home directory called “config” and put something like this in there…
Of course if you’re sure it’s not in there and you want to be really lazy just copy and paste this into your terminal window. That will append the command to your config file or create it if it does not exist.
echo 'Host *
ServerAliveInterval 120' >> ~/.ssh/config
Hope that helps!