Running Multiple Terminal Sessions with Tmux
As a Linux sysadmin, we often deal with multiple terminals and tasks that need to be managed simultaneously. This is where tmux comes to the rescue. tmux (Terminal Multiplexer) is a tool that allows us to open multiple terminal sessions in a single terminal window. This means we can run various processes in different sessions without having to open multiple terminal windows. tmux is very useful because it has the ability to retain sessions even if the connection is lost, so we won’t lose the running processes.
Advantages of tmux:
- Multiplexing: It can create multiple sessions within a single window, and each session can have multiple windows and panes. This is very helpful for managing various tasks or servers simultaneously.
- Persistence: It can keep sessions alive even after logging out. This is very useful for running long-term processes without worrying about losing the session.
- Flexibility: It allows easy switching between windows and panes, making task management more efficient.
Install tmux
To install tmux, you can use the package manager appropriate for your Linux distribution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | # Debian based sudo apt install tmux # RHEL based sudo dnf install tmux # Arch Linux sudo pacman -S tmux # openSUSE sudo zypper install tmux # Alpine Linux sudo apk add tmux |
How to Use tmux
Starting a Session
Start a new session by typing the command:
1 | tmux |
The above command will create a new session with an ID represented by a number.
To assign a name to the session for easier recognition, use the -s
option:
1 | tmux new -s session-name |
Example:
1 | tmux new -s install-aapanel |
If the session name is too long, it will be truncated. You can set the width of the session name display by creating a tmux configuration file:
1 | nano ~/.tmux.conf |
For example, to set the width to 20 characters:
1 | set -g status-left-length 20 |
Navigation
Creating a new window in a session:
1 | ctrl + b, then c |
Switching between windows in a session:
1 | ctrl + b, then window ID (e.g, 0, 1, 2, etc) |
The *
character indicates the current window.
Splitting the screen into horizontal panes:
1 | ctrl + b, then % |
Splitting the screen into vertical panes:
1 | ctrl + b, then " |
Switching between panes:
1 | ctrl + b, then use the arrow keys |
Detach and Attach
Detach from a session without terminating it:
1 | ctrl + b, then d |
Reattach to a session:
1 | tmux attach -t session-name |
Stopping a Session
Display a list of running sessions:
1 | tmux ls |
Stop a session, either by ID or name:
1 2 | tmux kill-session -t session-ID tmux kill-session -t session-name |
Autorun Tmux
If you want to automatically start tmux
when accessing the shell, you need to add a script to the configuration file of each shell you use.
bash shell
Open the .bashrc file:
1 | nano ~/.bashrc |
Add the following script:
1 2 3 | if command -v tmux >/dev/null 2>&1; then [ -z "$TMUX" ] && exec tmux fi |
zsh shell
Open the .zshrc file:
1 | nano ~/.zshrc |
Add the following script:
1 2 3 | if [[ -z "$TMUX" ]]; then tmux attach-session -t default || tmux new-session -s default fi |
fish shell
Open the fish shell configuration file config.fish:
1 | nano ~/.config/fish/config.fish |
Add the following script:
1 2 3 4 5 | if status is-interactive if not set -q TMUX tmux end end |
tmux is an incredibly useful tool for any Linux sysadmin who needs to manage multiple tasks and terminals efficiently. With features like multiplexing, persistence, and high flexibility, tmux is a top choice for terminal management.
If you found this article helpful and would like to support my work, consider making a donation through PayPal. Your support helps me continue creating useful content and tutorials. Thank you!
Donate via PayPal: https://paypal.me/musaamin