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:

How to Use tmux

Starting a Session
Start a new session by typing the command:

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:

Example:

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:

For example, to set the width to 20 characters:

Navigation

Creating a new window in a session:

Switching between windows in a session:

The * character indicates the current window.

Splitting the screen into horizontal panes:

Splitting the screen into vertical panes:

Switching between panes:

Detach and Attach
Detach from a session without terminating it:

Reattach to a session:

Stopping a Session
Display a list of running sessions:

Stop a session, either by ID or 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:

Add the following script:

zsh shell
Open the .zshrc file:

Add the following script:

fish shell
Open the fish shell configuration file config.fish:

Add the following script:

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

Leave a Reply

Your email address will not be published. Required fields are marked *