如何使用Linux的screen命令

A Linux terminal on a laptop with other terminal sessions superimposed behind it.
fatmawati achmad zaenuri/Shutterstock Fatmawati achmad zaenuri / Shutterstock

With the Linux screen command, you can push running terminal applications to the background and pull them forward when you want to see them. It also supports split-screen displays and works over SSH connections, even after you disconnect and reconnect!

使用Linux screen命令,您可以将正在运行的终端应用程序推送到后台,并在需要查看它们时将其向前拉。 它还支持分屏显示,并且即使您断开连接并重新连接后,也可以通过SSH连接工作!

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

The screen command is a terminal multiplexer, and it’s absolutely packed with options. To say it can do a lot is the granddaddy of understatements. The man page runs to over 4,100 lines.

screen命令是一个终端多路复用器 ,并且绝对包含选项。 说它可以做很多事是轻描淡写的祖父。 手册页超过4,100行

The following are the most common cases in which you would use the screen command, and we’ll cover these further in this article:

以下是使用screen命令的最常见情况,我们将在本文中进一步介绍这些情况:

  • The standard operation is to create a new window with a shell in it, run a command, and then push the window to the background (called “detaching”). When you want to see how your process is doing, you can pull the window to the foreground again (“reattach”) and use it again. This is great for long processes you don’t want to accidentally terminate by closing the terminal window.

    标准操作是创建一个带有外壳的新窗口,运行命令 ,然后将窗口推到后台(称为“分离”)。 当您想查看过程的运行情况时,可以再次将窗口拉到前台(“重新连接”)并再次使用。 这对于不需要关闭终端窗口意外终止的长进程非常有用。

  • Once you’ve got a screen session running, you can create new windows and run other processes in them. You can easily hop between windows to monitor their progress. You can also split your terminal window into vertical or horizontal regions, and display your various screen windows in one window.

    一旦运行了screen会话,就可以创建新窗口并在其中运行其他进程。 您可以轻松地在窗口之间跳来监视其进度。 您还可以将终端窗口分为垂直或水平区域,并在一个窗口中显示各种screen窗口。

  • You can connect to a remote machine, start a screen session, and launch a process. You can disconnect from the remote host, reconnect, and your process will still be running.

    您可以连接到远程计算机,启动screen会话并启动进程。 您可以断开与远程主机的连接,然后重新连接,您的进程仍将运行。

  • You can share a screen session between two different SSH connections so two people can see the same thing, in real-time.

    您可以在两个不同的SSH连接之间共享一个screen会话,这样两个人可以实时看到同一件事。

安装画面 (Installing screen)

To install screen on ubuntu, use this command:

要在ubuntu上安装screen ,请使用以下命令:

sudo apt-get install screen
The "sudo apt-get install screen" command in a terminal window.

To install screen on Manjaro, use the following command:

要在Manjaro上安装screen ,请使用以下命令:

sudo pacman -Sy screen
The "sudo pacman -Sy screen" command in a terminal window.

On Fedora, you type the following:

在Fedora上,键入以下内容:

sudo dnf install screen
The "sudo dnf install screen" command in a terminal window.

屏幕入门 (Getting Started with screen)

To start screen, simply type it as shown below and hit Enter:

要开始screen ,只需按如下所示键入它,然后按Enter:

screen
The "screen" command in a terminal window.

You’ll see a page of license information. You can press the Space bar to read the second page or Enter to return to the command prompt.

您会看到一个许可证信息页面。 您可以按空格键阅读第二页,或按Enter键返回命令提示符。

The "screen" command license information in a terminal window.

You’re left at the command prompt, and nothing much seems to have happened. However, you’re now running a shell inside a multiplexed terminal emulator. Why is this a good thing? Well, let’s start a process that’s going to take a long time to complete. We’ll download the source code for the latest Linux kernel and redirect it into a file called latest_kernel.zip.

您留在命令提示符下,似乎什么都没发生。 但是,您现在正在多路复用终端仿真器中运行Shell。 为什么这是一件好事? 好吧,让我们开始一个过程,该过程将需要很长时间才能完成。 我们将下载最新Linux内核的源代码,并将其重定向到一个名为latest_kernel.zip的文件中。

To do so, we type the following:

为此,我们键入以下内容:

curl https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.5.9.tar.xz > latest_kernel.zip
The "curl https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.5.9.tar.xz > latest_kernel.zip" command in a terminal window.

Our download begins, and the curl output shows us the progress.

我们的下载开始,并且curl输出向我们显示进度。

The output from the "curl https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.5.9.tar.xz > latest_kernel.zip" command in a terminal window.

We can’t show you an image of the next bit, because it’s a keystroke sequence. You type Ctrl+A, release those keys, and then press d to detach the screen.

我们无法显示下一张图片,因为它是按键序列。 您键入Ctrl + A,释放这些键,然后按d分离屏幕。

The download process is still running but the window showing the download is removed. You’re returned to the terminal window from which you launched the screen session. A message tells you that a screen window labeled 23167.pts-0.howtogeek has been detached.

下载过程仍在运行,但是显示下载的窗口已删除。 您将返回到启动screen会话的终端窗口。 一条消息告诉您,已分离出标有23167.pts-0.howtogeekscreen窗口。

You need the number from the start of the window name to reattach it. If you forget it, you can always use the -ls (list) option, as shown below, to get a list of the detached windows:

您需要窗口名称开头的数字来重新附加它。 如果您忘记了它,可以始终使用-ls (列表)选项,如下所示,以获取分离的窗口的列表:

screen -ls
The "screen -ls" command in a terminal window.

When you’re ready, you can use the -r (reattach) option and the number of the session to reattach it, like so:

准备就绪后,可以使用-r (重新连接)选项和会话号来重新连接它,如下所示:

screen -r 23167
The "screen -3 23167" command in a terminal window.

The window that’s been working away in the background is now brought back to your terminal window as if it had never left.

一直在后台运行的窗口现在像从未离开过一样被带回到您的终端窗口。

A reattached screen session restored to the terminal window.

If it’s a process that’s going to run through to its conclusion it will eventually complete. If it’s a continual process, you’ll eventually want to terminate it. Either way, when the process ends, you can type exit to exit from the screen. Alternatively, you can press Ctrl+A, and then K to forcibly kill a window.

如果这是一个要完成的过程,它将最终完成。 如果这是一个连续的过程,您最终将要终止它。 无论哪种方式,当过程结束时,您都可以键入exit以退出screen 。 或者,您可以按Ctrl + A,然后按K强制杀死一个窗口。

Type the following command:

键入以下命令:

exit
The "exit" command in a terminal window.

You’re returned to your previous terminal window, which will still show the command you used to reattach the window. Because we closed our one and only detached window, we get a message that screen is terminating.

您将返回上一个终端窗口,该窗口仍将显示用于重新连接该窗口的命令。 因为我们关闭了唯一的分离窗口,所以我们收到一条消息,指出screen即将终止。

The "screen" command terminating message in a terminal window.

使用命名屏幕会话 (Using Named screen Sessions)

You can use the -S (session name) option to name your screen session. If you use a memorable name rather than the numerical identity of the session, it’s more convenient to reconnect to a session. We type the following to name our session “bigfile”:

您可以使用-S (会话名称)选项来命名screen会话。 如果使用令人难忘的名称而不是会话的数字标识,则重新连接到会话更方便。 我们输入以下内容将会话命名为“ bigfile”:

screen -S bigfile
The "screen -S bigfile" command in a terminal window.

When screen launches our session, we see a blank window with a command prompt. We’re going to download a big file, so we can use a long-running process as an example.

screen启动会话时,我们将看到一个带有命令提示符的空白窗口。 我们将下载一个大文件,因此我们可以使用一个长时间运行的过程作为示例。

We type the following:

我们输入以下内容:

curl http://ipv4.download.thinkbroadband.com/1GB.zip > bigfile.zip
The "curl http://ipv4.download.thinkbroadband.com/1GB.zip > bigfile.zip" command in a terminal window.

When the download starts, we press Ctrl+A, and then press D to detach the session. We type the following to use the -ls (list) option with screen to see the details of our detached session:

下载开始后,我们按Ctrl + A,然后按D断开会话。 我们在screen键入以下内容以使用-ls (列表)选项以查看分离会话的详细信息:

screen -ls
The "screen -ls" command in a terminal window.

Behind the numeric identifier (23266), we see the name of our session (bigfile). We type the following, including the session’s name, to reattach it:

在数字标识符(23266)的后面,我们看到了会话的名称(bigfile)。 我们键入以下内容(包括会话名称)以重新附加它:

screen -r bigfile
The "screen -r bigfile" command in a terminal window.

We’re reconnected to our download window and see the long download is still in progress.

我们重新连接到我们的下载窗口,看到长时间的下载仍在进行中。

Curl download output in a reattached screen session in a terminal window.

When the download is complete, we type exit to close the session window.

下载完成后,我们键入exit以关闭会话窗口。

在多个Windows中使用屏幕 (Using screen with Multiple Windows)

So far, we’ve used screen to place a single process in the background in a detached window. However, screen is capable of doing much more than that. Next, we’ll run a few processes that allow us to monitor some aspects of our computer.

到目前为止,我们已经使用screen在一个分离的窗口中在后台放置一个进程。 但是, screen的功能远不止于此。 接下来,我们将运行一些过程,使我们可以监视计算机的某些方面。

We type the following to start a screen session called “monitor”:

我们键入以下内容以启动名为“ monitor”的屏幕会话:

screen -S monitor
The "screen -S monitor" command in a terminal window.

At the command prompt in our new window session, we’ll launch dmesg and use the -H (human-readable) and -w (wait for new messages) options. This will display the kernel buffer messages; new messages will appear as they occur.

在新窗口会话中的命令提示符处,我们将启动dmesg并使用-H (人类可读)和-w (等待新消息)选项。 这将显示内核缓冲区消息 ; 新消息将出现。

We type the following:

我们输入以下内容:

dmesg -H -w
The "dmesg -H -w" command in a terminal window.

The existing messages appear. We’re not returned to the command prompt because dmseg is waiting for new messages, and will display them as they arrive.

出现现有消息。 我们没有返回到命令提示符,因为dmseg正在等待新消息,并将在它们到达时显示它们。

The "dmsesg" output in a terminal window.

We want to run another application, so we need a new screen window. We press Ctrl+A, and then C to create a fresh window. We’re going to use watch to repeatedly run vmstat, so we get a frequently updated display of the virtual memory usage on our computer.

我们要运行另一个应用程序,因此我们需要一个新的screen窗口。 我们按Ctrl + A,然后按C创建一个新窗口。 我们将使用watch 重复运行 vmstat ,因此我们会经常更新计算机上虚拟内存使用情况的显示

At the new command prompt, we type the following:

在新的命令提示符下,键入以下内容:

watch vmstat
The "watch vmstat" command in a terminal window.

The vmstat output appears and updates every two seconds.

出现vmstat输出,并每两秒钟更新一次。

The "vmstat" output in a terminal window.

Our two processes are now running. To hop between the screen windows, you press Ctrl+A, and the number of the window. The first one we created is window zero (0), the next is window 1, and so on. To hop to the first window (the dmesg one), we press Ctrl+A and 0.

我们的两个流程现在正在运行。 要在screen窗口之间切换,请按Ctrl + A和窗口编号。 我们创建的第一个是窗口零(0),下一个是窗口1,依此类推。 要跳到第一个窗口( dmesg一个窗口),请按Ctrl + A和0。

Returning to the "dmesg" screen in a terminal window.

If we press Ctrl+A and 1, it takes us back to the vmstat window.

如果按Ctrl + A和1,则将我们带回到vmstat窗口。

Returning to the "vmstat" screen in a terminal window.

That’s pretty nifty! We can press Ctrl+A, and then D to detach from this session; we can reattach later. Both sessions will still be running. Again, to switch between the windows, we press Ctrl+A and the number (0 or 1) of the window we want to switch to.

那真是太好了! 我们可以按Ctrl + A,然后按D脱离此会话; 我们可以稍后再附加。 两个会话仍将运行。 同样,要在窗口之间切换,请按Ctrl + A和要切换到的窗口编号(0或1)。

Let’s go to the next step and view both screens in one window. When you do this, you’ll stretch your terminal window to a size that makes this step useful. Our examples are constrained to the size of our screenshots, so our windows will look a little cramped.

让我们转到下一步,并在一个窗口中查看两个屏幕。 执行此操作时,您将把终端窗口拉伸到使此步骤有用的大小。 我们的示例限于屏幕截图的大小,因此我们的窗口看起来有些局促。

To do this, we press Ctrl+A, and then Shift+S (a capital “S” is required).

为此,我们先按Ctrl + A,然后按Shift + S(需要大写的“ S”)。

The window splits into two “regions.”

窗口分为两个“区域”。

A "screen" window session split into two horizontal regions in a terminal window.

The top region still displays vmstat, and the bottom region is blank. The cursor is highlighted in the screenshot below. To move it to the lower region, we press Ctrl+A, and then Tab.

顶部区域仍显示vmstat ,底部区域为空白。 光标在下面的屏幕快照中突出显示。 要将其移至下部区域,请按Ctrl + A,然后按Tab。

A "screen" window session split into two horizontal regions with cursor in the bottom region in a terminal window.

The cursor moves to the lower region, which really is just an empty space. It isn’t a shell, so we can’t type anything in it. To get a useful display, we press Ctrl+A, and then press “0” to display the dmesg window in this region.

光标移动到下部区域,它实际上只是一个空白区域。 它不是外壳,所以我们不能在其中键入任何内容。 要获得有用的显示,请按Ctrl + A,然后按“ 0”以显示该区域中的dmesg窗口。

The "screen" session with a horizontal split pane showing a different process in each region in a terminal window.

This gives us both live outputs in one split window. If we press Ctrl+A and D to detach the window, and then reattach it, we’ll lose the split-pane view. However, we can restore it with the following keyboard shortcuts:

这使我们两个实时输出都在一个拆分窗口中。 如果我们按Ctrl + A和D来分离窗口,然后重新附加它,我们将失去拆分窗格视图。 但是,我们可以使用以下键盘快捷键来还原它:

  • Ctrl+A, S: Split the window horizontally.

    Ctrl + A,S:水平分割窗口。

  • Ctrl+A, Tab: Move to the lower region.

    Ctrl + A选项卡:移至下部区域。

  • Ctrl+A, 0: Display window zero in the lower region.

    Ctrl + A,0:在下部区域显示窗口零。

We can take things even a step further. We’ll now split the lower pane vertically, and add a third process to the display. With the cursor in the lower region, we press Ctrl+A and C to create a new window with a shell in it. The lower region displays the new window and gives us a command prompt.

我们可以更进一步。 现在,我们将垂直分割下部窗格,并在显示中添加第三个过程。 将光标放在下部区域中,我们按Ctrl + A和C可以创建一个带有外壳的新窗口。 下部区域显示新窗口,并为我们提供命令提示符。

A "screen" session with a horizontal split pane showing a new window in the lower region in a terminal window.

Next, we run the df command to check file system usage:

接下来,我们运行df命令来检查文件系统的使用情况

df
A "screen" session with a horizontal split pane, and "df" typed in the lower region in a terminal window.

When we see df running, we hit Ctrl+A and the pipe character (|). This splits the lower region vertically. We press Ctrl+A and Tab to move to the new region. Next, we press Ctrl+A and 0 to display the dmesg window.

当看到df正在运行时,我们按Ctrl + A和竖线字符(|) 。 这将垂直分割下部区域。 我们按Ctrl + A和Tab移到新区域。 接下来,我们按Ctrl + A和0来显示dmesg窗口。

A "screen" session showing three panes in a terminal window.

You can also move from region to region, and add more vertical or horizontal splits. Here are some more useful key combinations:

您还可以在区域之间移动,并添加更多垂直或水平分割。 以下是一些更有用的组合键:

  • Ctrl+A: Hop back and forth between the current and previous regions.

    Ctrl + A:在当前区域和先前区域之间来回跳动。

  • Ctrl+A, Q: Close all regions except the current one.

    Ctrl + A,Q:关闭除当前区域以外的所有区域。

  • Ctrl+A, X: Close the current region.

    Ctrl + A,X:关闭当前区域。

在SSH上使用屏幕 (Using screen Over SSH)

With screen, you can start a window session, detach it so it’s still running in the background, log off or back in, and reattach the session.

使用screen ,您可以启动窗口会话,将其分离以使其仍在后台运行,然后注销或重新登录,然后重新连接该会话。

Let’s make an SSH connection to our computer from a different one with the ssh command. We have to provide the name of the account with which we’re going to connect and the address of the remote computer.

我们使用ssh命令从另一台计算机建立SSH 连接 。 我们必须提供将要连接的帐户的名称以及远程计算机的地址。

For our example, we type the following:

对于我们的示例,我们键入以下内容:

ssh dave@192.168.4.30
The "ssh dave@192.168.4.30" command in a terminal window.

After we authenticate on the remote computer and log in, we type the following to start a screen session called “ssh-geek”:

在远程计算机上进行身份验证并登录后,我们键入以下内容以启动名为“ ssh-geek”的screen会话:

screen -S ssh-geek
The "screen -S ssh-geek" command in a terminal window.

For demonstration purposes, we’ll run top in the screen window, but you could start any long-running or endless process.

出于演示目的,我们将在screen窗口top运行,但是您可以启动任何长时间运行或无休止的过程。

We type the following:

我们输入以下内容:

top
The "top" command in a terminal window.

Once top is running in the window, we hit Ctrl+A, and then D to detach the window.

一旦top在窗口中运行,我们按Ctrl + A,然后按D分离窗口。

The "top" process running in a "screen" window in a terminal window.

We’re returned to the original, remote terminal window.

我们返回到原始的远程终端窗口。

User returned to their original terminal window

If we type exit, as shown below, it disconnects the SSH session and we’re back on our local computer:

如果我们输入exit ,如下所示,它将断开SSH会话的连接,然后返回本地计算机:

exit
The "exit" command in a terminal window.

We type the following to reconnect:

我们键入以下内容以重新连接:

ssh dave@192.168.4.30
The "ssh dave@192.168.4.30" command in a terminal window.

After we’re reconnected and logged in, we can type the following to reattach the screen session:

重新连接并登录后,我们可以输入以下内容重新连接screen会话:

screen -r ssh-geek
The "screen -r ssh-geek" command in a terminal window.

We’re now reconnected to our still-running instance of top.

现在,我们重新连接到仍在运行的top实例。

The "top" process running in a terminal window.

This is great if you want to start a process on one machine, and then pick up wherever you left off on another.

如果您想在一台机器上启动一个进程,然后在另一台机器上停下来的地方继续工作,那就太好了。

共享屏幕会话 (Sharing a screen Session)

You can also use a screen session to allow two people to see and interact with the same window. Let’s say someone running Fedora on his computer wants to connect to our Ubuntu server.

您还可以使用screen会话来允许两个人看到同一窗口并与之交互。 假设某人在其计算机上运行Fedora想要连接到我们的Ubuntu服务器。

He would type the following:

他将键入以下内容:

ssh dave@192.168.4.30
The "ssh dave@192.168.4.30" command in a terminal window.

After he’s connected, he starts a screen session called “ssh-geek” using the -S (session name) option. He also uses the -d (detach) and -m (enforced creation) options to create a new screen session that’s already detached.

连接后,他使用-S(会话名称)选项启动名为“ ssh-geek”的屏幕会话。 他还使用-d (分离)和-m (强制创建)选项来创建已经分离的新screen会话。

He types the following:

他键入以下内容:

screen -d -m -S ssh-geek
The "screen -d -m -S ssh-geek" command in a terminal window.

He types the following, using the -X (multiscreen mode) option to attach the session:

他使用-X (多屏模式)选项附加以下会话来键入以下内容:

screen -X ssh-geek
The "screen -X ssh-geek" command in a terminal window.

On a Manjaro computer, another person connects to the Ubuntu computer with the same account credentials, as shown below:

在Manjaro计算机上,另一个人使用相同的帐户凭据连接到Ubuntu计算机,如下所示:

ssh dave@192.168.4.1
The "ssh dave@192.168.4.1" command in a terminal window.

Once she’s connected, she types the screen command and uses the -X (multiscreen mode) option to join the same window session, like so:

连接后,她输入screen命令并使用-X(多屏模式)选项来加入相同的窗口会话,如下所示:

screen -X ssh-geek
The "screen -X ssh-geek" command in a terminal window.

Now, anything either person types, the other will see. For example, when one person issues the date command, they both see it as it’s typed, as well as its output.

现在,任何人键入任何内容,对方都会看到。 例如,当一个人发出date命令时,他们都会看到键入的日期以及它的输出。

The "date" command in a terminal window.
Output from the "date" command in a terminal window.

Both people are now sharing a screen session that’s running on a remote Ubuntu computer.

现在,两个人都共享在远程Ubuntu计算机上运行的screen会话。



For a piece of software that first saw the light of day in 1987, screen still packs a good productivity wallop. Familiarizing yourself with it will be time well spent!

对于一款在1987年首次亮相的软件来说, screen仍然具有很高的生产率。 熟悉它会花费很多时间!

翻译自: https://www.howtogeek.com/662422/how-to-use-linuxs-screen-command/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值