JoBins Engineering

JoBins Engineering

Save SSH session using tmux

Kushal Raj Shrestha's photo
Kushal Raj Shrestha
·Jul 29, 2022·

4 min read

Save SSH session using tmux

Subscribe to our newsletter and never miss any upcoming articles

Play this article

Table of contents

  • Problem
  • Solution
  • Let's start formally
  • There's more!

Problem

As a developers we often ssh into remote servers and run commands which needs to be actively tracked. For example, we have to run a console command that saves a huge pile of image files from our local storage to a S3 bucket and shows progress, time remaining, etc. Now, we could run the command in background and loose track of the progress update OR we run it in simple ssh session and keep our terminal active till the command completes, which may be hours or if not days. And, to make things worse our internet just lost connection. This is a real headache, isn’t it?

Enter tmux.

What is tmux?

tmux is a terminal multiplexer application from which you can start multiple tmux sessions and then open multiple windows inside those sessions. A session can be actively displayed in terminal (attached state) or can stay in background (detached state) executing any given command. A window occupies the entire screen and can be split into rectangular panes.

With tmux you can easily switch between multiple programs in one terminal, detach them and reattach them to a different terminal.

tmux sessions are persistent, which means that programs running in tmux will continue to run even if you get disconnected.

Solution

We ssh into our remote server

ssh forge@my-awesome-app.com

Now create a tmux session

tmux

This will open a new unnamed tmux session and start a shell that we can use. We can now start our long running console command in this session. We can now detach from (log out) and attach (login back) to this session anytime. It is that simple.

Note: tmux may already be installed on the server or if not read here on installing tmux.

Advantages of using tmux

  • As long as the server is running our tmux session is available
  • Share session with other users
  • Enables multiple windows and panes within a single terminal window.

Let's start formally

All tmux commands start with a prefix, which by default is ctrl+b. When we are in a tmux session we run tmux commands using prefix key and then a designated key to perform action.

For example, press ctrl+b and then d to detach from a session.

Open tmux session named image-uploader

tmux new -s image-uploader

List all tmux sessions

tmux ls

Attach to session named image-uploader

tmux at -t image-uploader

Kill a session Terminate the active tmux session.

exit

Check tmux version

tmux -V

With these simple commands you can start using tmux.


There's more!

Windows and Panes

Within a tmux session we can open multiple windows and panes to speed up our work. Windows is similar to our browser tabs and Panes is like splitting a window into multiple terminals.

tmux command

These are some useful tmux command that can be triggered using prefix ctrl+b then following keys:

c : Create a new window.

0 to 9 : Select windows 0 to 9.

& : Kill the current window.

l : Move to the previously selected window.

n : Change to the next window.

d : Detach the current client.

" : Split the current pane into two, top and bottom.

% : Split the current pane into two, left and right.

Customizing tmux

tmux configuration file is generally stored in ~/.tmux.conf. If not you can create it and change configuration parameters to customize tmux.

Here is an example of ~/.tmux.conf configuration file:

# allow reload of this file with PRE r
bind r source-file ~/.tmux.conf \; display "Reloaded."

# switch prefix to control-a, unmap b, allow double-a to go through
set -g prefix C-a
unbind C-b
bind C-a send-prefix

# -r repeat time (Default 500 millis)
set -g repeat-time 2000

# colors
set -g default-terminal "screen-256color"

# mouse mode (scrolling, etc)
# tmux 2.1
setw -g mouse on

# tmux < 2.1
# setw -g mode-mouse on
# # set -g mouse-select-pane on
# set -g mouse-resize-pane on
# set -g mouse-select-window on

# remove delay
set -sg escape-time 1

# set {window,pane} index to start at 1
set -g base-index 1
setw -g pane-base-index 1

Plugins

We can use additional plugins to get the more out of tmux. Install tmux plugins

Reference

 
Share this