Overview#
Screen is a powerful terminal multiplexer that allows you to run multiple full-screen windows (virtual terminals) on a single physical terminal. This is a particularly useful tool, especially when you need to run long-running processes or keep sessions from being disconnected in remote sessions. Screen allows applications to run in the background.
Installation#
Installation on Linux:
sudo apt install screen
Installation on Mac:
brew install screen
Options#
Option | Description |
---|---|
-4 | Only resolve the hostname to an IPv4 address. |
-6 | Only resolve the hostname to an IPv6 address. |
-A -[r|R] | Adapt all windows to the new display width and height. |
-c file | Read configuration file instead of '.screenrc'. |
-d (-r) | Detach the running screen (and reattach here). |
-dmS name | Start as a daemon: run screen in detached mode. |
-D (-r) | Detach and logout remote connections (and reattach here). |
-D -RR | Do whatever is needed to get a screen session. |
-f | Enable flow control, -fn = off, -fa = auto. |
-h lines | Set the size of the scrollback buffer. |
-i | Interrupt output earlier when flow control is on. |
-l | Turn on login mode (update /var/run/utmp), -ln = off. |
-ls [match] | Do nothing, just list our SockDir [on possible matches]. |
-L | Turn on output logging. |
-Logfile file | Set the name of the log file. |
-m | Ignore $STY variable, create a new screen session. |
-O | Choose optimal output rather than exact vt100 emulation. |
-p window | Preselect the named window if it exists. |
-q | Quiet startup. Exit with non-zero return code if unsuccessful. |
-r [session] | Reattach to a detached screen process. |
-R | Reattach if possible, otherwise start a new session. |
-s shell | Shell to execute, instead of $SHELL. |
-S sockname | Name this session <pid>.sockname instead of <pid>.<tty>.<host> . |
-t title | Set the title (name) of the window. |
-U | Tell screen to use UTF-8 encoding. |
-x | Attach to a not detached screen (multi display mode). |
-X | Execute <cmd> as a screen command in the specified session. |
Examples#
- Start a new screen session
screen
- Name a screen session for easy session management
screen -S <name>
- Detach a running screen session and put it in the background (in the session environment)
ctrl + a; d
- List running screen sessions
screen -ls
- Reconnect to a running screen by specifying the PID
screen -r <pid>
- Reconnect to a running screen by specifying the name
screen -r <name>
- Lock a screen session
ctrl + a; x
A password is required to open the session.
- Kill a detached screen session
screen -S <name-or-pid> -X kill
- Quit a detached screen session
screen -S <name-or-pid> -X quit
Common Shortcuts#
- Create a new window in the current session
ctrl + a;c
Switching Windows
Switch to the next window:
ctrl-a;n
Switch to the previous window:
ctrl-a;p
List all windows:
ctrl-a;w
Kill a Window
Close the current window:
Ctrl-a;k
Rename a Window
Rename the current window:
Ctrl-a;A
Split Screen
Split the screen horizontally:
Ctrl-a;S
Split the screen vertically (supported in newer screen versions):
Ctrl-a;|
Switch to the next split screen:
Ctrl-a;TAB
Close the current split screen:
Ctrl-a;X
Practical Examples#
Remote Sessions#
If you need to operate through ssh on a remote server and want to keep the session from being interrupted, you can use screen:
ssh user@server_ip
screen -S remotesession
Perform operations in the screen session, and even if the network connection is interrupted, your session will continue to run. You can reconnect after reestablishing the connection:
ssh user@server_ip
screen -r remotesession
Multitasking#
Handle multiple tasks in the same screen session: screen -S multitask
Run a task in the first window, such as monitoring system logs: tail -f /var/log/syslog
Create a new window: Ctrl-a;c
Perform other tasks in the new window, such as editing a file: vim /etc/hosts
Switch between them using Ctrl-a;n
and Ctrl-a;p
.
Exiting a Screen Session#
After closing all windows with exit
or Ctrl-d
, the screen session will automatically end.
Cover image from: https://wallhaven.cc/w/x6yzoz