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'
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.
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.
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)
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.
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!
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.
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.
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
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:
pi@pivpn:~ $ which tmux
/usr/bin/tmux
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.
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:
pi@pivpn:~ $ tmux new -s MrTmux
Also, if you list the active tmux sessions, you will see it. (tmux refers to sessions as windows)
Code:
pi@pivpn:~ $ tmux list-sessions
MrTmux: 1 windows (created Wed Aug 11 20:13:33 2021) [237x62] (attached)
pi@pivpn:~ $
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.
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!
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:
pi@pivpn:~ $ 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.
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
Last edited: