Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

When I connect via ssh terminal to certain servers, it timeouts and "freezes" the terminal (doesn't accept, doesn't disconnect, can't Ctrl-C to kill the ssh process or anything).

This is in Ubuntu's Gnome-terminal, though it seems to be pausing the terminal input/output, and doesn't affect the operation of the Gnome-terminal software itself. so less a bug with gnome-terminal than an annoying inconsistency with ssh.

So is there a way to prevent regain the terminal from ssh connections that have timed out?

share|improve this question

4 Answers 4

up vote 115 down vote accepted

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.

share|improve this answer
    
Thanks, that sounds pretty much like what I'm looking for. –  Kzqai Jan 21 '10 at 17:40
1  
Thanks sblair for helping me with the wording, it's much appreciated. I changed "should not" back to "must not" because ssh checks the permissions of the file and if it is world or group readable it fails. –  Ludwig Weinzierl Oct 1 '11 at 20:26
    
Great answer thank you! –  Travis Bear Apr 19 '13 at 20:26
    
This is not what the OP asked. He's not getting kicked off due to inactivity. He's attempting to connect and his terminal is freezing. –  Cerin Jul 2 at 16:43
    
Where is this ~/.ssh/config? –  User Sep 26 at 21:48

Press Enter, ~, . one after the other to disconnect from a frozen session.

The section "ESCAPE CHARACTERS" in the ssh man page explains the underlying details.

share|improve this answer
5  
This answer appears to me to more accurately answer the question, and in any case, was the answer I was looking for. –  CoatedMoose Dec 2 '12 at 5:06
    
Note that you need to uncomment the line EscapeChar ~ in /etc/ssh/ssh_config (or ~/.ssh/ssh_config if you prefer). –  aditya menon Aug 3 '13 at 6:19
    
@adityamenon No, EscapeChar ~ is already the built-in default. –  Peter Eisentraut Aug 4 '13 at 20:48
    
Hmm, it did not work for me in Ubuntu 12.04 till I found that line and uncommented it... –  aditya menon Aug 4 '13 at 20:55

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.

share|improve this answer
1  
+1 for the screen as login shell hint! –  Georgi Kirilov Sep 5 '13 at 11:00

Try appending -o ServerAliveInterval=30 to your connection string (30 means 30 seconds and can of course be adjusted)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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