Has this happened to you?
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.
You can prevent this by learning how to use Linux screen.
In fact, you can learn the basics in just 2 minutes.
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 as part of our server management services.
2 Minute Intro
(Sorry for the font size, will record this again soon.)
Linux Screen How-To
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. Linux 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 Linux screen 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 Linux 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.
Screen key bindings, page 1 of 4. Command key: ^A Literal ^A: a break ^B b only Q clear C other ^A colon : pow_break B copy ^[ [ pow_detach D detach ^D d prev ^P p ^? digraph ^V readbuf < displays * redisplay ^L l fit F removebuf = flow ^F f reset Z focus ^I screen ^C c hardcopy h select ' help ? silence _
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.
Linux 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.)
top - 09:10:33 up 35 days, 17:26, 1 user, load averag Tasks: 131 total, 1 running, 130 sleeping, 0 stoppe Cpu(s): 0.4%us, 0.2%sy, 0.0%ni, 99.4%id, 0.0%wa, 0 Mem: 12302040k total, 6363652k used, 5938388k free, Swap: 1052248k total, 12k used, 1052236k free,
Now open a new window with: "Ctrl-A” “c”
To get back to top, use “Ctrl-A “n”
top - 09:06:14 up 35 days, 17:22, 1 user, load averag Tasks: 131 total, 1 running, 130 sleeping, 0 stoppe Cpu(s): 3.1%us, 0.3%sy, 0.0%ni, 96.5%id, 0.0%wa, 0 Mem: 12302040k total, 6342984k used, 5959056k free, Swap: 1052248k total, 12k used, 1052236k free, PID USER PR NI VIRT RES SHR S %CPU %MEM 1 root 15 0 10372 712 596 S 0.0 0.0 2 root RT -5 0 0 0 S 0.0 0.0 3 root 34 19 0 0 0 S 0.0 0.0
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 Linux 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 Linux 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.
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 Linux 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.
Linux 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.
Latest posts by Jeff Huckaby (see all)
- 3 Quick Tweaks to Boost cPanel Performance [Video] - April 1, 2014
- The 20 Best WordPress Security Tips on the Web - March 12, 2014
- Warning: Hackers Are Using SSH Tunnels to Send Spam - March 5, 2014