Ask HN: moving beyond screen to persist SSH sessions?
Is there any way to accomplish this in any OS? Every time I have posted this problem elsewhere, everyone shouts "screen!" but I think the HN audience understands that while screen is cool, it doesn't even come close to resolving this issue; so I'm hoping for a better answer here.
To give an example of my problem:
Let's I have 5 SSH windows open doing various things (perhaps I am running top, emacs, a window that i'm using to type svn commands, etc). I also have port-forwarding set up so that I can use Firefox securely through a SOCKS proxy, and access websites that are only available from behind the SSH machine.
If my wireless network switches or even drops for 1 second, I now have 5 dead SSH sessions. I have to somehow reinitialize them, and when I do it's a mess to get them back in the correct state. I can solve the latter with screen. How can I solve the former automatically, without wasting time re-establishing those connections?
35 comments
[ 5.4 ms ] story [ 88.2 ms ] threadWhat you want would be cool but it can't be done with TCP/IP.
it can't be done with TCP/IP. It surely can, but would have required someone to figure it out. Surely a combination of an SSH client which reconnects (and therefore temporarily remembers your password/key) with screen might do it. But I'm hoping someone has been in the same situation and has figured out a somewhat elegant solution.
You could try using ssh-agent to reduce the pain of your connectivity issues, but screen really is the answer.
The best you can hope for is either to set some stuff up as described so SSH automatically relogins to a screen session, or find a better network connection. Fundamentally, you can't patch around a bad network connection.
As for port forwarding, you will still be disconnected from everything on connectivity blips when ssh has to reconnect if you use ssh port forwarding. Try a VPN setup instead. You might have to fiddle with the host to keep it from sending RST's/FIN's as soon as you drop (though it shouldn't unless a packet for you comes in during the blip, I think). When you come back, if you come back soon enough, the connections should be restored without having been disconnected. (AS LONG as you have the same IP address on the VPN, so use a static IP setup)
Also, if you run your SSH -through- the VPN connection and recover quickly enough SSH shouldn't disconnect at all either.
More Edit: For long connectivity lapses (changing locations) all your TCP connections -will- drop. This is not something you can work around.
Regarding the VPN, could this connection be automatically established upon joining a wireless network? (i think you see where i'm going with this question)
This is pretty much what I do already, except that I don't switch connections as frequently as you. But when I go to a new place, it's just connect with ssh and reattach with screen.
Secondly, you should be able to set scripts to trigger when a network interface comes up in most linux distros, but how to do it varies, for example /etc/network/ifup.d/ in ubuntu.
This script sets up a persistent, "secure" SSH key agent on the remote machine that can be reused for as many disconnects and reconnects as I feel like making until the server (if ever) needs a reboot. Then it starts or reconnects to a named screen session that I can continue using as if nothing ever happened.
Combine this method with a few different aliases as needed, and a local session key agent to only input your key's passphrase once, and then after a disconnect, press up-enter in each terminal as necessary, and boom, you're back in business.
--
On a related note, a drop of network connectivity should not be killing your SSH connection, especially for as short a time span as one second. SSH is designed to stay connected through at least 30 seconds of network loss; after the connection is regained, it should automatically reconnect to the SSH server and continue the session where it left off, even maintaining a buffer of unsent input during the connection loss.
I've personally suspended and then unsuspended my laptop within a one-or-two minute period without losing my SSH session, althought that's likely stretching things.
What's certain is that a 1-2 second lapse in your connection should most certainly not be dropping your SSH connection; you should definitely be investigating that to figure out what the problem is.
1. Get an Android.
2. Hack it and install the debian subsystem.
3. see: http://brad.livejournal.com/2400054.html
So now instead of running an ssh to a remote server you are running an ssh to your android which then hosts all of the outward ssh sessions. Because of the androids 2g/3g you can't lose your connection (well not easily).
Laptop: network changes as you move from one wifi hotspot to another.
I'm advocating moving his ssh connections to route through the android rather than directly from the laptop to the server.
One question is when does sshd know when to ditch interrupted sessions.
My question raised the point that the server would not be able to tell the difference, after an interruption, between session state that the client will hope to resume and session state that should be discarded. That state would accumulate until some routine cleared it out.
From the ssh_config manpage:
"TCPKeepAlive
Specifies whether the system should send TCP keepalive messages to the other side. If they are sent, death of the connection or crash of one of the machines will be properly noticed. However, this means that connections will die if the route is down temporarily, and some people find it annoying.
The default is ``yes'' (to send TCP keepalive messages), and the client will notice if the network goes down or the remote host dies. This is important in scripts, and many users want it too.
To disable TCP keepalive messages, the value should be set to ``no''."
[1] http://www.openhip.org/
[2] http://en.wikipedia.org/wiki/Host_Identity_Protocol
[3] http://infrahip.hiit.fi/
A thing you can do to get more stable ssh connections on your side is to run a nat service on the machine. For example, running connections on a OS X machine that has parallels installed (virtualization software) and that runs NAT is far more stable than a machine without.
http://en.wikipedia.org/wiki/Mobile_IP
But I don't know if that has ever really been used outside of a research context.
http://www.commandlinefu.com/commands/view/357/ssh-tunnel-wi...
I haven't read the comments here yet but hopefully someone will have at least a partial solution to save my sanity :)