ETOOBUSY 🚀 minimal blogging for the impatient
SSH into tmux
Sometimes an alias on the local machine can save precious tenths of seconds when connecting to a remote machine with tmux.
I sometimes connect to some host in the inside of a lab, passing through an intermediate jumphost.
+--------+ +----------+ +--------+ | laptop |-->| jumphost |-->| target | +--------+ +----------+ +--------+
Up to some time ago, it meant doing something like this:
foo@laptop$ ssh jumphost bar@jumphost$ ssh target galook@target$
This is a lot of typing. Additionally, sometimes the VPN would just go away, disconnecting all precious sessions, so it didn’t take long for me to adopt tmux to work around both problems. Hightly recommended as something to put in your #toolbox. So, the real interaction is actually more like this:
foo@laptop$ ssh jumphost bar@jumphost$ ssh target galook@target$ tmux attach -t mysession
I recently became aware of a solution provided by OpenSSH to the double jump problem - see ProxyJump and ProxyCommand for the details. The bottom line is that I can do this:
foo@laptop$ ssh target galook@target$ tmux attach -t mysession
i.e. connect to the target using a single command from the laptop.
We can compact more
It never occurred to me before that I can also call tmux directly,
because you can pass
ssh a command to execute. It’s not exactly
foo@laptop$ ssh target tmux attach -t mysession open terminal failed: not a terminal foo@laptop$ echo $? 1
The problem is that, in this case,
ssh is not requesting the
allocation of a remote terminal, because it thinks that it’s a
one-shot command that does not need one. It’s easy to ask for a terminal
though, by means of
foo@laptop$ ssh -t target tmux attach -t mysession
At this point, we can compact this into a shell alias:
alias target='ssh -t target tmux attach -t mysession' # first attempt - we can do better - read on!
mysession does not exist?
mysession does not already exist in target as a tmux session,
you’ll get an error:
foo@laptop$ ssh -t target tmux attach -t my-missing-session can't find session my-missing-session Connection to target closed.
This hint by Wesley Baugh hits the nail right in the head in this
case: just always ask for creation of a session with that name, but use
-A to reuse any existing one with that same name:
foo@laptop$ ssh -t target tmux new -A -s my-missing-session
So, going back to the original example, the right alias would be:
alias target='ssh -t target tmux new -A -s mysession'
And this is it for today, cheers!