序章

Screen is a console application that allows you to use multiple terminal sessions within one window. プログラムはシェルセッション内で動作し、ウィンドウマネージャーがウィンドウを管理する方法と同様に、他のターミナルセッションのコンテナーおよびマネージャーとして機能します。

There are many situations where creating several terminal windows is not possible or ideal. You might need to manage multiple console sessions without an X server running, you might need to access many remote cloud servers, or you might need to monitor a running program’s output while working on some other task.

There are modern, all-in-one solutions to this problem, like tmux, but screen is the most mature of them all, and it has its own powerful syntax and features.

Step 1 – Installing Screen

In this tutorial, we’ll be using Ubuntu 22.04, but outside of the installation process, everything should be the same on every modern Linux distribution.

Screen is often installed by default on Ubuntu. You can also use apt to update your package sources and install screen:

  1. sudo apt update
  2. sudo apt install screen

Verify that screen has been installed by running which screen:

  1. which screen
Output
/usr/bin/screen

You can begin using screen in the next step.

Step 2 – Using Screen

To start a new screen session, run the screen command.

  1. screen
[secondary_label Output
GNU Screen version 4.09.00 (GNU) 30-Jan-22

Copyright (c) 2018-2020 Alexander Naumov, Amadeusz Slawinski
Copyright (c) 2015-2017 Juergen Weigert, Alexander Naumov, Amadeusz Slawinski
Copyright (c) 2010-2014 Juergen Weigert, Sadrul Habib Chowdhury
Copyright (c) 2008-2009 Juergen Weigert, Michael Schroeder, Micah Cowan, Sadrul Habib
Chowdhury
Copyright (c) 1993-2007 Juergen Weigert, Michael Schroeder
Copyright (c) 1987 Oliver Laumann

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2, or (at your option) any later version.
…
                  [Press Space for next page; Return to end.]

You’ll be greeted with the licensing page upon starting the program. Just press Enter to continue.

What happens next may be surprising. We are given a normal command prompt and it looks like nothing has happened. Did screen fail to run correctly? Let’s try a quick keyboard shortcut to find out. Press Ctrl+a, followed by v:

Output
screen 4.09.00 (GNU) 30-Jan-22

画面からバージョン情報をリクエストしたところ、画面で正しく実行されていることを確認できるフィードバックを受け取りました。

Now is a great time to introduce the way that we will be controlling screen. Screen is mainly controlled through keyboard shortcuts. Every keyboard shortcut for screen is prefaced with Ctrl-a (hold the control key while pressing the “a” key). That sequence of keystrokes tells screen that it needs to pay attention to the next keys we press.

You’ve already used this paradigm once when we requested the version information about screen. Let’s use it to get some more useful information, by entering Ctrl-a ?:

Output
Screen key bindings, page 1 of 2. Command key: ^A Literal ^A: a break ^B b license , removebuf = clear C lockscreen ^X x reset Z colon : log H screen ^C c copy ^[ [ login L select ' detach ^D d meta a silence _ digraph ^V monitor M split S displays * next ^@ ^N sp n suspend ^Z z dumptermcap . number N time ^T t fit F only Q title A flow ^F f other ^A vbell ^G focus ^I pow_break B version v hardcopy h pow_detach D width W help ? prev ^H ^P p ^? windows ^W w history { } quit \ wrap ^R r info i readbuf < writebuf > kill K k redisplay ^L l xoff ^S s lastmsg ^M m remove X xon ^Q q [Press Space for next page; Return to end.]

This is the internal keyboard shortcut screen. You’ll probably want to memorize how to get here, because it’s an excellent quick reference. As you can see at the bottom, you can press Space to get more commands.

Okay, let’s try something more fun. Let’s run a program called top in this window, which will show us some information on our processes.

  1. top
Output
top - 16:08:07 up 1:44, 1 user, load average: 0.00, 0.01, 0.05 Tasks: 58 total, 1 running, 57 sleeping, 0 stopped, 0 zombie Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 507620k total, 262920k used, 244700k free, 8720k buffers Swap: 0k total, 0k used, 0k free, 224584k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 3384 1836 1288 S 0.0 0.4 0:00.70 init 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:00.11 ksoftirqd/0 5 root 20 0 0 0 0 S 0.0 0.0 0:00.12 kworker/u:0 6 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0 7 root RT 0 0 0 0 S 0.0 0.0 0:00.07 watchdog/0 8 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 cpuset 9 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 khelper 10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs 11 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns 12 root 20 0 0 0 0 S 0.0 0.0 0:00.03 sync_supers 13 root 20 0 0 0 0 S 0.0 0.0 0:00.00 bdi-default 14 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kintegrityd 15 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kblockd 16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ata_sff 17 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khubd 18 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 md

Okay, we are now monitoring the processes on our VPS. しかし、表示されているプログラムに関する詳細情報を見つけるためにいくつかのコマンドを実行する必要がある場合はどうでしょうか。 We don’t need to exit out of “top”. これらのコマンドを実行するための新しいウィンドウを作成できます。

The Ctrl-a c sequence creates a new window for us. これで、他のウィンドウで行っていた監視を中断することなく、必要なコマンドを実行できます。

その別のウィンドウはどこに行きましたか? We can get back to it using Ctrl-a n.

This sequence goes to the next window that we are running. The list of windows wrap, so when there aren’t any windows beyond the current one, it switches us back to the first window.

Ctrl-a p changes the current window in the opposite direction. したがって、3つのウィンドウがあり、現在3番目のウィンドウにいる場合、このコマンドは2番目のウィンドウに切り替わります。

A helpful shortcut to use when you’re flipping between the same two windows is Ctrl-a Ctrl-a.

This sequence moves you to your most recently visited window. したがって、前の例では、これにより3番目のウィンドウに戻ります。

At this point, you might be wondering how you can keep track of all of the windows that we are creating. Thankfully, screen comes with a number of different ways of managing your different sessions. First, we’ll create three new windows for a total of four windows and then we’ll try out Ctrl-a w, one of Screen’s window management tools. Enter Ctrl-a c Ctrl-a c Ctrl-a c Ctrl-a w:

Output
0$ bash 1$ bash 2-$ bash 3*$ bash

We get some useful information from this command: a list of our current windows. Here, we have four windows. Each window has a number and the windows are numbered starting at “0”. 現在のウィンドウには、番号の横にアスタリスクがあります。

So you can see that we’re currently at window #3 (actually the fourth window because the first window is 0). You can get back to window #1 quickly by using Ctrl-a 1.

We can use the index number to jump straight to the window we want. Let’s see our window list again using Ctrl-a w:

Output
0$ bash 1*$ bash 2$ bash 3-$ bash

As you can see, the asterisk tells us that we’re now on window #1. Let’s try a different way of switching windows with Ctrl-a &ldquo;:

Output
Num Name Flags 0 bash $ 1 bash $ 2 bash $ 3 bash $

We get an actual navigation menu this time. You can navigate with either the up and down arrows. Switch to a window by pressing Enter.

This is pretty useful, but right now all of our windows are named “bash”. That’s not very helpful. Let’s name some of our sessions. Switch to a window you want to name, for example with Ctrl-a 0 and then use Ctrl-a A:

Output
Set window's title to: bash

Using the Ctrl-a A sequence, we can name our sessions. You can now backspace over “bash” and then rename it whatever you’d like. We’re going to run top on window #0 again, so we’re going to name it monitoring.

Verify the result with Ctrl-a &ldquo;:

Output
Num Name Flags 0 monitoring $ 1 bash $ 2 bash $ 3 bash $

Now we have a more helpful label for window #0. したがって、ウィンドウを作成して名前を付ける方法はわかっていますが、ウィンドウが不要になったときにウィンドウを削除するにはどうすればよいでしょうか。 We use the Ctrl-a k sequence, which stands for “kill”.

Output
Really kill this window [y/n]

Step 3 – Managing Screen Sessions

When you want to quit screen and kill all of your windows, you can use Ctrl-a \.

Output
Really quit and kill all your windows [y/n]

This will destroy our screen session. 作成したウィンドウと未完成の作業はすべて失われます。

But we want to explore one of the huge benefits of using “screen”. We don’t want to destroy the session, we want to detach it. Detaching allows our programs in the screen instance to continue to run, but it gives us access back to our base-console session (the one where we started “screen” from initially). The screen session is still there, it will just be managed in the background. Use Ctrl-a d to detach.

Output
[detached from 1835.pts-0.Blank]

So our session is now detached. どうすればそれに戻ることができますか?

  1. screen –r

The -r flag stands for reattach. We are now back in our screen session. ただし、複数の画面セッションがある場合はどうなりますか? What if we had started a screen session and detached it, and then started a new screen session and detached that as well?

Try running screen, then detaching with Ctrl-a d, then running screen again, then detaching with Ctrl-a d again.

How do we tell screen which session to attach?

  1. screen –ls
Output
There are screens on: 2171.pts-0.Blank (07/01/2013 05:00:39 PM) (Detached) 1835.pts-0.Blank (07/01/2013 03:50:43 PM) (Detached) 2 Sockets in /var/run/screen/S-justin.

Now we have a list of our sessions. We can reattach the second one by typing its id number after the -r flag.

  1. screen –r 1835

2台の別々のコンピューターまたはターミナルウィンドウにセッションを接続する場合はどうなりますか? You can use the -x flag, which lets you share the session.

  1. screen –x

Step 4 – Managing Terminals Within Screen

There are a number of commands that help you manage the terminal sessions you run within screen.

To copy text, you can use Ctrl-a [.

This will give you a cursor that you can move with the arrow keys or with HJKL. Move to where you want to start copying, and press Enter. Move to the end of where you’d like to copy and press Enter again. 次に、テキストがクリップボードにコピーされます。

One thing to be aware of is that this is also Screen’s mechanism for scrolling. If you need to see some text that is off the screen, you can hit Ctrl-a [ and then scroll up off of the screen.

We can paste text that we copied with Ctrl-a ].

別の画面ウィンドウで実行されているプログラムを監視することもできます。

Let’s say that you’re compiling something in one window and you want to know when it’s completed. You can ask screen to monitor that window for silence with Ctrl-a _, which will tell you when no output has been generated for 30 seconds.

Let’s try it with another example. Let’s have screen tell us when our window is finished pinging Google 4 times.

  1. ping –c 4 www.google.com

Then input Ctrl-a _:

Output
The window is now being monitored for 30 sec. Silence.

Now we can do work in another window and be alerted when the task in this window is complete by entering Ctrl-a 1:

Output
Window 2: silence for 30 seconds

We can also do the opposite and be alerted when there is activity happening on a specific window.

  1. sleep 20 && echo “output”

Then, enter Ctrl-a M.

Output
Window 2 (bash) is now being monitored for all activity.

We will now be alerted when the command produces output. To see results, use Ctrl-a 1:

Output
Activity in window 2

Let’s say we are going to be doing some important changes and we want to have a log of all of the commands we run. We can log the session with Ctrl-a H.

Output
Creating logfile "screenlog.1".

画面領域

If we need to see multiple windows at once, we can use something that screen calls “regions”. We create more regions by splitting the current region. To split the current region horizontally, we can use Ctrl-a S.

This will move our current window to the top half of the screen and open a new blank region below it. We can get to the lower screen with Ctrl-a [tab].

次に、下部の領域に新しいウィンドウを作成するか、通常の方法でビューを別のウィンドウに変更できます。

If we want to kill the current region, we can use Ctrl-a X.

That destroys the region without destroying the actual window. This means that if you were running a program in that region, you can still access it as a normal window, the view into that window was destroyed.

If we want to make a vertical split, we can use Ctrl-a | instead.

The controls for vertical splits are the same as horizontal splits. If we’ve added a few different regions and want to go back to a single region, we can use Ctrl-a Q, which destroys all regions but the current one.

Step 5 – Using Byobu with Screen

A great enhancement for screen is a program called byobu. It acts as a wrapper for screen and provides an enhanced user experience. Ubuntuでは、次のコマンドでインストールできます。

  1. sudo apt install byobu

Before we begin, we need to tell byobu to use screen as a backend. これは、次のコマンドで実行できます。

  1. byobu-select-backend
Output
Select the byobu backend: 1. tmux 2. screen Choose 1-2 [1]:

We can choose screen here to set it as the default terminal manager.

Now, instead of typing screen to start a session, you can type byobu.

  1. byobu

When you type Ctrl-a for the first time, you’ll have to tell byobu to recognize that as a screen command.

Output
Configure Byobu's ctrl-a behavior... When you press ctrl-a in Byobu, do you want it to operate in: (1) Screen mode (GNU Screen's default escape sequence) (2) Emacs mode (go to beginning of line) Note that: - F12 also operates as an escape in Byobu - You can press F9 and choose your escape character - You can run 'byobu-ctrl-a' at any time to change your selection Select [1 or 2]:

Select 1 to use byobu as normal.

The interface gives you a lot of useful information, such as a window list and system information. Ubuntuでは、セキュリティ更新プログラムがいくつのパッケージにあるかがわかります。番号の後に赤い背景に感嘆符が続きます。

One thing that is different between using byobu and screen is the way that byobu actually manages sessions. If you run byobu again once you’re detached, it will reattach your previous session instead of creating a new one.

To create a new session, use byobu –S:

  1. byobu –S sessionname

Change sessionname to whatever you’d like to call your new session. あなたは現在のセッションのリストを見ることができます:

  1. byobu –ls
Output
There are screens on: 22961.new (07/01/2013 06:42:52 PM) (Detached) 22281.byobu (07/01/2013 06:37:18 PM) (Detached) 2 Sockets in /var/run/screen/S-root.

And if there are multiple sessions, when you run byobu, you will be presented with a menu to choose which session you want to connect to.

  1. byobu
Output
Byobu sessions... 1. screen: 22961.new (07/01/2013 06:42:52 PM) (Detached) 2. screen: 22281.byobu (07/01/2013 06:37:18 PM) (Detached) 3. Create a new Byobu session (screen) 4. Run a shell without Byobu (/bin/bash) Choose 1-4 [1]:

現在のセッションのいずれかを選択したり、新しい屏風セッションを作成したり、屏風を使用せずに新しいシェルを取得したりすることもできます。

One option that might be useful on a cloud server you manage remotely is to have byobu start up automatically whenever you log into your session. That means that if you are ever disconnected from your session, your work won’t be lost, and you can reconnect to get right back to where you were before.

ログインするたびに屏風が自動的に起動するようにするには、ターミナルに次のように入力します。

  1. byobu-enable
Output
The Byobu window manager will be launched automatically at each text login. To disable this behavior later, just run: byobu-disable Press <enter> to continue…

As it says, if you ever want to turn this feature off again, type:

  1. byobu-disable

自動的に起動しなくなります。

Conclusion

In this tutorial, you installed and used screen and then byobu to manage terminal sessions. You learned several different shortcuts for detaching and switching between multiple running environments on the fly. Like many mature Unix terminal interfaces, Screen can be idiosyncratic, but it is also powerful and ubiquitous – you never know when it may come in handy.