tmux (multiple terminals)

what is tmux?

Tmux is a terminal multiplexer an alternative to GNU Screen. In other words, it means that you can start a Tmux session and then open multiple windows inside that session. Each 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.

All commands in Tmux start with a prefix, which by default is ctrl+b.

how to use tmux

# syntax
tmux <options> <session>

##### create a new session
tmux new -s session_name

##### show all running tmux sessions
tmux ls

##### attach to a session
root@Corrosie:~# tmux ls
testSession: 1 windows (created Sun Aug  9 09:11:00 2020)
root@Corrosie:~# tmux attach-session -t testSession

##### tmux options (in the tmux session)
Ctrl+b c # Create a new window (with shell)
Ctrl+b 0 # Switch to window 0 (by number ) [IMPORTANT]
Ctrl+b n # go to the next screen (right side)
ctrl+b p # go to the previous screen (left side)
Ctrl+b , # Rename the current window
Ctrl+b % # Split current pane horizontally into two panes [IMPORTANT]
Ctrl+b " # Split current pane vertically into two panes [IMPORTANT]
Ctrl+b o # Go to the next pane (alternative = ctrl+b <terminalscreen number>
Ctrl+b x # Close the current pane
Ctrk+b z # increase the window size of you current terminal to the full size, use this combination again to shrink it to origin size
ctrl+b [ # scroll through your terminal output, use 'q' to quit this feature.

##### switching through tmux panes
ctrl+b <ARROW KEYS> 

##### detaching from sessions
ctrl+b d # detach from current terminal session
ctrl+b D # detach from the entire terminal session

##### kill all tmux sessions
# in any terminal:
tmux kill-server

tmux script - specific

Here is an example of a tmux script, this script will connect you to your own tmux session. with predefined tmux windows, and tools.

note: this scripts requires that tools are installed, like openVAS, Metasploit, openvpn etc. Also, it is redirecting to local directories that are not present on a default Linux installation. If you would like to try and test this, check out the next subject "tmux script - general example"

#!/bin/bash

sudo service openvpn start
sleep 2

SESSION="$USER"

tmux ls > /dev/null

if
        [ "$?" == 1 ]
then
        tmux -2 new-session -d -s $SESSION

# setup multiple windows to attach 5 tmux sessions
        tmux new-window -t $SESSION:1 -n 'general usage'
        tmux selectp -t 1
        tmux new-window -t $SESSION:2 -n 'recon'
        tmux selectp -t 2
        tmux send-keys "cd ~/Documents/hacking/useful_repos/recon/autorecon/results" C-m
        tmux send-keys "clear" C-m
        tmux split-window -v -t 2
        tmux send-keys "cd ~/Documents/hacking/useful_repos/recon/autorecon" C-m
        tmux send-keys "clear" C-m
	      tmux send-keys "sudo python3 autorecon.py "
        tmux new-window -t $SESSION:3 -n 'enum/attack'
        tmux selectp -t 3
        tmux send-keys "cd ~/Documents/hacking/useful_repos/recon/autorecon" C-m
	      tmux send-keys "clear" C-m
        tmux new-window -t $SESSION:4 -n 'msf'
        tmux selectp -t 4
        tmux send-keys "msfconsole" C-m
        tmux send-keys "clear" C-m
	      tmux new-window -t $SESSION:5 -n 'repositories'
	      tmux selectp -t 5
	      tmux send-keys "cd ~/Documents/hacking/useful_repos" C-m
        
# open a terminal and connect to your current session.

        gnome-terminal -- tmux attach -t $SESSION
else

        gnome-terminal -- tmux attach -t $SESSION
fi

tmux script - general example

#!/bin/bash
SESSION="$USER"

tmux ls > /dev/null

if
        [ "$?" == 1 ]
then
        tmux -2 new-session -d -s $SESSION

# setup multiple windows to attach 5 tmux sessions
        tmux new-window -t $SESSION:1 -n 'read /etc/passwd'
        tmux selectp -t 1
        tmux send-keys "cat /etc/passwd" C-m
        tmux new-window -t $SESSION:2 -n 'terminal 2 | splitted panes'
        tmux selectp -t 2
        tmux split-window -v -t 2
        tmux send-keys "cd /etc" C-m
        tmux send-keys "echo 'Note, i am in the /etc directory!'" C-m
        tmux new-window -t $SESSION:3 -n 'Terminal 3'
        tmux new-window -t $SESSION:4 -n 'Terminal 4'
        tmux selectp -t 4
        tmux new-window -t $SESSION:5 -n 'Terminal 5'
        tmux selectp -t 5
# open a terminal and connect to your current session.
        tmux attach -t $SESSION
else

        tmux attach -t $SESSION
fi

#####
# make sure the permissions are correctly set to execute the script

Last updated