Linux screen Command: Create and Manage Detachable Terminal Sessions

screen is a terminal multiplexer that allows you to create multiple virtual terminals within a single physical terminal, and to detach sessions and later reattach them. It is useful when you need to maintain an interactive working environment even if your SSH connection drops. The basic flow is: create a session → run tasks → detach with Ctrl+A then D → reconnect with screen -r.

If your goal is simply to run non-interactive commands in the background, nohup might be simpler. On the other hand, if you need to reopen the running screen to input commands, screen or tmux is suitable.

When is screen used?

GNU Screen multiplexes a single terminal into virtual terminals used by multiple processes. Programs in each window continue to run even when they are not visible on the screen or the entire session is detached. Detailed behavior and full commands can be found in the GNU Screen official documentation.

  • When running long-running tasks such as compilation, data processing, or backups on a server accessed via SSH
  • When you need to detach the task screen and reconnect from another computer
  • When operating multiple terminal windows such as shells, log monitoring, and editors within a single SSH connection
  • When you need to later check the screen of a running interactive program

screen sessions cannot withstand server reboots, nor do they automatically restart when a program fails. For continuous services, use a service manager like systemd.

Installing and Starting Your First Session

Installing screen

Install it using the package manager according to your distribution. You may need administrator privileges.

sudo apt update
sudo apt install screen
sudo dnf install screen
sudo pacman -S screen

You can check installation status and version as follows.

screen --version

Creating a Named Session

Using the -S option to name the session makes it easier to distinguish between multiple tasks. Use a short string for the name that reflects the purpose of the task.

screen -S backup

When a new Screen window opens, execute commands inside it as usual. You can check whether the current shell is inside Screen by using environment variables.

printf '%s\n' "$STY"

Starting a session with a command

You can also specify a program to run while creating a session.

screen -S backup ./backup.sh

If the specified program finishes and there are no other windows in the session, the Screen session may also end. If you need to check the results on the screen after the work is done, it is more convenient to first create a shell session and then run commands inside it.

Frequently used Screen shortcuts

The Screen command basically works in a two-step method: press Ctrl+A and then the next key. The Ctrl+A, D below means that you release Ctrl+A before pressing D, not pressing the two keys simultaneously.

Shortcut keys Function Description
Ctrl+A, D Detach session Exit the current terminal while keeping the program running.
Ctrl+A, C Create a new window Create a new shell window in the current session.
Ctrl+A, N Next window Move to the next virtual terminal window.
Ctrl+A, P Previous window Move to the previous virtual terminal window.
Ctrl+A, " Window list Select the window to move from the list.
Ctrl+A, A Rename window Give the current window an easily recognizable title.
Ctrl+A, [ Copy/Scroll mode Browse previous output or copy text.
Ctrl+A, H Toggle logging Start or stop logging the output of the current window to a log file.
Ctrl+A, K Close current window After confirmation, remove the current virtual window.
Ctrl+A, ? Help Displays the current list of key bindings.

If you need to send Ctrl+A to the program itself, use Ctrl+A followed by A. Shortcut keys may vary depending on the .screenrc configuration.

Detach and reconnect a session

Detach while keeping the work

Press Ctrl+A followed by D inside a Screen session to detach the session. After detaching, the programs inside the session will continue to run even if the SSH connection is closed.

Even if the terminal connection is unexpectedly lost, if the default autodetach setting is enabled, the session will automatically detach.

Checking the list of sessions

screen -ls

The output shows session identifiers and statuses like Detached and Attached. If there are multiple sessions, you need to specify the name or full identifier to connect.

Reattaching to a detached session

screen -r backup

If a session is connected to another terminal, check the list first. Use the following form only if you are sure you want to detach the previous connection and attach it to the current terminal.

screen -d -r backup

-d -r can interrupt other users' normal work screens, so be careful in shared accounts or collaborative environments.

Using multiple windows in one session

Creating and naming windows

After creating a new window with Ctrl+A, C, you can name it with Ctrl+A, A. For example, you can use the first window to run tasks, the second window to check logs, and the third window to monitor system status.

Moving Between Windows

You can move to the next or previous window using Ctrl+A, N and Ctrl+A, P. If there are many windows, it is faster to open the list with Ctrl+A, " and select the desired window.

Properly Closing Windows and Sessions

Executing exit in the current shell will close that window. When the last window is closed, the Screen session ends as well.

exit

If you simply want to detach a session, you should use Ctrl+A, D instead of exit. Forcibly closing a window with running programs may corrupt their work results.

Using Scroll and Log History

Browsing Previous Output

Press Ctrl+A, [ to enter copy/scroll mode. You can use the arrow keys, Page Up, and Page Down to look at previous output and press Esc to exit. The number of preserved lines is affected by the settings and the -h option.

screen -h 10000 -S analysis

Recording Window Output to a File

Press Ctrl+A, H to toggle the logging of the current window's output. The default log file name is usually screenlog.0, and you can also enable automatic logging when starting a new session.

screen -L -Logfile backup-screen.log -S backup

Logs may include passwords, tokens, and personal information displayed on the screen. Check the storage location and file permissions, and for long-running tasks, establish a separate retention policy to prevent logs from growing indefinitely.

Problem Situations and Security Precautions

When a session remains in Attached state

Immediately after a network disconnection, a session may remain Attached. First, confirm whether it is actually in use on another terminal, then detach the previous display with screen -d -r session_name and connect it to the current terminal.

Dead or inaccessible session entries can be cleaned up with screen -wipe after thoroughly confirming that no processes are running.

Access permissions for sessions of the same account

Screen sessions follow the permissions of the operating system user account. If multiple people share the same account, others can reconnect to the session, so avoid sharing accounts and separate SSH keys from account privileges. It is also important not to leave the session with an administrative shell or sensitive screen open when away.

Comparison of screen, nohup, tmux, systemd

Tools Suitable tasks Key features
screen Interactive terminal tasks that need reconnection Supports session detachment, reconnection, and multiple windows.
tmux Interactive tasks and complex screen splitting Rich features for managing windows and panels, with extensive user customization options.
nohup Simple non-interactive batch tasks Ignores SIGHUP but does not restore the terminal screen.
systemd Services that must always be running You can configure boot linkage, restart on failure, dependencies, and log management.

You can see how to run non-interactive tasks even after closing the terminal in How to use the nohup command.

Frequently Asked Questions

Will the task continue even if the SSH connection is disconnected?

If Screen's automatic detach works properly and the server continues to run, the programs inside the session are also maintained. However, this does not prevent server reboots, errors in the process itself, or termination by the administrator.

What should I do if multiple sessions appear when using screen -r?

After checking the list with screen -ls, specify the session name or full identifier like screen -r backup. Creating sessions with meaningful names can reduce selection mistakes.

What should I do if Ctrl+A conflicts with a program's shortcut?

You can send the Ctrl+A character to the program by pressing A after Ctrl+A. To change Screen's command key itself, you can use the -e option or configure it in .screenrc, but you also need to adjust the shared documentation and shortcuts in the team.

Can Screen sessions be restored after a reboot?

No. Screen only maintains the processes and virtual terminals on a running system; it does not automatically restore sessions and processes after a reboot. If you need automatic startup after a reboot, configure the service with systemd or use a scheduler suitable for the job.

Summary

The basic usage is to create a session with screen -S name, detach with Ctrl+A, D, and reconnect with screen -r name. By using multiple windows and the logging function, you can systematically continue interactive work even over unstable remote connections.

However, Screen is not a service monitoring tool. Programs that require reboot integration or automatic recovery should be operated with a service manager, and Screen should be used for terminal tasks that require a person to reconnect and operate.

More in This Category
Linux clear Command: Clear the Terminal Display

Linux clear Command: Clear the Terminal Display

Learn how to use the Linux clear command to clear the visible terminal screen and understand scrollback behavior, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux history Command: Review and Reuse Shell Command History

Linux history Command: Review and Reuse Shell Command History

Learn how to use the Linux history command to review, search, reuse, and manage shell command history, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux Tutorial / Install and Update Packages with APT and DNF

Linux Tutorial / Install and Update Packages with APT and DNF

Compare APT on Debian or Ubuntu with DNF on Rocky Linux for searching, installing, updating, and removing packages, including repository and transaction checks.

Linux Tutorial / Read Files with cat, less, more, head, and tail

Linux Tutorial / Read Files with cat, less, more, head, and tail

Learn when to use cat, less, more, head, and tail to read Linux files, with a small reproducible example and guidance for long files and logs.

Linux chgrp Command: Change a File's Group

Linux chgrp Command: Change a File's Group

Learn how to change a File's Group with the Linux chgrp command, including practical examples, key options, and important precautions.

Linux systemctl Command: Manage systemd Services

Linux systemctl Command: Manage systemd Services

Learn how to manage systemd Services with the Linux systemctl command, including practical examples, key options, and important precautions.

Linux sudo Command: Run Commands with Elevated or Another User's Privileges

Linux sudo Command: Run Commands with Elevated or Another User's Privileges

Learn how to use the Linux sudo command to run approved commands with elevated privileges or as another user, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux rmdir Command: Remove Empty Directories

Linux rmdir Command: Remove Empty Directories

Learn how to remove empty directories with Linux rmdir, delete empty parent paths, diagnose failures, and understand when rm -r is different.

Linux uptime Command: Check Uptime and Load Averages

Linux uptime Command: Check Uptime and Load Averages

Learn how to use the Linux uptime command to check how long the system has been running and interpret load averages, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Best Linux Distributions for Web Servers: Ubuntu, Debian, or Rocky Linux?

Best Linux Distributions for Web Servers: Ubuntu, Debian, or Rocky Linux?

Compare Ubuntu Server, Debian, and Rocky Linux for web hosting based on support lifecycle, package freshness, documentation, operational stability, and application requirements.