Sample Header Ad - 728x90

Don't nest tmux in a remote ssh shell

5 votes
1 answer
3071 views
I've set up the .zshrc of my user account on all of the hosts to which I connect to automatically start tmux on login as long as it's not already running.
[ -z "$TMUX" ]; then
   tmux attach -d || tmux new
fi
This works well until I ssh (or mosh) into my own account on a remote host from within a tmux session on the local host. Since the $TMUX macro isn't passed from the local host to the remote, tmux launches on the remote host and I now have two nested tmux sessions. Is there a way to avoid this while keeping the auto-launching behaviour? Ideally I'd like the remote shell to know that it's being launched from within a tmux session on the host that is connecting and to not launch a second tmux instance. I've already tried checking $TERM in the remote shell but it is always xterm-256color regardless of whether it is running within a tmux session on the local machine.
Asked by Gene Goykhman (191 rep)
Jan 30, 2018, 05:23 PM
Last activity: Jan 30, 2018, 07:01 PM