Skip to content

Linux screen command (terminal multiplexing)

The screen command in Linux allows the shell to be multiplexed, creating virtual “windows” that allow you to execute more than one program at the same time, changing the terminal windows. It is very useful when you are logged into a remote server using ssh, alternating “windows” on a single connection. There are several tools that perform multiplexing, and among them, the most common are screen and tmux.

These tools are useful for running a program, accessing a database, and even verifying LOG files on a single SSH connection. For example, a web programmer can alter a JavaScript script in a window with vim, access a MySQL database, and check logs from node.js or a web server.

It should be noted that these tools are not always installed as standard in distributions and can be installed using the package manager.

Screen control

$ screen [command [parameters]]

The screen command is a powerful window manager that multiplexes a physical terminal between several processes. It allows the user to open several instances of different terminals on the same physical terminal. You can even share your terminal with other users.

If you needed to open several SSH connections with your Linux to have more than one terminal available, it’s because you don’t know the screen yet.

Example:

$screen

Once you type screen, it will open a terminal like any other. But it will enable several commands.

To see the HELP screen, type Ctrl-A and “?” (without quotes).

All screen commands must be preceded by Ctrl-A.

Imagine that you are connected via SSH to a server and you are going to download a large file that will take 2 hours. You can run the download “inside” the screen, disconnect from the terminal, and then reconnect later, even from another computer.

$screen $wget http://servidornaweb.com.br/arquivomuitogrande.tar.gz

You can type Crtl-A and the “d” key to disconnect from the terminal. Now you can even log out of the SSH session.

When you want to reconnect to the screen terminal, reconnect to the SSH session, and then type the command:

$screen —r

If you have more than one session open on the screen, you must enter the session you want to connect to. To see which sessions are open:

$ screen -ls <br></br>There is a screen on: <br></br>12604.pts-0.svnserver (Attached) <br></br>1 Socket in /var/run/screen/s-ec2-user.

To connect to session 12604:

$screen —r 12604

You can also open multiple sessions with Ctrl-A, then “c”.

To switch between sessions, you must type Ctrl-A and then “n” or “p” to navigate forward (Next) or backward (Previous). This prevents you from having to connect multiple times via SSH.

You can also enable the log of everything that was entered in the terminal by activating the log using the Ctrl-A and “H” command. To turn off logging, just press Ctrl-A and “H” again.

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share