Tmux is a terminal multiplexer. It allows you to run and manage several command prompts simultaneously from one tmux session.
Tmux uses a client/server model, which allows it to persist connections. You can start a session from one computer, run several programs running in it, and then disconnect. You can later reconnect to the same session from a different computer and the same programs will be running.
On Debian and Ubuntu systems, install tmux with apt.
$ sudo apt-get update && sudo apt-get -y install tmux
On Redhat, CentOS, and other RHEL-based systems, install tmux with yum.
$ sudo yum install update && sudo yum -y install tmux
$ tmux new -s start
You will see a bar at the bottom of the screen with the session name tmux created, start, in brackets. This is because tmux automatically logs in with your user account when it creates new panes.
Once inside a tmux session, you use a prefix key to trigger commands to tell tmux what to do. The default prefix key is CTRL + B. For example, if you want to tell tmux to create a new pane by splitting your screen into two vertical sections, you first type CTRL + B, then %.
If you typed CTRL + B, then % as shown above, then you have two panes on your screen.
To switch between different open panes, use the arrow keys with the prefix.
tmux list-windows
, then press ENTER to confirm that you have three windows opened.tmux attach -t YOUR_SESSION_NAME
.tmux list-sessions
– List existing tmux sessions.tmux new -s session-name
– Create a new tmux session named session-name.tmux attach -t session-name
– Connect to an existing tmux session named session-name.tmux switch -t session-name
– Switches to an existing tmux session named session-name.Instead of using the arrow keys to switch between different panes, you can use the Vim keys H, J, K, L. To do this, update the tmux configuration file ~/.tmux.conf
. If this file doesn’t exist, create it.
Add these lines (the first line is a comment) to the configuration file to enable pane navigation using Vim keys:
# hjkl pane traversal
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
After updating the configuration file, restart tmux. You can load the new configuration without restarting tmux by sourcing the updated configuration file:
tmux source-file ~/.tmux.conf
Many of the key bindings in the following sections are based on the Meta key. On most modern computers and keyboards, the Meta key is not one specific key. Depending on the system settings and the terminal emulator in use, the Meta key could be bound to either ESC, ALT, COMMAND, WIN, or Opt
. Where you see META as part of a key binding, try using the ESC key. If it doesn’t work, try one of the other keys mentioned above.
When a window has multiple panes, you can change the size of individual panes.
The above key combinations resize the pane “continuously” as long as they are held pressed. To resize the pane one step at a time:
Whether a particular key combination (above) increases or decreases the dimensions of the pane depends on the positioning of that pane relative to other panes.
If the terminal emulator supports mouse use, you can resize panes by dragging their borders.
Tmux has five different preset layouts for arranging multiple panes in a window:
Start tmux and create 4 (or any number of) panes.
To cycle through the different preset layouts use CTRL + B, then SPACE
You can also choose a specific layout:
When a window has multiple panes, you can move the panes around and swap their positions.
Note: As described previously, CTRL + B, then O cycles the cursor through the different panes. The above sequence is for cycling the positions of the panes themselves.
In a layout with multiple panes in a window, it is sometimes necessary to move a pane to a different location. Swapping pane positions (as described above) isn’t always sufficient, because it disturbs the positions of all the other panes. It is also sometimes necessary to move a pane to a different window in the same session.
Use the command break-pane
in the tmux command prompt to detach a pane from the window. join-pane
reattaches the pane.
To access the tmux command prompt, press the prefix CTRL + B, then press :. This will open up a prompt at the bottom of the terminal. To exit it without entering any commands, press CTRL + C.
To detach a pane, switch to the pane to be detached, activate the tmux command prompt (CTRL + B, then :), and enter the command:
break-pane -dP
This will detach the active pane and show its “ID”. This ID is of the format X:Y.Z
where X, Y, and Z are numbers. The previous pane becomes the new active pane. The ID is shown on top of the (new) active pane. Note down this ID and then dismiss it by pressing ESC.
The detached pane can be reattached next to any pane in any window in the same session. Switch to the pane next to which you want to reattach the detached pane. Activate the tmux command prompt and enter:
join-pane -vs X:Y.Z
This reattaches the detached pane below the active pane (vs
stands for vertical split). To reattach the detached pane on the right side of the active pane:
join-pane -hs X:Y.Z
In the above command, hs
stands for horizontal split.
When you start tmux, it creates a new server instance and creates a session within that server.
Tmux clients and servers are separate processes. They communicate via a UNIX socket. By default, tmux stores sockets in the /tmp
directory. Sockets for servers created by an individual user are stored under a subdirectory named tmux-UID
where UID
is the UNIX UID of that user. A user whose UID
is 1001 will have its sockets stored in /tmp/tmux-1001
. The owner of this subdirectory is user1
. By default, no other user or group can access this subdirectory.
The steps described in this section have been tested on tmux versions 3.1 and 3.3 on a vanilla Debian 11 installation. They should be applicable to all recent tmux versions.
Note: On security-hardened Operating Systems, such as those involving SELinux, changing groups and permissions can need additional steps. Discussing those is beyond the scope of this guide. It is assumed you know how to change groups and permissions on your Operating System.
Tmux version 3.3 (released in June 2022) introduced security related changes in the way session sharing works. These changes involve the use of the newly added server-access
command. Check the version of your tmux installation using tmux -V
. In practice, this leads to one additional step for tmux versions 3.3 and higher.
Consider a system with two users: user1
with UID 1001 and user2
with UID 1002.
In order for user2
to connect to tmux sessions created by user1
, user2
) must have access to the socket of the server created by user1
so that clients created by user2
can communicate with the server created by user1
, and (for versions 3.3 and higher) have access to the server, granted by user1
with the server-access
command.
In following the steps below, use the appropriate user IDs and usernames based on your own system. Check the UID (and other details) of a user user1
:
$ id user1
The command id
without any parameters shows the UID (and other details) of the current user.
If there are no additional user accounts on your system, create a new user before testing these steps.
The steps below describe how user1
can grant access to its tmux sessions to user2
.
user1
.$ tmux
This will create the socket (sub)directory if it did not previously exist.$ ls -hdl /tmp/tmux-1001
user1
and user2
, this will make it easier to manage permissions. Create a new group tmuxusers
:# groupadd tmuxusers
user1
and user2
to this group:# usermod -a -G tmuxusers user1 # usermod -a -G tmuxusers user2
/tmp/tmux-1001
directory to tmuxusers
:# chgrp -R tmuxusers /tmp/tmux-1001
user2
needs full access to the sockets of tmux servers started by user1
. Give the group full permissions on the directory:# chmod -R g+rwx /tmp/tmux-1001
tmuxusers
and that the group has full permissions on the directory:$ ls -hdl /tmp/tmux-1001
$ exit
user1
(again), create a new tmux session and specify the name of the socket:$ tmux -L socket1
-L
option creates a named socket socket1
, with the path /tmp/tmux-1001/socket1
.user2
access to the tmux server:$ tmux server-access -w -a user2
user2
write access to the session. So, user2
can also enter commands in the session. It is possible to give user2
only read access. With read access, user2
can only view the contents of the session but cannot enter any commands. To give user2
read access, use -r
instead of -w
in the above command:$ tmux server-access -r -a user2
user1
can revoke the permissions granted to user2
:$ tmux server-access -d user2
# chgrp tmuxusers /tmp/tmux-1001/socket1
tmuxusers
and that group has access to the file:# ls -l /tmp/tmux-1001/socket1
$ tmux ls
user2
. Start tmux and specify the path of the named socket created by user1
:$ tmux -S /tmp/tmux-1001/socket1 ls
The above command shows the list of sessions on the server started by user1
and listening on socket1
.$ tmux -S /tmp/tmux-1001/socket1
$ tmux -S /tmp/tmux-1001/socket1 attach -t N
user2
is now connected to the tmux session of user1
.
In general, using nested sessions in tmux is neither necessary nor recommended. It is, however, useful when connecting to a remote tmux session from within a local tmux session. Suppose you are in a tmux session on the local computer. In one of the panes (or windows), you log in to a remote server which also has running tmux sessions. When you connect to a tmux session on this remote server, you end up with nested sessions. The remote tmux session is nested inside the local tmux session.
To pass a key sequence to the nested session, consecutively press the prefix key combination for both sessions. Assuming that both sessions use the default prefix key, press CTRL+B twice to access the nested session.
For example, to do a vertical split in the nested session, enter: CTRL+B, followed by (again), CTRL+B, then %.