Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Multitask in Linux Terminal With Screen

· 6 min read · Home Tech

In this article
  1. What GNU Screen Actually Does
  2. Starting and Detaching Sessions
  3. Managing Multiple Windows Within One Session
  4. Keeping Long Jobs Alive After Disconnect
  5. Screen vs. tmux: Which Should You Use?
  6. Common Problems and How to Fix Them
  7. Security and Safety Considerations
  8. Frequently Asked Questions

Yes, you can multitask in the Linux terminal with screen. GNU Screen is a terminal multiplexer that lets you run multiple shell sessions inside a single terminal window, keep them alive after you disconnect, and reattach to them later from any machine.

What GNU Screen Actually Does

Screen creates virtual terminals, each running its own shell or command, all managed from one physical terminal. When you start a long-running process inside a screen session and then detach, that process keeps running in the background even if you close the terminal, log out, or lose your SSH connection. This is the core difference from simply backgrounding a job with &: screen gives you a full interactive shell you can return to, not just a process you can check on.

Screen has been around since 1987 and is installed by default on most Linux distributions and macOS. It is one of the oldest and most widely used terminal multiplexers, which means it is stable, well-documented, and available on virtually any Unix-like system you will encounter.

Starting and Detaching Sessions

The most common workflow is to start a named session, run a command, detach, and reattach later. Here are the essential commands:

  • screen -S work — start a new session named "work"
  • screen -ls — list all active sessions
  • screen -r work — reattach to the session named "work"
  • screen -d -r work — detach from elsewhere and reattach here
  • screen -X -S work quit — kill the session from outside

Inside a session, press Ctrl-a followed by d to detach. The session keeps running; you get back to your normal prompt. To exit a session entirely, type exit or press Ctrl-d in the shell.

Managing Multiple Windows Within One Session

Screen also lets you split one session into multiple windows, each running its own shell. This is handy for editing a file in one window while compiling in another, or tailing a log while running commands.

  • Ctrl-a c — create a new window
  • Ctrl-a n / Ctrl-a p — switch to next / previous window
  • Ctrl-a " — list all windows and pick one
  • Ctrl-a A — rename the current window
  • Ctrl-a k — kill the current window

You can also split the screen into regions: Ctrl-a S splits horizontally, Ctrl-a | splits vertically, and Ctrl-a Tab moves focus between regions. Each region can show a different window, giving you a poor-man's tmux layout.

Keeping Long Jobs Alive After Disconnect

The most common reason people use screen is to run a long job over SSH and not worry about the connection dropping. For example, if you are compiling a large program or transferring a big file, you start screen, launch the job, detach, and go do something else. When you come back, you reattach and see the output exactly as if you never left.

This works because screen's server process runs independently of your terminal. The moment you detach, the session is owned by the screen daemon, not by your SSH connection. Even if your laptop sleeps or your network drops, the process continues on the remote machine. When you reconnect, screen -r brings you back to the same state.

One caveat: if the machine itself reboots, all screen sessions are lost unless you set up screen to restart them. That is a more advanced configuration involving screenrc and systemd, but for most uses, screen survives disconnects, not reboots.

Screen vs. tmux: Which Should You Use?

tmux is the main alternative to screen. Both do the same basic job, but they differ in several ways:

  • Age and availability: screen is older and more likely to be pre-installed. tmux is newer and often needs to be installed.
  • Configuration: screen uses ~/.screenrc; tmux uses ~/.tmux.conf. tmux's config syntax is generally considered cleaner.
  • Split panes: tmux handles vertical and horizontal splits more intuitively out of the box. Screen's region system works but feels more clunky.
  • Status bar: tmux shows a customizable status bar with session, window, and system info. Screen has no built-in status bar; you have to configure one with hardstatus.
  • Scripting: tmux has a more powerful command interface for scripting and automation. Screen's scripting is older and less flexible.

If you are already comfortable with screen, there is no urgent reason to switch. If you are starting fresh, many users find tmux easier to learn because of its better defaults and documentation. But screen's ubiquity means it is often the only option on a minimal server, so knowing it is still valuable.

Common Problems and How to Fix Them

Screen is reliable, but a few issues come up regularly.

"Cannot open your terminal '/dev/pts/0' - please check." This usually happens when you try to reattach from a terminal that does not support the required features. The fix is to run script /dev/null before starting screen, or to use screen -x instead of -r.

Multiple sessions with the same name. If you start two sessions both named "work", screen -r work will fail because it is ambiguous. Use screen -ls to see the session IDs, then reattach with the full ID like screen -r 12345.work.

Scrolling doesn't work. By default, screen's scrollback is disabled. You can enter copy mode with Ctrl-a [, then use arrow keys or Page Up/Page Down to scroll. Press Esc to exit copy mode. To increase scrollback, add defscrollback 10000 to your ~/.screenrc.

Screen sessions die after reboot. As mentioned, screen does not survive a reboot. You need to either restart your jobs manually or set up a systemd service that starts screen sessions at boot. A simple approach is to add screen -dmS name command to a startup script.

Security and Safety Considerations

Screen sessions are owned by your user, so other users on the same machine cannot attach to them unless they have root access. However, if you leave a session running with sensitive data visible, anyone who can become you (e.g., via sudo) can see it. Always detach before walking away, and use screen -X lock to lock the screen if you need to step away temporarily.

Another point: screen does not encrypt your session. If you are using screen over an unencrypted connection like telnet, your keystrokes and output are visible on the network. Always use SSH or a VPN for remote work.

Finally, be careful with Ctrl-a k — that kills the current window, and if it is the only window, it ends the session. There is no undo. If you meant to detach, press Ctrl-a d instead.

Frequently Asked Questions

How do I exit a screen session without killing it?

Press Ctrl-a then d to detach. The session keeps running in the background. You can reattach later with screen -r.

Can I share a screen session with another user?

Yes, but only if you both have the same user account, or if you use multi-user mode with multiuser on and acladd in your screenrc. By default, sessions are private to the user who started them.

Why does my screen session die when I close the terminal?

It shouldn't. If it does, you likely never detached properly, or you are using screen in a way that ties it to the terminal (e.g., running screen inside another screen without the right options). Make sure you detach with Ctrl-a d before closing.

How do I copy text from a screen session?

Enter copy mode with Ctrl-a [, move the cursor to the start of the text, press Space, move to the end, press Space again to copy. Then paste with Ctrl-a ].

What is the difference between screen and nohup?

nohup runs a command and detaches it from the terminal, but you cannot reattach to it interactively. Screen gives you a full interactive session you can return to. Use nohup for simple fire-and-forget commands; use screen for anything you might want to interact with later.

Can I run graphical programs inside screen?

Screen only handles text. If you need a graphical interface, use X11 forwarding with SSH (ssh -X) or a VNC server. Screen is for terminal-based applications.