| Gert: | blog |
| Mario: | blog |
| Bert: | blog |
| Guy: | blog |
| Maarten: | blog |
I wrote this Gnu Screen guide in December ‘08 with the previous incarnation of linux.com in mind. January 2009 however, linux.com started on its new and exciting path as a community site and the article just sat on my machine, waiting. So I decided to publish it on my blog in a four part series over the following month. Maybe someone can use it
Have you ever had to run a time consuming process (say mkfs or compiling something) over a shoddy ssh connection? Or cleanly run several commands in parallel without having several open connections to that server? Or maybe you were programming and wanted a mysql prompt, a bash prompt and several text editors open? How about that ssh tunnel you need open and dont want to close by accident? And did you ever need to perform the same task on several different machines? Or connect to a serial terminal? Or maybe you just want a log file of what you re doing..
These are day to day situations any linux user will run into, that can be quite frustrating. That is, before you discover Gnu screen!
“Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells.” Or at least, thats what the site calls it. Sounds easy enough, but there are an amazing amount of different uses for this incredible piece of software. The above list is some of the uses I could readily think about – some of the ways I regularly use screen, but like any decent linux tool – the sky really is the limit! I’ll be looking into different issues, so lets jump into some of the practical solutions for those problems.
The Fun Stuff – Nethack
Because linux is all about FUN (or at least written by fun loving people), screen has a cool option where all messages are given nethack style, instead of the standard bland wording. Screen has to be compiled using the Nethack option, but most linux distributions do.
The nethack option it toggled by the presence of the $NETHACKOPTIONS environment variable or a ~/.nethackrc file.
No other window.
becomes
You cannot escape from window 0!
or
Creating logfile "screenlog.0".
becomes
You add to your scroll of logging named "screenlog.0".
The nethack ones are actually More Fun, really!
The Fun Stuff – Different screen windows
Up till now, we covered using screen and the posibilities of several sessions. But because switching between sessions is not really what you d call user friendly or quick, screen has an interesting mechanism called windows. One screen session can contain several windows and switching between these is simple using a number of shortcuts. The existing screen terminal is always the first window. To create a new window, use the following shortcut.
C-a c C-a C-c (screen) Create a new window with a shell and switch to that window.
This creates a new window & switches to that window. Now you have two (or more if you want) different windows in the same screen session. To switch between the different sessions, you can use the following shortcuts.
C-a space C-a n C-a C-n (next) Switch to the next window. C-a backspace C-a h C-a p C-a C-p (prev) Switch to the previous window.
This is a good way to switch between the windows, but it quickly grows old when you have a lot of open windows to switch between. To make things a bit more manageable; screen has several other shortcuts.
C-a ’ (select) Prompt for a window name or number to switch to.C-a 0 (select 0) ... ... C-a 9 (select 9) C-a - (select -) Switch to window number 0 - 9, or to the blank window.
These help, but there is still a better way.
C-a " (windowlist -b) Present a list of all windows for selection.
This shortcut gives the user an interface where he can select an open window.
Num Name Flags  0 bash $  1 bash $ 2 bash $
And to keep this heap of windows organised, there are yet 2 more shortcuts.
C-a A (title) Allow the user to enter a name for the current window. C-a N (number) Show the number (and title) of the current window.
These are a lot of different shortcuts and mechanisms, but in the end – its all worth the effort! Lets take a look at the list of examples we made at the start of the article and solve another one!
I dont think the practical side of this example needs much more explaining. Opening the screen session, creating the different windows, renaming the windows and using them appropriately should be straightforward enough. Setting the whole thing up only needs to be done once, since you can simply detach the session, However if you need this setup on a regular basis, you can configure your “.screenrc” to load it at startup. More on the .screenrc later.
The Fun Stuff – Sending commands to all screen windows
Once you re used to the multiple windows, you might run into a situation where you want to send a same command to several of these open windows. Screen provides in the “at” command to do this. First you ll need to open command line mode.
C-a : (colon) Enter command line mode.
This way you can type a command once, but you ll still have to enter each separate window. But there is a better way. As an example we ‘ll send “ls -l” to all the windows.
at "#" stuff "ls -l^M"
This command is barely readable, so let’s pick it apart! The first part is ‘at [identifier][#|*|%] command’. The at command sends the text parameter to all the windows you specified in the identifier. You can match the criteria to either the window name or number with #, the user name with * or the displays, using %. The next part is the command you want to run in the selected windows. We’re using “stuff” to stuff the command we want to execute into the input buffer of the selected windows. Stuff is really straightforward. It simply stuffs the string you gave as a parameter. Next problem is the command. Or rather having it executed! To get screen to put an “enter” after the command, to execute the command, add “^M” at the end. You can do a lot more with this than just sending an ls to the input. Any screen command, like renaming, moving windows around, whatnot .. is available in combination with “at”.
This also applies to screen session using the -X parameter on the command line, for those special occasions where you just dont want to connect to the session or when you’re scripting something wholly exciting!
Special thanks go out to Guy Van Sanden, who helped me figure out this command.
The possibilities of this command are vast and this is only one of the many that come to mind. The first and most obvious is when you need to close a screen session with too much windows to close by hand. Stuffing an “exit^M” into the open windows until the screen sessions are alle effectively closed is quite usefull!
The real luxury however is when you need to install the same software on several different machines. You start by opening ssh connections to the boxes you want to administer. You might consider using the .screenrc to open the connections and ssh keys to facilitate the loging in. Once logged in, you can send the appropriate commands (for example “sudo yum install htop”) to the different screen sessions, effectively managing all those machines at the same time. This is ofcourse something to use with extreme caution – imagine deleting the wrong directory on one of those boxes – but a conscient administrator should be able to use this to his advantage!
The articles in this series were updated to gertschepens.be; These articles focus on the following topics..
October brought me a new job in the Open Source world when I started working for Open-Future, a newly created (Belgian) company dedicated to Open Source consultancy.
We will be organizing an official launch party on December 07 in the Oude Abdij, Kortenberg. I hope that we can push Open Source adoption in Belgium, which is rather slow compared to other countries!
I had to install Windows for someone who needed Office (for a specific thing). So I installed VirtualBox on his Karmic PC and created a VM.
When the Hard Disk dialog came up, I choose 3 GB thinking it's only Windows + Office without data... Boy was I wrong.
After installing Windows XP and Office 2003, a dialog popped up saying that I had less than 60 MB left! I was shocked that I needed so much space for nearly no software (let alone data)...
I'm now hugging my Ubuntu machine, greatfull that it can fit more than just minesweeper on the odd gigabyte.
Not using windows for months on end actually makes me forget just how bad it really is...
I got an error yesterday when trying to create a xen virtual machine with a lot of disks.
I had to add 32 disks on the machine, which would be used to backup the hard drives.
The error was Unable to find number for device (sdq1). I had devices up to sdab1, but he already stuck at the sdq1.
How to work around this problem you ask? Really easy it seems: Just use other device "types" for the disks beginning at sdq1. So sdq1 became hda1.
And all went well.
Viva Xen!!
I wrote this Gnu Screen guide in December ‘08 with the previous incarnation of linux.com in mind. January 2009 however, linux.com started on its new and exciting path as a community site and the article just sat on my machine, waiting. So I decided to publish it on my blog in a four part series over the following month. Maybe someone can use it
Have you ever had to run a time consuming process (say mkfs or compiling something) over a shoddy ssh connection? Or cleanly run several commands in parallel without having several open connections to that server? Or maybe you were programming and wanted a mysql prompt, a bash prompt and several text editors open? How about that ssh tunnel you need open and dont want to close by accident? And did you ever need to perform the same task on several different machines? Or connect to a serial terminal? Or maybe you just want a log file of what you re doing..
These are day to day situations any linux user will run into, that can be quite frustrating. That is, before you discover Gnu screen!
“Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells.” Or at least, thats what the site calls it. Sounds easy enough, but there are an amazing amount of different uses for this incredible piece of software. The above list is some of the uses I could readily think about – some of the ways I regularly use screen, but like any decent linux tool – the sky really is the limit! I’ll be looking into different issues, so lets jump into some of the practical solutions for those problems.
The fun stuff – Command Parameter
So far for the basics. We ve covered the strenght of being able to attach & detach a session, but screen has a wad of other options that prove fun toys.
Screen allows you to provide a command as a parameter.
screen <Screen Parameters> <command> <Command Parameters>
Screen executes the command given in the parameter. The parameters before the command are parsed by screen, the parameters after the command are passed on to the command. Without additional parameters, screen will open a session, execute the command and close afterwards. Screen cleans up after closing so any output to the terminal is erased afterwards.
$ screen ls [screen is terminating]
While this might not seem too spectacular, this really opens a world of possibilities.
The first benefit is obviously when starting a remote command you don’t want interrupted by a failing connection. You could right as easily start a screen session and type the commands there, but in the end, you ‘ll notice that typing “screen” before the command is a tad quicker than doing it in the two passes. This is of course only the case if the command you need to run does not finish with any meaningfull output you need to study.. although if it does, you can always pipe it into a file. I frequently use this for moving files or even running a vi or emacs session
screen mv foo bar
The other and most important benefit is scripting. The option to run commands in a screen session out of a script allows for a broad spectrum of different possibilities. My favorite example is a screen – ssh wedding..
One of my daily screen applications is an icon I have on my desktop. It opens an ssh tunnel to a designated server, authentication over ssh is handled by a public key and the screen options are as such that the session is either created or reattached. All my outgoing mail trafic is tunneled trough ssh to make sure that I dont have to change servers depending on what location I am working at.
#!/bin/bash if [[ $( screen -ls | grep -c tunnel.Gen ) == 1 ]] then gnome-terminal -e "screen -r tunnel.Gen" --hide-menubar -t "Gen" else screen -d -m -S tunnel.Gen ssh user@host -L 25025:smtp.server.com:25 gnome-terminal -e "screen -r tunnel.Gen" --hide-menubar -t "Gen" fi
The workings of the script should be quite obvious, so I’ll skip that part and shed some light on the screen parameters. The “screen -r ..” part should be clear by now, this reattaches the screen session. The “-d -m” option tells screen to create a new session that is not attached to the terminal. And the “-S” option names the session, as we discussed before.
Thus effectively creating a tunnel window that can be automatically closed & opened at will, without losing the tunnel; and can effectively be terminated by exiting the ssh connection.
The Fun Stuff – Multi display mode
When we were talking about reattaching an attached session, we talked about another option instead of detaching the session.
Screen allows multi display mode. Before we get all excited, I want to point out a small detail in the setup. The users need to have access to the screen session. As we discussed before, this means they somehow both need the same user rights. This can be achieved by both logging in as the same user or by sudo or the likes. Two users on the same account does admittedly sound messy on a sysadmin level, but this might actually be worth while.
Multi Display Mode allows different users to connect to the same screen session together. This provides a powerfull tool for teaching someone terminal related knowledge in a shared environment or even have an attempt at solving an advanced problem together. To start such a session, both users should be able to access the same session (user). One of them starts the session as usual – preferably with an easy name.
screen -S Shared
And then have the other person connect to the session using the “-x” parameter.
screen -x Shared
& that will give you a nice shared session where you can have some fun together.
This is a trick that thoroughly helped me when simultaneously installing several servers with a colleague some years ago. This way we could continue each others work when necessary and avoided conflicting operations on the same machine.
This technique has been featured on Linux.com before though. Using screen for remote interaction by Philip J. Hollenback. The article goes in much more detail, although I have to admit the setuid aspect Really sets me off. The security consequences are just too big. But no comment on screen on that part. There is simply no way to do what needs to be done without the setuid flag. The other option would be to have the sessions available to all users and that would be an actual flaw, while the setuid flag is a potential flaw.
In the end, we can only conclude that this is a powerful option with lots of potential; but only under the right special circumstances.
The Fun Stuff – Copy Mode
After using screen for a while, one thing that will probably bother you is that the classic shift – pgup / pgdown doesnt work. Being unable to scroll back to earlier output or check command output beyond one page soon turns out to be quite the nuicance. To solve this, copy/scrollback mode – or vi command mode – can be used.
C-a esc (copy) Enter copy/scrollback mode.
This opens the buffer in a vi like mode where you can scroll back and forth in the buffer and use a whole set of different commands. Commands for scrolling around in the buffer, several ways of searching the text, several commands for selecting text in different patterns and yanking it for using elsewhere.
To make full use of the numerous keys in this mode, you should look up the “copy” chapter in the screen man page.
The articles in this series were updated to gertschepens.be; These articles focus on the following topics..