Starting Tmux Automagically When Connecting to SSH

・1 min read

I recently noted in a blog post that I use a short snippet to either connect to an existing tmux session when I start a new SSH connection, or create a new tmux session if an existing one doesn’t exist.

The problem is, I have been using Termius more often than not recently and using the snippet feature as mentioned in the previous blog post.

I needed to make this work within a normal terminal today and thought I would add the snippet as a blog post so I don’t forget for next time.

In ~/.ssh/config we just need to add the following:

Host servername  
    HostName servername.domain.tld
    User username
    Port 22
    RequestTTY yes
    RemoteCommand tmux att -t session -d || tmux new-session -s session

Then, just run ssh servername and the SSH connection will be create and will either attach to an existing tmux session (and disconnect other sessions) or create a new one.