Hacker News new | ask | show | jobs
by a-dub 1504 days ago
tmux sessions are incredibly useful for keeping multiple projects organized.

i just wish i could connect to remote tmux back ends and recover windows as organized locally after a network cut.

so the local tmux presents the ui and performs session management, and the remote tmux backends hold the shells to provide network cut resilience, but still rely on the local ui instance for all user interaction (so cut/paste buffers and mousing and such are preserved)

6 comments

Modern ssh supports forwarding of Unix domain sockets

On remote host:

    $ tmux -S /tmp/remote.sock
    <now detach from session: prefix d>
On local host:

    $ ssh -L /tmp/local.sock:/tmp/remote.sock user@remote
    $ tmux -S /tmp/local.sock attach

I Typed this on a phone and not validated but it looks good to me right now
Have you tried this before? As far as I can tell, tmux expects to pass the client's terminal fd over the unix socket. When I try it, I get something like this in strace (passing fd 7):

  > sendmsg(5, {..., msg_control=[{cmsg_len=20, cmsg_level=SOL_SOCKET, cmsg_type=SCM_RIGHTS, cmsg_data=[7]}], msg_controllen=24, msg_flags=0}, 0) = 16
And then the client relays an error:

  > open terminal failed: not a terminal
I hadn’t, i just tried it now and yeah it doesn’t work :thumbsdown:
6 months or a year ago I started using "mosh" to connect to my main machine from my laptops, and I run tmux on that machine. When I have a network cut, I just wait a few seconds and it's back in business, whether that's moving locations (I use a VPN so my IP is always the same, currently Nebula but has been wireguard and ZeroTier in the past), legit network problems, switching to my cell phone...
i've done this with mosh in past which gets you part of the way there.

problem is that it then doesn't work for local sessions. so you run it locally, which gives you local sessions and window management, but then all remote connections have to be via mosh -or- you mosh into the datacenter which is good for accessing infra, but leaves you having to run a separate instance for local work.

Agreed. In my case, all my "local" work is on my workstation via mosh, and my datacenter work is also done in that same tmux session. My laptop(s) are just thin clients for that work. That's great for the Chromebooks, but kind of a shame for in the case of a more capable laptop... For my workflow though, it works well.
Can you elaborate further on what gain I would have by having the tmux UI "rendered locally" on my system vs the SSH host? I'm obviously missing something based on your description.

For example: SSH into server, start tmux on that system. If I am disconnected for some reason, SSH back into server, tmux attach -t 0 (or whatever ID I have) and I've recovered my session. Cut/copy/paste, mouse use, etc., are all treated as they are through any SSH session.

a sibling comment suggests a reason:

> the local tmux then remembers history and allows looking up commands on the remote shell, that would be huge.

This is exactly my set up as I work on a number of remote systems daily.

I have an "outside" tmux session that can be run at either my desktop or laptop. When I start fresh, I open an outside tmux session and run a startup script. This preloads my outside session's copy buffers with named passwords from the `pass` password manager and creates windows to each of my remote sites. The shell for each of these windows is set to a script that does a simple loop of network check than attempt to ssh to the remote site. This ssh connection has an environment variable set that my remote bashrc will see. When this variable is set, my remote "inside" tmux connection is automatically recalled (or created if it doesn't exist.)

I have a script runs when I put the laptop to sleep that kills all the SSH connections cleanly, and when it wakes up again, every window reconnects to the remote machines and opens the remote tmux connections again. Everything is exactly where it was before and needs no intervention.

+1

If the local tmux then remembers history and allows looking up commands on the remote shell, that would be huge.

Currently all my local work is saved but ssh into another machine? All that work is lost forever unless I explicitly save it.

Isn't this just:

$ ssh <hostname>

$ tmux --list-sessions

and

$ tmux --attach-session -t <session #>

?