Use Linux screen command to start multiple sessions on a remote terminal

Published by Alexander Braun on 01 Nov 2017 - tagged with Linux

In this post I will provide you a quick overview of the screen command. Screen can be used to open multiple interactive shell sessions within one remote ssh or local terminal session. This comes quite handy when you have to execute long running tasks on a remote Linux server, need multipe shell session or in an unstable network environment.

I'm sometimes surprised how many developers and infrastructure admins I have met, that never heard of or ever used the screen command. I use screen quite often, especially when I have to deal with the following situations:

  • Starting a task at work and continue from home
  • Copying large files
  • Long-running processes, e.g. creating a backup
  • Starting a long-running compilation task or updating a remote Linux server
  • Having an unstable network connection, that causes ssh sessions to be terminated frequently
  • When I need multiple shell sessions on the same Linux box

The idea behind screen is to start one or more terminal session within an existing session. These sessions can be detached and later re-attached to another terminal session. In case the network gets interrupted and you are disconnected the sessions will be detached automatically and after logging in you can pick up where you left before you were disconnected.

Installation

Screen is available for most Linux distributions.

Ubuntu and Linux Mint

sudo apt-get install screen

CentoOS, Red Hat an Fedora

sudo yum intall screen

Basic usage

Start and terminate a session

To start a new shell session you simply have to enter:

screen -S session_name

This creates a new session with the name "session_name" and immediately switches to the new instance. To terminate and exit the session you simply have to enter exit.

Detach and re-attach

When inside a screen based session you can press CTRL SHIFT A followed by d to detach the current session and return to your original session.

When you want to re-attach a screen session you might first want list all existing sessions using screen -ls.

$ screen -ls
There is a screen on:
	8707.session_name	(2017-11-01 08:19:00 PM)	(Detached)

After you identified the name of the session, here "session_name", you can re-attach it using screen -r session_name.

The ability to detach and re-attach sessions is one of the powerful features provided by screen. This allows you to start a session at one location, e.g. at work, detach the session and re-attach it from home. Just in case you forgot to detach the session, you can use screen -d session_name to first detach it and then use the command above to re-attach it.

Trouble Shooting

In one of my environments - a QNAP NAS Box - I get the following error message when I try to start a screen session.

[~] # screen -R screen_name
/var/run/utmp: No such file or directory
Cannot find terminfo entry for 'xterm-256color'.

For me the following solution that was described here resolved my issue.

[~] # export TERM=xterm-xfree86
[~] # TERMINFO='/usr/share/terminfo/' screen -S screen_name

This was a quick introduction to the screen command. Screen includes a lot more features and I recommend to check out the User Manual to get more information.