Screen? A must for SSH

If you've ever used SSH to connect to a server, you 'll know its limitations: if you want to open a new window, you'll need to create a second SSH connection to the server. And if the connection breaks during the SSH tunnel, you've lost your progress. This is where Screen comes in.

Screen, which calls itself a "full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells)." is a usefull tool to use 1 SSH connection, but use multiple screens to work in. So you can have 1 screen to write some scripts in, another to tail a logfile and a third to check your IRC messages ;-)

Screen is installed by default on most installations, you can verify this by running the "which screen" command. If it's not installed, try to apt-get or yum it -- it's in most repositories.

You can start screen by typing:

# screen

This makes sense, right? :-)
You'll probably notice that not much happens if you type that. At least, it seems like not much as happened. In fact, you've just opened a new "screen" to type your commands. The program "screen" has a few commands of its own, in order to create a new window, and navigate through the open ones.

Once you've opened "screen", you can see a command list by typing "CTRL + A", followed by "?" (the question mark). By typing "CTRL + A" you state that the next signal is to be sent to the program "screen", in stead of to the Shell (like you would in a normal shell). You'll see a list of all bindings known to "screen".

Start a new window by typing "CTRL + A" + "c". The C stands for Create -- I know, too obvious. A new window will be created. In order to test this, type the command "top". Then create a new window, by using "CTRL + A" + "c". You'll see top disappear, and a new window will open. Type in some commands of your choice, and return to the previous window, by doing "CTRL + A" + "n". The "n" stands for "Next", and will open the next screen. "CTRL + A" + "p" would've opened the previous screen.

Closing a window, can be done by typing "exit" (like you would in a normal shell). This will cause you to fall back to the previous monitor you opened, or to your main prompt -- where you started screen, showing you a message such as "[screen is terminating]" -- so you'll know you've hit the main shell.

The biggest advantage in using screen, is that you can "detach" a screen-session. This means you return to the normal shell, but the processes started in "screen" are still active in the background. You can detach yourself by typing "CTRL + A" + "d". Again, obvious that "D" stands for Detach. This gives you more flexibility for managing your server(s): you can start a number of processes, quietly exit the shell and return a couple of hours later to pick up the session started in screen.

13 comments on “Screen? A must for SSH
  1. else says:

    Do you know why screen freezes when pressing (accidentally) STRG+S? Thanks.

  2. admin says:

    Pressing CTRL-S allows you to search into previous screen-sessions.

    You can always exit “screen” by pressing CTRL-D.

  3. else says:

    Unfortunately it’s not possible to exit screen by pressing CRTL+D after previously having pressed CTRL+S… Can anyone confirm this? Using Debian Etch 4.0.

  4. dailynet says:

    Ctrl+S is a “Pause” in linux environtment, Try pressing Ctrl+Q to un”Pause”.

  5. Adam Kane says:

    Nice write up, there is a related article your readers might learn more from located at http://www.foogazi.com/2006/08/25/ssh-screen-easy-admining/ that discusses how to use screen and ssh with passwordless keys to easily administrate multiple servers.

  6. Jake says:

    If you accidently leave your screen attached and you want to connect to it, you can use “screen -d nameofsession” (sans quotes of course) to detach it, and then attach it on the computer you’re on.

  7. Making your own .screenrc file in your home directory can be very nice as well. This is what I use and it really helps manage screen better:

    hardstatus alwayslastline
    hardstatus string ‘%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f %t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %d/%m %{W}%c %{g}]’

  8. Matt Simmons says:

    @Jake

    I know a reply a few months later is probably too late, but you can reattach

  9. Chris Irwin says:

    The man page is pretty informative. -S gives the session a name (easier to resume). Even easier is using “-d -R”, which creates or resumes a session with a given name.

    screen -d -R WorkSession

  10. Matti says:

    The addition of ~/.screenrc is worth noting. I’ve edited the main article, there are a lot of options available when creating/editing your config file!

  11. Mat says:

    If you use emacs-style command line editing, CTRL-A (go to the beginning of line) is a bad command key. Add the following to your ~/.screenrc to change it to CTRL-O (the letter o):

    escape “^Oo”

    It’s also great to reduce many xterms to a single one!
    -mat

  12. jt says:

    thanks for the tutorial! is that supposed to be a regular single quote (‘) at the end of the hardstatus string in the ~.screenrc file?
    i had to change it to make it work.

  13. Matti says:

    @Jt; indeed, that’s a normal single quote – somehow the code gets screwed up, can’t seem to edit it (always changes back) :-)

2 Pings/Trackbacks for "Screen? A must for SSH"
  1. […] Screen? A must for SSH must….how about dtach…. […]

  2. […] viaScreen? A must for SSH ~ Mattias Geniar. Posted by PJ Michel at 23:18 Tagged with: irc, screen, ssh Kogitae AE Administrateur Système / Chef de projet Freelance Michel Pierre-Jacques 23 rue de la cornée 70800 Anjeux […]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Advertisement

Why ads?

I'm glad you made it to this blogpost. I hope it helps solve your problem. So why then do I show ads on the site? Writing content, testing it and making sure the layout isn't totally b0rked takes time. A lot of time. The ads are a way to pay back a small portion of that time.

And as you know running a site costs (a bit of) money: the domain name, webhosting, time spent writing and updating content, ... So if you like the content of this blog, consider disabling your AdBlocker for this domain. Thanks!

Recent posts

Looking for help?

Tired of fixing all these tech-problems yourself? We've got an excellent team at Nucleus, a top-class Belgian hosting provider, that can help you.

Discover our Managed Hosting, where skilled engineers manage your servers and keep them up-to-date, so you can focus on your core business. We use a variety of Configuration Management Systems such as Puppet to make sure every config is reviewed, unit-tested and guaranteed to be working.

Want to get in touch? Find me as @mattiasgeniar on Twitter or via the contact-page on this blog.