This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has sudo access and that you precede each of the privileged commands with sudo

screen command is a terminal multiplexer for linux. It can be used to multiplex several virtual consoles for accessing multiple separate login sessions inside a single terminal window. It can also be used to detach and reattach sessions from a terminal. One of it’s advantages is that the remote process continues running even when the user is disconnected.

It is very useful in cases when you are doing work on your remote server and it is a time taking task, or when you accidentally disconnects from a server due to internet interruption or power disconnection, you can reconnect to a session using this command and resume the work where you left.

In this tutorial, we will show you the multiple ways to use the screen command with the help of examples.

Installing screen Command

You can install the screen command by running the following command on your system

On Debian/Ubuntu

root@codesposts:~$ apt-get install screen

On RHEL/CentOS

root@codesposts:~$ yum install screen

Starting Linux screen

To start the linux screen for the first time, you can simply run the screen comamnd

root@codesposts:~$ screen

This will create a screen session or window in your current SSH terminal.

Closing A Screen Session

If the tasks are finished on a screen session, you can close the session by simply running exit command.

root@codesposts:~$ exit

Creating New Screen Session

You can create a new screen session by the following command

ctrl + a + c

Managing Multiple Sessions

If you are using more than one sessions, you can use the following commands to mange them.

Switching To Next Session

ctrl + a + n

Switching To Previous Session

ctrl + a + p

Detaching A Session

If you want to detach from a session and return to your normal SSH terminal, you can run the following command

ctrl + a + d

Displaying List Of Current Sessions

If you want to display the list of the current sessions of the screen, you can run the following command in your normal SSH terminal

root@codesposts:~$ screen -ls

Re-attaching To A Session

To re-attach to a session, you can run the following command:

root@codesposts:~$ screen -r

If you have more than one sessions, then you need to provide the session ID of the session you want to reattach to. You can get the session ID by the above command displaying the list of all sessions.

root@codesposts:~$ screen -r 10356

Lock The Screen

You can also lock the screen in the session using the following shortcut command

ctrl + a + x