قالب وردپرس درنا توس
Home / Tips and Tricks / How to use tmux on Linux (and why it's better than the screen)

How to use tmux on Linux (and why it's better than the screen)

  A stylized Linux terminal on a laptop with shell sessions in the background.
fatmawati achmad zaenuri / Shutterstock

The Linux command tmux is a terminal multiplexer, like the screen . Its advocates are many and correct, so we decided to compare the two. Is tmux really better, or is it just a matter of preferring what you know?

tmux vs. screen

Both tmux and GNU screen commands are terminal multiplexers. They let you have multiple windows in a single terminal window and jump back and forth between them. A window can be divided into boxes, each giving you an independent command line.

You can also delete a session and it becomes a headless device running in the background ̵

1; you can even close the terminal window that started it. When you are ready, you can open a new terminal window and reattach the remaining session. You can also do this via an SSH connection.

You can delete a session on a computer, go home and log in to the remote computer. When you reconnect, you can reattach the background port and use it interactively again.

What is the on-screen command?

The command screen is also a terminal multiplexer and it is full of options. To put down everything you can do with it, check out our in-depth article.

This time we will concentrate on tmux . As we continue, we will mention how the screen handles the same function or function.

Only one thing annoyed us with the screen . We cover it when we get to it and see if tmux performs better.

RELATED: How to use Linux screen command [19659008] Install tmux

While the screen is generally installed by default on popular Linux distributions, tmux is not. To install tmux on Ubuntu, type the following:

  sudo apt-get install tmux 

  sudo apt-get install tmux in a terminal window.

In Manjaro you can use pacman :

  sudo pacman -Sy tmux 

  sudo pacman -Sy tmux in a terminal window.

On Fedora 31, tmux is already installed.

Starting a tmux session

To start tmux just type it and press Enter:


  tmux in a terminal window.

The terminal window will display a status bar when you are in a tmux session.

 A new tmux session in a terminal window.

The right side of the status bar shows the host name and time and date. The left side shows the following session related information:

  • [0]: This is the session name. By default, they are numbered, starting with zero. We describe how you can give meaningful names to the sessions below.
  • 0: bash *: 0 indicates that this is the first window of this session. The only process running during this session is bash . If you run a program, the name is displayed here. The star (*) means this is the window you are looking at. Each time you create a new window in a tmux session, its window number and the name of the program running in it are added to the status field.

screen command does not give you a status bar. You have to fly blind and trust your mind to know what's going on, which requires some practice.

On the positive side, you will not lose a number of terminal window properties. Of course, you would normally expand your terminal window so that it is worth using a terminal multiplexer. In that case, the loss of a row for the status bar is not much of a problem. We have provided the images from the terminal windows here in standard size so that you can see the information.

Commands are given to tmux with keystrokes, and there are two parts to this. First, press Ctrl + B to get tmux 's attention. You then quickly press the next button to send a command to tmux . Commands are given by pressing letters, numbers, punctuation or arrow keys.

It is the same on the screen except you press Ctrl + A to get attention.

To close the window, press Ctrl + B and then quickly press X. The status bar turns yellow. You are then asked to confirm that you want to kill the window.

Press Y to close the window or N if you change your mind. You do not need to press Enter afterwards. Y or N is enough to register your choice.

 tmux session with an amber field and close this window yes or no quickly, in a terminal window.

If you press Y, the window will close. Because this is the only window in this session, the session ends.

 Command prompt after closing a tmux session in a terminal window

tmux session is closed and you returned to the command line from where you started tmux . You will see "[exited]" in the terminal window.

It may seem like it states the obvious, but it is a confirmation that you have closed the session and not left it disconnected and running. We will discuss removal sessions below.

Starting a Named tmux Session

If you regularly start several tmux sessions, you will quickly appreciate the feature of giving each of them a meaningful name. You can also name sessions on the screen but they do not appear anywhere in the session windows.

To start tmux with a session name, use the new (new session) command and the option -s (session name). Our session will be called "geek-1", so we write the following:

  tmux new -s geek-1 

  tmux new -s geek-1 in a terminal window.

When the session tmux is loaded, "geek-1" is displayed as the first entry in the status bar on the far left.

 A tmux session named "geek-1" to the left of the status bar.

Adding more Windows

To create a new window in the current session, press Ctrl + B and then C. You will get a blank terminal window in the current session. So we have something running in this new window, let's start the dmesg command with -w (follow) option:

  dmesg -w 

  dmesg -wi a terminal.

Now we have two windows in the session; one runs top and the other dmesg . However, we can only see one at a time (more on that in a moment).

 dmesg runs in window two of a tmux session, in a terminal window.

Take a look at the left side of the status bar. We are still in session "geek-1" tmux . In window zero, top is run and in window one is run dmesg . The star (*) after dmesg tells which window is visible.

To jump between windows, press Ctrl + B and then one of the following keys:

  • N : Show the next window.
  • P: Show previous window.
  • 0 to 9: Display a window number 0 to 9.

You can also select a window from a list. Pressing Ctrl + B and then W will display a list of windows.

 tmux window list displayed in a terminal window.

To move the amber height field, press Up or Down Arrows, Home or End. The lower section of the screen shows a preview of the contents of the selected window.

Press Enter to go to the selected window, or Esc to leave the window list without changing.

Deleting and Attaching Sessions

If you press Ctrl + B, then D, you will delete the session. It will continue to run in the background, but you will not be able to see or interact with it.

We started top during the session so we have a driving process to demonstrate with. Then we press Ctrl + B and then D. The session disappears and becomes a background session.

 tmux message after a session is deleted, in a terminal window.

We return to the original terminal window. There is a message from tmux stating that the session is linked. It also reminds us of the name we gave to the session. This is handy because it is what we use to associate with a background session and then restore it to an interactive session.

To attach a stand-alone session, we use the self-explanatory attach-session command with the option -t (target session). We will also enter the name of the session we want to remember.

We write the following:

  tmux attach-session -t geek-1 

  tmux attach-session -t geek-1 in a terminal window.

Our session will return and become a visible, interactive session again.

 A reset tmux session in a terminal window.

Any lengthy or continuous processes that you started before removing the session will still run in the background (unless they are ready) when you join the session.

screen can do this, but not so intuitively.

Managing Multiple Sessions [19659005] Let's open a new terminal window and start a new tmux session called "geek-2":

  tmux new -s geek-2 

  tmux new -s geek-2 in a terminal window.

During that session we start dmesg :

  dmesg -w 

  dmesg -w in a terminal window. [19659006] Now we have our original "geek-1" tmux session, and a new one is called "geek-2."

 tmux session geek-2 runs dmesg in a terminal window.

The status bar shows that this session is called "geek-2", and it has a window that runs dmesg .

If we press Ctrl + B, and then D, we disconnect that session.

 Free-standing tmux session geek-2 in a terminal widnow.

Back in session "geek-1" tmux we press Ctrl + B and then S to see a list of tmux sessions.

 list of tmux sessions displayed in a terminal window.

To be clear, this is a list of sessions. The similar screen we saw earlier was a list of windows in a single session.

You can move the amber highlight bar by pressing the up and down arrows, home and end. The lower section shows a preview of the contents of the selected session.

 list of tmux sessions displayed in a terminal window.

If you press the right arrow, the windows for the selected session are displayed.

 tmux session list of window details is displayed in a terminal window.

Press Enter to go to the selected session or window or Esc to exit the session list without changing sessions. If you select a new session, your current one will be disconnected and the one you selected will be attached.

We removed the "geek-2" session before doing so. However, you can do this with sessions that are still linked to their original terminal windows. When you do so, all screen changes will be displayed simultaneously in both tmux sessions.

screen command can also do this via a similar set of commands.

Working with window panes

If you press Ctrl + B and then double-quote (""), you divide the window horizontally into two panes.

 tmux session with horizontal panes in a terminal window.

This only affects the current window; the others in the session will not change. We have used the command tmux ls in the upper pane to list the windows in this session. There are two, and the status bar tells us we are in window one. If we jump to window zero by pressing Ctrl + B, and then 0 (zero), we see that it is just as we left it.

These are two independent command lines, not two views in a window; they are distinct and separate shells. We can show this by running a different command in each box.

We write the following:

uname -a

ls -hl

To move from one box to another, press Ctrl + B and then either up, down, left or right arrow.

 Two different commands in two boxes in a tmux session in a terminal widow. [19659006] If you press Ctrl + B and then the percent sign (%) it divides the current box vertically.

<img class = "alignnone wp-image-671897 size-full" data-pagespeed-lazy-src = "https://www.howtogeek.com/wp-content/uploads/2020/05/x15.png .pagespeed.gp + jp + jw + pj + ws + js + rj + rp + rw + ri + cp + md.ic.TRIiGXl9UU.png "alt =" tmux session with vertical and horizontal shared panes in a terminal window.

” width=”646″ height=”382″ src=”/pagespeed_static/1.JiBnMqyl6S.gif” onload=”pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);” onerror=”this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);”/>

Press Ctrl + B and then Q to make tmux briefly flash the number on each box.

 tmux showing box number in a terminal window. [19659006] These numbers are used in instructions and messages from tmux . Press Ctrl + B and then X to close the current window. The status bar changes to amber and you are asked to confirm that you want to close that box number. Press Y to remove the box, or N to leave things as they are.

 tmux that is prompted to delete a box in a terminal window.

Pressing Y will delete the route. [19659006]   tmux with two horizontal panes in a terminal window.

The command also has panes, but again they are less intuitive to use. What annoys us with the screen is that if you delete a session with boxes, they disappear when you reset that session. This gets old very fast.

A Ctrl + B Cheat Sheet

We have included a cheat sheet with the various commands you can use in tmux below.

Session Commands

  • S: List sessions.
  • $: Rename current session.
  • D: Release current session.
  • Ctrl + B, and then ?: View Help Page in tmux .

Window Commands

  • C: Create a new window.
  • ,: Rename the current window.
  • W: List the windows.
  • N: Go to the next window.
  • P: Move to previous window.
  • 0 to 9: Move to the specified window.

Box Commands

  • %: Create a horizontal split.
  • “: Create a vertical split.
  • H or left arrow: Move to the box on the left.
  • In or right arrow: Move to the box on the right.
  • J o r Arrow down: Move to the box below.
  • K or up arrow: Move to the box above.
  • Q : Show short box number.
  • O: Move the boxes in order. Each push takes you to the next until you go through all of them.
  • }: Change the position of the current box with the next one.
  • {: Change the location of the current box. with the previous.
  • X: Close this box.

How they compare

In terms of functionality, the screen and tmux work in the same way and offer the same main functions. This is how you get access to the features that are significantly different. tmux offers narrower and more convenient ways to access the various functions. However, that is not the only difference.

The ability to rename sessions and windows in tmux is neat, and the fact that it retains the boxes when you reset a session is a game changer.

screen on the other hand, you completely lose boxes when you disconnect and reset a session. This is almost annoying enough to avoid loosening in the first place.

There is so much more to tmux including its incredibly flexible scripting features. You owe it to yourself to check it out.

Source link