tmux在linux的使用_如何在Linux上使用tmux(以及为什么它比屏幕更好)

tmux和screen都是Linux终端多路复用器,允许在一个窗口中管理多个命令行界面。本文对比了两者,指出tmux的命名会话、保留窗格等优势,并详细介绍了tmux的安装、使用方法,包括创建会话、添加窗口、窗格操作、分离与附加会话等,适合Linux用户提升效率。
摘要由CSDN通过智能技术生成
tmux在linux的使用

tmux在linux的使用

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

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

Linux tmux命令是终端多路复用器,screen 。 它的拥护者很多,也很积极,因此我们决定将两者进行比较。 tmux真的更好吗,还是只是更喜欢您所知道的?

tmux与屏幕 (tmux vs. screen)

Both the tmux and GNU screen commands are terminal multiplexers. They allow you to have multiple windows within a single terminal window, and to jump back and forth between them. A window can be divided into panes, each of which gives you an independent command line.

tmux和GNU screen命令都是终端多路复用器。 它们使您可以在一个终端窗口中拥有多个窗口,并在它们之间来回跳转。 窗口可以分为多个窗格,每个窗格都为您提供一个独立的命令行。

You can also detach a session and it becomes a headless entity running in the background—you can even close the terminal window that launched it. When you’re ready, you can open a new terminal window and reattach the still-running session. You can also do this over an SSH connection.

您还可以分离会话,它变成在后台运行的无头实体,甚至可以关闭启动该会话的终端窗口。 准备就绪后,您可以打开一个新的终端窗口并重新连接仍在运行的会话。 您也可以通过SSH连接执行此操作。

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

您可以在一台计算机上分离会话,回家然后登录到远程计算机。 重新连接后,您可以重新连接后台会话,然后再次以交互方式使用它。

屏幕命令是什么? (What’s the screen Command?)

The screen command is also a terminal multiplexer, and it’s packed with options. For the lowdown on everything you can do with it, check out our in-depth article.

screen命令也是终端多路复用器,并且带有选项。 有关您可以使用它的所有功能的最低要求,请查看我们的深入文章

This time, we’re going to concentrate on tmux. As we go along, we’ll mention how screen handles the same feature or function.

这次,我们将专注于tmux 。 在进行过程中,我们将提到screen如何处理相同的功能。

Only one thing annoyed us about screen. We’ll cover that when we get to it, and see if tmux fares any better.

关于screen只有一件事使我们恼火。 我们将在开始时进行介绍,并查看tmux票价是否更好。

安装tmux (Installing tmux)

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

虽然通常在流行Linux发行版中默认安装screen ,但tmux却没有。 要在Ubuntu上安装tmux ,请输入以下命令:

sudo apt-get install tmux
sudo apt-get install tmux in a terminal window.

On Manjaro you can use pacman:

在Manjaro上,您可以使用pacman

sudo pacman -Sy tmux
sudo pacman -Sy tmux in a terminal window.

On Fedora 31, tmux is already installed.

在Fedora 31上,已经安装了tmux

开始一个tmux会话 (Starting a tmux Session)

To start tmux, just type it and hit Enter:

要启动tmux ,只需键入它并按Enter:

tmux
tmux in a terminal window.

The terminal window will show a status bar when you’re in a tmux session.

当您处于tmux会话中时,终端窗口将显示状态栏。

A new tmux session in a terminal window.

The right side of the status bar shows the hostname, and the time and date. The left side shows the following session-related information:

状态栏的右侧显示主机名以及时间和日期。 左侧显示以下与会话相关的信息:

  • [0]: This is the session name. By default, they’re numbered, starting with zero. We cover how you can give meaningful names to sessions below.

    [0]:这是会话名称。 默认情况下,它们是从零开始编号的。 我们将在下面介绍如何为会议指定有意义的名称。

  • 0:bash*: The 0 indicates this is the first window in this session. The only process running in this session is bash. If you run a program, its name will appear here. The asterisk (*) means this is the window you’re 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 bar.

    0:bash *: 0表示这是此会话中的第一个窗口。 在此会话中运行的唯一进程是bash 。 如果您运行程序,其名称将显示在此处。 星号(*)表示这是您正在查看的窗口。 每次在tmux会话中创建新窗口时,其窗口号和在其中运行的程序的名称都会添加到状态栏中。

The screen command doesn’t give you a status bar. You have to fly blind and rely on your wits to know what’s going on, which takes a bit of practice.

screen命令没有screen状态栏。 您必须盲目飞行,并依靠自己的智慧来了解发生了什么,这需要一些练习。

On the plus side, you won’t lose a line of terminal window real estate. Of course, you’d normally expand your terminal window to make using a terminal multiplexer worthwhile. In that case, the loss of one line for the status bar isn’t much of an issue. We’ve left the images of the terminal windows here at the default size so you can see the information.

从好的方面来说,您不会丢失任何一排终端窗口房地产。 当然,通常您会扩大终端窗口,以使用终端多路复用器值得。 在这种情况下,状态栏的一行丢失不是什么大问题。 我们将终端窗口的图像保留为默认大小,以便您查看信息。

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

使用击键将命令发送给tmux ,这有两部分。 首先,按Ctrl + B引起tmux的注意。 然后,您快速按下一个键向tmux发送命令。 通过按字母,数字,标点符号或箭头键来发出命令。

It’s the same in screen, except you press Ctrl+A to get its attention.

screen上是一样的,除了您按Ctrl + A引起注意。

To close the window, press Ctrl+B, and then quickly hit X. The status bar turns amber. You’re then prompted to confirm you want to kill the window.

要关闭窗口,请按Ctrl + B,然后快速按X。状态栏变为琥珀色。 然后提示您确认要杀死该窗口。

Press Y to close the window or N if you change your mind. You don’t have to press Enter afterward; Y or N is enough to register your choice.

如果您改变主意,请按Y关闭窗口,或按N。 之后,您不必再按Enter键。 Y或N足以注册您的选择。

tmux session with an amber status bar and close this window yes or no prompt, in a terminal window.

If you press Y, the window closes. Because this is the only window in this session, the session is terminated.

如果按Y,则窗口关闭。 因为这是该会话中的唯一窗口,所以会话终止。

Command prompt after closing a tmux session in a terminal window

The tmux session is closed and you’re returned to the command line from which you launched tmux. You’ll see “[exited]” in the terminal window.

tmux会话已关闭,您将返回到启动tmux的命令行。 您将在终端窗口中看到“ [退出]”。

This might seem like it’s stating the obvious, but it’s a confirmation you’ve closed the session and not left it detached and running. We’ll discuss detaching sessions below.

看起来似乎很明显,但这是在确认您已关闭会话,而不是将其分离并运行。 我们将在下面讨论分离会议。

开始一个命名的tmux会话 (Starting a Named tmux Session)

If you regularly start multiple tmux sessions, you’ll quickly appreciate the functionality of giving each of them a meaningful name. You can name sessions in screen, too, but they’re not displayed anywhere in the session windows.

如果您定期启动多个tmux会话,您将很快体会为每个名称赋予一个有意义的名称的功能。 您也可以在screen命名会话,但是会话窗口中的任何地方都不会显示它们。

To start tmux with a session name, use the new (new session) command, and the -s (session name) option. Our session is going to be called “geek-1,” so we type the following:

要以会话名称启动tmux ,请使用new (新会话)命令和-s (会话名称)选项。 我们的会议将称为“ geek-1”,因此我们键入以下内容:

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

When the tmux session loads, “geek-1” is displayed as the first entry in the status bar, at the far left.

tmux会话加载时,“ geek-1”显示为状态栏中最左侧的第一个条目。

A tmux session with the name "geek-1" on the left side of the status bar.

添加更多窗口 (Adding More Windows)

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

要在当前会话中创建一个新窗口,请按Ctrl + B,然后按C。您将在当前会话中获得一个空白的终端窗口。 因此,我们将在新窗口中运行一些内容,让我们使用-w (follow)选项启动dmesg命令

dmesg -w
dmesg -w in a terminal window.

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

现在,我们在会话中有两个窗口; 一个运行在top ,另一个运行在dmesg 。 不过,我们一次只能看到一个(稍后会更多)。

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

Take a look at the left side of the status bar. We’re still in the “geek-1” tmux session. In window zero, top is running, and in window one, dmesg is running. The asterisk (*) after dmesg tells us which window is visible.

看一下状态栏的左侧。 我们仍在“ geek-1” tmux会话中。 在零窗口中,top正在运行,而在第一窗口中, dmesg正在运行。 dmesg之后的星号(*)告诉我们哪个窗口可见。

To hop between windows, press Ctrl+B, and then one of the followings keys:

要在窗口之间切换,请按Ctrl + B,然后按以下键之一:

  • N: Display the next window.

    N 显示下一个窗口。

  • P: Display the previous window.

    P:显示上一个窗口。

  • 0 to 9: Display a window numbered 0 to 9.

    0到9:显示编号为0到9的窗口。

You can also choose a window from a list. If you press Ctrl+B, and then W, a list of windows appears.

您也可以从列表中选择一个窗口。 如果先按Ctrl + B,再按W,将显示一个窗口列表。

tmux window list displayed in a terminal window.

To move the amber highlight bar, press the Up or Down Arrows, Home, or End. The bottom section of the display shows a preview of the content in the highlighted window.

要移动琥珀色的高亮条,请按向上或向下箭头,主页或结束。 显示屏的底部在突出显示的窗口中显示内容的预览。

Press Enter to move to the highlighted window, or Esc to leave the window list without switching.

按Enter键移至突出显示的窗口,或按Esc键退出窗口列表而不进行切换。

分离和附加会话 (Detaching and Attaching Sessions)

If you press Ctrl+B, and then D, you will detach the session. It will continue to run in the background, but you won’t be able to see or interact with it.

如果按Ctrl + B,然后按D,则将分离会话。 它会继续在后台运行,但是您将无法看到它或与之交互。

We’ve started top in the session so we have a running process to demonstrate with. Then, we press Ctrl+B, and then D. The session disappears and becomes a background session.

我们已经开始top在会话,所以我们有一个正在运行的进程与演示。 然后,我们按Ctrl + B,然后按D。会话消失并成为后台会话。

tmux message following detching a session, in a terminal window.

We return to the original terminal window. There’s a message from tmux telling us the session is detached. It also reminds us of the name we gave to the session. This is handy because that’s what we use to attach to a background session, and then restore it to an interactive one.

我们返回到原始终端窗口。 来自tmux的消息告诉我们会话已分离。 这也使我们想起了会议的名字。 这很方便,因为这是我们用来附加到后台会话,然后将其还原为交互式会话的方法。

To attach a detached session, we’ll use the self-explanatory attach-session command with the -t (target session) option. We’ll also provide the name of the session we wish to recall.

要附加一个分离的会话,我们将使用带有-t (目标会话)选项的不言自明的attach-session命令。 我们还将提供我们希望召回的会议的名称。

We type the following:

我们输入以下内容:

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

Our session returns and becomes a visible, interactive session again.

我们的会话返回并再次成为可见的交互式会话。

A restored tmux session in a terminal window.

Any long-running or continual processes you launched before detaching the session will still be running in the background (unless they’ve finished) when you attach the session.

当您附加会话时,在分离会话之前启动的所有长时间运行或连续的进程仍将在后台运行(除非它们已完成)。

screen can do this, but not as intuitively.

screen可以做到这一点,但是不那么直观。

处理多个会话 (Handling Multiple Sessions)

Let’s open another terminal window, and start a new tmux session called “geek-2”:

让我们打开另一个终端窗口,然后启动一个新的名为“ geek-2”的tmux会话:

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

In that session, we’ll start dmesg:

在该会话中,我们将开始dmesg

dmesg -w
dmesg -w in a terminal window.

Now, we’ve got our original “geek-1” tmux session, and a new one called “geek-2.”

现在,我们有了原始的“ geek-1” tmux会话,以及一个名为“ geek-2”的新会话。

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

The status bar shows us this session is called “geek-2”, and it has one window that running dmesg.

状态栏向我们显示此会话称为“ geek-2”,它有一个运行dmesg窗口。

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

如果我们按Ctrl + B,然后再按D,则将分离该会话。

Detached tmux session geek-2 in a terminal widnow.

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

回到“ geek-1” tmux会话中,我们按Ctrl + B,然后按S键以查看tmux会话列表。

list of tmux sessions displayed in a terminal window.

To be clear, this is a list of sessions. The similar display 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 bottom section displays a preview of the content in the highlighted session.

您可以通过按上下箭头,“主页”和“结束”来移动琥珀色高亮条。 底部显示突出显示的会话中内容的预览。

list of tmux sessions displayed in a terminal window.

If you press the Right Arrow, the windows for the highlighted session are displayed.

如果按向右箭头,则会显示突出显示的会话的窗口。

tmux session list with window detail displayed in a terminal window.

Press Enter to move to the highlighted session or window or Esc to leave the session list without changing sessions. If you select a new session, your current one detaches, and the one you selected is attached.

按Enter键移至突出显示的会话或窗口,或按Esc退出会话列表而不更改会话。 如果选择一个新会话,则当前会话将分离,并附加您选择的会话。

We detached the “geek-2” session before we did this. However, you can do this with sessions that are still attached to their original terminal windows. When you do, any screen changes will appear simultaneously in both tmux sessions.

在执行此操作之前,我们分离了“ geek-2”会话。 但是,您可以使用仍附加到其原始终端窗口的会话来执行此操作。 完成后,任何屏幕更改都会在两个tmux会话中同时出现。

The screen command can do this, too, via a similar set of commands.

screen命令也可以通过一组类似的命令来执行此操作。

使用窗玻璃 (Working with Window Panes)

If you press Ctrl+B, and then double quotation marks (“”), you split the window horizontally into two panes.

如果按Ctrl + B,然后再双引号(“”),则将窗口水平拆分为两个窗格。

tmux session with horizontal panes in a terminal window.

This only affects the current window; the others in the session won’t be changed. We’ve used the tmux ls command in the top pane to list the windows in this session. There are two, and the status line tells us we’re in window one. If we hop over to window zero by pressing Ctrl+B, and then 0 (zero), we see it is just as we left it.

这只会影响当前窗口。 会话中的其他人将不会更改。 我们在顶部窗格中使用了tmux ls命令来列出此会话中的窗口。 有两个,状态行告诉我们我们在第一个窗口中。 如果我们先按Ctrl + B跳至零窗口,然后再按0(零),我们将看到它与我们离开时一样。

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

这是两个独立的命令行,而不是一个窗口中的两个视图。 它们是不同且独立的外壳。 我们可以通过在每个窗格中运行不同的命令来显示这一点。

We type the following:

我们输入以下内容:

uname -a

uname -a

ls -hl

ls -hl

To move from one pane to another, press Ctrl+B, and then either the Up, Down, Left, or Right Arrow.

要从一个窗格移动到另一个窗格,请按Ctrl + B,然后按向上,向下,向左或向右箭头。

Two different commands in two panes in a tmux session in a terminal widow.

If you press Ctrl+B, and then the percentage sign (%) it splits the current pane vertically.

如果按Ctrl + B,然后再按百分号( %)则会垂直拆分当前窗格。

tmux session with vertical and horizontal split panes in a terminal window.

Press Ctrl+B, and then Q to make tmux briefly flash the number of each pane.

按Ctrl + B,然后按Q使tmux短暂闪烁每个窗格的编号。

tmux showing pane numbers in a terminal window.

These numbers are used in prompts and messages from tmux. Press Ctrl+B, and then X to close the current pane. The status bar changes to amber, and you’re prompted to confirm you want to close that pane number. Press Y to remove the pane, or N to leave things as they are.

这些数字用于tmux提示和消息中。 按Ctrl + B,然后按X关闭当前窗格。 状态栏变为琥珀色,并提示您确认要关闭该窗格号。 按Y删除窗格,或按N保留原样。

tmux prompting to delete a pane in a terminal window.

If you press Y, the pane is removed.

如果按Y,将删除窗格。

tmux with two horizontal panes in a terminal window.

The screen command also has panes, but, again, they’re less intuitive to use. The thing that annoys us about screen is if you detach a session with panes, they disappear when you reattach that session. This gets old very quickly.

screen命令也具有窗格,但是同样,它们使用起来不太直观。 惹恼我们介绍一下事情screen是如果你断开与窗格的会议上,他们当您重新连接该会话消失。 这很快就变老了。

Ctrl + B备忘单 (A Ctrl+B Cheat Sheet)

We’ve included a cheat sheet of the different commands you can use in tmux below.

我们在下面提供了可在tmux中使用的不同命令的备忘单。

会话命令 (Session Commands)

  • S: List sessions.

    S:列出会话。

  • $: Rename current session.

    $:重命名当前会话。

  • D: Detach current session.

    D:分离当前会话。

  • Ctrl+B, and then ?: Display Help page in tmux.

    Ctrl + B,然后?:tmux显示帮助页面。

窗口命令 (Window Commands)

  • C: Create a new window.

    C:创建一个新窗口。

  • ,: Rename the current window.

    ,:重命名当前窗口。

  • W: List the windows.

    W:列出窗口。

  • N: Move to the next window.

    N:移至下一个窗口。

  • P: Move to the previous window.

    P:移至上一个窗口。

  • 0 to 9: Move to the window number specified.

    0至9:移至指定的窗口号。

窗格命令 (Pane Commands)

  • %: Create a horizontal split.

    %:创建水平分割。

  • “: Create a vertical split.

    “:创建垂直分割。

  • H or Left Arrow: Move to the pane on the left.

    H或向左箭头:移至左侧窗格。

  • I or Right Arrow: Move to the pane on the right.

    I或向右箭头:移到右侧窗格。

  • J or Down Arrow: Move to the pane below.

    J或向下箭头:移到下面的窗格。

  • K or Up Arrow: Move to the pane above.

    K或向上箭头:移至上方的窗格。

  • Q: Briefly show pane numbers.

    :简要显示窗格编号。

  • O: Move through panes in order. Each press takes you to the next, until you loop through all of them.

    O:依次浏览窗格。 每按一次,便会跳至下一个,直到您遍历所有这些。

  • }: Swap the position of the current pane with the next.

    }:将当前窗格的位置与下一个交换。

  • {: Swap the position of the current pane with the previous.

    {:将当前窗格的位置与上一个交换。

  • X: Close the current pane.

    X:关闭当前窗格。

他们如何比较 (How They Compare)

In terms of functionality, screen and tmux both perform similarly and offer the same main features. It’s the way you access those features that is markedly different. tmux offers slicker, more comfortable ways to get to the various functions. However, that’s not the only difference.

在功能方面, screentmux性能相似,并提供相同的主要功能。 访问这些功能的方式截然不同。 tmux提供更轻松,更舒适的方式来实现各种功能。 但是,这并不是唯一的区别。

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

tmux重命名会话和窗口的功能很简洁,并且在重新连接会话时它保留窗格的事实改变了游戏规则。

screen, on the other hand, completely loses panes when you detach and reattach a session. This is almost annoying enough to make you avoid detaching in the first place.

另一方面,当您分离并重新连接会话时, screen完全丢失了窗格。 这几乎足以使您避免在一开始就分离。

There’s so much more to tmux, including its incredibly flexible scripting capabilities. You owe it to yourself to check it out.

tmux有更多功能,包括其难以置信的灵活脚本功能。 您应该自己检查一下。

翻译自: https://www.howtogeek.com/671422/how-to-use-tmux-on-linux-and-why-its-better-than-screen/

tmux在linux的使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值