Tmux is what is called a multiplexer. A multiplexer allows you to run multiple terminal sessions at once. Or run a terminal session in the background. This is useful when you need to access multiple ssh sessions at a time or just for a good general workflow. For example, you can run htop while downloading the latest system update, edit a config file and restart the service all on one tmux session. In this article, we will install tmux and go over some useful commands to get you started.
Let's go ahead and select the icon and search for the terminal in the search bar and then click on the terminal to open.
Next, let's update our system packages and install tmux
sudo apt update
sudo apt install tmux
Running your first tmux session
To open tmux In the terminal type tux and enter.
you should now see a screen like the one shown below:
With tmux open let's use our first command.
The above command will open a new terminal as you can see below in the screenshot. The new number for the bash window is highlighted at the bottom 1:bash. Make sure to hold cntrl+b, release then type c. This is with all tmux commands moving forward.
Detach from a session
If you would like to detach from a tmux session and go back to a normal session you can use the following command:
From the screenshot below, you can see the output.
Reattach to a session
To see a list of your tmux session use the following command below:
And use the command below to attach to a specific session
tmux attach-session -t 6
Above you can see that we have reattached to session 6 as it shows in bracket .
Create a session name
If you would like to name sessions for a better workflow you can use this command here. Replacing session_name with a name that you would like.
tmux new -s session_name
above you can see the newly created session name shells_session when using tmux ls
Killing a session
If you would like to completely end a session you can use the kill-session command using the following steps:
Switching between terminal windows
Another cool thing you can do is switch between the windows you created in your shell session using the command below.
To go directly to a terminal window session
CTRL+b “window number”
You can also split panes horizontally and vertically
Other commands you can use with tmux:
- Ctrl+b , Rename the current window
- Ctrl+b o Go to the next pane
- Ctrl+b ; Toggle between the current and previous pane
- Ctrl+b x Close the current pane
Tmux is a really cool tool for the Linux admin. It allows you to multitask or close out a session without it killing the terminal so you can do backups or upgrade your system without having to leave a terminal open, just type your command hit CTRL+b then d, and walk away.