screen is a very powerful and useful utility. Here, we’ll focus on just two aspects:
- how to get started if you’re not familiar with
- how multiple remote users can see and share the same shell session
screen is simple:
Depending upon the configuration, you will either see the terminal clear and your shell prompt appear at the top, or you may get a welcome message that may be dismissed by pressing
At this point, you can run commands in the usual way: nothing has changed. However, you are able to disconnect from the screen session by pressing
^A d (control-A followed by d). When you do that, the
screen session will “detach” and you are back where you were before you ran
screen, with a message informing you that you’ve detached from the
$ screen # work in shell, then press ^A d [detached from 21035.pts-8.awe] $
You can re-attach to the
screen session at any time. For example, you could:
sshto a remote system
- start a
- start a long process, such as a compilation
- detach from the
- go home
sshto the remote system system again
- resume your
Resuming a session can be as simple as:
If you have multiple screen sessions running, you may list them:
$ screen -ls There are screens on: 22488.pts-8.awe (27/11/17 16:48:36) (Attached) 22428.pts-8.awe (27/11/17 16:48:24) (Detached) 22415.pts-8.awe (27/11/17 16:48:20) (Detached) 21035.pts-8.awe (27/11/17 16:41:21) (Detached) 4 Sockets in /run/screen/S-kae.
You can re-attach to a specific screen session by specifying the
tty (as listed above):
$ screen -r 22428.pts-8.awe
You can only attach to a detached
screen session. If you forgot to detach the session before you left the office, you’ll see something like:
$ screen -r There is a screen on: 23009.pts-8.awe (27/11/17 16:52:26) (Attached) There is no screen to be resumed.
There are a variety of ways to detach a remote session and to reattach to it, and as ever the
screen(1) man page has full details. However, a command that will almost always do what you want is:
Quoting from the man page, this will: “Attach here and now. In detail this means: If a session is running, then reattach. If necessary detach and logout remotely first. If it was not running create it and notify the user.”
When you’ve finished using the screen session, you can logout (for example with
^D) in the usual way. The terminal will clear, and
screen will print a message to say that it is terminating.
Sharing a Shell Session
When training or mentoring someone, it can be useful for each to be able to look over the other’s shoulder. With remote working becoming ever more prevalent, that gets a little harder, but
screen can come to the rescue.
Both users should log into the same user account, perhaps via
ssh. The first runs
screen as before.
The second runs
screen with the
$ screen -x
Anything that either user types will appear to both users.
Helpful when two remote people are working on the same problem, or even if one user wants to temporarily give the other root privileges without divulging the root password (simply use
su and the session can become root for both users).
A Word of Warning
It is not good practice to log into a non-privileged account, run
screen and then elevate privileges, perhaps with the
su command. This shouldn’t be a problem in principle, but if the non-privileged account is a shared resource, anyone with access to it will now be able to access the session with elevated privileges. Better to log in, run
su and then run
Was This Linux Tip Useful?
Let us know in the comments below.