Linux Screen - Terminal Multiplexer

Linux Screen - Terminal Multiplexer

Ever been connected to a server via SSH, executing a long-running command, but don't want to or can't stick around for it to finish? Being a developer/sysadmin on the road means that I have encountered this a lot.

screen will solve this problem for you by multiplexing your terminal and allowing you to disconnect or reconnect to a shell session quickly and easily while allowing any "long-running" commands to finish without interruption.

Install Screen

If screen isn't already installed, you can do so with the following command:

Ubuntu:
sudo apt install screen

Fedora:
sudo yum install screen

To start screen, simply type screen. It will start the script, create an instance and attach to it. Once connected, execute commands as necessary.

Detach

In order to detach from a running instance and leave the terminal up, either to connect to it again later or to let running commands finish, type the following:

Ctrl + A + D


List Instances

Ready to reconnect but forgot the name or ID of an instance?

screen -ls

Output:

❯ screen -ls
There is a screen on:
	113948.pts-3.Server1	(03/30/2025 10:57:53 AM)	(Detached)
1 Socket in /run/screen/S-admin.

Reconnect

Need to reconnect to a previously detached instance?

screen -r 113948 - Sub ID number for instance ID from list or defined name.


Create Named Instance

If you would like to use names to be able to work with terminal instances, you can use the following command to create an instance with a specific name. I generally use this to define what the command is that I am going to be running, like rsync or dd. I will also make a general one named main

screen -S main

Command Reference

Options:

  • -a: It force all capabilities into each window’s termcap.
  • -A -[r|R]: It adapt all windows to the new display width & height.
  • -c file: It read configuration file instead of ‘.screenrc’.
  • -d (-r): It detach the elsewhere running screen (and reattach here).
  • -dmS name: It start as daemon: Screen session in detached mode.
  • -D (-r): It detach and logout remote (and reattach here).
  • -D -RR: It do whatever is needed to get a screen session.
  • -e xy: It change the command characters.
  • -f: It make the flow control on, -fn = off, -fa = auto.
  • -h lines: It set the size of the scrollback history buffer.
  • -i: It interrupt output sooner when flow control is on.
  • -l: It make the login mode on (update /var/run/utmp), -ln = off.
  • -ls [match]: It display all the attached screens.
  • -L: It turn on output logging.
  • -m: It ignore $STY variable, do create a new screen session.
  • -O: It choose optimal output rather than exact vt100 emulation.
  • -p window: It preselect the named window if it exists.
  • -q: It quiet startup. Exits with non-zero return code if unsuccessful.
  • -Q: It commands will send the response to the stdout of the querying process.
  • -r [session]: It reattach to a detached screen process.
  • -R: It reattach if possible, otherwise start a new session.
  • -S sockname: It name this session .sockname instead of …
  • -t title: It set title. (window’s name).
  • -T term: It use term as $TERM for windows, rather than “screen”.
  • -U: It tell screen to use UTF-8 encoding.
  • -v: It print “Screen version 4.06.02 (GNU) 23-Oct-17”.
  • -x: It attach to a not detached screen. (Multi display mode).
  • -X: It execute as a screen command in the specified session.

Shortcut keys Options:

  • Ctrl-a + c: It create a new windows.
  • Ctrl-a + w: It display the list of all the windows currently opened.
  • Ctrl-a + A: It rename the current windows. The name will appear when you will list the list of windows opened with Ctrl-a + w.
  • Ctrl-a + n: It go to the next windows.
  • Ctrl-a + p: It go to the previous windows.
  • Ctrl-a + Ctrl-a: It back to the last windows used.
  • Ctrl-a + k: It close the current windows (kill).
  • Ctrl-a + S: It split the current windows horizontally. To switch between the windows, do Ctrl-a + Tab.
  • Ctrl-a + |: It split the current windows vertically.
  • Ctrl-a + X: Close active Split window
  • Ctrl-a + Q: Close all Split windows
  • Ctrl-a + d: It detach a screen session without stopping it.
  • Ctrl-a + r: It reattach a detached screen session.
  • Ctrl-a + [: It start the copy mode.
  • Ctrl-a + ]: It paste the copied text.

Screen Config

By default, screen looks for a config file in either /etc/screenrc or ~/.screenrc if either file exists. I like to change a couple minor settings to run a little smoother:

# Turn off the welcome message
startup_message off

# Disable visual bell
vbell off

# Set scrollback buffer to 10000
defscrollback 10000

# Allow scrollback with mouse
termcapinfo xterm* ti@:te@

# Customize the status line
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'

In closing, screen is a great tool for being able to persist terminal sessions for extended periods of time. I hope this article gives you a new tool to add to your arsenal.

Chris R. Miller

Austin, TX
I like computers.