Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically start tmux on SSH session?

Tags:

ssh

tmux

People also ask

How do I start tmux automatically?

To configure your terminal to automatically start tmux as default, add the following lines to your ~/. bash_profile shell startup file, just above your aliases section. Save the file and close it. Then close and reopen the terminal to start using tmux by default, every time you open a terminal window.

Does tmux work over SSH?

With tmux you only need one SSH connection to the server. The great thing about tmux is it allows you to have multiple panes open at the same time, each with their own shell running, but using the same, single SSH connection.

How do I enable tmux in terminal?

Press Ctrl+B, and then Q to make tmux briefly flash the number of each pane. These numbers are used in prompts and messages from tmux . Press Ctrl+B, and then X to close the current pane.


Server-side configuration:

To automatically start tmux on your remote server when ordinarily logging in via SSH (and only SSH), edit the ~/.bashrc of your user or root (or both) on the remote server accordingly:

if [[ -n "$PS1" ]] && [[ -z "$TMUX" ]] && [[ -n "$SSH_CONNECTION" ]]; then
  tmux attach-session -t ssh_tmux || tmux new-session -s ssh_tmux
fi

This command creates a tmux session called ssh_tmux if none exists, or reattaches to a already existing session with that name. In case your connection dropped or when you forgot a session weeks ago, every SSH login automatically brings you back to the tmux-ssh session you left behind.

Connect from your client:

Nothing special, just ssh user@hostname.


Alright, I found a mostly satisfactory solution. In my local ~/.bashrc, I wrote a function:

function ssh () {/usr/bin/ssh -t "$@" "tmux attach || tmux new";}

which basically overwrites the ssh terminal function to call the built-in ssh program with the given arguments, followed by "tmux attach || tmux new".

(The $@ denotes all arguments provided on the command line, so ssh -p 123 user@hostname will be expanded to ssh -t -p 123 user@hostname "tmux attach || tmux new")

(The -t argument is equivalent to RequestTTY Force and is necessary for the tmux command.)


Don't do this on the server-side!

(It is potentially dangerous.) Instead, make use of ~/.ssh/config like so:

tmux 3.1 or newer¹ on the remote machine

Into your local ~/.ssh/config, put²:

Host myhost
  Hostname host
  User user
  RequestTTY yes 
  RemoteCommand tmux new -A -s foobar
  • Instead of RequestTTY yes you could call ssh with the -t switch; thank you, @kyb.
  • Off-topic, but if you're dealing with non-ASCII characters, I'd recommend to change that into tmux -u … for explicitly enabling Unicode support even on machines that don't have the proper environment variables set.

tmux 3.0a or older on the remote machine

Almost the same as above, but change the last line to³:

  RemoteCommand tmux at -t foobar || tmux new -s foobar

¹ As of 2021-07-28, the list of distributions shipping with tmux 3.1 or newer (see repology.org for what version is shipped for what distro) is quite long already. At this point, people should probably really push for legacy or outdated versions to be updated.

² new is short for new-session.

³ at is short for attach-session.


Only if, for some reason, you really, really can't do it client-side:

Using the remote's authorized_keys file

If you would rather not have an ~/.ssh/config file for whatever reason, or want the remote machine to force the connecting machine to connect to / open the session, add this to your remote ~/.ssh/authorized_keys:

command="tmux at -t foobar || tmux new -s foobar" pubkey user@client

This will, of course, work from all clients having the corresponding private key installed, which some might consider an upside –– but: should anything go wrong, it might not be possible to connect anymore without (semi-)physical access to the machine!


Connect:

ssh user@host -t "tmux new-session -s user || tmux attach-session -t user"

During session:

Use Ctrl+d to finish session (tmux window closes) or Ctrl+b d to temporary detach from session and connect to it again later.

Remember! If your server restarted session lost!

When you are inside tmux anytime you can use Ctrl+b s to see sessions list and switch current to another.

Fix your .bashrc:

I recommend you to define universal function in your .bashrc:

function tmux-connect {
    TERM=xterm-256color ssh -p ${3:-22} $1@$2 -t "tmux new-session -s $1 || tmux attach-session -t $1"
}

It uses 22 port by default. Define your fast-connect aliases too:

alias office-server='tmux-connect $USER 192.168.1.123'
alias cloud-server='tmux-connect root my.remote.vps.server.com 49281'

Login without password:

And if you don't want to type password everytime than generate .ssh keys to login automatically:

ssh-keygen -t rsa
eval "$(ssh-agent -s)" && ssh-add ~/.ssh/id_rsa

Put your public key to the remote host:

ssh-copy-id -p <port> user@hostname

Additional tips:

If you want to use temporary session-id which corresponds with a local bash session use as tmux id:

SID=$USER-$BASHPID
ssh user@host -t "tmux new-session -s $SID || tmux attach-session -t $SID"

I used lines from @kingmeffisto (I'm not allowed to comment that answer) and I added an exit so terminating tmux also terminates the ssh connection. This however broke SFTP sessions so I had to check for $SSH_TTY instead of $SSH_CONNECTION.

EDIT 4/2018: Added test for interactive terminal via [[ $- =~ i ]] to allow tools like Ansible to work.

if [ -z "$TMUX" ] && [ -n "$SSH_TTY" ] && [[ $- =~ i ]]; then
    tmux attach-session -t ssh || tmux new-session -s ssh
    exit
fi