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.
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.
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.
As mentioned above, GNU Screen is the way to go. It allows 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.
man screen
is your friend as usual, and the OS package should be calledscreen
if it is not installed by default.Basics are:
Start a screen session (on your remote host):
Disconnect from your screen session: CTRL-A, d
Reconnect to your screen session after logging back in again:
Open another screen 'window': CTRL-A, c
Cycle through your open screen windows: CTRL-A, space
There is lots of cool stuff you can do with screen. I've been using it for over 10 years, and I'm still 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:
Change IP. Stay connected: Mosh automatically roams as you move between Internet connections. Use Wi-Fi on the train, Ethernet in a hotel, and LTE on a beach: you'll stay logged in. Most network programs lose their connections after roaming, including SSH and Web apps like Gmail. Mosh is different.
Makes for sweet dreams: With Mosh, you can put your laptop to sleep and wake it up later, keeping your connection intact. If your Internet connection drops, Mosh will warn you — but the connection resumes when network service comes back.
Get rid of network lag: SSH waits for the server's reply before showing you your own typing. That can make for a lousy user interface. Mosh is different: it gives an instant response to typing, deleting, and line editing. It does this adaptively and works even in full-screen programs like emacs and vim. On a bad connection, outstanding predictions are underlined so you won't be misled.
No privileged code. No daemon: You don't need to be the superuser to install or run Mosh. The client and server are executables run by an ordinary user and last only for the life of the connection.
Same login method: Mosh doesn't listen on network ports or authenticate users. The mosh client logs in to the server via SSH, and users present the same credentials (e.g., password, public key) as before. Then Mosh runs the mosh-server remotely and connects to it over UDP.
Runs inside your terminal, but better: Mosh is a command-line program, like ssh. You can use it inside xterm, gnome-terminal, urxvt, Terminal.app, iTerm, emacs, screen, or tmux. But mosh was designed from scratch and supports just one character set: UTF-8. It fixes Unicode bugs in other terminals and in SSH.
Control-C works great: Unlike SSH, mosh's UDP-based protocol handles packet loss gracefully, and sets the frame rate based on network conditions. Mosh doesn't fill up network buffers, so Control-C
always works to halt a runaway process.
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
rscreen
that comes with autossh)tmux
This 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.
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.
Sometimes I forgot to run screen too and losing my unfinished work. In this case, though we can't reattach to a broken SSH session, reparenting a running program to a new terminal and resuming what you were doing is still possible thanks to
reptyr
.After accidentally disconnected from a SSH session, the first thing first is to run
screen
lest the connection gets broken again. Then in the new session, runps aux | grep {The process to be resumed}
to get the PID. With the PID, you could tryreptyr {PID}
orreptyr -T {PID}
(if there are subprocesses) to continue the work.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.
This answer is still relevant, screen exists, is supported, still does the job, etc, but there are other alternatives now: tmux offers extra features, and both it and screen are more commonly (in my experience) used wrapped in Byobu. All three are available in the standard repositories of major Linux distributions.
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).