Skip to content

Instantly share code, notes, and snippets.

@jimblue
Forked from lukasnellen/00-ssh-tmux-iterm.md
Created March 20, 2021 23:43
Show Gist options
  • Select an option

  • Save jimblue/202d9434bc0bc55d34da626ba674452e to your computer and use it in GitHub Desktop.

Select an option

Save jimblue/202d9434bc0bc55d34da626ba674452e to your computer and use it in GitHub Desktop.
Summary of my remote tmux and ssh configuration to benefit from the iterm2-tmux integration, with ssh authentication socket refreshing

Configuration to use remote tmux over ssh in iterm

This is a compilation of information I found in different postings on the net.

All manual invocation

Basic remote tmux session

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 tmhost
   #...
   remote> tmux -CC

This will open a new tmux session in a new iterm window.

Invoke tmux as the command to ssh

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 tmhost -t tmux -CC

Naming the remote session to be able to re-attach

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 tmhost -t tmux -CC new -A -s tmux-main

Setting up a host configuration

Automatic ssh-agent socket handling and renewal

# This is a fragment of ~/.ssh/config (local)
Host tmhost
HostName remote.host.some.where
ForwardAgent yes
RemoteCommand tmux -CC new -A -s tmux-main
RequestTTY yes
#! /bin/bash
# This is ~/.ssh/rc (remote)
if [ ! -S $HOME/.ssh/ssh_auth_sock -a -S "$SSH_AUTH_SOCK" ]; then
ln -sf $SSH_AUTH_SOCK ~/.ssh/ssh_auth_sock
fi
# This is ~/.tmux.conf (remote)
set -g update-environment "DISPLAY SSH_ASKPASS SSH_AGENT_PID SSH_CONNECTION WINDOWID XAUTHORITY"
set-environment -g 'SSH_AUTH_SOCK' $HOME/.ssh/ssh_auth_sock
set -g set-titles on
# not all distros have a tmux terminal type
#set -g default-terminal tmux
set -g default-terminal xterm-color
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment