Is there a way to connect to an ssh session that was disconnected? We are having problems with our network connection to a remote site that we are working on separately; however, in the mean time we experience a large number of disconnects due to lost packets while connected to servers at the remote location. Many times the session stays active for a while, and sometimes it happens to be in the middle of some action (file editing, running some process, etc...) that I need to get back to rather than restart if possible.
|
|
UPDATE: For an actual answer see zero_r's answer below This isn't an answer, but a workaround. Use screen. When you first log in, run screen. You get another shell, run commands in that. If you're disconnected, the screen process keeps the terminal alive so that your shell and the processes it is running don't fall over. When you reconnect, run 'screen -r' to resume. There's a bunch more to configuring and using screen, but the above should workaround your problem. |
|||||||||
|
|
Try to set ClientAliveInterval (e.g. 60) and TCPKeepAlive (yes or no) to appropriate values on the serverside sshd.conf . This should keep your session alive even if the connection gets lost for minutes. |
|||||||||||||||||||||
|
|
As mentioned above, GNU Screen is the way to go. It will allow you to have a 'screen session' on the remote box that you can run multiple commands in, via multiple 'screen windows'. This will simply detach if your parent SSH connection dies, keeping all the subprocesses running within it alive and well. ' Basics are:
There is lots of cool stuff you can do with Screen. I've been using it for over 10 years, and still am finding out new features. It's my favourite Unix utility. |
|||||
|
|
I can't believe no one has mentioned MOSH; Mosh is a seperate protocol that can hook into the SSH login process, it keeps your session alive after days of disconnection, changing IP, high latency and so on. It is explained on the home page better than I can explain it so I have copied the description below. My experiences and advice are that I use it on my Android mobile, it's a life saver when travelling and SSH'ing. The same is true on my laptop when tethered with mobile on the train for example. I recommend compiling from source to get the latest version, the repo version for me inside Ubuntu has a few annoyances in it which are fixed in the newest version (at the time of writing).
Features from the website:
|
|||||||||||||
|
|
autossh watches your connection and if it goes down, it reconnects. It is more reliable than keepalives. If you connect to a screen session, you'll continue right from where you disconnected (see |
|||||||||||||||||
|
|
I'd install and start screen to fix your problem. Screen will let you reconnect to a previous screen session. Apart from that, screen also let's you do cool things like split your screen, view the console etc. You can find more info here and here. For starters, if you get disconnected, you can use
to view your sessions and
to reconnect to a disconnected one. |
|||
|
|
tmuxThis one is a classic. Use it whenever you run the risk of losing connection to a terminal.
Just like that, you're back in action. |
|||||
|
|
As others have pointed out, screen is generally the best solution for this and it adds a host of other useful features too. You can setup your profile on the remote machine to automatically start and/or reconnect to screen on login, which saves you forgetting to start screen the one time you need it because you suffer a connection drop. See http://tlug.dnho.net/?q=node/239 (or search Google for many other examples dones slightly different ways). |
|||||
|
|
a more modern alternative to screen, alas not available to some types of "virtualisation" (e.g. in cygwin you can have "screen", but not "tmux" due to the way it's designed), but wherever you have the option to install tmux, I'd strongly recommend to go for that one insted of screen. |
|||
|
|
|
Here's another non-screen solution. Putty tray allows you to do just that, it has a reconnect on disconnect option that isn't in regular or putty-ng https://puttytray.goeswhere.com/ it's a fork of putty with other options, go to connection settings and there are 2 options, one for 'attempt to reconnect on connection failure', and 'attempt to reconnect on startup'. |
|||||||||||||
|
|
While screen will keep your shell session open on the remote server if your ssh session drops, it won't do anything about the problem of ssh connections being dropped. As zero_r suggests, try tuning your ssh connection with keep alives and long timeouts. I suggest you track down the cause of the lost packets causing the problems and fix that instead of working around it. |
|||||||||
|