At a glance
screen -S NAMEstarts a named session andCtrl-a ddetaches from it.screen -lslists sessions,screen -r NAMEreattaches,screen -d -r NAMEtakes it over from another terminal.Ctrl-a [enters copy mode to scroll back.screen -S NAME -X quitkills a session from outside.
Install screen and start a named session
On Debian and Ubuntu, install GNU screen with sudo apt install screen; on Fedora, use sudo dnf install screen. Run screen -v to check the version. These examples were tested on screen 5.0.2 from July 2026 and on the 4.09.01 build in Debian 13, which behave the same for everything here.
sudo apt install screen
screen -v
screen -S trainGive every session a name so you can find it later: screen -S train opens one called train. Everything inside screen starts with Ctrl-a: press Ctrl-a, let go, then press the next key. Ctrl-a ? shows the full list.
Detach and reattach
Press Ctrl-a d to detach and leave everything running. Ctrl-a D D detaches and logs you out of the SSH session in one go.
Run screen -ls from your normal shell to see what is running. With nothing running it prints No Sockets found in ... and exits with status 1.
$ screen -ls
There is a screen on:
7583.train (10/30/26 00:12:21) (Detached)
1 Socket in /run/screen/S-user.To get back in, run screen -r train, or just screen -r when there is only one session. If the list shows (Attached) because an old SSH connection did not close cleanly, screen -d -r train detaches that terminal and attaches here; screen -D -r train logs the other side out too. screen -x train attaches without detaching anyone, so two terminals share one view.
| Keys or command | What it does |
|---|---|
screen -S NAME | Start a new session named NAME |
screen -dmS NAME cmd | Start a detached session running cmd |
Ctrl-a d | Detach and leave the session running |
Ctrl-a D D | Detach and log out of the SSH session |
screen -ls | List sessions |
screen -r NAME | Reattach to a detached session |
screen -d -r NAME | Detach it elsewhere and reattach here |
screen -x NAME | Attach next to another terminal, sharing the view |
Ctrl-a c | Create a window |
Ctrl-a n and Ctrl-a p | Next or previous window |
Ctrl-a " | Pick a window from a list |
Ctrl-a A | Name the current window |
Ctrl-a k | Kill the current window (asks first) |
Ctrl-a S and Ctrl-a | | Split top and bottom, or left and right |
Ctrl-a Tab | Move to the next region |
Ctrl-a [ | Enter copy mode to scroll and copy |
Ctrl-a ] | Paste |
screen -S NAME -X quit | Kill a session from outside |
Windows and split regions
One session can hold several shell windows. Ctrl-a c creates one, Ctrl-a n and Ctrl-a p step through them, Ctrl-a 0 to Ctrl-a 9 jump to a number and Ctrl-a Ctrl-a flips back to the last one.
With several open, Ctrl-a A names the current window so Ctrl-a " shows a readable list. Ctrl-a k kills a window after asking first, and Ctrl-a \ asks once and then kills all windows and the session.
Ctrl-a S splits the display top and bottom and Ctrl-a | splits it left and right. A new region starts out blank: press Ctrl-a Tab to move into it and Ctrl-a c to start a shell. Ctrl-a X removes the current region and Ctrl-a Q keeps only the one you are in.
Scrollback, copy mode and logs
screen keeps its own history for each window, so your terminal's scrollbar and mouse wheel usually do not show it. Press Ctrl-a [ (or Ctrl-a Esc) to enter copy mode. Move with the arrow keys or h, j, k, l, scroll half a page with Ctrl-u and Ctrl-d and a full page with Ctrl-b and Ctrl-f, and jump to the top or bottom with g and G.
Press Space or Enter once to set the first mark and again to set the second; that copies the text between them and leaves copy mode. Ctrl-a ] pastes it. Keys without a copy mode meaning, such as Esc or q, leave without copying.
The compiled default for defscrollback is only 100 lines, and Debian's /etc/screenrc raises it to 1,024. Start with screen -h 10000 or put defscrollback 10000 in ~/.screenrc so long output does not roll off the top.
echo 'defscrollback 10000' >> ~/.screenrc
screen -L -Logfile train.log -S train-L writes everything the window prints to a log file. Without -Logfile, screen writes screenlog.0 in the directory where you started it, where 0 is the window number. Ctrl-a H toggles logging for the current window, and screen flushes the log to disk every 10 seconds by default (logfile flush).
If logging was off, screen -S train -X hardcopy -h /tmp/train.txt dumps the window and its scrollback to a file.
Run a job in a detached screen
screen -dmS train python -u train.py starts a detached session running the command, but the window closes and the session ends as soon as the command exits. To keep the shell and the final output, start an empty session, type the command into it with stuff and quit it from outside when you are done:
screen -dmS train
screen -S train -X stuff 'python -u train.py\n'
screen -r train
screen -S train -X quitscreen expands $NAME variables inside the stuff string before the shell sees it, so write a literal dollar sign as \$. Other ways to keep a job alive without a multiplexer are in the nohup guide.
Checking on the job still means SSHing back in and reattaching just to see whether it finished.
FAQ
How do I detach from screen without stopping my program?
Press Ctrl-a, let go, then press d. You drop back to your normal shell while the session and everything in it keeps running. Ctrl-a D D detaches and logs you out of SSH in one step.
What do I do when screen -r says the session is attached?
Run screen -d -r train to detach it from the other terminal and attach it here. screen -D -r train also logs the other side out, and screen -x train attaches next to it instead.
How do I kill a screen session?
From outside, run screen -S train -X quit. From inside, Ctrl-a k kills the current window and Ctrl-a \ kills all windows and ends the session; both ask for confirmation first.
How do I scroll up in screen?
Press Ctrl-a [ to enter copy mode, scroll with the arrow keys, Ctrl-u and Ctrl-d or Ctrl-b and Ctrl-f, and press Esc to go back. To keep more than the compiled 100 lines, start with screen -h 10000 or add defscrollback 10000 to ~/.screenrc.
Where does screen save its log file?
With screen -L -Logfile train.log -S train it writes train.log. With -L alone or Ctrl-a H, it writes screenlog.0, numbered by window, in the directory where screen was started.
Check on long runs from your phone
Change one import and every tqdm bar prints a link you can open on any device. Get a Discord or Slack message when the run finishes, fails or stalls.
pip install tqdm-liveSources
- GNU screen manual gnu.org
- GNU screen home page gnu.org
- GNU screen 5.0.2 release announcement lists.gnu.org
- Debian 13 screen package packages.debian.org
- Fedora screen package packages.fedoraproject.org