GNU screen is not a new tool and while I’ve known about it for a long time, I’ve never had a use for it. What is the point of having multiplexed terminals when I can simply launch more (under Ubuntu)? When I’m in Windows, I can just launch more SSH connections. While having more screens does make organization of many sessions orderly, I prefer a larger screen and the ability to watch several different sessions at once. So while it might not be useful as just a multiplexer, it turns out GNU Screen is great when you have a dedicated server (a term I am using loosely, basically I mean any machine that’s online 24/7) from which to start AND a decent number of machines to connect to.
My use of GNU Screen is as follows: I have a dedicated screen server inside the company intranet from which I connect (SSH) to all the other servers (which is only 6 at this point in time). I also SSH out to two mail servers run by the operations team. Lastly I have 2 SSH connections outgoing to my personal servers (including the one that runs this site).
Why a dedicated server? Well, that’s easy. The real power in screen is the ability to detach and reattach. If you shut down/crash your laptop, the screen session on the server will not be lost (unlike if you did it from your laptop, in which case it would be lost). Additionally, your server will (hopefully) have an always on connection so that the SSH connections will stay connected (theoretically forever). Better yet, you can reconnect to that screen server from anywhere AND you can even force “steal” a session.
If I leave screen running on my laptop at work and find myself needing to work on something on the road, I fire up Better Terminal Emulator Pro on my Xoom tablet and reconnect to the screen/shell server then forcibly steal the session. This saves me a massive amount of effort (because typing on tablet is bad enough, using symbols make it far worse), always allows me to connect, and is much more secure as the servers require SSH keys (which I’m not about to go leaving on a tablet). Once I get home, I can connect again via PuTTY to finish any lingering emergency work.
Using screen is super easy, but I wanted to share a few commands for the uninformed to make it even faster for them to get started:
- screen — (From CLI) Launch screen
- screen -r — (From CLI) Reattach a session locally
- screen -Dr — (From CLI) Detach the session & reattach it locally
- screen -Dr — (From CLI) List any screen sessions & their status
The following are key combos in screen itself. They always start with Control+A, then another key
- Ctrl+a a — Creates a new window and switch to it
- Ctrl+a n — Switch to the next window
- Ctrl+a p — Switch the previous window
- Ctrl+a 0…9 — Switch to window # whatever
- Ctrl+a ” — List all the screen windows and allow you to switch to them
- Ctrl+a d — Disconnect from screen
This is, of course, by no means an exhaustive list of options. These are the most common commands that I use (as being fairly new to screen), as such you’re likely to need them as well. I’d also like to point out Ryan Lane’s post on shell servers which is what originally inspired me to do the same. He also lists some useful tips for configuring your .screenrc and .bashrc files to make your server just that much easier to use (I love the forces server name titles).