Persistent SSH Sessions with Tmux
Tags: ssh, tmux, linux, termux
Published: 2019-03-26 05:39:28

---
title: Persistent SSH Sessions with Tmux
published: true
description: Let's try setting up our SSH sessions to persist across exits, logouts and devices.
cover_image: https://thepracticaldev.s3.amazonaws.com/i/7kev8hr54sn905sdxcf2.jpg
tags: ssh, tmux, linux, termux
---
## 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](Hackernoon) has a [solid beginner's guide here](https://hackernoon.com/a-gentle-introduction-to-tmux-8d784c404340) 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:

```bash
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](https://termux.com) 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](https://reddit.com/user/authorice)_