Unlocking the Full Potential of Linux: Mastering the Screen Command

Unlocking the Full Potential of Linux: Mastering the Screen Command

Learn how to efficiently manage terminal commands in the background with Linux's powerful screen command Easily detach and reattach sessions, create named sessions, utilize multiple windows, SSH into remote sessions, and even share your screen with others

Key Takeaways

The Linux screen command is a versatile tool that allows you to run terminal applications in the background and switch back to them when needed.

This software feature enables split-screen displays and can be utilized over SSH connections, even after disconnecting and reconnecting.

Screen allows users to effortlessly create new windows, simultaneously run multiple processes, detach and reattach sessions, as well as share sessions between multiple users in real-time.

With the screen command in Linux, you can easily move running terminal applications to the background and bring them back to the forefront whenever necessary. Additionally, it allows you to create split-screen displays and remains functional over SSH connections, even if you disconnect and reconnect!

What Is the screen Command?

The screen command is a feature-rich terminal multiplexer. Its versatility goes beyond what words can express. The man page itself spans over 4,100 lines.

In this article, we will delve into the most frequently encountered scenarios where the screen command proves invaluable.

To begin, the standard procedure involves creating a new window with a shell, executing a command, and then sending the window to the background (known as "detaching"). When you need to check the status of your process, you can bring the window back to the foreground ("reattach") and utilize it again. This method is particularly useful for lengthy processes that you want to avoid accidentally terminating by closing the terminal window.

Once you have initiated a screen session, you can generate new windows and run additional processes within them. You can effortlessly switch between windows to monitor their progress. Additionally, you have the option to split your terminal window into vertical or horizontal sections and view all of your screen windows within one window.

Connect to a remote machine, initiate a screen session, and execute a process. Even if you disconnect from the remote host and then reconnect, your process will continue to run.

It is possible to share a screen session between two separate SSH connections, allowing two individuals to simultaneously view the same information in real-time.

Install Linux screen

To install screen on ubuntu, use this command:

sudo apt-get install screen

Unlocking the Full Potential of Linux: Mastering the Screen Command

To install screen on Manjaro, use the following command:

sudo pacman -Sy screen

Unlocking the Full Potential of Linux: Mastering the Screen Command

On Fedora, you type the following:

sudo dnf install screen

Unlocking the Full Potential of Linux: Mastering the Screen Command

Getting Started with Linux screen

To start screen, simply type it as shown below and hit Enter:

screen

Unlocking the Full Potential of Linux: Mastering the Screen Command

You'll see a page of license information. You can press the Space bar to read the second page or Enter to return to the command prompt.

Unlocking the Full Potential of Linux: Mastering the Screen Command

Type the following command to download the source code for the latest Linux kernel and redirect it to a file named latest_kernel.zip:

curl https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.5.9.tar.xz > latest_kernel.zip

Unlocking the Full Potential of Linux: Mastering the Screen Command

Our download begins, and the curl output shows us the progress.

Unlocking the Full Potential of Linux: Mastering the Screen Command

Unfortunately, we are unable to provide you with a visual representation of the following action as it involves a series of keystrokes. To detach the screen, please press Ctrl+A and then release those keys before pressing the letter 'd'.

Although the download process is still ongoing, the window displaying the download progress will no longer be visible. Instead, you will be redirected back to the terminal window from which you initiated the screen session. You will receive a notification indicating that a screen window labeled 23167.pts-0.howtogeek has been detached.

You need the number from the start of the window name to reattach it. If you forget it, you can always use the -ls (list) option, as shown below, to get a list of the detached windows:

screen -ls

Unlocking the Full Potential of Linux: Mastering the Screen Command

When you're ready, you can use the -r (reattach) option and the number of the session to reattach it, like so:

screen -r 23167

Unlocking the Full Potential of Linux: Mastering the Screen Command

The window that's been working away in the background is now brought back to your terminal window as if it had never left.

Unlocking the Full Potential of Linux: Mastering the Screen Command

Enter the following command:

exit

Unlocking the Full Potential of Linux: Mastering the Screen Command

You are brought back to your previous terminal window, and you can still see the command you used to reattach the window. Since we have closed our only detached window, a message appears indicating that the screen is terminating.

Unlocking the Full Potential of Linux: Mastering the Screen Command

Detach from Linux Screen Session and Other Shortcuts

We briefly touched on how to detatch yourself from a screen session, but it bears repeating. We've also thrown in some other common shortcuts you should get comfortable with.

When utilizing a screen, shortcuts typically consist of pressing Ctrl + a followed by a letter. This action effectively notifies the screen to search for a different input.

Main Keys

Second Key

Action

Ctrl + a

d

Disconnect from the current screen

Ctrl + a

"

View all of the available screen windows

Ctrl + a

1,2,3...9

Connect to a screen with a particular number

Ctrl + a

i

View information about the current screen

Ctrl + a

k

Kills the current window

Ctrl + a

\

Kills all windows and terminates screen

Using Named screen Sessions on Linux

While this list isn't comprehensive, it should suffice for most of your tasks. Furthermore, we will discuss additional useful keybinds for specific scenarios later in the article. If you wish to explore the complete list of keybinds, you can refer to the official GNU screen website for more detailed documentation.

To conveniently reconnect to a session, you may utilize the -S (session name) option in order to name your screen session. To name our session "bigfile," the following command can be used:

screen -S bigfile

Unlocking the Full Potential of Linux: Mastering the Screen Command

When screen launches our session, we see a blank window with a command prompt. We're going to download a big file, so we can use a long-running process as an example.

We type the following:

curl http://ipv4.download.thinkbroadband.com/1GB.zip > bigfile.zip

Unlocking the Full Potential of Linux: Mastering the Screen Command

Once the download initiates, we simply press Ctrl+A and then press D to detach the session. To obtain a comprehensive overview of our detached session, we utilize the -ls (list) option in conjunction with screen, typing the subsequent command.

screen -ls

Unlocking the Full Potential of Linux: Mastering the Screen Command

Behind the numeric identifier (23266), we see the name of our session (bigfile). We type the following, including the session's name, to reattach it:

screen -r bigfile

Unlocking the Full Potential of Linux: Mastering the Screen Command

We're reconnected to our download window and see the long download is still in progress.

Unlocking the Full Potential of Linux: Mastering the Screen Command

When the download is complete, we type exit to close the session window.

Using screen with Multiple Windows

Up until now, we have utilized screen to launch a solitary process in a separate window. Nevertheless, screen possesses a broad range of functionalities beyond that. Moving forward, we will initiate several processes that enable us to keep track of various aspects of our computer.

We type the following to start a screen session called "monitor":

screen -S monitor

Unlocking the Full Potential of Linux: Mastering the Screen Command

To view the kernel buffer messages as they occur, we need to run the "dmesg" command at the command prompt in our new window session. To ensure readability, we will use the "-H" option for human-readable format and the "-w" option to wait for new messages. By executing this command, new messages will continuously appear on the display.

We type the following:

dmesg -H -w

Unlocking the Full Potential of Linux: Mastering the Screen Command

The existing messages appear. We're not returned to the command prompt because dmseg is waiting for new messages, and will display them as they arrive.

Unlocking the Full Potential of Linux: Mastering the Screen Command

In order to launch a different application, we require a fresh screen window. To accomplish this, we utilize the key combination Ctrl+A followed by C. Our intention is to employ the "watch" command, ensuring the continuous execution of the "vmstat" utility. This will generate a dynamically updated visual representation of the virtual memory usage on our computer.

At the new command prompt, we type the following:

watch vmstat

Unlocking the Full Potential of Linux: Mastering the Screen Command

The vmstat output appears and updates every two seconds.

Unlocking the Full Potential of Linux: Mastering the Screen Command

Both of our processes are currently operational. In order to switch between the screen windows, you will need to press Ctrl+A followed by the corresponding number of the desired window. The initial window is identified as zero (0), followed by window 1, and so forth. To navigate back to the first window (referred to as the dmesg window), simply press Ctrl+A and 0.

Unlocking the Full Potential of Linux: Mastering the Screen Command

If we press Ctrl+A and 1, it takes us back to the vmstat window.

Unlocking the Full Potential of Linux: Mastering the Screen Command

That's amazing! We have the option to detach from this session by pressing Ctrl+A, followed by D. This allows us to reattach later while keeping both sessions running. Furthermore, to switch between the windows, all we need to do is press Ctrl+A and the corresponding number (0 or 1) of the desired window to switch to.

To proceed, let's combine both screens into a single window. To do so effectively, adjust the size of your terminal window accordingly. Please note that our examples are limited to the size of our screenshots, which may result in slightly crowded windows.

To achieve this, press Ctrl+A followed by Shift+S (ensure the "S" is capitalized).

The window splits into two "regions."

Unlocking the Full Potential of Linux: Mastering the Screen Command

The vmstat remains visible in the upper region, while the lower region appears empty. The screenshot below highlights the cursor, demonstrating its current position. To relocate the cursor to the lower region, press Ctrl+A followed by Tab.

Unlocking the Full Potential of Linux: Mastering the Screen Command

The cursor navigates to the lower area, which essentially constitutes an unoccupied space. It does not serve as a command prompt, preventing us from inputting any text. In order to obtain a helpful visual representation, we utilize the keyboard combination Ctrl+A, followed by pressing "0" to open the dmesg window within this specific region.

Unlocking the Full Potential of Linux: Mastering the Screen Command

This allows us to have live outputs displayed in a single split window. Detaching the window by pressing Ctrl+A and D, and subsequently reattaching it, will cause the split-pane view to be lost. Nevertheless, we can restore it using the following keyboard shortcuts:

Ctrl+A, S: Split the window horizontally.

Ctrl+A, Tab: Move to the lower region.

Ctrl+A, 0: Display window zero in the lower region.

We can elevate the approach even further by vertically dividing the lower pane and introducing a third process to the view. With the cursor placed in the lower section, we conveniently utilize Ctrl+A and C to generate a fresh window housing a shell. This new window appears in the lower area and grants us access to a command prompt.

Unlocking the Full Potential of Linux: Mastering the Screen Command

Next, we run the df command to check file system usage:

df

Unlocking the Full Potential of Linux: Mastering the Screen Command

To split the lower region vertically while df is running, press Ctrl+A and the pipe character (|). Then, use Ctrl+A and Tab to navigate to the new region. Finally, press Ctrl+A and 0 to open the dmesg window.

Unlocking the Full Potential of Linux: Mastering the Screen Command

You can also move from region to region, and add more vertical or horizontal splits. Here are some more useful key combinations:

Ctrl+A: Hop back and forth between the current and previous regions.

Ctrl+A, Q: Close all regions except the current one.

Ctrl+A, X: Close the current region.

Using screen Over SSH

Start a window session with the screen command, detach it to keep it running in the background, log off or back in, and reattach the session.

To establish an SSH connection to our computer from another one, use the ssh command. Specify the account name to connect with and the address of the remote computer.

For our example, we type the following:

ssh dave@192.168.4.30

Unlocking the Full Potential of Linux: Mastering the Screen Command

After we authenticate on the remote computer and log in, we type the following to start a screen session called "ssh-geek":

screen -S ssh-geek

Unlocking the Full Potential of Linux: Mastering the Screen Command

For demonstration purposes, we'll run top in the screen window, but you could start any long-running or endless process.

We type the following:

top

Unlocking the Full Potential of Linux: Mastering the Screen Command

Once top is running in the window, we hit Ctrl+A, and then D to detach the window.

Unlocking the Full Potential of Linux: Mastering the Screen Command

We're returned to the original, remote terminal window.

Unlocking the Full Potential of Linux: Mastering the Screen Command

If we type exit, as shown below, it disconnects the SSH session and we're back on our local computer:

exit

Unlocking the Full Potential of Linux: Mastering the Screen Command

We type the following to reconnect:

ssh dave@192.168.4.30

Unlocking the Full Potential of Linux: Mastering the Screen Command

After we're reconnected and logged in, we can type the following to reattach the screen session:

screen -r ssh-geek

Unlocking the Full Potential of Linux: Mastering the Screen Command

We're now reconnected to our still-running instance of top.

Unlocking the Full Potential of Linux: Mastering the Screen Command

This is great if you want to start a process on one machine, and then pick up wherever you left off on another.

Sharing a screen Session

In addition, a screen session can be utilized to enable two individuals to simultaneously view and engage with a shared window. For instance, if an individual is operating Fedora on their computer and wishes to establish a connection with our Ubuntu server.

He would type the following:

ssh dave@192.168.4.30

Unlocking the Full Potential of Linux: Mastering the Screen Command

Once connected, he initiates a screen session named "ssh-geek" with the -S option. Moreover, he employs the -d and -m options to establish a new screen session that is automatically detached.

He types the following:

screen -d -m -S ssh-geek

Unlocking the Full Potential of Linux: Mastering the Screen Command

He types the following, using the -x (multiscreen mode) option to attach the session:

screen -x ssh-geek

Unlocking the Full Potential of Linux: Mastering the Screen Command

On a Manjaro computer, another person connects to the Ubuntu computer with the same account credentials, as shown below:

ssh dave@192.168.4.1

Unlocking the Full Potential of Linux: Mastering the Screen Command

Once she's connected, she types the screen command and uses the -X (multiscreen mode) option to join the same window session, like so:

screen -X ssh-geek

Unlocking the Full Potential of Linux: Mastering the Screen Command

Now, anything either person types, the other will see. For example, when one person issues the date command, they both see it as it's typed, as well as its output.

Unlocking the Full Potential of Linux: Mastering the Screen Command

Unlocking the Full Potential of Linux: Mastering the Screen Command

Both people are now sharing a screen session that's running on a remote Ubuntu computer.

For a piece of software that first saw the light of day in 1987, screen still packs a good productivity wallop. Familiarizing yourself with it will be time well spent!

Linux Commands

Files

tar · pv · cat · tac · chmod · grep · diff · sed · ar · man · pushd · popd · fsck · testdisk · seq · fd · pandoc · cd · $PATH · awk · join · jq · fold · uniq · journalctl · tail · stat · ls · fstab · echo · less · chgrp · chown · rev · look · strings · type · rename · zip · unzip · mount · umount · install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · vi · nano · mkdir · du · ln · patch · convert · rclone · shred · srm · scp · gzip · chattr · cut · find · umask · wc · tr

Processes

alias · screen · top · nice · renice · progress · strace · systemd · tmux · chsh · history · at · batch · free · which · dmesg · chfn · usermod · ps · chroot · xargs · tty · pinky · lsof · vmstat · timeout · wall · yes · kill · sleep · sudo · su · time · groupadd · usermod · groups · lshw · shutdown · reboot · halt · poweroff · passwd · lscpu · crontab · date · bg · fg · pidof · nohup · pmap

Networking

netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw · arping · firewalld