screen commands and shortcuts cheat sheet
Installation and startup
-
sudo apt install screenDebian and Ubuntu: installs screen.
-
sudo dnf install epel-releaseAlmaLinux and Rocky Linux: the screen package is in the EPEL repository, so you enable it before installing.
-
sudo dnf install screenAlmaLinux and Rocky Linux: installs screen after you enable the EPEL repository.
-
screen -vShows the screen version installed on the server.
-
screenOpens a new unnamed session. For everyday work, it's better to name the session so it's easy to get back to.
Sessions: open, detach, and reattach
-
screen -S workOpens a new session named work.
-
Ctrl+a dDetaches the terminal from the session. The session and all its processes keep running on the server, even after the SSH connection is closed.
-
screen -lsLists your sessions, each in the form pid.tty.host, with the time it was opened and whether it is attached to a terminal or detached.
-
screen -r workReattaches to the detached work session, for example after your SSH connection dropped.
-
screen -rReattaches to a detached session without naming it. Useful when there is only one detached session on the server.
-
screen -d -r workAttaches to the work session, and if it is still attached to another terminal, for example an old connection that dropped, detaches it first.
-
screen -D -RAttaches right away: when a session exists, it detaches other terminals from it, logs them out of the server as well, and attaches to it. When there is no session, it opens a new one.
-
screen -x workAttaches to the work session even when it is already attached to another terminal, without detaching it. Both terminals see the same screen, for example for working together or watching from a second connection.
-
screen -d -m -S job ./long-task.shOpens a session named job in the background, without attaching to it, and runs the script in it. While the script runs, you can attach to the session with screen -r job, and when the script finishes, the session closes.
-
Ctrl+a D DDetaches the terminal from the session and also logs you out of the server. The session keeps running.
-
screen -wipeLists the sessions like screen -ls, and removes sessions whose process no longer exists (dead) from the list.
-
Ctrl+a \Caution: closes all windows in the session and the processes running in them, and ends screen.
The Ctrl+a command key
-
Ctrl+aThe screen command key. Press Ctrl+a, release it, and then press the command key. For example, Ctrl+a d means Ctrl+a followed by the d key.
-
Ctrl+a aSends Ctrl+a to the program running in the window, for example to jump to the start of the line in Bash.
-
Ctrl+a ?Lists the screen shortcuts.
-
Ctrl+a :Opens the screen command prompt at the bottom of the screen. At this prompt, you type screen commands, for example scrollback 5000.
-
escape ^BbA line in ~/.screenrc that sets Ctrl+b as the command key instead of Ctrl+a. The b after the key combination means that pressing Ctrl+b followed by b sends Ctrl+b to the program in the window.
-
screen -e ^Bb -S workOpens a session with Ctrl+b as the command key, without changing the configuration file.
Windows
-
Ctrl+a cOpens a new window with a shell and switches to it. Each window fills the whole screen.
-
Ctrl+a n / Ctrl+a pSwitch to the next and the previous window, respectively.
-
Ctrl+a 0...9Switches to a window by its number, from 0 to 9.
-
Ctrl+a 'Prompts for a window number or name and switches to that window.
-
Ctrl+a "Shows a list of all windows and lets you choose a window with the arrow keys and Enter.
-
Ctrl+a wBriefly shows the list of windows, with the number and name of each window, in the message line at the bottom of the screen.
-
Ctrl+a Ctrl+aSwitches back to the window that was displayed before the current one.
-
Ctrl+a ARenames the current window, for example to logs, so it's easy to spot in the window list.
-
Ctrl+a kCaution: closes the current window and the process running in it.
Splitting the screen into regions
-
Ctrl+a SSplits the current region in two, top and bottom. The new region opens empty.
-
Ctrl+a |Splits the current region in two, left and right.
-
Ctrl+a TabMoves the focus to the next region. In an empty region, press Ctrl+a c for a new window, or Ctrl+a and a number to show an existing window in it.
-
Ctrl+a XCloses the current region. The window that was shown in it keeps running, and you can get back to it from the window list.
-
Ctrl+a QCloses all regions except the current one. The windows themselves keep running.
Scrolling, search, and copy (copy mode)
-
Ctrl+a [Enters copy mode, where you can scroll back through the window's output, search it, and copy from it. Ctrl+a Esc also enters this mode.
-
h j k lIn copy mode: move the cursor left, down, up, and right. The arrow keys also move the cursor.
-
Ctrl+b / Ctrl+fIn copy mode: scroll one full screen up and down, respectively.
-
g / GIn copy mode: go to the start and to the end of the history.
-
/ / ?In copy mode: search forward and backward. Type the text and press Enter, and the n key moves to the next match.
-
SpaceIn copy mode: the first press sets the start of the selection, and the second press sets its end and copies the selected text. Enter works the same way.
-
EscExits copy mode. Any key that has no function in copy mode exits it.
-
Ctrl+a ]Pastes the text most recently copied in copy mode into the current window.
Logs, monitoring, and locking
-
Ctrl+a HStarts or stops saving the current window's output to the file screenlog.n, where n is the window number. The file is saved in the directory where the window was opened.
-
screen -L -S workOpens a session named work and saves the output of its windows to a log file from the very start.
-
screen -L -Logfile ~/work.log -S workLike the previous command, with a log file name of your choice instead of screenlog.0.
-
Ctrl+a MStarts or stops monitoring the current window for activity. When output appears in it while you work in another window, a message is shown in the message line.
-
Ctrl+a _Starts or stops monitoring the current window for silence: a message is shown when there is no output in it for 30 seconds, for example when a long installation has finished.
-
Ctrl+a xLocks the terminal. screen accepts no commands until the lock is released.
Sending commands to a session from outside
-
screen -S work -X stuff $'uptime\n'Types the uptime command and Enter in the current window of the work session, as if they were typed on the keyboard, without attaching to the session. Caution: the command runs immediately, so first check which window is active in the session.
-
screen -S work -X quitCaution: closes the work session, all its windows, and the processes running in them.
The ~/.screenrc configuration file
-
startup_message offTurns off the startup screen with the copyright notice that is shown every time screen starts.
-
defscrollback 10000Sets how many lines of history are kept in each new window. The default is only 100 lines, and a very high value increases memory use.
-
caption alwaysShows a caption line at the bottom of the screen with the window's number and name, even when only one window is shown.
-
deflog onTurns on saving the output to a log file in every new window.
-
logfile flush 1Writes the output to the log file every second, so you can follow it almost in real time, for example with tail -f.
-
:source ~/.screenrcAt the screen command prompt: reloads the configuration file after you edit it, without closing the session. Settings that apply to new windows, such as defscrollback, take effect only in the windows you open afterward.
No matching items.
Sessions, windows, and regions
GNU Screen runs your terminal inside a session that is kept on the server. When the SSH connection drops, the session keeps running, and so does an update, a file copy, or a long script running in it. After you reconnect, you run screen -r and pick up exactly where you left off. Each session can have several windows, and you can split the screen into several regions, each showing a different window, for example editing a file in one region while following a log in another.
How to read the shortcuts on this page
Almost every screen shortcut starts with the command key Ctrl+a. Press it, release it, and only then press the next key. The shortcuts in the copy mode category work only after you enter that mode, without Ctrl+a before them. Commands that start with a colon are typed at the screen command prompt, which opens with Ctrl+a :, without the colon. The lines in the last category go in the ~/.screenrc file in your home directory, and they are loaded the next time screen starts.
Detaching with Ctrl+a d leaves the session running, while the exit command in the last window of the session closes it. If you prefer tmux, the equivalent commands are on the tmux commands and shortcuts page.
On a Linux VPS, you connect over SSH with full root access, so you can install screen and use it to run updates, backups, and long tasks that keep running even when the connection from your computer drops.
screen commands and shortcuts FAQ
What are the benefits of GNU Screen when working on a server?
GNU Screen keeps your work environment on the server itself. A long-running command, such as a system update or a large file copy, keeps running even when your SSH connection drops or you shut down your computer, and you can get back to it from any computer. On top of that, a single connection lets you open several windows and split the screen, for example editing a configuration file next to the service log.
How do I get back to a screen session after my SSH connection drops?
Connect to the server again and run screen -ls to see your sessions, and then run screen -r followed by the session name, for example screen -r work. When the session is still marked as attached because the previous connection didn't close properly, the screen -d -r work command detaches it and attaches to it.
How do I scroll back through the output in screen?
Press Ctrl+a followed by [ to enter copy mode, and then scroll with the arrow keys, or a full screen at a time with Ctrl+b and Ctrl+f. The Esc key takes you back to the regular screen. By default, only 100 lines of history are kept in each window, so we recommend adding the line defscrollback 10000 to ~/.screenrc.
Can I change the Ctrl+a command key?
Yes. Add the line escape ^Bb to ~/.screenrc, and from the next time you start screen, all shortcuts start with Ctrl+b instead of Ctrl+a. If you keep Ctrl+a, you can still send it to the program in the window, for example to jump to the start of the line in Bash, by pressing Ctrl+a followed by a.
What is the difference between screen and tmux?
Both tools keep a session on the server that keeps running after the connection drops, and both let you work in several windows and split the screen. The main difference in everyday work is the command key: Ctrl+a in screen and Ctrl+b in tmux, and the commands and configuration file settings also differ. The choice is mostly a matter of habit, and the tmux commands are on the tmux commands and shortcuts page.