Using GNU Screen with SSH

This post is probably old news to experienced UNIX sys admins, but I thought the information might be useful to less knowledgeable folks like me. I also hope that the resulting conversation will help uncover even more knowledge we can all put to good use.

I’ve messed around with the screen utility off and on for a while. One thing I’d never quite figured out, though, was how using screen helped with SSH sessions. I kept seeing references to using screen to help keep things running when you needed to disconnect from an SSH session. That seems like a useful feature, so I decided to dig into it and see what I could figure out.

In the end, what I figured out was this:

  • I needed to install screen on the remote host(s). In my case, the remote hosts were OpenBSD (I removed the secret back doors), so a quick pkg_add corrected that issue.
  • I had to recreate my .screenrc file on the remote host(s). Fortunately, my .screenrc is very simple—it only enables the ability to use the iTerm2/Terminal scrollbar to scroll back and increases the scrollback buffer—so that was no big deal.

With these changes in place, you can then use this command to connect to a remote host:

ssh -t <> screen -R

On the first connection, this command will create a new screen session. When you’re done with this SSH session and want to disconnect, just detach from the screen session (typically using Ctrl-a d). That also disconnects the SSH session, but here’s the kicker: your screen session is still running—as are any processes you had running in that session.

When you go to reconnect, use the same command again and it will reconnect you to your existing screen session and you’ll be right back where you left off. Pretty handy!

<aside>By the way, the -t in the SSH command is necessary; without it, you’ll get a “Must be connected to a terminal” error message and it won’t work properly.<aside>

I’m sure this barely scratches the surface of the useful tricks one could perform using screen, so I challenge any and all readers to submit other useful tricks in the comments below. Or, if there is a better way of doing what I’m discussing in this article, please speak up!

Tags: , ,


  1. Robert’s avatar

    # add to .bash_profile
    exec /usr/bin/screen -R

    Then ssh to server like normal. If you exit the session will close normally, but you can still Ctrl-a d to disconnect and leave it running if needed. Just ssh in again and be reconnected.

    Use Ctrl-a c to multiplex the screen session and you only have to ssh once to get multiple remote terminals.

  2. Chris Cowley’s avatar

    You can also have multiple sessions running. After you have disconnected from one you can list them with screen -ls, then screen -ra to re-attach to your disconnected session.

  3. Ray’s avatar

    screen is probably more widely deployed, but tmux[1] is the future! :-)


  4. vantmet’s avatar

    Gah, pity the command has to come at the end, or there would be a really neat alias in there.

  5. phocean’s avatar

    Screen is not maintained anymore. It has some bugs and the code has a terrible reputation of beeing a huge mess.

    Now Tmux, the equivalent with a BSD license, is highly recommended. There are many configuration files available for downloading and offering the same key mapping as Screen.

  6. larstr’s avatar

    Good article, Scott! I’ve been using screen since ~94 and my first posting at the VMTN forums was actually a request to get screen as a supported package in ESX 2.0. :)


  7. Josh’s avatar

    On OpenBSD, you could have used their built-in screen replacement – tmux. Same idea, cleaner implementation, no pkg to install.

  8. Sharninder’s avatar

    Can you post your .screenrc also ? I would also like to increase the scrollback buffer for my terminal.

  9. slowe’s avatar


    Thanks for the comments, and the suggestions for tmux. Is there a Mac OS X package for tmux? I guess it’s not really necessary, since it only executes on the remote host…just curious. (Note: I haven’t really searched yet.)


    Just add “defscrollback XXXX” to your .screenrc; this will expand the scrollback buffer.

    Keep the comments coming!

  10. jcsmith’s avatar

    You may want to stick with screen on your OSX install for the time being as I believe apple patches it to interact with their environment a little better.

  11. slowe’s avatar

    Jcsmith, thanks for the information. I think I might be able to leverage tmux remotely on the OpenBSD hosts, but I’ll stick with screen locally as you suggested.

  12. Thomas’s avatar

    What secret backdoors are you referring to? You have me worried now…

Comments are now closed.