Linux Screen Command: Multiplex Terminal and Keep SSH Sessions Alive
The screen command in Linux allows the shell to be multiplexed, creating virtual "windows", which allow running more than one program at the same time, switching between terminal windows. It is very useful when you are logged into a remote server using ssh, switching "windows" in a single connection. There are several tools that do multiplexing, and among them, the most common are screen and tmux.
These tools are useful for, in a single SSH connection, running a program, accessing a database, and also checking LOG files. For example, a Web developer can edit a javascript script in a window with vim, access a mysql database, and check the logs of node.js or a web server.
It is important to note that these tools are not always installed by default in distributions, and can be installed using the package manager.
Screen command
$ 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 different terminal instances on the same physical terminal. You can also share your terminal with other users.
If you needed to open several SSH connections to your Linux to have more than one terminal available, it is because you don't know 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 screen HELP, type Ctrl-A and "?" (without quotes).
All screen commands must be preceded by Ctrl-A.
Imagine 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" 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 Ctrl-A and the "d" key to disconnect from the terminal. Now you can even disconnect from 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 in screen, you must specify the session you want to connect to. To see which sessions are open:
$ screen -lsThere is a screen on: 12604.pts-0.svnserver (Attached)1 Socket in /var/run/screen/S-ec2-user.
To connect to session 12604:
$ screen –r 12604
You can also open several 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 avoids having to connect several times via SSH.
You can also enable logging of everything typed in the terminal, enabling the log through the Ctrl-A and "H" command. To disable the log, just press Ctrl-A and "H" again.
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Like it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.