Skip to content

Screen Command Linux

screen command in Linux provides the ability to launch and use multiple shell sessions from a single ssh session. When a process is started with ‘screen’, the process can be detached from session & then can reattach the session at a later time. When the session is detached, the process that was originally started from the screen is still running and managed by the screen itself. The process can then re-attach the session at a later time, and the terminals are still there, the way it was left.

Syntax:

screen [-opts] [cmd [args]]

Escape key

All screen commands are prefixed by an escape key, by default C-a (that’s Control-a, sometimes written ^a). To send a literal C-a to the programs in screen, use C-a a. This is useful when when working with screen within screen. For example C-a a n will move screen to a new window on the screen within screen.
 

Getting in

screen -S <name>Start a new screen session with session name
screen -lsList running sessions/screens
screen -xAttach to a running session
screen -r <name>Attach to a running session with name
screen -dRRThe “ultimate attach”Attaches to a screen session. If the session is attached elsewhere, detaches that other display. If no session exists, creates one. If multiple sessions exist, uses the first one.

Getting out

screen -d <name>Detach a running session
C-a dDetach
C-a D DDetach and logout (quick exit)
“C-a : quit”Exit screenor exit all of the programs in screen
C-a C-\Force-exit screen(not recommended)

Help

C-a ?See helpLists keybindings. The man page is the complete reference, but it’s very long.

Window Management

C-a cCreate new window
C-a C-aChange to last-visited active window(commonly used to flip-flop between two windows)
C-a <number>Change to window by number(only for windows 0 to 9)
C-a ‘ <number or title>Change to window by number or name
C-a n or C-a <space>Change to next window in list
C-a p or C-a <backspace>Change to previous window in list
C-a “See window list(allows you to select a window to change to)
C-a wShow window bar(if you don’t have window bar)
C-a kKill current window(not recommended)
C-a \Kill all windows(not recommended)
C-a ARename current window

Split screen

C-a SSplit display horizontally
C-a |Split display verticallyor C-a V (for the vanilla vertical screen patch)
C-a tabJump to next display region
C-a XRemove current region
C-a QRemove all regions but the current one

Scripting

Send a command to a named sessionscreen -S <name> -X <command>
 
Create a new window and run ping example.comscreen -S <name> -X screen ping example.com
 
Stuff characters into the input buffer using bash to expand a newline character (from here)screen -S <name> [-p <page>] -X stuff $’quit\r’
 
A full example# run bash within screenscreen -AmdS bash_shell bash # run top within that bash sessionscreen -S bash_shell -p 0 -X stuff $’top\r’ # … some time later # stuff ‘q’ to tell top to quitscreen -S bash_shell -X stuff ‘q’ # stuff ‘exit\n’ to exit bash sessionscreen -S bash_shell -X stuff $’exit\r’

Misc

C-a C-lRedraw window
C-a [Enter copy modeor C-a <esc> (also used for viewing scrollback buffer)
C-a ]Paste
C-a MMonitor window for activity
C-a _Monitor window for silence
C-a C-vEnter digraph (for producing non-ascii characters)
C-a xLock (password protect) display
C-a :Enter screen command
C-a HEnable logging in the screen session