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

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

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.
















Hi! Great article. You might want to check out byobu https://launchpad.net/byobu (formerly called screen-profiles). It really makes setting up and configuring screen so much simpler, and provides a very useful configuration out of the box.
Hey, you may also want to note how to add password protection to detached screen.
Does this make you return to the directory you were until logout? thanks. (to save cd’s
Wow you made that pretty difficult! Here’s what I have in my .profile script:
# auto-run screen, but only once
# MUST be done after local .zprofile which usually include PATH munging.
if ps x | grep “SCREEN -S MainScreen” | grep -v grep &> /dev/null
then
echo “Screen is already running.”
else
echo “Starting screen…”
screen -S MainScreen
fi
This way you always log into the remote box the same way, and you can decide after you’re there whether or not you want to use screen. If so, it’s as simple as “screen -x”.
I also have finally worked out a perfect system for automatically re-attaching to ssh-agent forwarding, which was really annoying me before. LMK if you are interested in it.
Alan
[...] An easy way to use GNU Screen over SSH | Earth Info (tags: development tools linux ssh screen) [...]
Why do something so complicated? Is it really so hard to just tunnel in and type screen?
Let me guess, you’re an Ubuntu user? Ubuntu breeds bad Linux usage habits. No offense but you are making this waaaaay to hard. ssh hostname then screen -rd. Congrats on finding out about screen though. It’s good stuff. @Eric – wtf are you talking about password protecting a detached screen. Think about it: you’re trying to password protect a process that belongs to you.
@flakbas – That’s a tad harsh, if accurate. Though I don’t even need to put the “-rd” in there. Just “screen” does all I need.
Ubuntu does breed bad Linux habits, just like BASIC breeds bad programming habits.
I’d love for Ubuntu users to just try Arch and actually use Linux for real.
@Yaro – Yeah maybe a little harsh. The “-rd” is to basically reattach a screen session.
@flakbas – At the moment I use Ubuntu (I did mention that in the post, so it wasn’t such a great guess), but I have experience with Slackware, Redhat and Gentoo. I think you are being a little hard on Ubuntu users in fact you sound pretty distributionist to me:)
The reason I created the method above is so you don’t have any problems with multiple sessions. A simple “ssh server” and then “screen -rd”, Causes a number of errors in different circumstances,
1) If screen isn’t already running you get “There is no screen to be detached.”
2) But more of a serious error for me, If you open a second ssh terminal your the first screen session is detached.
I wasnt claiming to do anything special, I just wanted to make sure when I used screen, it worked everytime without any error messages.
Does this trick affect bandwidth ? (I sometimes use web-based ssh clients like http://electrica-ms.mures.rdsnet.ro/ but the speed of the connection isn’t the same as with a normal ssh client)
@Phil, Screen doesn’t use much bandwidth, I should think it will work fine..give it a go
[...] 参考文章: http://en.wikipedia.org/wiki/GNU_Screen http://www.ibm.com/developerworks/cn/linux/l-cn-screen/ http://www.earthinfo.org/an-easy-way-to-use-gnu-screen-over-ssh/ [...]