GNU screen is the original terminal multiplexer. Start a session, run a long task, detach, log out — the task keeps running, and you can reattach later from anywhere. It’s preinstalled on many systems and still widely used for game servers, long migrations and scripts.

Install

sudo apt install screen     # Debian/Ubuntu
sudo dnf install screen     # RHEL family (EPEL on some releases)

The command key

screen shortcuts start with Ctrl+a, then a key — written as C-a below.

Start, detach, reattach

screen                  # new session
screen -S migrate       # new named session

Run your command, then:

Keys Action
C-a d Detach — the session keeps running

Later:

screen -ls              # list sessions
screen -r migrate       # reattach
screen -r               # reattach if there's only one
screen -d -r migrate    # detach it elsewhere (e.g. a dead connection) and reattach here
screen -x migrate       # attach to a session that's already attached (shared view)

If your SSH connection dropped, the session may still show as “Attached”. screen -d -r fixes that.

End a session

Type exit in the last window, or press C-a k (kill window) and confirm. From outside:

screen -S migrate -X quit

Windows

Keys Action
C-a c Create a new window
C-a n / C-a p Next / previous window
C-a 0…9 Go to window number
C-a " List windows and choose
C-a A Rename the window
C-a k Kill the current window

Split regions

Keys Action
C-a S Split horizontally
C-a | Split vertically
C-a Tab Move to the next region
C-a c Open a new window in an empty region
C-a X Close the current region
C-a Q Close all regions except the current one

Scrollback and copy

Press C-a [ (or C-a Esc) for copy mode. Scroll with arrows or Page Up/Down; search with /. Press Space to start and end a selection, then C-a ] to paste. Esc exits copy mode.

Increase the scrollback buffer in ~/.screenrc: defscrollback 10000.

Logging a session

screen -L -S import       # log everything to screenlog.0

Or toggle with C-a H inside a running window. Useful for keeping a record of long maintenance work.

Start a detached session from a script

screen -dmS backup bash -c 'rsync -a /data/ backup:/data/; exec bash'
screen -S backup -X stuff 'echo hello\n'      # send keystrokes to it

The -dm combination starts a session already detached. This pattern is common for running game servers (Minecraft, Terraria) from scripts — sending stop to the console with -X stuff for clean shutdowns. For new setups, a systemd service is more robust.

A simple ~/.screenrc

startup_message off
defscrollback 10000
hardstatus alwayslastline
hardstatus string '%{= kG}[ %H ] %{= kw}%-w%{= BW}%n %t%{-}%+w %=%{= kG}%Y-%m-%d %c'
termcapinfo xterm* ti@:te@

This hides the splash screen, increases scrollback, adds a status bar with the host, windows and time, and lets your terminal’s own scrollbar work.

screen vs tmux

screen tmux
Prefix key C-a C-b
Splits Yes (less convenient) Yes, flexible panes
Scripting/control -X commands Rich command interface
Configuration .screenrc .tmux.conf
Availability Very widespread Default on many newer systems
Serial consoles screen /dev/ttyUSB0 115200 No

Both solve the main problem — keeping work alive across disconnects. If you’re starting fresh, tmux is generally more pleasant; if screen is what’s installed, it does the job.

Troubleshooting

  • There is no screen to be resumed — wrong name or the session ended. screen -ls to check.
  • Cannot open your terminal '/dev/pts/1' after su to another user — run script /dev/null first, then screen, or start screen as that user via sudo -iu user.
  • Ctrl+a doesn’t jump to the start of the line — screen captures it; press C-a a to send a literal Ctrl+a.

Related: nohup and background jobs for fire-and-forget commands.

  • screen
  • Linux
  • SSH