How many times have you secure shelled into your data center Linux servers, only to have (for whatever reason) your remote secure shell connection broken? When you reconnect to that server, is that critical command you issued still running? Or did the severed connection put the kibosh on the command? Not only can this be incredibly confusing, it can also be a cause for you to lose precious work and time.
Fortunately, there's a way around this: The screen command. With screen, you can reconnect to that server and pick up where you left off with your last command. I want to show you how to make use of this Linux admin tool. I'll be demonstrating on Ubuntu Server 16.04, but screen is available for nearly all Linux distributions.
What is screen? Simply put, screen is a full-screen window manager that multiplexes a physical terminal between several processes. When you call the screen command, it creates a single window where you can work as normal. You can open as many screens as you need, switch between them, detach them, list them, and reconnect to them.
Let's walk through the process of using this handy command.
On Ubuntu Server 16.04, the screen tool is already installed. If you find it's not there, you can install it with the command sudo apt install screen. If you're using Fedora, that install command would be sudo dnf install screen. That's all there is to installation.
Secure Shell to your remote Linux server. Before you start working, issue the command screen. You will be greeted by a welcome window (Figure A).
You can customize that screen to make it easier to remember what screen is running what command, like so:
screen -S NAME_OF_COMMAND
Where NAME_OF_COMMAND is the actual command you'd run.
Hit either the Spacebar or Enter to exit the welcome window. You will now find yourself at what looks like a standard prompt. Let's issue the command sudo tail -f /var/log/syslog. The tail command will start following input on the syslog log file (as expected). Now let's open a new screen by hitting the [Ctrl]+[a]+[c] key combination. This will give you a new prompt. Now let's ping our server with the command ping 192.168.1.208 (insert the IP address of your server). With that ping running, we can switch back to the previous screen with the key combination [Ctrl]+[a]+[p] (which switches to the previous screen). You should now be back to your sudo tail -f /var/log/syslog screen. Hit the [Ctrl]+[a]+[n] (which switches to the next screen), and you're back to the ping command.
But what happens when you lose your connection? Are your commands lost? No. Issue the command screen -ls and you'll see your two screens listed (Figure B).
Each of those screens has a session ID, which allows you to reconnect. So our session IDs are:
You can reconnect to one of those screens with the command like so:
The one issue we might have is that the only screens you can reconnect to are those listed as Disconnected. This will happen if screen detects a network disconnection. If you become disconnected, and the screens haven't detached, you can detach and reconnect with them manually with the command:
screen -rd 2216.pts-0.ubuntu16
Or, say you initially started a screen with the command screen -S kernel-compile. You can reconnect to that screen with the command:
Where ID is the 4-digit ID number.
If that screen hasn't detached (Figure C), you can reconnect with the command:
screen -rd ID.kernel-compile
Where ID is the screen ID number.
If you need to step away from the terminal, but want the command to continue running, you can manually detach the screen to reconnect with it later with the key combination [Ctrl]+[a]+[d]. This will return you to the standard prompt.
Finally, to exit a screen, simply type the exit command and you're back to the standard bash prompt. Make sure, when you're done with your screens, to reconnect to them, end their commands, and exit.
A must-use command
If you frequently remote into your Linux servers, and you want to make sure to not lose your command line work due to a questionable network connection, screen is a must-use command. To learn more about what this can do, issue the command man screen and read through the manual page.
- How to install Hyperledger Fabric on Ubuntu (TechRepublic)
- How to monitor your Apache web server logs with log.io (TechRepublic)
- How to monitor a Linux log file in real time (TechRepublic)
- How to add users to groups in Linux (TechRepublic)
- Here's what happens to CoreOS now that Red Hat owns it (ZDNet)
Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.