Skip to content

GNU Screen

GNU screen is a terminal multiplexer, which is a CLI tool that enables virtual terminals which you can attach and detach from, allowing you to leave commands running on the server when you log out. You can log back in and reattach to the screen session and it appears that you are right back at the original terminal.

See also tmux, which is a more modern replacement. If you haven't used tmux or screen before, use tmux unless you need a feature that screen has but tmux does not.

Examples

These all assume that your config has ctrl-a set up as the the command character, which is the default.

Rename a screen tab

ctrl-a shift-a

Show a list of sessions

ctrl-a "

Detach from a screen session

ctrl-a d

re-attach to a specific screen session

screen -x "$screen_session_id"

Sharing your screen

In order to share your screen the /usr/bin/screen binary needs to be suid, which is a significant security risk.

ctrl-a :multiuser on
ctrl-a :acladd [username]

Open three IPMI consoles

This snip opens several new tabs, each with a name and a start command including the name.

for host in app{213..215}prod ; do
    screen -t "${host}" consoleto "${host}"
done

Open a series of new tabs and run ssh as root

For some reason screen doesn't like the ssh user@host syntax, so use ssh -l

for host in app{215..222}prod ; do
    screen -t "${host}" ssh -l root "${X}" puppetd -ov
done

Terminal Emulation for Serial Ports

You must first figure out the name of the device that is connecting to your serial port, such as a USB adapter. Then use syntax such as the following:

screen /dev/tty.usbDeviceName 9600

Split Screen

ctrl-a S to create a split screen, ctrl-a [tab] to switch between. The splits are destroyed when re-attaching.

.screenrc

Using ~/.screenrc you can define many variables to customize the look of your screen tool, including tabs, clock and colors. Here's an example that gives all three:

caption always "%{Mk}%?%-Lw%?%{km}[%n*%f %t]%?(%u)%?%{mk}%?%+Lw%? %{mk}"
hardstatus alwayslastline "%{kW}%H %{kB}|%{km} %l ~ %=%{km}%c:%s %D %M/%d/%Y "

Bugs

In Ubuntu with a Mac keyboard connected, sometimes the backspace key functions incorrectly. set TERM=vt100 before running screen to fix this.

See also

  • tmux - similar functionality, way more resource efficient and more widely used.
  • dvtm - similar functionality.
  • reptyr - Takes over a pty, useful for moving a pid running outside of screen to running within screen.