> For the complete documentation index, see [llms.txt](https://cajac.gitbook.io/ctf-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cajac.gitbook.io/ctf-notes/terminals/screen.md).

# screen

screen is a screen manager with VT100/ANSI terminal emulation.

When **screen** is called, it creates a single window with a shell in it (or the specified command) and then gets out of your way so that you can use the program as you normally would. Then, at any time, you can create new (full-screen) windows with other programs in them (including more shells), kill existing windows, view a list of windows, turn output logging on and off, copy-and-paste text between windows, view the scrollback history, switch between windows in whatever manner you wish, etc.

All windows run their programs completely independent of each other. Programs continue to run when their window is currently not visible and even when the whole screen session is detached from the user's terminal.

When a program terminates, screen (per default) kills the window that contained it. If this window was in the foreground, the display switches to the previous window; if none are left, screen exits. Shells usually distinguish between running as login-shell or sub-shell. Screen runs them as sub-shells, unless told otherwise.

## Installation

Use `sudo apt install screen` to install if needed.

## Session Handling

### Create session

To create a named session

```bash
screen -S <session_name>
```

Or just `screen` to use the default naming.

### List sessions

To list sessions

```bash
screen -ls
```

The output will look something like this

```bash
hacker@terminal-multiplexing~finding-sessions:~$ screen -ls
There are screens on:
        139.session_8d09d8d31e3d1e37    (Detached)
        141.session_b94a9304b47df3cb    (Attached)
        145.session_76bbb19ce7b0b6f2    (Detached)
3 Sockets in /home/hacker/.screen.
hacker@terminal-multiplexing~finding-sessions:~$ 
```

### Reattach

To reattach to a session

```bash
screen -r <session_name_or_Pid>
```

If you only have one session you can use just `screen -r`.

<table><thead><tr><th width="172">Shortcut</th><th>Description</th></tr></thead><tbody><tr><td><code>Ctrl+a</code>   <code>d</code></td><td>Detach from current session</td></tr><tr><td></td><td></td></tr><tr><td></td><td></td></tr></tbody></table>

## Window Handling

Inside a single screen session, you can have multiple windows.

<table><thead><tr><th width="169">Shortcut</th><th>Description</th></tr></thead><tbody><tr><td><code>Ctrl+a</code>   <code>c</code></td><td>Create a new window</td></tr><tr><td><code>Ctrl+a</code>   <code>n</code></td><td>Next window</td></tr><tr><td><code>Ctrl+a</code>   <code>p</code></td><td>Previous window</td></tr><tr><td><code>Ctrl+a</code>   <code>0</code>  through <code>Ctrl+a</code>   <code>9</code></td><td>Jump directly to window 0-9</td></tr><tr><td><code>Ctrl+a</code>   <code>"</code></td><td>Bring up a selection menu of all of the windows</td></tr></tbody></table>

## Usage information

<details>

<summary>screen -h</summary>

```bash
┌──(kali㉿kali)-[~]
└─$ screen -h
Use: screen [-opts] [cmd [args]]
 or: screen -r [host.tty]

Options:
-4            Resolve hostnames only to IPv4 addresses.
-6            Resolve hostnames only to IPv6 addresses.
-a            Force all capabilities into each window's termcap.
-A -[r|R]     Adapt all windows to the new display width & height.
-c file       Read configuration file instead of '.screenrc'.
-d (-r)       Detach the elsewhere running screen (and reattach here).
-dmS name     Start as daemon: Screen session in detached mode.
-D (-r)       Detach and logout remote (and reattach here).
-D -RR        Do whatever is needed to get a screen session.
-e xy         Change command characters.
-f            Flow control on, -fn = off, -fa = auto.
-h lines      Set the size of the scrollback history buffer.
-i            Interrupt output sooner when flow control is on.
-ls [match]   or
-list         Do nothing, just list our SockDir [on possible matches].
-L            Turn on output logging.
-Logfile file Set logfile name.
-m            ignore $STY variable, do 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. Exits with non-zero return code if unsuccessful.
-Q            Commands will send the response to the stdout of the querying process.
-r [session]  Reattach to a detached screen process.
-R            Reattach if possible, otherwise start a new session.
-s shell      Shell to execute rather than $SHELL.
-S sockname   Name this session <pid>.sockname instead of <pid>.<tty>.<host>.
-t title      Set title. (window's name).
-T term       Use term as $TERM for windows, rather than "screen".
-U            Tell screen to use UTF-8 encoding.
-v            Print "Screen version 4.09.01 (GNU) 20-Aug-23".
-wipe [match] Do nothing, just clean up SockDir [on possible matches].
-x            Attach to a not detached screen. (Multi display mode).
-X            Execute <cmd> as a screen command in the specified session.

```

</details>

## Resources

screen - Linux manual page: <https://www.man7.org/linux/man-pages/man1/screen.1.html>
