frame

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Register

[Linux] Guide to tmux

MichailMichail Moderator
edited May 2023 in Linux Applications

Introduction

Tmux is a terminal multiplexer that allows you to create multiple terminal sessions and run more than one process simultaneously. The principle of the "multiplexer" is to create several windows ("pseudo-terminals"), as well as the ability to divide these windows into several parts, the so-called "panes". Each window is completely independent, which allows you to run several processes at once within the same SSH connection.

The main advantage of tmux is that you can disconnect from a session while tmux will keep all processes running. You can then reconnect and return to those processes, which can be very handy when working over SSH. So if you lose your SSH connection for some reason, you will be detached from your tmux session, however, running processes will not be affected and they will run in the background.

1. Installing tmux

The tmux installation command may differ depending on the distribution of Linux.

On Debian based distribution (Ubuntu, Debian) connect and run the following command:

apt install tmux

On RedHat based distributions (CentOS, AlmaLinux) enter this command:

yum install tmux

2. Creating tmux session

To create first tmux session, enter the following command:

tmux

This will create a new tmux session and automatically set the session name to "0". Each time you enter this command, a new tmux session will be created with the corresponding number in name (0, 1, 2...)

3. Creating a window

Now let's create a new window. The main tmux keyboard shortcuts starts with the CTRL + B combination, followed by the command name. To create a new window, press CTRL+B on your keyboard, release, and then press C:

CTRL+B C

4. Transition between windows

Now that we have two windows, we can navigate between them. To do this, press CTRL+B on your keyboard, release, and then press 0 (goes to the first window) or 1 (goes to the second window). As you can see, the last number you need to press depends on the window you want to open:

CTRL+B 0 (1,2...)

5. Splitting tmux windows vertically

Let's divide one of the windows into vertical panes. Press the following combination:

CTRL+B %

You now have 2 vertical panes. Each of them is completely independent, so using these paness it is now possible to run 2 processes at the same time:

6. Splitting tmux windows horizontally

Now let's divide our window horizontally:

CTRL+B "

You can combine horizontal and vertical window splits to get as many panes as you need within a single window.

7.Transition between panes

To navigate between panes, press CTRL+B, release, and then press arrow key, depending on which direction you want to go (Left, Right, Up, Down).

CTRL+B <arrow key>

For example, if we want to move from the right pane to the left pane, we need to press the following combination:

CTRL+B <-

To close pane, simply enter the exit command:

exit

8. Detaching from tmux session

To detach from current tmux session, press the following combination:

CTRL+B D

9.Creating a new session and assigning name to it

To create a new session with the name that you want, enter the following command:

tmux new -s [name]

10. Running process at the background in tmux

To show how tmux works in the background, in this example we copy two large files from one directory to another using rsync.

Since the files are large, copying wil take some time. Although the rsync process is not finished, we can dettach from tmux session and close ssh connection. This will not affect rsync process.

After a while you can connect to the server and attach to this tmux session again. To attach to the tmux session, enter following command:

tmux attach-session -t [Session name]

As you can see, disconnecting from the server had no effect on rsync and the file copy process completed successfully in the background:

11. List tmux sessions

You can list all created tmux sessions by entering the following command:

tmux ls

The output will also show how many windows open in each session.

12. Kill tmux sessions

If you no longer need the created tmux session and want completely remove it, enter the following command

tmux kill-session -t [name]

To kill all created tmux sessions you can type the following command:

tmux kill-server

13. Customizing tmux shortcuts

By default, the tmux shortcut starts with the CTRL+B prefix, but you can change it to something more convenient for you. To do this, you need to create a tmux configuration file by entering the following command:

vi $HOME/.tmux.conf

Inside the file, you need to set the new prefix and remove the old one. In this example, we'll set the new prefix as CTRL+Q.

To set the prefix to the CTRL+Q, add following line to the configuration file:

set -g prefix C-q

To remove old prefix, add one more line:

unbind C-b

After that, save the file.

Please note: Be sure to reload the tmux.conf file after making changes to it. You can do this by entering the following command:

tmux source-file $HOME/.tmux.conf

Now all tmux shortcuts will start with CTRl+Q.

14. Using mouse to resize panes

If you want to use the mouse to resize panes, add the following line to your tmux configuration file and save your changes:

set -g mouse on

Now you can resize panes by using a mouse. You can point the mouse at the line between the panes, click and start moving it to the side, thereby changing the size of the panes.


Sign In or Register to comment.

Time4VPS

Learn how to install a web and database server, email, FTP client or other applications. Discover and share information on server security or optimization recommendations.
Feel free to join our constantly expanding community, participate in discussions, strengthen your knowledge on Linux and Windows server management!
© 2013 - 2024 Time4VPS. All rights reserved.

Get In Touch