Using GNU Screen

Posted by Steve on Sun 17 Oct 2004 at 19:54

Tags: ,

GNU Screen is an often overlooked application which allows you to run programs in a console section, detach from them and then later resume them. They even keep running when you logout.

Part of the reason why the program is often overlooked is because it has a very complicated description. The GNU Screen Homepage describes it as :

"Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. Each virtual terminal provides the functions of the DEC VT100 terminal... [snip]"

Whilst that's a very technically accurate description of the program it doesn't tell most newcomers exactly what the program does.

Essentially GNU Screen is a windowing system for the console.

When you run it inside an xterm, or a remote SSH login, you can create multiple new windows all in the same session, switch between them, or view two or more at once.

In addition to this a screen session is independent of your connection, so you can connect from one location, run things inside a screen session then detach. All the programs keep running and you can later reconnect and resume working right where you left off.

It's the attaching and detaching that makes screen so useful as it allows you to start long running programs, leave them running in the background and later on come back to them. This is very useful for things like console based IRC clients (irssi-text for example).

Lets start with an example which make it easier to understand what's going on.

If you run screen you will see the following text:

Screen version 4.00.02 (FAU) 5-Dec-03

Copyright (c) 1993-2002 Juergen Weigert, Michael Schroeder
Copyright (c) 1987 Oliver Laumann

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program (see the file COPYING); if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

Send bugreports, fixes, enhancements, t-shirts, money, beer & pizza to
screen@uni-erlangen.de


                        [Press Space or Return to end.]

Press space and you will be presented with what looks like an ordinary shell window. To test this run a command or two, for example:

skx@lappy:~$ ls /tmp
gconfd-skx  keyring-9hZRfE  mapping-skx  orbit-skx  ssh-MDkRIT1001
skx@lappy:~$

There you can see I've run the command ls /tmp, and you can see the output.

Now we get to see the power of the program. Press Ctrl+A, then d. You will see a message saying that your screen session has been detached, and will be back at your original prompt:

skx@lappy:~$ screen
[detached]
skx@lappy:~$

So what happened to your shell? Well it's still there! Simply run screen -R and you will be back at the session you detached from.

This shows you the basics of using the program, you've learnt how to run a copy of screen, run a command inside of it and detach from it (Ctrl+A d) then reconnect (screen -R).

This allows you to do a lot already, connect to a host from your office via ssh, start up screen and run your favourite text based IRC client. At the end of the day you can detach from it - leaving it stil there to return to from your home machine.

But we've only scratched the surface of what screen can do, remember we said that it could run multiple windows? Lets create another one.

Press Ctrl + a then c (for create). You'll have a new fresh shell session.

Toggle between the two shells you should have by pressing Ctrl + a then Ctrl + a again.

You should see that you're switching between two independent shell sessions. If you press Ctrl + a then " you'll see a list of the windows you have available to switch between.

This window list shows the window number on the left, along with the name of the window. By default the windows will be named after the shell you are running, for example bash.

To rename the current window use Ctrl + a then A.

When it comes to time to close a window you have two choices, you can either type exit in the shell session to close it which will close that window too, or you can use Ctrl +a k to kill the current window.

As well as using one window to switch between you can display two, or more, windows at the same time!

Use Ctrl + a and S (Capital 's' for split) and your window will split in two. You can move between the two halves of the window with Ctrl + a and TAB.

By default the new window will be empty, but you can switch another window into that place by using Ctrl + a repeatedly, as we've already covered.

To make it more obvious which window you're currently working in you can setup a small status bar across the bottom of the window which show you how many windows you have open, and which one you are working with.

To do this create a file .screenrc in your home area and place the following inside it:

# An alternative hardstatus to display a bar at the bottom listing the
# windownames and highlighting the current windowname in blue. (This is only
# enabled if there is no hardstatus setting for your terminal)
hardstatus on
hardstatus alwayslastline
hardstatus string "%{.bW}%-w%{.rW}%n %t%{-}%+w %=%{..G} %H %{..Y} %m/%d %C%a "

(You can override all the default settings in your local file, or update the global file in /etc/screenrc to affect all users).

To sum up we've seen that screen is a very powerful tool for executing commands that run for a long time, and running multiple programs all at the same time in a single session.

We've not covered all the options, just enough to get started with. Running man screen will give you lots more options.

Here's a list of all the commands we've covered:

  • screen
    • Run a new screen session
  • screen -R
    • Reattach to a previously detatched session
  • Ctrl-a c
    • Create a new window.
  • Ctrl-a k
    • Kill the current window - after confirmation
  • Ctrl-a Ctrl-a
    • Switch to the other window
  • Ctrl-a S
    • Split the current window in two.
  • Ctrl-a TAB
    • Move between split sections of the screen.
  • Ctrl-a A
    • Give the the current window a name.
  • Ctrl-a "
    • List all windows - move around to change the window with the arrow keys

 

 


Posted by Anonymous (127.0.xx.xx) on Mon 18 Oct 2004 at 19:03

Great and very useful site here. I was very suprised by the sheer amount of content!

[ Parent | Reply to this comment ]

Posted by Steve (127.0.xx.xx) on Mon 18 Oct 2004 at 19:06
[ View Steve's Scratchpad | View Weblogs ]

Thanks - I'm hoping that this will continue and that others will contribute content as time goes on.

-- Steve.org.uk

[ Parent | Reply to this comment ]

Posted by Anonymous (127.0.xx.xx) on Mon 18 Oct 2004 at 23:02

The -x flag is cool. Let's you attach a screen session to different places at the same time so you could walk somebody through something for example...

[ Parent | Reply to this comment ]

Posted by crispy (81.79.xx.xx) on Wed 4 May 2005 at 21:05
I agree, Screen is easily overlooked. With GUIs everywhere you may wonder what is the use of a text based multiplexor.
I use it for two things: teaching my minions how to admin Unix/Linux without them getting out of their seats (using the previously mentioned -x option), and just the flexibility of being able to continue from where you left off, from anywhere.
You can start a session via telnet from a windows box in the office, ctrl-a d out of it. Pick it up from a Sun box in the computer room, ctrl-a d out of it. Carry on from a WYSE terminal in the warehouse... carry on from a ssh session at home via VPN...
It really is very useful - the manual description doesn't do it justice - just try it.

If you have ever used the wonderful VNC program ( www.realvnc.com , www.tightvnc.com , like PCanywhere) you'll be familiar with the idea. Screen is a simpler, more efficient way of doing the same with text screens, with the advantage of multiple screens too.

Then again, VNC probably deserves a tip page of it's own...

[ Parent | Reply to this comment ]

Posted by Steve (82.41.xx.xx) on Wed 4 May 2005 at 21:07
[ View Steve's Scratchpad | View Weblogs ]

I think you're right, vnc probably does deserve a writeup of its own, especially some of the newerver packages like x11vnc, or vtgrab.

Steve
-- Steve.org.uk

[ Parent | Reply to this comment ]

Posted by Steve (82.41.xx.xx) on Sat 7 May 2005 at 17:28
[ View Steve's Scratchpad | View Weblogs ]

I posted a small piece now - remotely administering machines graphically, with VNC.

It doesn't cover all the available options but it's a good introduction piece.

Steve
-- Steve.org.uk

[ Parent | Reply to this comment ]

Posted by simms (69.157.xx.xx) on Wed 25 May 2005 at 19:11
screen kicks ass. i've been using it for years now and it's really a great way to make use of that PuTTY or xterm window efficiently.

but wait -- there's more :)
if you ever need to ssh to another machine and run screen there while already in a screen session on the first (i.e., nested screens, if you will), you'll realize that CTRL+A can no longer be used as the 'magic' key for both screen sessions.
fortunately, the 'magic' screen key can be changed on a session-by-session basis, and can be set to any reasonable value. for example, using
screen -e^Vv
to start your session will make CTRL-V the 'magic' key.

just make sure you use a reasonable key for the purpose -- CTRL-S, CTRL-X, CTRL-D are bad ideas ;)

[ Parent | Reply to this comment ]

Posted by Anonymous (129.21.xx.xx) on Thu 18 Jan 2007 at 20:48
Just quote your command. ie, if I have a screen within a screen, to open a shell in the first one is 'C-a c', right? But to do the same thing in the inner screen is just a 'C-a a c' - 'C-a a' sends a 'C-a' to whatever is running. So for three nested screens, I think you'd go 'C-a a a c'...

[ Parent | Reply to this comment ]

Posted by Anonymous (77.192.xx.xx) on Fri 8 Feb 2008 at 16:17
Many thanks, how many times did I say "damn I did screen -r and I was already in a screen and now I'm stuck!" I'm happy this time I asked google for "gnu screen nested".

I found an usefull tip about the C-a S (split) command, you can adjust the size with C-a :resize n (n is the number of lines, also +n and -n to add or remove lines).

[ Parent | Reply to this comment ]

Posted by Anonymous (134.76.xx.xx) on Wed 13 Jun 2012 at 15:10
great! Thanks!

[ Parent | Reply to this comment ]

Posted by Anonymous (67.86.xx.xx) on Wed 8 Jun 2005 at 04:27
Is there any way to change the name of a screen in a script ? For example, I have bash aliases to make connecting to various other systems simpler. I just do

Ctrl-a c app

to create a new screen, then the alias app takes me to the application server. I would also like that app alias to rename the screen appropriately, so *I* don't have to.

[ Parent | Reply to this comment ]

Posted by Steve (82.41.xx.xx) on Wed 8 Jun 2005 at 14:33
[ View Steve's Scratchpad | View Weblogs ]

You can setup the name of the initial screen session with the -S or -t flags.

See the manpage for details.

There's also the "shelltitle" option you can use in your ~/.screenrc file.

I'm not sure if that helps if you wish to create new screens though...

Steve
-- Steve.org.uk

[ Parent | Reply to this comment ]

Posted by Anonymous (62.172.xx.xx) on Thu 4 May 2006 at 11:45
Hello. Try:

screen -X title newtitle

This will change the current screen window title to "newtitle". You can use it outside or inside of screen. I don't think this will work if you have more than one screen running.

You can probably figure out how to re-alias ctrl-a c to run this command :)

HTH

[ Parent | Reply to this comment ]

Posted by Anonymous (72.83.xx.xx) on Tue 10 Aug 2010 at 20:05
You can change the current window title from within a shell script. The screen manual describes the title-string escape-sequence as

<esc>kname<esc>\

So, for example, if you are creating a shell script and want to change the window title to 'appserver', you could include

echo -ne '\ekappserver\e\\'

in your script.

[ Parent | Reply to this comment ]

Posted by rbernabe (24.168.xx.xx) on Thu 21 Jul 2005 at 03:11
Another cool thing about the screen utility is setting up a shared screen. You can set up a shared screen easily and work on projects together with others, or whatever else you can think of. I've used it before to learn how to do something while watching a friend type away in BASH. Heres a short howto I found by googling (located at http://www.kuro5hin.org/story/2004/3/9/16838/14935 ):

"I'm helping a friend in his programming studies, usually over the phone. We'll often use a shared screen session, so we can watch each other work on code. The sequence is: he creates a screen session, hits (assuming ^Z is your escape char) ^Z:multiuser on[RET]^Z:acladd piquan[RET] (where my username is piquan); then I run (from a normal, non-screen terminal) screen -x jrh/ (where his username is jrh)."

[ Parent | Reply to this comment ]

Posted by joloc (84.188.xx.xx) on Wed 25 Jan 2006 at 14:23
Multiuser sessions require GNU screen to be installed setuid. This being a security risk, Debian's screen has dropped the setuid bit with 3.9.4-1 (it is installed setgid utmp instead). Here's what the Debian maintainer wrote to the freebsd-security list (http://www.atm.tut.fi/list-archive/freebsd-security/msg02119.html ) on the issue:
# From: Adam Lazur
# Date: Fri, 27 Sep 2002 11:57:06 -0400

"For full functionality, screen must also be setuid root to do multi-user. This is another permissions setup problem (on the named pipes this time) that making screen setuid root avoids. It's on my todo list to fix things to that a setgid utmp screen will work with multi-user screen "out of the box" in the future."
I am not aware of any fix to this, except of course to `chmod u+s /usr/bin/screen' if you're not worried about setuid. Anyway, root can always access a multiuser session, provided the session owner adds it to the ACL.

[ Parent | Reply to this comment ]

Posted by jparrella (65.254.xx.xx) on Wed 13 Jun 2007 at 21:01
[ View Weblogs ]
Using screen 4.0.3 (that is Etch) if one user opens a screen, another one logged in with the same username in a different terminal can access and share it with 'screen -x' One thing I would like to do is to allow one user to write in the screen and the others to watch it but not being able to interact with it. I used to have a small wrapper which called screen -x with an unprivileged user and put that as the login shell so we made free online courses using screen. But, of course, gems seems to be a better option for that (aptitude install gems)

[ Parent | Reply to this comment ]

Posted by Wottan (82.146.xx.xx) on Fri 19 Aug 2005 at 10:08
Wonderful, every time I come here, I find something new and interesting to me. Please, go on the same way.

[ Parent | Reply to this comment ]

Posted by Steve (82.41.xx.xx) on Fri 19 Aug 2005 at 12:53
[ View Steve's Scratchpad | View Weblogs ]

Thanks - good to know people enjoy the site.

I'm hoping that more and more people will return and submit pieces of their own - otherwise I'll likely run out of inspiration sometime soon!

Steve
-- Steve.org.uk

[ Parent | Reply to this comment ]

Posted by Anonymous (87.74.xx.xx) on Wed 10 May 2006 at 18:03
Thanks a lot, I'd talked to lots of ppl about the wonders of screen, but this is the clearest description of how to use it. A. GNUbie

[ Parent | Reply to this comment ]

Posted by Anonymous (24.210.xx.xx) on Thu 20 Jul 2006 at 12:47
Anyone know if its possible to run a command in several screen sessions at the same time?

[ Parent | Reply to this comment ]

Posted by Anonymous (66.60.xx.xx) on Tue 19 Sep 2006 at 19:57
It *is* possible, though I forget the command, and I don't have the time to look it up right now. Read through the man page, specifically the section on commands. :)

[ Parent | Reply to this comment ]

Posted by cmdln (75.18.xx.xx) on Sun 29 Jul 2007 at 02:18
Another interesting way to use screen is for automatic session logging. Basically start screen automattically when somone logs in.

I have a small writeup http://anders0n.net/blog/?p=42

[ Parent | Reply to this comment ]

Posted by lionslair (203.161.xx.xx) on Sat 4 Oct 2008 at 09:54
I have been trying to get screen to start when I login in. I added the below to my .bashrc but it does not start when I ssh into it.

This is what I placed in my .bashrc


if [ -n "$SSH_CONNECTION" ] && [ -z "$SCREEN_EXIST" ]; then
export SCREEN_EXIST=1
screen -DR
fi

Can anyone let me know what I am doing wrong?

[ Parent | Reply to this comment ]

Posted by Anonymous (66.41.xx.xx) on Mon 18 May 2009 at 02:53
screen -rd || screen

[ Parent | Reply to this comment ]

Posted by Anonymous (70.30.xx.xx) on Fri 14 Aug 2009 at 20:16
There is something rather annoying I've noticed while using screen for a couple years now...

When many computers have the same screen session to be able to do stuff at the same time (like two people sitting at the same computer, but over SSH) (example)
PC1: screen -x "Project"
PC2: screen -x "Project"

The problem is here:
When you create a new window in the screen (CTRL+A + C), it appears to only be in the current instance. That means that if PC1 creates a new window, if PC2 does 'CTRL+A + N', a warning will pop saying that there are no other windows... So apparently, when two people are in a same session, only the main window is visible to all participants, not all the windows in the screen.

Is this a bug?

[ Parent | Reply to this comment ]

Posted by Anonymous (59.145.xx.xx) on Tue 20 Oct 2009 at 06:36
Nice post. A very short and quick intro to screen

Thanks,
Maniraj Patri

[ Parent | Reply to this comment ]

Posted by knightsamar (117.195.xx.xx) on Wed 7 Apr 2010 at 21:51
Hey, superb article! I had heard about screen but as you said, the technical jibba-jabba is a big thing that stops from people getting to it.

Thanks for writing this wonderful guide :)

[ Parent | Reply to this comment ]

Posted by eneville (87.112.xx.xx) on Sat 19 Jun 2010 at 13:34
Screen is one of my favourite programs, along with vim, mutt and bash of course. One thing that people forget about screen is that it's capable of automation too which extends its power even further, here's a short example of using it in this way:

http://www.s5h.net/wiki/screen#Automation

[ Parent | Reply to this comment ]

Posted by Anonymous (89.212.xx.xx) on Tue 29 May 2012 at 12:42
Another thing, pressing CTRL + A and then D, will only detach from the current (open) screen session.

[ Parent | Reply to this comment ]

Sign In

Username:

Password:

[Register|Advanced]

 

Flattr

 

Current Poll

What do you use for configuration management?








( 836 votes ~ 10 comments )