How to install and use Screen to manage Terminal Sessions on Linux

Use Screen to Manage Terminal Sessions on Ubuntu Debian CentOS RedHat

Screen is a terminal multiplexer that allows you to run multiple terminal sessions inside a single terminal window or SSH session. It can be very useful on remote servers like Ubuntu cloud servers where you want to run long-running processes that you can disconnect from and reattach to later.

Here is how to install and use screen on an Ubuntu cloud server:

Installing Screen

Screen should be installed by default on most Ubuntu distributions. You can check if it is already installed by running:

$ screen -v

If it is installed, you will see output like:

Screen version 4.08.00 (GNU) 5-Oct-20

If it is not installed, install it with:

$ sudo apt update
$ sudo apt install screen

Starting a screen Session

To start a new screen session, just run:

$ screen

You will be presented with a blank terminal screen. Anything you run here will be inside the screen session.

You can also start a named screen session by providing a session name:

$ screen -S session1

To see a list of running screen sessions:

$ screen -ls

Detaching and Reattaching to a Screen

To detach from a screen session while keeping it running, press Ctrl+A followed by D.

You will return to your normal terminal prompt while the screen session continues running in the background.

To reattach to a detached screen:

$ screen -r session1

Replace “session1” with the name of your screen session.

You can omit the session name to attach to your most recent detached session.

Handy Screen Commands

Here are some handy commands to use inside a screen session:

  • Ctrl+A D – Detach from the current session
  • Ctrl+A K – Kill the current session
  • Ctrl+A C – Create a new window (tab) in the session
  • Ctrl+A N – Switch to next window
  • Ctrl+A P – Switch to previous window
  • Ctrl+A 0-9 – Switch to window number 0-9
  • Ctrl+A A – Rename the current window
  • Ctrl+A \ – Split current region horizontally
  • Ctrl+A | – Split current region vertically

Configuring Screen

You can configure options for screen in the ~/.screenrc file. Useful settings include:

# Enable mouse scrolling  
termcapinfo xterm* ti@:te@
# Change escape key from Ctrl+A to Ctrl+B
escape ^Bb
# Change screen log file location
logfile ~/.screens/screenlog.%n

See the screen manual page for more configuration options.

Scrolling in Screen

Screen does not allow scrolling up through previous output like a normal terminal.

To enable scrolling, you need to use a screen command:

  • Ctrl+A [ – Enter scrollback mode. You can now scroll through your terminal history with the arrow keys.
  • Ctrl+C – Exit scrollback mode.

Alternatively, you can enable scrollback buffer in your ~/.screenrc file:

defscrollback 30000 

This will store the last 30,000 lines of output and allow you to scroll through them.

Screen with SSH

You can attach and reattach to a screen session over SSH.

Start a screen on the remote server, then detach it as normal with Ctrl+A D.

Log out of your SSH session and log back in later. Reattach to your previous session using:

$ screen -r

This allows you to continue running processes on a remote server even when disconnected.

Tmux vs Screen

Screen has some similarities with tmux, another terminal multiplexer. Key differences:

  • Tmux has a more modern codebase and some additional features like splitting panes
  • Screen is more lightweight and simpler, using less memory/resources
  • Screen commands use Ctrl+A, tmux use Ctrl+B
  • Tmux is still actively maintained, screen development has slowed

So in summary, tmux may be preferable for new users who want more features, but screen remains a solid choice for lightweight terminal multiplexing.

Screen Man Page

The screen man page contains a wealth of useful information on all the command line options and configuration settings available.

You can access it by running:

$ man screen

Some highlights from the man page:

Command Line Options

Here are some common command line options when launching screen:

  • -S sessionname – Name the new screen session
  • -d -m – Start screen in detached mode
  • -r – Reattach to a detached screen session
  • -ls – List currently running screen sessions
  • -wipe – Clear screen’s internal state and remove all sessions

Configuration File

The ~/.screenrc file controls screen’s behavior:

# Set default window name  
screen -t shell
# Set screen logging file
log on
logfile ~/screenlog.0
# Set scrollback buffer size
defscrollback 30000

Bound Commands

Binding commands to keystrokes is done like:

# Bind Ctrl+A C to create a new window  
bind C create 
# Bind Ctrl+A n to switch to next window
bind n next

Copy/Paste

Screen offers copy/paste support:

# Enter copy mode
Ctrl+A [ 
# Begin selecting text with arrow keys
# Copy selected text
Ctrl+A ]
# Paste copied text
Ctrl+A ]

Windows

Managing windows:

# Create new window
$ screen -t test
# Switch between windows 
Ctrl+A n 
# Kill current window
$ quit

This covers some of the most popular screen usage direct from the man page. Refer to man screen for more details.

Conclusion

Screen is a handy utility for running processes on remote servers like Ubuntu cloud VMs where you want to detach and reattach to sessions.

Key tasks covered:

  • Installing screen on Ubuntu
  • Starting and detaching screen sessions
  • Reattaching to screen sessions
  • Using screen commands for managing windows
  • Configuring scrollback buffer and ~/.screenrc settings
  • Integrating screen with SSH

Screen remains relevant today alongside newer tools like tmux and is worth learning if you manage remote Linux servers. It provides an easy way to keep sessions running even when you get disconnected from the server.

LEAVE A COMMENT