DEV Community

Cover image for Persistent SSH Sessions with Tmux
Michael Okoko
Michael Okoko

Posted on

Persistent SSH Sessions with Tmux

Hello SSH

The SSH (Secure Shell) is a widely used protocol that helps you drop into a remote system, do what you want to do, and get out. Which means, you could check your server logs remotely from an android device, monitor the raspberry pie lying somewhere in your bedroom from your office machine, or go full hacker mode and do stuff like keeping a connection alive when your firewall keeps dropping it or tunneling a network service securely through a remote machine.
But here's the thing. Each time we exit/logout from our remote SSH server, we lose our session and with it, every program that was running. And we don't want that.
That is, we might want an opened program, say an IRC client, a crawler script, anything, to continue running even when we've left it alone, without us starring at black screens with walls of white and green text, but this is where "bare-bones SSH" falls short, and Tmux takes the shine - a little of it anyway.

Meet Tmux

Tmux - which is actually short for "Terminal Multiplexer" - is a tool that lets you create different windows and panes in a single terminal window, while also letting you persist those sessions so you can continue from where you left off the last time (you see what I'm saying?)

To the meat...

Setting up tmux

If you have used tmux and are already comfortable with it, feel free to jump to the next section. If you haven't, it's a fairly straightforward process so let's breeze through it:

You are highly likely to find the tmux package in your linux distro's package repositories so installing it is as easy as:

Ubuntu/Debian

sudo apt install tmux

Archlinux/Archlinux Derivates

sudo pacman install tmux

CentOS

sudo yum install tmux

Now, navigating tmux is a bit more involved and though mouse support exists, it is almost always better to get the hang of a couple of commands. The good thing is all of the commands are configurable which means you get to map them to fit your workflow better. Hackernoon has a solid beginner's guide here to help you find your way around if you're just starting out though.

Ehem...Persistent Sessions.

First, we modify our zsh/bash configuration file (depending on if you have seen the light or not your preferred shell ) to automatically create a new tmux session if there's no available session, or attach itself to an existing one if it exists. So open up the configuration file i.e ~/.bashrc or ~/.zshrc and type the following:

if [ $(tty) == /dev/tty1 ]; then
  /usr/bin/tmux new -s 0
  /usr/bin/tmux attach -t 0
fi

Next, we setup our profile to automatically attach itself to the existing tmux session (labelled '0') so we append the following to our ~/.profile (you'll have to create it if it doesn't exist.):

/usr/bin/tmux attach -t 0

Side Attraction: Termux for Android

Termux is an Android terminal emulator that actually works. It comes with niceties such as not requiring root access and a pretty loaded package repository, as well as been able to compile your own packages on your device. To use SSH on termux is as simple as running pkg install openssh in the app - after installing the termux app of course. Installing the openssh package also set up your SSH keys, which you can install on your remote SSH server and get going.

Now we can hop back into our SSH server, but this time, with an extra coolness.

Cover Image Credit: u/authorice

Latest comments (8)

Collapse
 
ncot_tech profile image
James

This is something I've been trying to do too, however I couldn't get your method to work on my setup (LXC containers inside Proxmox). It wouldn't create the initial tmux session.

However, I found this Stack Overflow question which has a useful answer:

stackoverflow.com/questions/343253...

Putting tmux new -A -s mySession in my .profile works.

I'm also using mosh to persist the connection when my PC goes to sleep.

Collapse
 
sndp487 profile image
Sandeep

I was getting sessions should be nested with care, unset $TMUX to force at the top of tmux when using tmux attach || tmux new. What happens is tmux also executes .bashrc/profile.

So now I use this:

if [[ ! "$TERM" =~ "screen" ]]; then
tmux attach -t default || tmux new -s default
fi

in my .bashrc

TERM is set to 'screen' in tmux environment.

Collapse
 
idoko profile image
Michael Okoko

Glad you got it to work, also, I've only heard praises for mosh. I should take it for a spin one of these days.

Collapse
 
darkflib profile image
Mike Preston

Turn on keepalives in ssh config...

autossh -M 0 -t user@host tmux new -s sessname -A

This allows me to roam across networks and seemlessly reconnect if the connection fails.

It works on my 120mile commute across multiple networks flawlessly... and across laptop sleeps.

Collapse
 
rizwankh profile image
Rizwan

Same here. I also use tmuxinator which is just awesome!

Collapse
 
optio50 profile image
optio50

Just like screen?

Collapse
 
idoko profile image
Michael Okoko

Yeah, though I personally prefer tmux to screen.

Collapse
 
josefwells profile image
Josef Wells

I use 'mosh' for UDP based ssh, this allows me to have a persistent ssh to a host where I then launching my tmux sessions. Now if my connection falters (laptop WiFi/wired or otherwise) my ssh stays up and my tmux sessions are safe on the host.