Blog Links

An easy way to use GNU Screen over SSH

GNU Screen is a free terminal multiplexer developed by the GNU Project since at least 1995. It allows a user to access multiple separate terminal sessions inside a single terminal window or remote terminal session. It is useful for dealing with multiple programs from the command line, and for separating programs from the shell that started the program. – wikipedia

heckert_gnusmall
I’ve tried the GNU Screen utility a number of times but never really got it working very well. What I really wanted was to start using it, develop my understanding and start using new features later. Most importantly I didn’t want my productivity to drop whilst I was learning it. The main problem I found when using it over ssh is you end up with unwanted multiple screen sessions. So after spending a number of days working out my config, this post details how to use Screen with minimal effort and maximum value.

What the config means in practice is that you can quickly login to a remote machine restoring your previous environment. So, you can create a login session on a remote machine, close your window down, even turn-off your PC, then re-connect to the same session that has been either running a job or patiently waiting, whilst you were away.

Remote Machine Setup

ssh into your remote machine and Install screen on your, in Ubuntu its simply:

sudo apt-get install screen

But as its a GNU app, every Linux distribution should have it easily installable.

Into a file called .screenrc file in your home directory, paste the following config

hardstatus on
hardstatus alwayslastline
startup_message off
termcapinfo xterm ti@:te@
hardstatus string "%{= kG}%-w%{.rW}%n %t%{-}%+w %=%{..G} %H %{..Y} %m/%d %C%a "
screen -t bash1 1
screen -t bash2 2
screen -t bash3 3
screen -t bash4 4

Essentially the config tells Screen to show a status bar at the bottom of the xterm, and the 4 lines at the end instruct it to start 4 sub shells (Screen calls these windows, but they aren’t gui windows) each with the name bash1, bash2, bash3 and bash4.
That should be all the config you need on the remote machine,

Local Setup

Next on your desktop machine create a file (shell script) called “remote_screen” in your home directory, and copy this code into it:-

orig=`/usr/X11R6/bin/xprop -id $WINDOWID | perl -nle 'print $1 if /^WM_NAME.+= "(.*)"$/'`
if [ -z $orig ];
then
orig=`hostname`
fi
echo -e "\033]2;$1\007"
ssh -t $1 screen -S main -xRR -p $2 -q
echo -e "\033]2;$orig\007"

Then make it executable using

chmod +x remote_screen

This script sets the xterm title, runs Screen on the remote machine (with some options) and then once complete, restores your original xterm title.
It takes two parameters. The first is the destination machine (or IP) and secondly (optional) the name of the active bash shell (window) that screen starts with.

Usage

Now instead of using “ssh destination” to connect into your remote server use

remote_screen destination [window_name]

The first time you use this command you will ssh into the destination and automatically start screen. The screen will start and spawn the 4 sub shells called, bash1, bash2, bash3 and bash4

4 shells running in screen

Most screen commands by default start with Ctrl+a, You can move between the Screen windows (bash shells) using

Ctrl+a  n     -    Go to the next window
Ctrl+a  p     -    Go to the previou window
Ctrl+a [1-4]  -    Go to the window numbered 1-4

When you have finished your session you can either just close down the xterm window, or use

Ctrl+a d      -     Detach from screen

Which will return you back to your local shell

There are many more commands to Screen, but those few should be enough to get you going.

Ctrl+a ?      -     Show a help page

If next time, you connect using

remote_screen destination bash3

Not only will you return to the 4 shells you were previous using, you will also jump straight to the shell named bash3.

Additionally, I make an alias so I can access the remote machine via a one word command

alias lime='remote_screen lime'

Then

lime bash3

Takes me straight to the a specific machine and a specific shell.

To make sure scrollback buffer works properly in your Xterm windows, ‘edit’, “Profile Preferences” and make sure under the “Scrolling” tab, the “Use keystrokes to scroll on alternate screen” is OFF

Any problems please drop a comment, and I’ll do my best to help.

If this has been useful to you, and you would like to buy me a coffee, or help towards my monthly server costs please click here to make a donation via paypal.

13 comments to An easy way to use GNU Screen over SSH

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>