How to Use Screen in Linux [List, Attach, Detach, Close]

Screen is an application that allows you to create multiple shell sessions. When you open a new shell session using screen, screen creates a new process in the background. Even if you leave this session with the "Ctrl + a + d" combination, the screen command continues to run the session in the background. It allows you to connect to the same session again later.

This article covers Screen command setup and "How to Use Screen on Linux?" includes topics.

Advertisement

 

Installing Screen utility on Linux

The setup on Redhat based operating systems(Fedora, Centos, AlmaLinux, Rocky Linux,etc) is as follows:

sudo dnf -y install screen

In older versions:

sudo yum -y install screen

In Debian-based operating systems (Ubuntu, Pardus, Linux Mint, Kali Linux, etc.) it is as follows:

sudo apt install screen -y

In older versions:

sudo apt-get install screen -y

On Gentoo Linux:

sudo emerge -a sys-apps/screen

On Arch Linux:

Advertisement
sudo pacman -S screen

On Arch Linux:

sudo zypper install screen

 

How to Use Screen on Linux?

The screen command is most often used to ensure that a long-running script does not terminate even if the terminal is closed.

After installation, you can login to the application by typing screen in the terminal:

foc@fedora:~$ screen

screen

To end the Screen session, you must type exit while logged in:

foc@fedora:~$ exit

foc@fedora:~$ screen
[screen is terminating]

 

10 Common Screen Command Examples

Let's explain the 10 most used screen commands you will need, with examples.

 

1. List Screen Sessions

These two commands can be used to list previously opened screen sessions. Both give the same screen output:

Advertisement
foc@fedora:~$ screen -list
There is a screen on:
9846.pts-6.fedora (Attached)
9316.golinuxcloud (Attached)
9074.pts-1.fedora (Attached)
3 Sockets in /run/screen/S-foc.

-ls output:

foc@fedora:~$ screen -ls
There are screens on:
9846.pts-6.fedora (Attached)
9074.pts-1.fedora (Attached)
2 Sockets in /run/screen/S-foc.

 

2. Connect to previous screen session

Type the session you want to connect from the listed screen sessions as follows:

foc@fedora:~$ screen -a -r 9074.pts-1.fedora

 

3. Close the open screen session

The -X and -S parameters are used to end the open session. The session id is followed by "quit":

foc@fedora:~$ screen -X -S 9074.pts-0.fedora quit

 

4. Switching between screen sessions

To switch between sessions,

  • CTRL + a + n: Connects to the next session.
  • CTRL + a + p: Returns to the previous session

 

5. Name the newly opened session

In the new screen session, give a name with the -S parameter. This name can define the work done in that session.

foc@fedora:~$ screen -S golinuxcloud

Then you can see the session opened with this name:

Advertisement
foc@fedora:~$ screen -ls
There are screens on:
13350.golinuxcloud (Attached)
11997.pts-3.fedora (Attached)
2 Sockets in /run/screen/S-foc.

 

6. Detach last screen session (Screen Disconnect)

"CTRL + A + D" does the same job as the key combination. After this command, the session ends.

foc@fedora:~$ screen -d 13350.golinuxcloud
[13350.golinuxcloud detached.]

The latest status of the session is as follows:

foc@fedora:~$ screen -ls
There are screens on:
	13350.golinuxcloud	(Detached)
	11997.pts-3.fedora	(Attached)

 

7. Reattach the screen session

It is used to reattach a screen session which was detached in past. Let's connect to the 13350.golinuxcloud session:

foc@fedora:~$ screen -d 13350.golinuxcloud

The latest status of the session:

foc@fedora:~$ screen -ls
There are screens on:
	13350.golinuxcloud	(Attached)
	11997.pts-3.fedora	(Attached)

 

8. Lock the Screen Session

If you want to lock the Screen session at that moment, use the CTRL + a + x switch combination.

Screen used by foc <foc> on fedora.
Password:

 

9. Take the log of the Screen session

Use the -L parameter when starting the Screen session. The screenlog.0 file will be formed in the directory where you started the session.

Advertisement
foc@fedora:~$ screen -L
foc@fedora:/tmp$ ls -la screenlog.0 
-rw-r--r--. 1 foc foc 72 Oct 24 20:58 screenlog.0

 

10. Set the name of the window

Give the window a name with the "-t" parameter.  In previous sessions, the default "[Screen 0: Bash]" value is replaced with the new value. The image of the window in the last case:

How to Use Screen in Linux [List, Attach, Detach, Close]

 

Summary

When it is in the screen session, CTRL+A activates switch connections. Some are as follows:

key	binding
?	Show key bindings.
A	Set a title for the current window.
c	Create a new window.
n	Switch to the next window.
p	Switch to the previous window.
0-9	Switch to window number n.
w	Show a list of windows.
"	Present a list of windows for selection.
F	Resize the window to the current region size.
[ or esc	Enter copy/scrollback mode.
k	Destroy current window.

For more details about Screen, run the following command at the terminal:

foc@fedora:~$ screen -help
Use: screen [-opts] [cmd [args]]
or: screen -r [host.tty]

Options:
-a Force all capabilities into each window's termcap.
-A -[r|R] Adapt all windows to the new display width & height.
-c file Read configuration file instead of '.screenrc'.
-d (-r) Detach the elsewhere running screen (and reattach here).
-dmS name Start as daemon: Screen session in detached mode.
...

 

What’s Next

You should also check screen alternative in newer distributions i.e List of 50+ tmux cheatsheet and shortcuts commands

 

References

linux.die.net - Screen-Linux man page

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X