Taking Command of the Terminal with GNU Screen

2372

GNU Screen is one of the most useful utilities you can have at your disposal if you spend any time at all working at the command line. Screen allows you to manage multiple shell sessions from one terminal window or console, view multiple shell sessions at the same time, and even log into the same session from more than one location at a time.

The screen utility is a “window manager” that allows you to organize and work with multiple shell sessions in a very powerful way. Utilizing screen, you can manage shell sessions in all kinds of useful ways. This tutorial will show you how to display two or more sessions simultaneously in the same window, “detach” from a session and log in later, and even log into the same session from more than one system.

One note about the commands shown in this tutorial: They are case-sensitive, so Ctrl-a S is not the same as Ctrl-a s. Also, GNU Screen runs on a lot of platforms. This tutorial specifically focuses on newer releases of Screen found on modern Linux releases such as openSUSE 11.2 and Ubuntu 9.10. If you’re running an older Linux release or using Screen on Mac OS X or another Unix-based system, some behavior may be different. For example, Mac OS X Snow Leopard ships with Screen 4.00.03, which is a bit on the older side compared to Screen on the version shipping with modern Linux distros.

Let’s get started with the easy stuff. To start a screen session, type screen. You will see a welcome message with the version of screen and a note at the bottom to hit space or return to continue. So do that and then you will see a standard shell prompt. Now Screen is managing your session.

Managing Windows

So far, not much has changed, but screen can do a lot more than just run a single instance of your shell. Screen allows you to run several shells at the same time. To start a new instance run Ctrl-a c. This will create a new shell. Where did the old session go? It’s still running. To see all your current sessions, and to choose between them, run Ctrl-a “ and you’ll see something like this:

You can select the session you want with the arrow keys or by entering the number of the session you want and hitting Enter. This can get a bit confusing, though, if you’re running quite a few sessions and they all are just named “bash.” To rename a session, use Ctrl-a A and then enter a session name.

Another way to switch between sessions is to use Ctrl-a n and Ctrl-a p to move between next and previous sessions, respectively.

Want to see more than one session at a time? No problem! Screen can split a terminal or console display into several “windows” that can each display a session. To split the window horizontally, use Ctrl-a S. Some newer releases of screen (later than 4.0.3-10) also support windows split vertically. To split a screen vertically, use Ctrl-a | and then Ctrl-a Tab to enter the new window. To start a new shell in that window, use Ctrl-a c or use Ctrl-a ” to choose an existing session to display in that window.

You might have noticed that most screen commands start with Ctrl-a. What if you want to use Ctrl-a for something else? To send that to the shell, run Ctrl-a a.

Attaching and Detaching from Screen

One of the most powerful features of screen is the ability to detach and attach to running sessions. If you tend to work from several machines, it might be a good habit to start an instance of screen when you log in. Then if you’re at a different system you can simply attach or re-attach to the running session and resume where you left off.

Note that you can also share a screen session, so it’s not necessary to detach from a session in order to log into the same session from another location. So, for example, if you’ve logged into a system remotely using SSH and then started a screen session, you could then go to another computer and attach to the same session.

To see which sessions are running, use screen -ls. This will display all running sessions, and the process IDs (PIDs). If you have more than one session running, you will need to know the PID to attach or reattach to an existing session. To detach a session, use Ctrl-a d. If that’s the only session running, you can reattach with Ctrl-a r If more than one session is detached, you’ll need to run Ctrl-a r XXXXX where XXXXX is the PID.

To connect to a session that’s still attached, use Ctrl-a x XXXXX instead. This will let you re-attach to an existing session without any problems.

Can I have a Copy of That?

Want a quick and dirty way to take notes of what’s on your screen? Yep, there’s a command for that. Run Ctrl-a h and screen will save a text file called “hardcopy.n” in your current directory that has all of the existing text. Want to get a quick snapshot of the top output on a system? Just run Ctrl-a h and there you go.

You can also save a log of what’s going on in a window by using Ctrl-a H. This will create a file called screenlog.0 in the current directory. Note that it may have limited usefulness if you’re doing something like editing a file in Vim, and the output can look pretty odd if you’re doing much more than entering a few simple commands. To close a screenlog, use Ctrl-a H again.

Note if you want a quick glance at the system info, including hostname, system load, and system time, you can get that with Ctrl-a t.

Simplifying Screen with Byobu

If the screen commands seem a bit too arcane to memorize, don’t worry. You can tap the power of GNU Screen in a slightly more user-friendly package called byobu. Basically, byobu is a souped-up screen profile originally developed for Ubuntu. Not using Ubuntu? No problem, you can find RPMs or a tarball with the profiles to install on other Linux distros or Unix systems that don’t feature a native package.

Note that byobu doesn’t actually do anything to screen itself. It’s an elaborate (and pretty groovy) screen configuration customization. You could do something similar on your own by hacking your ~/.screenrc, but the byobu maintainers have already done it for you.

Since most of byobu is self-explanatory, I won’t go into great detail about using it. You can launch byobu by running byobu. You’ll see a shell prompt plus a few lines at the bottom of the screen with additional information about your system, such as the system CPUs, uptime, and system time. To get a quick help menu, hit F9 and then use the Help entry. Most of the commands you would use most frequently are assigned F keys as well. Creating a new window is F2, cycling between windows is F3 and F4, and detaching from a session is F6. To re-title a window use F8, and if you want to lock the screen use F12.

The only downside to byobu is that it’s not going to be on all systems, and in a pinch it may help to know your way around plain-vanilla screen rather than byobu.

For an easy reference, here’s a list of the most common screen commands that you’ll want to know. This isn’t exhaustive, but it should be enough for most users to get started using screen happily for most use cases.

  • Start Screen: screen
  • Detatch Screen: Ctrl-a d
  • Re-attach Screen: screen -x or screen -x PID
  • Split Horizontally: Ctrl-a S
  • Split Vertically: Ctrl-a |
  • Move Between Windows: Ctrl-a Tab
  • Name Session: Ctrl-a A
  • Log Session: Ctrl-a H
  • Note Session: Ctrl-a h

Finally, if you want help on GNU Screen, use the man page (man screen) and its built-in help with Ctrl-a :help. Screen has quite a few advanced options that are beyond an introductory tutorial, so be sure to check out the man page when you have the basics down.