The magic of the screen command

The Linux command prompt without ‘screen’ is like using a browser without tabs. I used to have 10-15 different server sessions open in different putty windows. This was a nightmare to alt+tab through to find the right server. This was also opened the door for accidentally executing a command on the wrong server. Let me welcome you to the magic of the screen command. With this command you can have limitless sessions open all in a single putty window. These sessions can have whatever you want in it and are completely separate of each other – same server, multiple servers, your game of snake, etc. The first thing to get an effective screen session setup is to setup your .screenrc file. Mine is setup with colors, 30,000 lines of scrollback, and 7 different server lables on the bottom. Note that screen starts its session numbers at 0: .screenrc:

termcapinfo xterm [email protected]:[email protected]
defscrollback 30000
vbell_msg "[[[ ding ]]]"
vbell off

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

screen -t util01 0
screen -t svr01 1
screen -t svr02 2
screen -t svr03 3
screen -t svr04 4
screen -t svr05 5
screen -t svr06 6

Now that our screenrc file is set, just type the command ‘screen’ to start up a new screen session. This session will start with 7 "tabs" for each of the servers that were defined in the screenrc file. You will need to go into each "tab" and ssh to that server. Here are some helpful commands once in screen. Ctrl+a is how you start sending pretty much any command to screen. So ctrl+a then another character. This list is huge (‘man screen’ for a full list) but these are the ones that I find most helpful:

[space]     switch to the next window in line
#     switch to the specified screen number. If you have screens above #9 then you'll need to issue an apostrophe followed by the desired screen number
A     a capital A will allow you to rename the current screen's title
d     detach from the current screen session
K     Kill the current window. Very helpful if the window freezes for some reason

In addition to these, the ‘screen -ls’ bash command will show you screens that are currently attached to a socket. So if your connection to the server where you had your screen session open dies, all you need to do is reconnnect to that server and then reconnect your screen session. Your screen session does not die when you disconnect from the server. For example, I just left work and VPN’d into my network at work. I then opened a putty session to the server where my screen sessions live.

[[email protected] ~]$ screen -ls
There is a screen on:
        4161.pts-0.util01v      (Detached)
1 Socket in /var/run/screen/S-jchapman.

[[email protected] ~]$

I see that the screen session is already detached. This means that I can reattach it to my current session. If it wasn’t detached, then you would have to first detach it with -d and then reattach it to the current session with -r. So this command will reattach the screen session that is listed above:

[[email protected] ~]$ screen -d 4161.pts-0.util01v

If there’s only one session listed (as there is in this example) I can also leave out the screen session name parameter and it will just connect to that one You can do all sorts of cool things like screen sharing. Hopefully this is enough for you to get up and running with screen. The man page is a great source for more information as well.  

Leave a Reply

Your email address will not be published.