This is a compilation of information I found in different postings on the net.
tmux
can be invoked in command mode using tmux -CC
. The simplest way to get a remote tmux session into a window of iterm is to invoke it on the remote host
local> ssh rmhost
#...
rmhost> tmux -CC
This will open a new tmux session in a new iterm window.
To run tmux
as the remote command argument to ssh
, it need a pseudo-terminal attached. This means invoking ssh
with the -t
flag:
local> ssh rmhost -t tmux -CC
If you want to re-attach to a remote session, you have to know its name. The easisest way to is to create a session with a well known name and re-attach to that session later. For that, we invoke tmux
with the command new -A -s tmux-main
. The session name is set using the -s
flag, the -A
flag specifies that new
behaves like attach
if the session exists. The command is now
local> ssh rmhost -t tmux -CC new -A -s tmux-main
The final command we put together above is a much on the fingers if you use it a lot. The solution is either to set up a shell alias or a special Host
configuration in your ~./.ssh/config
file, as shown below in ssh--config
. If you do that, the command to invoke becomes
local> ssh tmhost
You might want to set up two shorthands, one for normal ssh accesses and one for ssh with tmux.
The socket of the ssh agent to access the unlocked keys changes with every ssh login. You loose access to your ssh keys when you re-attach to your tmux session. To avoid this, we can link the current authentication socket to a well-known name. We use ~/.ssh/ssh-auth-sock
. This link can be set and updated in ~/.ssh/rc
, see the ssh--rc
example below. To use this name, set the SSH_AUTH_SOCK
environment variable in your ~/.tmux.conf
file, see the tmux.conf
example below.
NB Your global sshd
configuration might prohibit the use of the ~/.ssh/rc
file. This is controlled by the administrator of the remote host.
NB The ~/.ssh/ssh-auth-sock
gets set if it doesn't point to a valid socket. This means it might not point to the authentication socket of the ssh session running your tmux session if you have an earlier, regular ssh session open. If that is the case, the socket connection will be lost the moment you log out from that session.
Thanks for this!
You might reconsider RemoteCommand in ssh config though - it will break scp and rsync. Take a look at https://unix.stackexchange.com/questions/628607/how-to-bypass-remotecommand-option-in-ssh-config
I now have this in my fish config:
I wrote about my own solution for SSH agent / X11 forwarding here: https://cyounkins.medium.com/fixing-broken-ssh-x11-forwarding-with-tmux-and-fish-32500642f6f2