Learning TMUX — Personal Notes
Introduction:
Hey all, figured I might as well publish my personal notes for ReMux the TMUX lab on THM while getting ready for my OSCP. Enjoy!
TMUX Start
To start a new tmux session just run the tmux command with no arguments. The first session create will have the name “0”.
To change the session name from “0” -> “box-dev”. Must first learn how tmux is called. All commands within a tmux session all start with the tmux prefix is. By default, the tmux prefix is “Ctrl b”.
Name of Session, change and new session.
After the tmux prefix. To the hotkeys to change the current tmux session’s name is “shift $”. To change the name, use the following command
ctrl b shift $Then name it whatever you want!
To spawn a new tmux session without attaching to it. In the example image below. The -s argument is used to specify the session name for the new session. -d allows us to not go into it ASAP. Typed as:
tmux new -s <new-session-name> -dTo reattach to an active tmux session. Run tmux with the attach option and -t followed by the desired session name. (This is just going into the tmux session)
tmux attach -t <session-name>Exiting TMUX without Closing.
Exiting a tmux session without closing it can be done with the prefix. Followed by d
ctrl b dKill Sessions
Delete a single session by its session name. Is done with the kill-session option with tmux. Followed by -t and the
tmux kill-session -t <session-name>WARNING! By deleting the session. Anything open in that session will be lost if not saved before the tmux kill-session.
The command below allows you to kill all sessions with the exception of the one that follows the -t.
tmux kill-session -t <session-name> -aChange what Directory is used by default.
To change the base starting directory. Must first learn about tmux prompt or command mode. The tmux prompt allows tmux sessions to run tmux commands without the tmux binary name. Useful when the terminal has been filled with other text. Enter a tmux prompt with prefix shift :
ctrl b shift :Followed by
attach -c /path/to/new/starting/directoryAttach can be shortened to
a -c /path/to/new/starting/directoryWithin a nested tmux session, we change the name of the second session by saying:
ctrl b ctrl b shift $Panes in Tmux
Split terminal horizontally:
ctrl b shift "Split terminal vertically:
ctrl b shift %Exit:
exitMove around:
ctrl b <arrow-keys>The main problem with this is using the arrow keys again, messes some stuff up, so what you can do is:
ctrl b oThis will Cycle through all open terminals!
To swap between the most used tmux panes if more than two panes are open. “ctrl b ;” is the better option. “ctrl b o” is better when there are just two panes open.
ctrl b ;If a tmux pane isn’t responsive and ctrl-c isn’t resolving the issue. Force close or kill the currently selected tmux pane with the prefix and x. Then y to confirm.
ctrl b x yTo move the currently selected pane. In a clockwise rotation. Do prefix shift }
ctrl b shift }To move the currently selected pane counter-clockwise. Do prefix shift {
ctrl b shift {To cycle through the built-in pane layouts one at a time. Do the prefix with the spacebar. This can also be an alternative using a clockwise or counter-clockwise option when there are only two panes open on the current tmux window.
ctrl b spacebarYou can swap locations of individual panes! Before swapping, you can identify pane numbers with ctrl b q .
Then type in tmux command:
:swap-pane -s <source pane final swap location> -t <currently selected destination pane>For example, if I want to switch Top Left (0) and Bottom Right (3)
ctrl b shift :
swap-pane -s 3 -t 0More Pane…
Tmux windows are like a new terminal tab that you can easily swap from and more. To create a new empty tmux window to work on. Do prefix c.
ctrl b cTo change the name of current window:
ctrl b ,Oh NO. The recon window has been cluttered with multiple panes and can’t view the Nmap output. To detach a pane into its own window. move the desired pane to move to its own window. Then prefix shift !.
ctrl b shift !To cycle between windows can be done with prefix n for the next window. Or prefix p for the previous windows.
ctrl b p (previous)
ctrl b n (next)Another method to switch to the desired window is to use prefix w. This will list all the tmux windows. Using the arrow keys to select the desired windows and enter-key to select the highlighted window.
ctrl b wIf a tmux window has been borked and needs to be terminated quickly. Killing the window will also close any panes open within the currently selected tmux window. To kill or close a tmux window do prefix shift & and followed by y to close the window. Or n to keep it open.
ctrl b shift &
ctrl b shift & y
ctrl b shift & nTo join two different windows/panes back into one. Can be done with the window name or number. The command is run within tmux prompt or command mode. Prefix shift : then within the prompt :join-pane -t OR :join-pane -s . Where -s is the source name and -t is the destination window name.
First. Enter tmux prompt or command mode from the tmux session.
ctrl b shift :Second. Type join-pane with the desired option and window name.
join-pane -s <source-window-name>
join-pane -t <destination-window-name>Third. Hit Enter to run the join-pane command.
-v and -h can be used to merge vertically and horizontally.
With join-pane can you use the window number instead of the window’s name.
Copy Mode
Copy mode can be used to scroll up and down the page. If the text is larger than the length of the pane or window size.
You enter by:
ctrl b [You exit this mode with q!
Search for Words:
Search Up:
ctrl rSearch Down:
ctrl sExit these two modes with:
escCopy Text
To copy and paste within tmux copy-mode takes 4 steps. Note that this method will only apply to the tmux clipboard as follows.
ctrl b [- scroll to the start of the block of text you would like to copy enable highlighting with:
ctrl spacebarThen use the arrow keys to up to select all the text. Down if you start from the top instead.
- copy all the highlighted text to the tmux clipboard with:
alt wNote! Even though the highlight will disappear. The text still copied to the tmux clipboard.
- create a new file to paste the final text with:
ctrl b ]To double-check what the text that is currently copied to the tmux clipboard. Do prefix followed by shift #
ctrl b shift #Oh my Tmux
Tmux by default doesn’t have a default configuration file. That doesn’t mean you can make one.
Before crafting a custom configuration file for tmux. It might be a good idea to show all the options of the defaults. With:
tmux show -gWhen creating the tmux configuration script it is important that. The file name is .tmux.conf and .tmux.conf is saved within the user’s home directory:
/home/username/.tmux.confIn addition to calling script though .tmux.conf is calling custom plugins. The plugins are shown were sourced from GitHub.
https://github.com/tmux-plugins/tmux-resurrect.git # save sessions to resume after a system reboot.
https://github.com/tmux-plugins/tmux-logging.git # plugin for creating command history to review later.
https://github.com/tmux-plugins/tmux-yank.git # plugin copy text from outside the tmux clipboard to use with other programs.
For the example below add the two following lines for tmux-logging. A plugin that can take pane screenshots, per pane history, and complete history logging.
NOTE. Double-check install instructions from their Github page if something doesn’t follow up.
Another .tmux.conf reload. To start command history logging for the currently selected pane do prefix shift p. This will start a log file within the current user’s home directory.
ctrl b shift PAdd config for custom VPN with THM
#!/bin/bash
# also comes with the repo as oreo-setup-script.sh
cd ~/
git clone <https://github.com/OreoByte/another_omt.git>
chmod +x ~/another_omt/thm_vpn
chmod +x ~/another_omt/curl_thm_vpn
cp ~/another_omt/simple_tmux.conf ~/.tmux.conf
Examples can be found at:
/usr/share/doc/tmuxMust be put into home for it to work!
#IMPORTANT NOTE
bind or bind-key options allow adding extra hotkey options without overwriting the default hotkeys
set options overwrite the default tmux hotkeys for the updated ones written within the .tmux.conf file
How can you run the desired plugin after loading it?:
run-shellChange prefix?
set -g prefix C-a