Blog
Linux Screen Tutorial and How To
Posted by Jeffrey Huckaby 01/01/2008
Using Linux Screen for Session Management
Lost your shell connection? Need multiple shell sessions?
You are logged into your remote server via SSH and happily plucking along at your keyboard and then it happens. Suddenly, the characters stop moving and then you get the dreaded “Connection Closed” message. You have just lost your session. You were halfway through some task and now you have to start over. Ugh. Well you can prevent this from happening by using screen. The Linux screen tool can not only save you from disconnection disasters, but it also can increase your productivity by using multiple windows within one SSH session. I use this tool all of the time in our server management work.
Linux Screen for Session Management!

Linux Screen showing the GNU page for Linux Screen Itself
Linux Screen Can Save you from that Lost Connection
What is Screen for Linux?
As the man page states, “Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells).” This can be a life saver when working on your dedicated server. Screen has a several great features for helping you administer your server more productively and safely. I am going to discuss the three features (multiple windows, logging, sessions) that I use the most. Once you mastered screen, you may want to check out our 12 Point Server Maintenance Checklist and put your new found skills to use.
Installing Screen on Linux
Chances are that you already have screen on your system. On most Red Hat distributions you can find it in /usr/bin/screen. To see if screen is in your path, you can use the which command:
[admin@ensim admin]$ which screen
If you do not have screen, then you can install it easily from an RPM or the package file for your system. On Cobalt Raq servers, you can safely use the RedHat RPMS appropriate for your system.
Screen RPMs: rpmfind
Screen Web site: GNU Screen
As you probably already have screen or can use an RPM, I am not going to cover the building of screen from source. Lets get on to how to use screen.
Using Screen
Screen is started from the command line just like any other command:
[admin@gigan admin]$ screen
You may or may not get a text message about screen. If you do not, then you probably think nothing has happened, but it has. You are now inside of a window within screen. This functions just like a normal shell except for a few special characters. Screen uses the command “Ctrl-A” as a signal to send commands to screen instead of the shell. To get help, just use “Ctrl-A” then “?”. You should now have the screen help page.
Screen key bindings, page 1 of 2.Command key: ^A Literal ^A: a
break ^B b lockscreen ^X x reset Z
clear C log H screen ^C c
colon : login L select " '
copy ^[ [ meta a silence _
detach ^D d monitor M split S
digraph ^V next ^@ ^N sp n suspend ^Z z
displays * number N time ^T t
fit F only Q title A
flow ^F f other ^A vbell ^G
focus ^I pow_break B version v
help ? pow_detach D width W
history prev ^P p ^? windows ^W w
info i readbuf < wrap ^R r
kill K redisplay ^L l writebuf >
lastmsg ^M m remove X xoff ^S s
license , removebuf = xon ^Q q
[Press Space for next page; Return to end.]
Key bindings are the commands the screen accepts after you hit “Ctrl-A”. You can reconfigure these keys to your liking using a .screenrc file, but I just use the defaults. The power of screen will become obvious, especially if you need to bounce around to different file system locations and leave processes running. For example, when I go in to clean out wasted disk space, I can remove files in one screen while hunting for other files in another.
Multiple Windows
Screen, like many windows managers, can support multiple windows. This is very useful for doing many tasks at the same time without opening new sessions. As a systems manager, I often have four or five SSH sessions going at the same time. In each of the shell, I may be running two or three tasks. Without screen, that would require 15 SSH sessions, logins, windows, etc. With screen, each system gets its own single session and I use screen to manage different tasks on that system.
Another trick I use is when I am editing configuration files. For example, if I want to harden SSH, I can cat out the configuration file in one window and then edit it in another.
To open a new window, you just use “Ctrl-A” “c”. This will create a new window for you with your default prompt. For example, I can be running top and then open a new window to do other things. Top stays running! It is still there. To try this for yourself, start up screen and then run top. (Note: I have truncated some screens to save space.)
Start top
Mem: 506028K av, 500596K used, 5432K free,
0K shrd, 11752K buff
Swap: 1020116K av, 53320K used, 966796K free
393660K cached
PID USER PRI NI SIZE RSS SHARE STAT %CPU %ME
6538 root 25 0 1892 1892 596 R 49.1 0.3
6614 root 16 0 1544 1544 668 S 28.3 0.3
7198 admin 15 0 1108 1104 828 R 5.6 0.2
Now open a new window with “Ctrl-A” “c”
[admin@ensim admin]$
To get back to top, use “Ctrl-A “n”
Mem: 506028K av, 500588K used, 5440K free,
0K shrd, 11960K buff
Swap: 1020116K av, 53320K used, 966796K free
392220K cached
PID USER PRI NI SIZE RSS SHARE STAT %CPU %ME
6538 root 25 0 1892 1892 596 R 48.3 0.3
6614 root 15 0 1544 1544 668 S 30.7 0.3
You can create several windows and toggle through them with “Ctrl-A” “n” for the next window or “Ctrl-A” “p” for the previous window. Each process will keep running while your work elsewhere.
Leaving Screen
There are two ways to get out of screen. The first is just like logging out of a shell. You kill the window with “Ctrl-A” “K” or “exit” will work on some systems. This will kill the current windows. If you have other windows, you will drop into one of those. If this is the last window, then you will exit screen.
The second way to leave screen is to detach from a windows. This method leaves the process running and simple closes the window. If you have really long processes, you need to close your SSH program, you can detach from the window using “Ctrl-A” “d”. This will drop you into your shell. All screen windows are still there and you can re-attach to them later. This is great when you are using rsync for server migration.
Attaching to Sessions
So you are using screen now and compiling that program. It is taking forever and suddenly your connection drops. Don’t worry screen will keep the compilation going. Login to your system and use the screen listing tool to see what sessions are running:
[root@gigan root]# screen -ls
There are screens on:
31619.ttyp2.gigan (Detached)
4731.ttyp2.gigan (Detached)
2 Sockets in /tmp/screens/S-root.
Here you see I have two different screen sessions. To re-attach to a session, use the re-attach command:
[root@gigan root]#screen -r 31619.ttyp2.gigan
Just use screen with the -r flag and the session name. You are now re-attached to the screen. A nice thing about this, is you can re-attach from anywhere. If you are at work or a clients office, you can use screen to start a job and then logout. When you get back to your office or home, you can login and get back to work.
Screen Logging
As a consultant, I find it important to keep track of what I do to someone’s server. Fortunately, screen makes this easy. Using “Ctrl-A” “H”, creates a running log of the session. Screen will keep appending data to the file through multiple sessions. Using the log function is very useful for capturing what you have done, especially if you are making a lot of changes. If something goes awry, you can look back through your logs.
Linux Screen Tips
Just wanted to mention to other cool tricks you can do with screen. Screen can monitor a window for activity or lack thereof. This is great if you are downloading large files, compiling, or watching for output. If you are downloading something or compiling, you can watch for silence. To start the monitor, go to the screen you want to monitor and use “Ctrl-A” “M” to look for activity or “Ctrl-A” “_” to monitor for silence. Then open or switch to a new window. When the monitor detects activity or silence, you will get an alert at the bottom with the window number. To quickly go to that window, use “Ctrl-A” “ (thats a quote mark, ctrl-a then a “). After you do this, just type in the number of the window and enter. To stop monitoring, go to that window and undo the monitor with the same command. For example, to stop monitoring for activity you would use “Ctrl-A” “M” again.
Screen is a great tool and I use it daily for tasking ranging from hardening ssh against brute force attacks to deploying Nginx web server for a client.
Reference
Screen was covered recently in Linux Magazine by Adam Lazur (Jan 2003, Issue 105). Much of his information was adapted for this rackTIP. Other information was collected from the man pages.
Comments
I used to work with nohup until some problems with it made me use screen which is far better from nohup.By the way nice post especially for newbies.
Thanks for tutorial! Close (x) will close screen on not ?
I typically just exit from the screen by typing “exit”.
Installed screen on an ubuntu server and couldn’t log back in.
Guessing it messed up some pam config.
Searching for anything with “screen” in it is an exercise in aggravation.
I have screen installed on 3 or 4 dedicated servers ... it’s silly but on one of them I have a bar in the bottom of it - which is very handy… and don’t know how to turn it on on the rest of my servers :)
extremely helpful and definitive. thank you for sharing
I am using ssh and screen to work remotely on a linux server with my macbook. It works quite well to some extend. The only problem I have is that there is nothing like “Ctr-A” on a mac keyboard. So I cannot use “K”, “p” and “n” directly. Does anybody know what shortcut I need?
Thanks.
@Laetitia - I use a macbook (intel osx 10.4) and have no trouble… im pretty sure you will find a “Ctrl” key on your mac keyboard, somewhere to the left of space bar and below your shift key.
http://images.apple.com/keyboard/images/gallery/wired_1_20070813.jpg
laetitia - mac keyboards have a “ctrl” key and of course the “a” key - surely you dont mean that?
We just got our first Mac in our office last week. Will have to ask our new staff member how to do it.
There is any way to run this tool on a linksys router wrt54GL with dd-wrt firmware on it?
Instead of using Ctl-A for commands, I use the backtick key (key to the left of the ‘1’ key).
To do this I entered the following commands into my .screenrc file:
defescape ``
escape ``
(those are 2 backticks for each entry)
Now to create a new screen window, all I have to enter is `c.
Great write-up. Just enough for me to go and find additional information & uses for the screen command.
Nice I used this to run fsck and remote back in later on from home as it requires user input and you cannot use nohup with fsck easily if you want user input to fix errors etc.
Thanks
This is a good tutorial. Thx.
chusty: Add “hardstatus alwayslastline” to your .screenrc
Hi,
What about env variables with screen command ? Do you export all you need in new window, or what procedure should one follow ?
Thanks !
Thanks for your article. I discovered control keys I didn’t know. The logging thing sounds great.
2 points I wanted to add:
Another nice thing with screen is that several access can be done at the same time.
That means several people can connect to the same screen and see live what others are doing.
Great stuff when ie you want to explain how something is working.. a bit like a live tuto.And you can also use the .screenrc file to change the way it shows the sessions. Ie, all the active sessions are shown at the bottom and colored, you can give them a name, you can choose the number of sessions to launch when starting screen and execute a command in these sessions, like a ssh tunnel or a monitoring software.
you can greatly improve the layout of screen by changing them.As it was someone who gave me that config file, I don’t know what options exactly do these tricks.
If you are interested to have mine to analyse the options, contact me.
quenenni, I would love to see you config file. Could you please post it?
Thank you, a great article, very helpful!
Thanks, It was very helpful.
Thanks, extremely well writen
quenenni,
I would also be interested in seeing
your config file. As Matthew requested, can you post it, or a url to where we can view it ? Thanks for your input.
Yop yop..
I sent the file to the website owner already few weeks ago, but it seems something went wrong.
Anyway, here it is:
http://quenenni.domainepublic.net/screenrc.txt
Put it in the home directory under the name ‘.screenrc’ .
I know you submitted a ticket in our sales area but I never saw the other email with the screen file. I wonder if the security system flagged it or someone else here deleted by mistake. Thanks for posting it again.
Thanks a lot, a friend showed me once how to use screen, and this is a great refresher/reference. It should be installed as default when you apt-get ssh-server
Yep, screen is a really useful tool.
At the moment I use the enhanced version called “byobu”, which adds a cool bar at the bottom of the window with useful information. (That’s probably what @chusty has found)
This tutorial is very clear and really helpful. Sometimes there are just a few small things that you need to get over a learning curve and this did it for me.
I am using screen on my Dreamhost hosted site to run Nodejs to experiment. I can log out of my ssh session and node is still chugging along. Thank you!
Glad you liked our tutorial. I think there are some newer tools out there, but I always fall back on screen for its simplicity and easy of use.
Just great! Specially thanks to @quenenni for your .screenrc !
Hi Jeff,
Useful tutorial - I’ve had the screen die on my occasionally (once in 2-3 months or so) - I am left helpless when it dies, basically all record of which machines I logged into etc are lost. Is there any way to resurrect a dead screen? Or port an existing screen to a different machine? I am looking for some way to recover a session either from its dead state or from another machine with a history file or something like that..
What is being shown with screen—list?
If it is listed as attached, you can use:screen -x -R
and it will force detach the screen. If it is listed as dead, the socket is no longer active and it cannot be recovered.
Cool. Will have to try it out!
Fantastic tutorial / HowTo! Clear and simple. I searched a while for a good one like yours. Thank you very much.
The Num Pad on my keyboard doesnt work with screen, also my keyboard layout is lost, but as soon as I close it, it gets back to normal, any ideas?
Thanks for writing this up! It was very helpful.
Haha, your intro paragraph summed up exactly how I used to feel before I discovered screen.
Kool info, very useful indeed….
Thanks for the feedback. I should probably do a screen tutorial part 2. I never imagined when I first wrote this years ago it would be so popular.
great tutorial
Perfect. Most screen tuts didn’t point out that you need to press CTRL+A THEEEN another key.. Thanks for the post.
Excellent article.
I heard screen from watching support work remotely. They actually maximized my putty and had 3 prompts going at the same time. one was counting and the other two were tail -f of different logs. I have been trying to recreate that using CTRL+A f this splits the screen but does not give me another prompt. Any thoughts on what I am doing wrong?
thanks for the excellent article!
Thanks for the nice article.
@quenenni you mentioned “That means several people can connect to the same screen and see live what others are doing.”
can you tell how to do this
To join a screen that is already attached, use:
screen -x screenid
You can get the screen ID number using list:
[root@gigan ~]# screen -list
There is a screen on:
29878.pts-6.gigan (Attached)screen -x 29878.pts-6.gigan
Will attach you to that screen. Both users have control so be careful.I use this when training or reviewing staff.
Best screen tutorial on the net!
Great intro. I was just starting to play around with screen, and yours was the first tutorial that came up on Google (I see why!). Thanks for putting this together.
Thanks for this tutorial! It helped me so much, this is EXACTLY what I needed :)
Dear Jeff
Awesome tutorial. I have been using Screens for last 6+ years and it works great. I leave all my windows as it is and next day i get my work as it is. Saves me lot of times.
Recently i was stuck on screen as it went in silent monitoring mode and i was not aware of it. I came here and learned about how to use silent monitoring.
Thanks a lot for this.
Regards
Chantu
Hands down the best intro to screen tutorial I’ve seen :bowdown: I wish I had this as a primer when I was trying to figure screen out.
The Linux command to obtain help screen for the rm command is
hello,
could you please tell me how can i run a matlab code in screen?
Thank you it was great
Great Tutorial, great tool! This will help me monitoring/maintaining root servers. Thanks for sharing!
That was fast and useful ! No unrequired detail.
I knew what screen was and what it did, but until this article I wasn’t proficient in using it. Thanks so much!
Thank you so much, your tutorial is great!
Thank you very much. It’s surely enlightening.
Don’t mean to necro an old post, but thought it was worth noting you are still getting hits on this great how to.
Excellent work!
Thanks for info, it was helpful
Dear Jeff.,
Awesome tutorial. I had learn much from this article.
I have a idea about the activity monitor. Since screen can monitor the activity or silent in a windows, is it possible to invoke a user specified script or command when the silent was detected?
If it is possible, we can get the information as soon as possible.
Thank you very much.
Nice Article to get started with screen.
I love it.Thanks
good post.. let me get into screen in 10mins. :)
Hello and thanks for the article. I use linux screen at work, but not long ago I moved to the Mac. The big problem is that it is not possible to use the mac keyboard to do simple Screen command (like ctrl+right arrow) for moving to the next screen or work with different tabs. Does anybody knows a way/tutorial/application to use linux screen command as in linux or Windows through Putty? Thanks in advance.
Great tutorial on screen - we’re just about to post a shorter tutorial on screen on our blog. (and no worries - we didn’t lift any ideas) ...check out our open source blog @ www.digitalboundary.net/wp
Thanks!
JG
Highly recommended - a very clear and concise tutorial.
Wow, I never used screen before but I wonder how I did it. Found this by google, managed to find the essentials in a couple mins, great one, thanks!!!
We run several screen instances from one box, eg. :
27185.pts-53.CentOS-57-64-minimal (Detached)
27209.pts-53.CentOS-57-64-minimal (Detached)
27281.pts-53.CentOS-57-64-minimal (Detached)Is there a way to ‘name’ a screen id, so instead of having to remember
the exact PID, i use screen -x 1 for pid 27185, screen -x 2 for pid 27209 and screen -x 3 for pid 27281 ??
say that I have 8 screen sessions running, I attach to one of them using screen -r <pid>, how do a ‘gather’ the other 7 sessions so that I can use the full functionality of stacking the screens?
@chip: Assuming you want named sessions for clarity, you can name a session in screen by starting screen as:
> screen -U -S session_name
You can then reattach to a session using:
> screen -R session_name
or kill/quit a session using:
> screen -S session_name -X quit
Hope that helps
@sanjayts: Thank you for responding back. I am very familiar with screen and a good deal of the the things that can can be done with it. My question is a little more out on the edge: I already have 8 separate screen sessions running and I ‘reattach’ to one with the normal method of screen -r <pid>, however, once I have that screen session attached I would like to be able to attach to the other 7 and stack them, much like we would do ctrl+a ‘c’ . In the end ‘gather’ all of these individual sessions and bring them under one roof so I can scroll through them using ctrl+a n or p (and see them listed, etc). Thanks
@chip: Oops, my previous post was actually meant for @Miki but I’ll take a stab at your question.
AFAIK, there is no way of concatenating multiple screen sessions. Some recommend attaching processes to the new terminal as here http://unix.stackexchange.com/questions/21470/concatenate-two-screen-sessions but I’m not sure if that’s what you want. As a good compromise, tmux allows jumping between sessions using the ‘choose-session’ command which should at least relieve you of detaching and reattaching just to view the contents of a different session. Good luck.
Great tutorial, thanks! Screen is awesome.
I can’t reattach a session of another user being root. It’s important for me because I administer a network and often users forget “screen” sessions on the hosts. Is it possible ? Thank you.
gr8 !!
Many a time my putty session disconnected during Linux patching. gr8 pain in the…
This one seems will ease my patching pains.Love this one. Thx a lot.
thank you
i’ve been having linux screen management problems for a while now,
maybe your post will help me to figure out some stuff
very good howto, go to basics is the best
Thanks, mate. Great tutorial.
Thanks a lot for the tutorial, it was very useful for me since I have only recently switched to Linux… and love it so far. I will keep watching the blog for more helpful updates.
Thanks for the great article!
I was looking to try using Screen on Linux and after reading this piece from you it looks rather simple.
Thanks for the great guide.
Anyone know how to increase max number of window in a single screen session? In my setup, I can open 40 . For 41th window, getting message “no more windows”.
On my Angstrom distribution screen doesn’t seem to remain active when the ssh connection drops (or is exited). screen -ls gives “no sockets”. Any idea’s how to get this working?
i need how to search fields by using shellscript
Hi, is it possible to display all the sessions in the status bar if we have more that 10 session in a single screen? The issue i see is that when i open more than 13 session the new session are getting hidden not displayed on the hard status bar. we can use ctrl-a w to see the list. I am looking for an option where we can display all the sessions (for example 20 session with tile) some thing like wrap and display the new windows created in the 2nd line on the hard status bar.
The name of the software is “GNU Screen”, not “screen” and definitely not “Linux screen”. By calling it that you’re mis-attributing the software and misleading your readers.
Fantastic tutorial - always knew that this tool existed, but never had a clear guide and path to get into it - going to be using this every day now!
Thank You so much,
this article definitely saved several hours ...thanks
thank you - a really nice introduction! very helpful
@Jeff - Thank you for your efforts! Thank you for taking time out of your life to post these write ups. Though it may be simple… from the remarks that you received after your post, it is evident that you helped people and you were on par covering the need for it. Kudos to you sir!
Arghhh…. happily using cygwin and getting kicked off regularly.
Ran this screen as it is supposed to protect against that and its decided to bugger around with my settings so now I can’t scroll up and down the large screen I happily had…
This tutorial is so cool.. opens a whole new world to managing my linux wiindows, thanks Jeffrey !!
I have to agree with everyone else here. This is a great overview tutorial for newbies like me. I have a feeling I will be back here many times. Bookmarked!
No, it’s not GNU. I have the 4th Berkeley distribution, and it’s just called ‘screen’ version 4.0.2. According to the man-page:
Its roots are a merge of a custom version 2.3PR7 by Wayne Davison and several enhancements to Oliver Laumann’s version 2.0. Note that all versions numbered 2.x are copy-right by Oliver Laumann.
Nothing to do with GNU.
Would I be right in assuming that this would not work well for GUI programs? If I start SSH with the “-X” option and run a remote graphical program, then the program will still die if I should lose my connection, since the X-Windows connection has broken, right? If not, how would I ever get back to the GUI interface?
Nothing like a comment on a five-year-old blog, but I second Matthew’s recommendation for Byobu. It’s in the repos for Debian and is preinstalled in Ubuntu server. A nice feature is automatic start, so once it’s enabled I can just use it without forgetting to launch it.
Add Comment
Commenting is not available in this section entry.SUBSCRIBE
Recent Blog Posts
-
Jeffrey Huckaby Honored in Fifth-Annual MSPmentor 250
Posted by rackAID 01/03/2013
-
Improve CRO, SEO and Usability with these Simple Tools
Posted by Jeffrey Huckaby 10/31/2012
-
12 Point Server Maintenance Checklist
Posted by Jeffrey Huckaby 10/29/2012
About Jeffrey Huckaby
maks · 11/19/2009 05:10 am