Raspberry Pi Server - Part 2: Setup TMUX on your raspberry with a few plugins for sessions

in #it7 years ago (edited)

In the previous post: https://steemit.com/it/@lynor/raspberry-pi-server-part-1-configure-ssh-only-access-and-fail2ban
we set up ssh access to our raspberry pi, made it quite restrictive and also set up fail2ban to reject connection attempts from suspicious IP addresses.

This time we will introduce a tool called tmux.

This is useful to start sessions on our raspberry which can be saved and restored even after disconnecting or even restarting the raspberry.

Let's get started!


TMUX

Get TMUX

Nowadays you can install tmux with the Advanced Packaging Tool (apt) for linux distributions:

Login to your raspberry with the steps from the first part (open a terminal and type: ssh <user>@<ip_address>)

then install tmux

sudo apt-get install tmux

TMUX Hotkeys

Before starting any fance things with tmux, lets get a basic set of hotkeys

change to your home directory:

cd ~

now download following settings file by brandon wamboldt:

wget https://raw.githubusercontent.com/brandonwamboldt/dotfiles/master/.tmux.conf

who by the way made this very nice website about describing how tmux works:

https://brandonwamboldt.ca/tmux-cygwin-1807/

One of the most important changes:

Once you start tmux, all tmux command have a special prefix, by default this is CTRL + b, this config file however changes the prefix to CTRL + Space

In my opinion this is great change, but you should always have this in mind.

There are more good settings for tmux in a second example file on:

Especially following part is quite helpful:

# Reload tmux config bind r source-file ~/.tmux.conf

I included this in my version as well.

Further help can be found on the tmux cheatsheet

my full configuration is this one:

# appearance customization
set -g status-bg black
set -g status-fg white
set -g window-status-current-fg green
set -g status-right 'Continuum status: #{continuum_status}'

# Custom modifier key
set -g prefix C-Space
unbind-key C-b
bind-key C-Space send-prefix

# Terminal improvements
set -g terminal-overrides "xterm*:XT:smcup@:rmcup@"
set-window-option -g automatic-rename on
set-option -g set-titles on
set -g mouse on
set -g default-terminal screen-256color
set -g status-keys vi
set -g history-limit 10000

# Clear scrollback buffer
bind l clear-history

# Custom key bindings to split the window
bind-key v split-window -h
bind-key s split-window -v

# Reload tmux config
bind r source-file ~/.tmux.conf

# No delay for escape key press
set -sg escape-time 0

# Shift arrow to switch panes
bind -n S-Left  select-pane -L
bind -n S-Right select-pane -R
bind -n S-Up    select-pane -U
bind -n S-Down  select-pane -D

# Control arrow to create panes
bind -n C-Down  split-window -v
bind -n C-Up    split-window -v -b
bind -n C-Right split-window -h
bind -n C-Left  split-window -h -b

# Easier window navigation
bind -n C-Tab     next-window
bind -n C-S-Tab   previous-window
bind -n C-S-Left  previous-window
bind -n C-S-Right next-window

# Reload tmux config
bind r source-file ~/.tmux.conf

# Ctrl + Alt + Left/Right to move windows
bind-key -n C-M-Left swap-window -t -1
bind-key -n C-M-Right swap-window -t +1

# Copy to cygwin clipboard
bind -n C-t run "tmux save-buffer - > /dev/clipboard"

# Kill tabs quicker
bind-key x kill-pane

# Plugins
# run-shell ~/.tmux-plugins/resurrect/resurrect.tmux

# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'

# Other examples:
# set -g @plugin 'github_username/plugin_name'
# set -g @plugin '[email protected]/user/plugin'
# set -g @plugin '[email protected]/user/plugin'

# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'

set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
set -g @continuum-save-interval '15'

TMUX Plugins

Let's get also some tmux plugins before you start out

create a .tmux/plugins folder in your home directory

cd ~
mkdir .tmux
mkdir .tmux/plugins

Now enter your plugins directory, and get all the repositories from the plugins you need

git clone https://github.com/tmux-plugins/tmux-resurrect
git clone https://github.com/tmux-plugins/tmux-continuum
git clone https://github.com/tmux-plugins/tpm
git clone https://github.com/tmux-plugins/tmux-resurrect

Save and Restore Session

Now you can save your session by prefix + CTRL + s
and restore it with prefix + CTRL + r

And continuum will save your session every 15 seconds

Try it out!

start tmux with the simple command

tmux

Now you can split your window with

CTRL - leftarrow

or create even a new window with

CTRL - c

Switch between your windows with

CTRL - SHIFT - leftarrow
CTRL - SHIFT - rightarrow

or close one of your windows with

prefix + CTRL + x

(remember, in our case prefix means CTRL + Space)

We can detach from our session with:

prefix + CTRL + d

Look what sessions are running:

tmux ls

And continue our session later by reattaching to it with:

tmux a -t 0

(In this case we reattach to session "0")

That is all for today, if you have questions please post.
Have fun with tmux and splitting your screens!

Thank you!
Cheers!