How to Use Linux screen Command

422
How to Use Linux screen Command

Here we can see, “How to Use Linux screen Command”

You can push running terminal applications to the background and pull them forward when you want to see them using the Linux screen command. It also works with split-screen displays and SSH connections, even after disconnecting and reconnecting!

What Is the screen Command?

The screen command is a terminal multiplexer that comes with a plethora of options. To say it’s capable of a lot is the granddaddy of exaggerations. There are over 4,100 lines on the man page.

The following are the most common uses of the screen command, which we’ll go over in more detail later in this article:

  • The standard procedure is to open a new window with a shell, run a command, and then “detach” the window from the screen. You may bring the window back to the foreground (“reattach”) and utilize it again when you want to see how your procedure is performing. This is useful for extended operations that you don’t want to end by closing the terminal window accidentally.
  • You can create new windows and execute other processes in them once you’ve started a screen session. You can quickly switch between windows to keep track of their development. You can also divide your terminal window into vertical and horizontal areas, allowing you to see all of your screen windows in one window.
  • You can connect to a remote machine, run a process, and establish a screen session. Your process will continue to execute even if you disconnect and reconnect from the remote host.
  • You can share a screen session between two SSH connections in real-time, allowing two users to see the same thing.
Also See:  Peacock Is Removing Offensive WWE Segments From Broadcasts

Installing screen

Use the following command to install a screen on Ubuntu:

sudo apt-get install screen

Use the following command to install a screen on Manjaro:

sudo pacman -Sy screen

You type the following in Fedora:

sudo dnf install screen

Getting Started with screen

Type the following into the Start Screen box and press Enter:

screen

A page with license information will appear. You can read the second page by pressing the Space bar, or you can return to the command prompt by pressing Enter.

You’re back at the command prompt, and it appears that nothing has happened. However, you’re now using a multiplexed terminal emulator to run a shell. What makes you think this is a good thing? So, let’s get started on something that will take a long time to complete. First, we’ll download the latest Linux kernel source code and save it to a file called latest kernel.zip.

To do so, type the following into the text box:

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

The download starts, and the curl output shows us how far we’ve come.

Because the next bit is a keystroke sequence, we can’t show you an image of it. To detach the screen, press Ctrl+A, release the keys, and then press d.

The download is still going on, but the window that displays it has been removed. You’ll be taken back to the terminal window where you started the screen session. A screen window labeled 23167.pts-0.howtogeek has been detached, according to a message.

To reattach the window, you’ll need the number at the beginning of the name. If you forget, you can always use the -ls (list) option to get a list of the detached windows, as shown below:

screen -ls

When you’re ready, reattach the session with the -r (reattach) option and the session number, as follows:

screen -r 23167

The window working in the background has been restored to your terminal window as if it had never been gone.

If a process is going to be carried out to the end, it will eventually be completed. If it’s a continuous process, you’ll want to stop it eventually. In either case, when the process is completed, type exit to exit the screen. Alternatively, you can kill a window by pressing Ctrl+A and then K.

Type the following command in the prompt:

exit

You’re taken back to your original terminal window, which still displays the command you used to reattach it. Finally, we get a notification that the screen is ending since we closed our only detachable window.

Using Named screen Sessions

You can name your screen session with the -S (session name) option. It’s easier to reconnect to a session if you use a memorable name rather than its numerical identity. To give our session the name “big file,” we type the following:

screen -S bigfile

We see a blank window with a command prompt when the screen starts our session. This is because we’re going to download a large file as an example of a long-running procedure.

The following is what we type:

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

When the download begins, press Ctrl+A, then D to disconnect the session. To see the specifics of our detached session, we write the following to utilize the -ls (list) option with screen:

screen -ls

We can see the name of our session behind the numeric identifier (23266). (big file). To reattach the session, type the following, including the session’s name:

screen -r bigfile

We’ve rejoined our download window, where we can see that the lengthy download is still ongoing.

We type exit to close the session window after the download is complete.

Using screen with Multiple Windows

We’ve only used a screen to run a single process in the background in a separate window so far. The screen, on the other hand, is capable of much more. Following that, we’ll run a few processes that will allow us to monitor the track of certain aspects of our computer.

To start a screen session called “monitor,” type the following:

screen -S monitor

We’ll run dmesg with the -H (human-readable) and -w (wait for new messages) options from the command prompt in our new window session. This will show the kernel buffer messages, with new messages appearing as they are received.

The following is what we type:

dmesg -H -w

Existing messages are displayed. We are not returned to the command prompt because dmseg is waiting for new messages and will display them as they arrive.

We’ll need a new screen window to run another application. To open a new window, we press Ctrl+A, then C. Finally, we’ll use a watch to run vmstat repeatedly to see the virtual memory usage on our computer in real-time.

Also See:  Why I Switched From Chrome to Firefox Quantum

We type the following into the new command prompt:

watch vmstat

The vmstat output appears every two seconds and updates.

Both of our processes are now active. To switch between the screen windows, press Ctrl+A followed by the window number. Window zero (0) is the first one we made, followed by window one, and so on. We press Ctrl+A and 0 to get to the first window (the dmesg one).

We can return to the vmstat window by pressing Ctrl+A and 1.

That’s quite clever! To disconnect from this session, press Ctrl+A, then D; we can reconnect later. Both sessions will continue to be held. To switch between the windows, press Ctrl+A followed by the number (0 or 1) of the window we want to switch.

Let’s go on to the next phase, which is to view both screens in a single window. Your terminal window will be stretched to a size that allows you to complete this step when you do this. Because the size of our screenshots limits us, our windows will appear a little crowded.

To do so, use Ctrl+A, then Shift+S (note that the “S” must be capitalized).

The window is divided into two sections, or “regions.”

The top section still shows vmstat, while the bottom is empty. In the image below, the pointer is highlighted. We hit Ctrl+A and then Tab to shift it to the bottom region.

The pointer moves down to the lower section, which is essentially vacant. We can’t type anything in it because it isn’t a shell. To get a helpful display, press Ctrl+A, then “0” to bring up the dmesg window in this area.

Both live outputs are now shown in a single split window. The split-pane view will be lost if we press Ctrl+A and D to detach the window and then reattach it. However, the following keyboard shortcuts can be used to restore it:

  1. Ctrl+A, S: Split the window horizontally.
  2. Ctrl+A, Tab: Go to the lowest part of the screen.
  3. Ctrl+A, 0: Show window zero in the lower half of the screen.

We can even take it a step further. Now we’ll vertically split the lower pane and add a third process to the display. We press Ctrl+A and C with the cursor in the lower region to open a new window with a shell. The new window is displayed in the lower region, and we are given a command prompt.

To check file system usage, we use the df command:

df

We press Ctrl+A and the pipe character (|) when we see df running. The lower region is split vertically as a result of this. To move to the new region, we press Ctrl+A and Tab. The dmesg window is then displayed by pressing Ctrl+A and 0 together.

You can also switch between regions and create more vertical or horizontal splits. Here are some more key combinations that you might find helpful:

  • Ctrl+A: Switch between the current and previous regions by pressing the Ctrl key.
  • Ctrl+A, Q: Close all regions except the current one.
  • Ctrl+A and X: Close the current region.

Using screen Over SSH

You can start a window session, detach it so it continues to run in the background, log off or back in, and then reattach the session with the screen.

Let’s use the ssh command to establish an SSH connection to our computer from another computer. The name of the account with which we’ll connect, as well as the address of the remote computer, must be provided.

We’ll use the following as an example:

ssh dave@192.168.4.30

We type the following to start a screen session called “ssh-geek” after we authenticate and log in on the remote computer:

screen -S ssh-geek

We’ll run top in the screen window for demonstration purposes, but you could start any long-running or endless process.

The following is what we type:

top

To disconnect the window after the top is running in it, press Ctrl+A and then D.

We’ve been redirected back to the remote terminal window.

When we type exit, as demonstrated below, the SSH session is terminated, and we are returned to our local computer:

exit

To rejoin, we input the following:

ssh dave@192.168.4.30

After we’ve reconnected and logged in, we may reattach the screen session by typing:

screen -r ssh-geek

We’ve reconnected to our top instance, which is still operating.

If you want to start a procedure on one machine and pick up where you left off on another, this is a perfect option.

Sharing a screen Session

A screen session can also allow two individuals to see and interact with the same window at the same time. For example, imagine someone on his computer is running Fedora and wants to connect to our Ubuntu server.

He’d type something like this:

ssh dave@192.168.4.30

After connecting, he uses the -S (session name) option to initiate a screen session called “ssh-geek.” He also creates a new screen session that is already disconnected using the -d (detach) and -m (enforced creation) parameters.

He writes down the following:

screen -d -m -S ssh-geek

To attach the session, he writes the following using the -x (multiscreen mode) option:

screen -x ssh-geek

Another person connects to the Ubuntu computer from a Manjaro computer using the same account credentials, as shown below:

ssh dave@192.168.4.1

She connects, then uses the screen command with the -X (multiscreen mode) option to join the same window session:

screen -X ssh-geek

Now, anything one of the types will be visible to the other. So when one person issues the date command, for example, they both see the command as it is typed and the output.

Both people are now using a remote Ubuntu computer to share a screen session.

For a piece of software that was first released in 1987, Screen still packs a punch in terms of productivity. However, it will be time well spent if you familiarise yourself with it.

Conclusion

I hope you found this information helpful. Please fill out the form below if you have any questions or comments.

User Questions:

  1. How does the screen command work in Linux?

A full-screen software program that multiplexes a physical console between multiple processes is known as the screen (typically interactive shells). It allows users to open multiple terminal instances from within a single terminal window manager.

  1. In Linux, what is screen mode?

In Linux, the screen command allows you to launch and use multiple shell sessions from a single ssh session. Furthermore, when a process is started with the command screen,’ it can be detached from the session and then reattached later.

  1. In Linux, how do you turn off a screen?

A screen session can be terminated by pressing CTRL+a and then ‘k.’ There are a couple of ways to kill a specific screen session from the command line using the screen command (non-interactively).

Also See:  Microsoft gives Windows Terminal a promotion in the latest Windows 10 builds
  1. Is Tmux a better alternative to screen?

Tmux is more user-friendly than Screen, and it includes a handy status bar with some helpful information. Tmux supports automated window renaming, although Screen does not. Tmux does not support session sharing with other users, although Screen does. Tmux is missing this critical feature.

  1. Tmux vs. Screen?

Screen vs Tmux? from linux