Skip to main content

Save Your SSH Session With Tmux

·3 mins
Table of Contents

SSH is a great piece of software. With it, you can remotely control a server as if you were sitting at it, from anywhere in the world. I occasionally find myself needing to SSH into my server from my phone to perform some maintenance. This is great and all, until the connection accidentally drops, and the currently running process never stops.

This happened to me recently, where I was editing a file in vim, and my session dropped. Inconveniently, I had to SSH back into my server, kill the vim process, and clean up the temp files it leaves behind. There must be a better way, right? I did some research and it turns out that using tmux would have saved my bacon.

What Is It? #

tmux is a terminal multiplexer. When you launch it, it creates a terminal session, and while in that session, you are able to detach from it. Once detached, the processes in that session continue to run, and you are able to reattach at any time. This is useful for long running tasks, such as running game servers, or running time consuming builds. In my previous situation, I could have launched a tmux session and did my editing in there. Once my connection dropped, all I needed to do was SSH back into my server, reattach to the tmux session, and continue editing. But we can do better than that.

Solution #

The solution I have been running for the past two months has been to edit my .zshrc file (.bashrc works too) and add this to the top:

#!shell
# If ssh, then start (or continue) a tmux session
if command -v tmux &>/dev/null && [[ -z "$TMUX" ]] && [ "$SSH_CONNECTION" != "" ]; then
    tmux attach-session -t ssh_tmux || tmux new-session -s ssh_tmux
fi

When you SSH into your server, it will check to see if there is a previous tmux session with the name “ssh_tmux”. If so, it will attach to it, else it will create a new one. This way, you can SSH into your server and automatically be in a tmux session. If you accidentally disconnected, it will pick back up where you left off. For those wondering, the if check is to make sure you have tmux, aren’t already in a tmux session, and makes sure that you are SSH’d into the server (so it doesn’t affect normal logins).

Make It Better #

You may notice that when you exit your shell, you are still SSH’d in your server. This is because you only exited the tmux session; the rest of the rc file was loaded and you are now in your regular shell (the one that was running tmux). You can add an exit command inside of your rc file to prevent this:

#!shell
# If ssh, then start (or continue) a tmux session
if command -v tmux &>/dev/null && [[ -z "$TMUX" ]] && [ "$SSH_CONNECTION" != "" ]; then
    tmux attach-session -t ssh_tmux || tmux new-session -s ssh_tmux
    exit
fi

This will make it so that when you SSH into your server, the regular shell will launch tmux, and then when tmux exits, the shell that ran tmux also exits (closing the connection).

PLEASE make sure this all works before you add the exit! If you somehow screw this up, you could be locked out of your server, where you SSH into it and are immediately kicked! The only way to “unlock” it is to have physical access to your server and log into it.