Back to Linux VPS

How to use Screen on Linux

How to use Screen on Linux
05 Jul, 2024 Linux VPS

You are using a Linux VPS via SSH, sometimes you are running a process on the server and it takes a lot of time, or sometimes the command is running but the network connection is lost, the SSH session is turned off, tasks are running stopped. At this time, Screen – an extremely useful software that every administrator needs to know about – will help us solve this problem, helping us continue working sessions on the server. So what is Screen ? How to use Screen on Linux.

  1. What is Screen?

Screen, also known as GNU Screen, is a terminal multiplexer, which means you can start a connection session and then open any number of windows (virtual terminals) inside the session there. Processes running in that connection session will continue to run even when our window is disconnected. And you can completely name these connection sessions so you can reconnect at any time.

  1. How to use Screen on Linux

Step 1: Log in to your Linux VPS using SSH

Buy a Linux VPS at DaintyCloud and log in to the VPS.

If you don’t know how to log in to a Linux VPS using SSH, please refer to our tutorial article:

WHAT IS LINUX VPS? HOW TO LOGIN TO BITVISE SSH CLIENT

Step 2: Check if there is a Screen on the Linux VPS

First of all, you need to check if there is a Screen on the Linux VPS. Currently, on most Linux operating systems such as Redhat and CentOS, the screen program is often pre-installed in the directory: /usr/bin/screen . To determine whether Screen is installed on the system, use the following command:

which screen

If the results are as follows, the system already has screen installed:

/usr/bin/screen

If not installed, the screen will display as follows:

/usr/bin/which: no screen in (/root/exec -l

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)

Step 3: Install Screen on Linux

You can use yum (CentOS) to install:

yum install screen

or atp-get (Ubuntu):

apt install screen

After installation we can use the following command to check the installed screen version.

screen --version

Step 4: Use Screen on Linux

After installing on the system, use the following command to create a screen window inside a terminal as follows:

screen

If you want to name the screen window for ease of management, use the following command:

screen -S name

There in, “name” is the name you want.

Now, You have a new window inside the terminal window. The interface of these two windows is the same, the difference is that screen allows you to use some commands from keyboard shortcuts. (Refer to command options and keyboard shortcuts on Screen at the end of the article)

Exit Screen:

To exit the screen, you can do one of the following:

  • Turn off the SSH software
  • Turn off Terminal
  • Or use the following key combination: “Ctrl-a” “d”.

This is the great advantage of screen, you can exit the screen and access it again anywhere, while the commands in screen continue to operate on the server. In case of sudden connection loss, screen automatically exits to keep your program safely running on the server.

List of Screens:

After exiting screen, I want to know how many screen windows are running. From the terminal window, use the following command:

screen -ls

Continue using the previously created Screen:

  • In case you only have 1 screen, you can quickly use the following command:

screen -r

  • In case there are multiple screens running, use the following command:

screen -ls

screen -x name

The first command is to view the list, and the second command is to continue to the screen named “name”, this part will change if you have created a Screen with a different name.

Turn off screen:

To turn off a Screen we use the command below:

screen -S name -X quit

Remember to change the name to the Screen name you want to turn off.

Above are some basic commands to use Screen on Linux.

You can refer to other command options below.

  1. Command options and keyboard shortcuts on Screen

This is the syntax used for the screen command:

screen [-opts] [cmd [args]]

Command options:

  • -a: This command forces all capabilities into each window’s termcap.
  • -A -[r|R]: It adjusts all windows to the changed width and height of the screen.
  • File -c Instead of ‘.screenrc‘: reads configuration file.
  • -d (-r): Delete the running screen elsewhere (and reattach it here).
  • -dmS name: Start as daemon, i.e. screen session in disconnected mode.
  • -D (-r): Disconnect and log off the remote (and reattach here).
  • -D -RR: Perform necessary actions to obtain a screen session.
  • -e xy: This changes command line characters.
  • -f :Used to enable flow control, -fn=off, -fa=auto.
  • -h: Used to set the size of the scrollback history buffer.
  • -i: Interrupt output earlier when flow control is enabled.
  • -l: Turn on login mode,(update /var/run/utmp), -ln=off.
  • -ls [match]: Used to display all attached screens.
  • -L: Enable output logging.
  • -m: Ignore the $STY variable and create a new screen session.
  • -O: Choose optimal output instead of exact vt100 emulation.
  • -p window: Used to preselect the named window if it exists.
  • -q: Silence the boot process. If it fails, it exits with a non-zero return code.
  • -Q: Helps send answers to stdout of the query process.
  • -r [session]: Reattach to the detached screen process.
  • -R: If possible, it will reattach; otherwise, it will create a new session.
  • -S sockname: It names the session as .sockname instead of …
  • -t title: Used to set the title (name of the window).
  • -T: Use a term like $TERM for windows instead of “screen”.
  • -U: Instructs the screen to use UTF-8 encoding.
  • -x: Attach to an integral monitor.
  • -X: Run as a screen command in the given session.

Shortcuts

While in a desktop session, you can use the following keyboard shortcuts:

  • Ctrl-a c: Create a new window (shell).
  • Ctrl-a k: Turn off the current window.
  • Ctrl-a w: List all windows (current window is marked with “*”).
  • Ctrl-a 0-9: Go to windows numbered 0-9.
  • Ctrl-a n: Go to the next window.
  • Ctrl-a Ctrl-a: Switch between the current window and the previous window.
  • Ctrl-a [: Start copy mode.
  • Ctrl-a ]: Paste the copied text.
  • Ctrl-a ?: Help (displays a list of commands).
  • Ctrl-a Ctrl-\: Exit the screen.
  • Ctrl-a D (Shift-d): Power off and log out.
  • Ctrl-a d: Detach but still open the shell window.

You can visit DaintyCloud to register for a free Linux VPS account and use Screen according to the instructions.

Good luck.

Share on