Introduction to screen command
screen is a command-line utility that multiplexes a physical terminal between several processes in Linux. It allows you to run multiple terminals from a single session.
With screen, you can disconnect from the terminal session and reconnect again to resume where you left it. You can continue even after you close the terminal or log out the user.
You can also connect remote computers to the screen session. While running an ssh session on a remote machine, the session may get disconnected. In such cases, screen comes in handy as you can resume the work in the terminal.
How to install screen
screen
may not be available by default in your system. You can use these commands to install screen
according to your Linux distros.
Install screen on CentOS, RHEL, Fedora
$ sudo dnf install screen
Install screen on Ubuntu and Debian
$ sudo apt install screen
Syntax to use screen command
The syntax for screen
command is as follows:
$ screen [-options] [cmd[args]]
Different examples to use screen command
1. Start a screen session
You can simply run screen
command to open a new terminal in the screen session.
$ screen
Sample Output:
After opening the screen session, you can press Ctrl+a
?
to view the list of all commands.
2. Start a named session
When creating a new session, you can specify a name for the session using the -S
option.
$ screen -S session_name
For example, the following command starts a screen session named ubuntu
.
$ screen -S ubuntu
You can use this name to reattach the session later.
3. Display the attached screen session
The screen command with -ls
flag shows all the screen sessions on the system. It includes both attached and detached sessions.
$ screen -ls
Sample Output:
4. Detach from the screen session
To detach from the screen session, you can press Ctrl+a
and d
character.
Or, you can also use the -d
option followed by the screen name or id.
$ screen -d ubuntu
OR
$ screen -d 2729
5. Reattach the screen session
The -r
option is used to reattach the screen session.
$ screen -r ubuntu
OR
$ screen -r 2729
Sample Output:
To verify the change, use the screen -ls
command.
6. Create a new windows
You can have multiple windows (from 0 to N) in a screen session. To create a new window with the existing shell, you can use Ctrl+a
followed by c
character.
The shell will be assigned to the first available number from 0 to N.
You can view the number and title of a window by pressing Ctrl+a
and N
.
7. Switch between windows
After creating multiple windows, you will need to switch between them. This can be done by pressing Ctrl+a
followed by the Space
or the shell number.
For example, to switch to shell number 3, press Ctrl+a
then 3
.
Ctrl+a
and n
: switch to the next window
Ctrl+a
and p
: switch to the previous window
8. Lock a screen session
To lock the screen session, you need to press Ctrl+a
and x
. This method uses your system's password to lock the session.
Sample Output:
Screen used by Rohan Timalsina <golinux> on ubuntu-PC. Password:
You can use your Linux password to unlock the screen.
9. Kill the screen session
You can press Ctrl+a
and K
to kill or terminate the screen window. It will prompt you for confirmation.
Ready kill this window [y/n]
Enter y
destroy the window.
Conclusion
Now you should have understood how to use the screen command and take advantage of multiple terminal sessions in Linux. screen can be useful when you need to run multiple programs from a terminal. It also ensures that your work is not lost after the terminal is closed.
Please let us know via the comments if you have any questions or feedback.
What's Next
15+ SSH command examples in Linux [Cheat Sheet]
How to transfer files over SSH with SSHFS in Linux & Windows
Further Reading