If you’re a Linux user, you’re going to have to get started with the Linux terminal at some point. Some terminal commands might be popular, others obscure, but in many cases, it’s easier to run a command through a terminal window than through a GUI.
But what happens if you need to run several commands at once? GNU Screen makes terminal multitasking like this easy; let’s get you started using it.
What Is GNU Screen?
GNU Screen is a tool for the Linux terminal that splits one terminal into several. It means you can run one command, say a
wget download of an image file, while switching to run a second, such as
systemctl, to check the S.M.A.R.T. status of your drive.
It allows you to run independent commands like these in separate sessions that you can connect and disconnect to at will.
It’s not just useful for your own PC; it’s almost essential if you’re managing a remote server. If you’re running commands on a server over SSH, what happens if you disconnect? The command might still be running, but you won’t be able to easily monitor or interact with it.
Screen deals with that problem for you. You can reconnect to a Screen session if you lose connection, or detach from it and leave it running in the background until you need to access it again.
How to Install Screen
Screen doesn’t come with most Linux distributions, but as it predates Linux, it’s well supported.
If you want to install it on an Ubuntu or Debian-based distro, run the following:
sudo apt-get update sudo apt-get install screen
Similarly, if you’re running Arch, open up your shell window and run:
sudo pacman -Syu sudo pacman -S screen
If you’re using Fedora, run this instead:
sudo yum update sudo yum install screen
Starting a Screen Session
Starting a Screen session is simple. Open up a terminal, or establish an SSH connection, and type
screen. You’ll see the Screen introduction window; hit space or your enter key to close it.
Once you do, your terminal screen will return to normal. There’ll be no obvious sign that you’re running a Screen session, but every command you run from this point will run within a session that you can now detach and reattach to at will.
Viewing and Detaching Screens
You’ll want to know how to connect and disconnect from a Screen session if you plan on using it again. If you’re already in a Screen session, hit Ctrl + A followed by the letter d (lower case).
The session and any commands currently running inside of it will detach to run in the background, ready for reconnection later. Assuming you only have one running Screen session, type:
This will reattach your session and allow you to continue. If you need to forcefully detach a session remotely, then reconnect to it yourself, type:
You can run more than one Screen session. If you want to reconnect to a specific session, you’ll need to find out the session process ID number. Type
screen -ls or
screen -r to list them.
As the image above shows, type
screen -r followed by the initial ID number at the start of each session. For example:
screen -r 25407
If you want to close a session and cancel any running commands within it, reconnect to it and type
Other Screen Terminal Commands to Remember
Screen has a few tricks up its sleeve for users who want to get the most out of it. Here are a few of the most common terminal commands for you to remember.
List Screen Keyboard Shortcuts
Like all good terminal programs, Screen has keyboard shortcuts for you to use. You’re already using one, Ctrl + A and d, to detach existing screens.
If you want to see the rest, simply type Ctrl + A followed by ? to give you a list you can work with.
Create and Switch Between Windows in a Session
You don’t need to switch between sessions to run commands; you can also switch between windows in one session.
To create a new window in your session, hit Ctrl + A followed by c (lower case) to create a new window. Your first window starts at number 0, your next window 1, etc.
Hit Ctrl + A and then filter through the numbers 0-9. To list each one, use Ctrl + A and then w (lower case) to see a list of sessions with a one digit ID.
Create a Session With a Name
A randomly generated ID is hard to remember; giving your session a name might make things easier. If you want to start a session with a name, type:
screen -S examplename
If you want to reconnect to this session by name, type:
screen -X examplename
Share a Screen Session
Want to share a terminal session with a colleague or a friend? Thanks to Screen, you can. Type:
Rather than detaching anyone currently connected to this session, you simply join it. Other users will see what you type and the commands you run; you’ll also be able to watch other users if they do the same.
Log Your Screen Output to a File
You might need to log your screen output to a file for maintenance or auditing reasons. To do so, type:
A session will start with the ability to log to a file with the name screenlog.x (where X is a number, starting from zero) in your home directory. To start a log in a session, type Ctrl + A followed by H (Shift + h).
Lock a Screen Session
If you want to protect a screen session, you can lock the session with your existing Linux password.
Type Ctrl + A followed by x (lower case) to lock a session while you’re currently connected to it.
This locks it in your current terminal window; type in your account password to unlock.
Terminal Multitasking Couldn’t Be Simpler
Thanks to GNU Screen, you don’t have to worry about waiting for a terminal command to finish. It’s useful if you’re looking to control remote servers, but it’s also a great tool for your home PC if you need to run several commands from one window.
It’s the perfect tool for system admins. It’s not the only command for your Linux terminal toolbox, however, which is why you should bookmark this cheat sheet of important Linux commands .
Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.