I want my SSH login session to remain connected after the computer goes to sleep and wakes up. From what I understand TCP can survive intermittent network problems, so can I change settings so that it can survive for a few hours or days? If not, is there anything similar to SSH that can do that?

I understand that I can set SSH up to automatically reconnect when network becomes available again. But this is not ideal because I want my login session to be in exactly the same place where it was before the laptop went to sleep (even if it's half-way through typing something in the shell command line).

I understand there are other workarounds, but I was hoping there's a way to avoid losing the shell session in the first place.

  • You could probably set up your server not to drop TCP connections for the period of several days (see knowledgebase.progress.com/articles/Article/000044970), but I imagine this would make the server very vulnerable to an attack in which the adversary would keep on opening new TCP connections until the server crashes / refuses to open new connection. – Witiko Oct 20 '16 at 20:41
  • 2
    Maybe mosh mosh.org Also think about running your session in a terminal emulator like screen or tmux nowadays, independent from ssh resumable. – rudimeier Oct 20 '16 at 21:18
  • At the time of this writing the question you link to has five answers, which completely agree to each other. So you're asking the same question and you hope for a different answer? – Satō Katsura Oct 21 '16 at 4:27
  • @SatoKatsura I thought my question is slightly different (prevent disconnection in the first place), but you're probably right. – max Oct 21 '16 at 6:31
  • 1
    For my part, I have some logic in my bashrc to attach to any existing sessions upon login and create a new one otherwise. Combine that with password less ssh and it's pretty darn convenient. I really wouldn't worry about preserving easily recreated TCP sessions. – Bratchley Oct 22 '16 at 1:19

screen or tmux are definitely the way to go. The ssh connection then becomes only a means to an end and it no longer matters (within reason) hour often it drops when idle.

If you're bothered about having to remember to start screen when you log in to the remote server it wouldn't be that difficult to have an interactive session automatically join an existing screen or start a new one if there wasn't one already running.

|improve this answer|||||
  • That's fine for an interactive session, but not when you have some application that wants a TCP connection to stay open. – Gilles 'SO- stop being evil' Oct 20 '16 at 21:40
  • @Gilles the OP refers to login sessions. Non-interactive traffic is a different scenario (in some cases the application itself maintains state and can reconnect). In a general scenario I'd consider something like OpenVPN, but as I tend to run my ssh sessions with keepalive enabled on both sides this still wouldn't work for me. – roaima Oct 20 '16 at 22:11
  • I agree but they also shoot down tmux in the OP as well. This seems like a XY problem tbh – Bratchley Oct 22 '16 at 1:21

A TCP connection should be able to survive disconnection, but some overzealous firewalls may drop idle connections, and it won't survive your IP address changing, if that can happen.

For interactive sessions, I would run Mosh. It implements an SSH-like terminal connection over UDP, supports reconnecting from another IP address if necessary, and has some features to support flaky connections.

Regardless of if you decide to go with SSH or Mosh, use screen or tmux too to make your session independent of the connection.

|improve this answer|||||
  • 1
    A TCP connection can survive physical disconnections for a few minutes. No TCP stack in existence could possibly buffer TCP connections over night. That's would be insane, for many, many reasons. – Satō Katsura Oct 21 '16 at 4:33

Rocks (Reliable sOCKetS) is a wrapper for programs that make client TCP connections, which detects when the TCP connection fails and automatically reconnects the client, even if it's moved to a different network. It was described in the paper Reliable Network Connections and is available for download. It's unmaintained, but it uses fairly stable interfaces, so it should still work today. It uses library preloading (LD_PRELOAD) to shadow standard library functions, so it works only with dynamically linked executables. You just run it as

rocks ssh …

Rocks handles client mobility, not server mobility. If the server moves, you may want to complement it with a SOCKS proxy.

For an interactive session, a good alternative is Mosh. It's designed for unreliable connections and supports client mobility. Mosh loses data if the connection is unreliable, so it's only usable for interactive sessions.

An alternative to Mosh if you have a mostly-reliable, but occasionally-moving connection is autossh to automatically reconnect, and screen or tmux running on the server. The screen/tmux session remains on the server even if the client disconnect, and autossh reconnects automatically if the connection goes down.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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