屏幕命令速查表

屏幕 Linux 命令备忘录

🖥️ GNU Screen Command Cheatsheet

GNU Screen is a terminal multiplexer that lets you run multiple shell sessions inside a single terminal window, detach them, and reattach later.

📦 Starting & Managing Screen Sessions

  • Start a new session
    screen 
  • Start with a session name
    screen -S session_name 
  • List running sessions
    screen -ls 
  • Attach to a session
    screen -r session_name 
  • Attach to the last detached session
    screen -r 
  • Kill a session
    screen -X -S session_name quit 

⌨️ Essential Key Bindings

Default prefix key: Ctrl-a (press before other keys)

Key BindingAction
Ctrl-a cCreate new window
Ctrl-a nNext window
Ctrl-a pPrevious window
Ctrl-a "List all windows
Ctrl-a 0..9Switch to window by number
Ctrl-a ARename current window
Ctrl-a dDetach session
Ctrl-a ?Help (show key bindings)
Ctrl-a kKill current window
Ctrl-a \Kill all windows & exit screen
Ctrl-a xLock screen
Ctrl-a SSplit screen horizontally
`Ctrl-a`
Ctrl-a tabSwitch focus between regions
Ctrl-a QClose all regions except current

🔀 Window & Region Management

  • Split horizontally
    Ctrl-a S 
  • Split vertically
    Ctrl-a | 
  • Switch region focus
    Ctrl-a tab 
  • Remove all splits except current
    Ctrl-a Q 

📂 Session Persistence

  • Detach session (keep running in background)
    Ctrl-a d 
  • Reattach session
    screen -r session_name 

⚙️ Configuration

  • Default config file:~/.screenrc
  • Example .screenrc:
    # Start with a named session startup_message off defscrollback 5000 hardstatus alwayslastline hardstatus string '%{= kG}[%H] %{= kw}%?%-Lw%?%{= kR}%n*%f %t%?(%u)%?%{= kw}%?%+Lw%? %{= kG}[%H]' 

🛠️ Useful Options

  • Scrollback buffer size
    screen -h 5000 
  • Execute command in new screen
    screen -dmS session_name command 
  • Log output
    Ctrl-a H 
    (toggle logging in current window)

🚪 Exiting Screen

  • Kill current window
    Ctrl-a k 
  • Kill all windows & quit
    Ctrl-a \ 
  • Exit normally
    • Close all programs in the window, then type exit.

🧠 Tips

  • Use named sessions (screen -S) to avoid confusion.
  • Combine with SSH to keep remote jobs running after disconnect.
  • Use .screenrc to customize behavior and status line.