Monday, February 5, 2018

tmux – A Powerful Terminal Multiplexer For Heavy Command-Line Linux User

https://www.2daygeek.com/tmux-a-powerful-terminal-multiplexer-emulator-for-linux


tmux stands for terminal multiplexer, it allows users to create/enable multiple terminals (vertical & horizontal) in single window, this can be accessed and controlled easily from single window when you are working with different issues.
It uses a client-server model, which allows you to share sessions between users, also you can attach terminals to a tmux session back. We can easily move or rearrange the virtual console as per the need. Terminal sessions can freely rebound from one virtual console to another.
tmux depends on libevent and ncurses libraries. tmux offers status-line at the bottom of the screen which display information about your current tmux session such as current window number, window name, username, hostname, current time, and current date.
When tmux is started it creates a new session with a single window and displays it on screen. It allows users to create Any number of windows in the same session.
Many of us says it’s similar to screen but i’m not since this offers wide range of configuration options.
Make a note: Ctrl+b is the default prefix in tmux so, to perform any action in tumx, you have to type the prefix first then required options.

tmux Features

  • Create any number of windows
  • Create any number of panes in the single window
  • It allows vertical and horizontal splits
  • Detach and Re-attach window
  • Server-client architecture which allows users to share sessions between users
  • tmux offers wide range of configuration hacks

How to Install tmux Command

tmux command is pre-installed by default in most of the Linux systems. If no, follow the below procedure to get installed.
For Debian/Ubuntu, use APT-GET Command or APT Command to install tmux.
$ sudo apt install tmux
For RHEL/CentOS, use YUM Command to install tmux.
$ sudo yum install tmux
For Fedora, use DNF Command to install tmux.
$ sudo dnf install tmux
For Arch Linux, use Pacman Command to install tmux.
$ sudo pacman -S tmux
For openSUSE, use Zypper Command to install tmux.
$ sudo zypper in tmux

How to Use tmux

kick start the tmux session by running following command on terminal. When tmux is started it creates a new session with a single window and will automatically login to your default shell with your user account.
$ tmux

You will get similar to above screenshot like us. tmux comes with status bar which display an information’s about current sessions details, date, time, etc.,.
The status bar information’s are below:
  • 0 : It is indicating the session number which was created by the tmux server. By default it starts with 0.
  • 0:username@host: : 0 is indicating the session number. Username and Hostname which is holding the current window.
  • ~ : It is indicating the current directory (We are in the Home directory)
  • * : This indicate that the window is active now.
  • Hostname : This shows fully qualified hostname of the server
  • Date & Time: It shows current date and time

How to Split Window

tmux allows users to split window vertically and horizontally. Let’s see how to do that.
Press (Ctrl+b), % to split the pane vertically.

Press (Ctrl+b), " to split the pane horizontally.

How to Move Between Panes

Lets say, we have created few panes and want to move between them. How to do that? If you don’t know how to do, then there is no purpose to use tmux. Use the following control keys to perform the actions. There are many ways to move between panes.
Press (Ctrl+b), Left arrow – To Move Left
Press (Ctrl+b), Right arrow – To Move Right
Press (Ctrl+b), Up arrow – To Move Up
Press (Ctrl+b), Down arrow – To Move Down
Press (Ctrl+b), { – To Move Left
Press (Ctrl+b), } – To Move Right
Press (Ctrl+b), o – Switch to next pane (left-to-right, top-down)
Press (Ctrl+b), ; – Move to the previously active pane.
For testing purpose, we are going to move between panes. Now, we are in the pane2 which shows lsb_release -a command output.

And we are going to move to pane0 which shows uname -a command output.

How to Open/Create New Window

You can open any number of windows within one terminal. Terminal window can be split vertically & horizontally which is called panes. Each pane will contain its own, independently running terminal instance.
Press (Ctrl+b), c to create a new window.
Press (Ctrl+b), n move to the next window.
Press (Ctrl+b), p to move to the previous window.
Press (Ctrl+b), (0-9) to immediately move to a specific window.
Press (Ctrl+b), l Move to the previously selected window.
I have two windows, first window has three panes which contains operating system distribution information, top command output & kernal information.

And second window has two panes which contains Linux distributions logo information. Use the following commands perform the action.

Press (Ctrl+b), w Choose the current window interactively.

How to Zoom Panes

You are working in some pane which is very small and you want to zoom it out for further work. To do use the following key binds.
Currently we have three panes and i’m working in pane1 which shows system activity using Top command and am going to zoom that.

When you zoom a pane, it will hide all other panes and display only the zoomed pane in the window.

Press (Ctrl+b), z to zoom the pane and press it again, to bring the zoomed pane back.

Display Pane Information

To know about pane number and it’s size, run the following command.
Press (Ctrl+b), q to briefly display pane indexes.

Display Window Information

To know about window number, layout size, number of panes associated with the window and it’s size, etc., run the following command.
Just run tmux list-windows to view window information.

How to Resize Panes

You may want to resize the panes to fit your requirement. You have to press (Ctrl+b), : then type the following details on the yellow color bar in the bottom of the page.

In the previous section we have print pane index which shows panes size as well. To test this we are going to increase 10 cells UPward. See the following output that has increased the pane1 & pane2 size from 55x21 to 55x31.

Syntax: (Ctrl+b), : then type resize-pane [options] [cells size]
(Ctrl+b), : then type resize-pane -D 10 to resize the current pane Down for 10 cells.
(Ctrl+b), : then type resize-pane -U 10 to resize the current pane UPward for 10 cells.
(Ctrl+b), : then type resize-pane -L 10 to resize the current pane Left for 10 cells.
(Ctrl+b), : then type resize-pane -R 10 to resize the current pane Right for 10 cells.

Detaching and Re-attaching tmux Session

One of the most powerful features of tmux is the ability to detach and reattach session whenever you need.
Run a long running process and press Ctrl+b followed by d to detach your tmux session safely by leaving the running process.
Now, run a long running process. For demonstration purpose, we are going to move this server backup to another remote server for disaster recovery (DR) purpose.
$ rsync -avzhe ssh /backup root@192.168.0.161:/backups/week-1/
You will get similar output like below after detached tmux session.
[detached (from session 0)]
Run the following command to list the available tmux sessions.
$ tmux ls
0: 3 windows (created Tue Jan 30 06:17:47 2018) [109x45]
Now, re-attach the tmux session using an appropriate session ID as follow.
$ tmux attach -t 0

How to Close Panes & Window

Just type exit or hit Ctrl-d in the corresponding pane to close it. It’s similar to terminal close. To close window, press (Ctrl+b), &.

No comments:

Post a Comment