Skip to content

How to Use the screen Command in Linux: Detach and Reattach

6 min read

Applies to: GNU Screen 4.9.1 (Ubuntu 26.04 and 24.04, Debian 13) and Screen 5.0.1 (AlmaLinux 10 from EPEL, Fedora 44); AlmaLinux 9 EPEL ships Screen 4.8.0

The screen command keeps a terminal session running on your server after you disconnect. Start a named session with screen -S name, run your command, then press Ctrl+a followed by d to detach. You can log out, and the job keeps running. Later, list your sessions with screen -ls and go back to one with screen -r name.

This is the usual way to run long jobs over SSH on a VPS or dedicated server: backups, large downloads, database imports, compiling software or a game server console. If the SSH connection drops, the job doesn’t stop.

1. Install screen

Ubuntu Server installs tmux by default, but not screen. Install it with your package manager:

# Ubuntu and Debian
sudo apt install screen

# AlmaLinux 9 and 10 (screen is in EPEL)
sudo dnf install epel-release
sudo dnf install screen

# Fedora
sudo dnf install screen

Check the version with screen -v. On Ubuntu 26.04 it prints Screen version 4.09.01 (GNU) 20-Aug-23; on AlmaLinux 10 it prints Screen version 5.0.1. The commands below work on both.

2. Start a named session and detach

  1. Connect to your server over SSH.
  2. Start a session with a name you will recognise later: screen -S backup
  3. Run your long command inside it, for example rsync -a /var/www/ /backup/www/.
  4. Press Ctrl+a, let go, then press d. Screen detaches and returns you to the normal shell:
[detached from 2542.backup]

You can now close the SSH window. The number (2542) is the process ID of the session; backup is the name you gave it.

3. List running sessions

$ screen -ls
There are screens on:
	2564.web	(10/10/26 04:51:40)	(Detached)
	2542.backup	(10/10/26 04:51:27)	(Detached)
2 Sockets in /run/screen/S-root.

Detached means nobody is connected and the session is waiting for you. Attached means a terminal is connected to it right now. Screen 5 prints the same list without the date column.

4. Reattach to a session

Use the name (or the number) from the list:

screen -r backup

If only one detached session exists, screen -r on its own is enough. These are the messages you may see instead, from our tests:

  • Several sessions exist, and you didn’t say which one:
    There are several suitable screens on:
    	2497.web	(10/10/26 04:53:49)	(Detached)
    	2490.backup	(10/10/26 04:53:46)	(Detached)
    Type "screen [-d] -r [pid.]tty.host" to resume one of them.
    Run screen -r backup (or screen -r 2490).
  • The name doesn’t exist: There is no screen to be resumed matching nosuch. Check the spelling with screen -ls. Sessions are stored per user (root’s are in /run/screen/S-root), so run the command as the user who started the session.
  • The session is still attached, usually because your old SSH connection broke and the server hasn’t noticed yet:
    There is a screen on:
    	2490.backup	(10/10/26 04:53:46)	(Attached)
    There is no screen to be resumed matching backup.
    Run screen -d -r backup. It detaches the old connection (that terminal shows a [remote detached from ...] message) and attaches yours.

Two related options: screen -x backup attaches without detaching anyone, so two people can watch and type in the same session. screen -d -R backup reattaches if the session exists and creates it if it doesn’t, which is handy in a login script.

What happens when SSH disconnects

We tested this by killing the terminal of an attached session, which is what a dropped SSH connection does. Screen detached the session automatically, and screen -ls showed it as (Detached) with the job still running. Reconnect over SSH and run screen -r.

5. Close or kill a session

  • From inside: type exit in the shell. When the last window closes, screen prints [screen is terminating] and the session is gone.
  • Kill the current window: press Ctrl+a then k, and answer y to Really kill this window [y/n].
  • From outside, without attaching: screen -S backup -X quit. This ends every program running in that session.

When no sessions are left, screen -ls prints No Sockets found in /run/screen/S-root.

Dead sessions appear when the screen process was killed or the server crashed. Screen 4.9 marks them (Dead ???) and screen -wipe removes them:

$ screen -wipe
There are screens on:
	2542.backup	(10/10/26 04:51:27)	(Detached)
	2564.web	(09/29/26 06:29:22)	(Removed)
1 socket wiped out.
1 Socket in /run/screen/S-root.

Screen 5.0.1 marks them (Remote or dead), and in our test screen -wipe left the entry in place. Check that the process is really gone (ps -p 66 prints only its header line and exits with code 1), then delete the leftover socket file, for example rm /run/screen/S-root/66.dead.

6. Windows inside one session

One session can hold several shells (screen calls them windows). Every shortcut starts with Ctrl+a, released, then one key:

  • c: create a new window
  • n and p: next and previous window
  • ": list windows and pick one with the arrow keys
  • A: rename the current window
  • [: copy mode, to scroll back through earlier output; move with the arrow keys and press Esc to leave (screen shows Copy mode aborted)
  • H: start or stop logging the window to a file
  • ?: show all key bindings
  • d: detach; k: kill the window

To see the windows of a session from outside, run screen -S backup -Q windows; our test printed 0- sh 1* logs (the * marks the active window).

7. Run a command in a detached session

For scripts and cron jobs, start a session that is detached from the beginning and runs one command:

screen -dmS web python3 app.py

The session ends when the command ends. To type a command into a running session without attaching, use stuff (the \n presses Enter):

screen -S backup -X stuff 'echo hello from stuff\n'

To save what is currently on its screen to a file, run screen -S backup -X hardcopy /tmp/backup.txt.

8. Log the output to a file

-L writes everything the session prints to screenlog.0 in the current directory. -Logfile chooses the file name. Both worked on Screen 4.9.1 and 5.0.1:

screen -dmS job2 -L -Logfile /root/job2.log bash -c "echo log line two; sleep 2"
cat /root/job2.log
log line two

9. Make it more comfortable with ~/.screenrc

The default scrollback is 1024 lines on Ubuntu and 1000 on AlmaLinux 10 (set in /etc/screenrc). For a longer history and no welcome page, create ~/.screenrc:

startup_message off
defscrollback 10000

New sessions pick it up; screen -S b -Q info then reported +10000 in our test.

screen, tmux or nohup?

For one command that only needs to survive logout, nohup is enough. If you want to come back to an interactive session, use screen or tmux. Both do the same job; tmux is installed on Ubuntu Server by default, so use whichever you already know.

Related: install and use tmux, start a command in the background and detach it, ways to keep SSH sessions and processes running after disconnection and record a terminal session with script.

Official documentation: GNU Screen manual.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Linux server management

hardening, patching, monitoring and automation for Ubuntu, Debian, RHEL and AlmaLinux.

Was this answer helpful?

Still stuck? Open a ticket