Adventures in tmux!

dcbrown73

Well-Known Member
Joined
Jul 14, 2021
Messages
361
Reaction score
336
Credits
3,199
Good day adventurers!

As a career Linux engineer who usually works on servers that exist in datacenters. A GUI environment is usually not an option and mastery of the Linux shell is of the utmost importance. Also important is the ability to multi-task on a single server. Whether it be running a command in one shell while monitoring the logs in a second one. The ability to do this not only makes things easier, but allows you to function in a far more effiecnt manor.

There are a couple of ways to accomplish this. You can open multiple terminals in your desktop operating system and ssh with each one. (sometimes there are single applications that allow you to open multiple terminals also) The second open and what I'm here to talk about is a Terminal Multiplexer.

We all know what a terminal is. Every time we go to a Linux command line, that is what we are using. The second term is Multiplexer. The definition of multiplexer is as follows: A device that interleaves several activities; a switching device.

If we combine them, we get a Terminal Multiplexer, or as the application I will introduce you today is. tmux!

Now, before I continue, tmux is not the only terminal multiplexer out there. Another one is called "screen". Screen is the one I cut my teeth on and used for well over 10 or probably 15 years. I only recently switched to tmux within the last couple years. The reason being, tmux is screen on steroids!

Lets get started with tmux.

As stated, tmux is a terminal multiplexer. This allows you to have multiple terminals in a single terminal window sort of like you have multiple tabs in a web browser. With this single terminal session, you can have multiple terminals into a single server, or use each terminal to connect to different servers.

First, lets find out if tmux is installed. You can do that by typing 'which tmux'
Code:
[email protected]:~ $ which tmux
/usr/bin/tmux
If it is not installed, you will need to install it. Installation of tmux is outside the scope of this tutorial, but it should be simple enough. If not Google is your friend.

To start tmux, we just run the command tmux in the terminal. You will know it started because it will clear the screen and then you will have a lime green bar at the bottom of your terminal.

1628726840718.png


You must also need to know how to get out of tmux. To exit tmux, just type "exit" in the console and it will return you to a standard console.

Now that you've exited tmux, don't restart it just yet. I work in an environment where there might be more than just myself connecting to a server. My compatriots might also be using tmux and we want to know who's tmux session is whos. To do this, when we start tmux, we will give it a named session.

Code:
[email protected]:~ $ tmux new -s MrTmux
Now, instead of saying [0] in the lower left hand corner of the screen, it will say MrTmux.

Also, if you list the active tmux sessions, you will see it. (tmux refers to sessions as windows)
Code:
[email protected]:~ $ tmux list-sessions
MrTmux: 1 windows (created Wed Aug 11 20:13:33 2021) [237x62] (attached)
[email protected]:~ $
As you can see, it has the name of my session, MrTmux. (more on this later)

Now that we have tmux running, we need a second terminal window so we can do... yeah, whatever!

To start a new tmux terminal within your current tmux session. We use the keyboard Kung Fu "CTRL+b c". To be clear. Hold control key down while pressing the letter "b". Then let up and quickly tap the letter "c". Most commands in tmux will triggered by the control string (CTRL+b) followed by a character to execute a specific command.

Lets create a new terminal. Execute the "CTRL+b c" command.

Once you do this, you will see something like the following on the lime green bar at the bottom.
1628727708648.png


This breaks down to The session name (MrTmux), followed by the two different terminals you've created. Terminal 0:bash, and terminal 1:bash. The word bash is the command that is currently executing. Below, I opened vim in the second terminal and switched back to the first. So, now I can see what windows is running what!
1628727818864.png


Now, how do you switch to the new terminal? We go back to the control string. "CTRL+b" Now, the easy way to remember what the command is to do it is, "n" = Next, or "p" = Previous. So, if you want to go forward t the next screen, it's "CTRL+b n", if you want to go back to a previous screen, its' "CTRL+b p"

Say you have 5 terminals open and you are on terminal 2 and you want to go to terminal 5 with out cycling with "CTRL+b n" three times in a row. You can just use the numeric value of the terminal you want to go to. "CTRL+b 5" and that will jump to straight to terminal windows 5.

If you are done with a specific terminal window and you only want to close that window. Just type exit while in that terminal and it will close and kick you back to the previous terminal.

Now, what if we are running a long process, but we need to reboot our computer or go home for the evening while this job is still running? We don't want to kill the process, but we don't want to stay at work all night either! This is where terminal multiplxers really shine! You can just disconnect from tmux, and your current jobs will continue to run while you go home or reboot your desktop computer!

To disconnect, you use the "d" command. "CTRL+b d". Lower case "d" for disconnect!

After rebooting or coming back to the office the next day, you want to reconnect to your original session. (another reason we named the session something specific!) To reconnect to an existing session, (in this case MrTmux) we would run the following tmux command.
Code:
[email protected]:~ $ tmux a -t MrTmux

To explain this command. Obviously, tmux is the application. The "a" parameter is for "attach" and the "-t MrTmux" you are specifying the named session you wish to reconnect too. With named session, it's a lot less confusing which session is yours if multiple people are using tmux on the same machine. You don't have to name the sessions, but it certainly makes it easier and less error prone!

There are tons of options when using tmux, but I want to show you one (well, sort of two) more. While working on something that requires two terminals. It can be a severe pain it the tookus flipping back and forth between the terminals. Sometimes you want to watch the output while monitoring the command itself. Here we can create new terminals on same screen!

If we want to split the panes vertically, we would issue the command CTRL+b " That is control+b then the quotes character. If you prefer to split them horizontally, we would use CTRL+b %. (percent character)

Here is a screenshot of mine split vertically, and then the bottom pane split horizontally.

1628728846148.png


Finally, I will finish this tmux tutorial by telling you how to navigate the panes when they are split in this manor! The trick is, "CTRL+b {arrow key of choice}" As you can see, the cursor is in the lower right hand pane. If I wanted to move to the pane to the left, I would use CTRL+B {LEFT-ARROW}" and it will move the into the pane to the left.

If you want to close a pane, move to the pane and type exit!

I hope this was helpful. (and I didn't butcher it too much grammar or spelling wise)
Dave
 

Attachments

  • 1628727818843.png
    1628727818843.png
    2.8 KB · Views: 116
Last edited:


Omegus

New Member
Joined
Aug 12, 2021
Messages
2
Reaction score
0
Credits
12
This guy clearly has no clue how to administer large scale Linux based operations....
 
OP
dcbrown73

dcbrown73

Well-Known Member
Joined
Jul 14, 2021
Messages
361
Reaction score
336
Credits
3,199
This guy clearly has no clue how to administer large scale Linux based operations....
Oh no! It's AstroTroll. :eek:

There seem to be two of us now. :p
 

KGIII

Super Moderator
Staff member
Gold Supporter
Joined
Jul 23, 2020
Messages
5,769
Reaction score
5,206
Credits
46,570
Sort of related:

I use Terminator (often) and that lets me split my terminal. Qterminal, the default in Lubuntu, also does the same.

It can be pretty handy, as has been clearly stated. Modern terminals are pretty robust with their feature set, usually at least supporting tabs.
 
OP
dcbrown73

dcbrown73

Well-Known Member
Joined
Jul 14, 2021
Messages
361
Reaction score
336
Credits
3,199
Sort of related:

I use Terminator (often) and that lets me split my terminal. Qterminal, the default in Lubuntu, also does the same.

It can be pretty handy, as has been clearly stated. Modern terminals are pretty robust with their feature set, usually at least supporting tabs.
Nice. Love that it's written in my swiss army knife of choice. (Python)

EDIT: Or was. Appears to be written in Java now.
EDIT #2. Maybe I'm wrong? Or there are two of them... The Python one says originally written by Chris Jones, the Java one says originally written by Phil Norman.
 

KGIII

Super Moderator
Staff member
Gold Supporter
Joined
Jul 23, 2020
Messages
5,769
Reaction score
5,206
Credits
46,570
'Tis in Python, I'm pretty sure. I'm not really here right now.

 

craigevil

Well-Known Member
Joined
Feb 24, 2021
Messages
342
Reaction score
348
Credits
2,346
byobu works great with tmux or screen.
Description: text window manager, shell multiplexer, integrated DevOps environment
Byobu is Ubuntu's powerful text-based window manager, shell multiplexer, and
integrated DevOps environment.
.
Using Byobu, you can quickly create and move between different windows
over a single SSH connection or TTY terminal, split each of those windows into
multiple panes, monitor dozens of important statistics about your system,
detach and reattach to sessions later while your programs continue to run in
the background.

Code:
Byobu is a suite of enhancements to tmux, as a command line
tool providing live system status, dynamic window management,
and some convenient keybindings:

  F1                             * Used by X11 *
    Shift-F1                     Display this help
  F2                             Create a new window
    Shift-F2                     Create a horizontal split
    Ctrl-F2                      Create a vertical split
    Ctrl-Shift-F2                Create a new session
  F3/F4                          Move focus among windows
    Alt-Left/Right               Move focus among windows
    Alt-Up/Down                  Move focus among sessions
    Shift-Left/Right/Up/Down     Move focus among splits
    Shift-F3/F4                  Move focus among splits
    Ctrl-F3/F4                   Move a split
    Ctrl-Shift-F3/F4             Move a window
    Shift-Alt-Left/Right/Up/Down Resize a split
  F5                             Reload profile, refresh status
    Alt-F5                       Toggle UTF-8 support, refresh status
    Shift-F5                     Toggle through status lines
    Ctrl-F5                      Reconnect ssh/gpg/dbus sockets
    Ctrl-Shift-F5                Change status bar's color randomly
  F6                             Detach session and then logout
    Shift-F6                     Detach session and do not logout
    Alt-F6                       Detach all clients but yourself
    Ctrl-F6                      Kill split in focus
  F7                             Enter scrollback history
    Alt-PageUp/PageDown          Enter and move through scrollback
    Shift-F7                     Save history to $BYOBU_RUN_DIR/printscreen
  F8                             Rename the current window
    Ctrl-F8                      Rename the current session
    Shift-F8                     Toggle through split arrangements
    Alt-Shift-F8                 Restore a split-pane layout
    Ctrl-Shift-F8                Save the current split-pane layout
  F9                             Launch byobu-config window
    Ctrl-F9                      Enter command and run in all windows
    Shift-F9                     Enter command and run in all splits
    Alt-F9                       Toggle sending keyboard input to all splits
  F10                            * Used by X11 *
  F11                            * Used by X11 *
    Alt-F11                      Expand split to a full window
    Shift-F11                    Zoom into a split, zoom out of a split
    Ctrl-F11                     Join window into a vertical split
  F12                            Escape sequence
    Shift-F12                    Toggle on/off Byobu's keybindings
    Alt-F12                      Toggle on/off Byobu's mouse support
    Ctrl-Shift-F12               Mondrian squares

/usr/share/doc/byobu/help.tmux.txt (END)

official site:
https://www.byobu.org/

10 min video tutorial:
https://www.youtube.com/watch?v=NawuGmcvKus

Screenshot at 2021-08-11 23-56-54.png
 
Last edited:

f33dm3bits

Gold Member
Gold Supporter
Joined
Dec 11, 2019
Messages
4,462
Reaction score
3,169
Credits
32,354
I love tmux, it's my favorite tool to keep my open remote sessions organized.
 
Last edited:
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Members online


Top