Home > bash > screen: a game changer tool for ssh

screen: a game changer tool for ssh

Recently I discovered the tool screen and it’s simply awesome. It’s “a wrapper that allows separation between the text program and the shell from which it was launched. This allows the user to, for example, start a text program in a terminal in X, kill X, and continue to interact with the program.” (source)

Here, in this post I want to show how to use screen with ssh and how it can simplify your daily work.

If you ssh to a server and you are inactive for a while, the server disconnects you. It depends on the server, but most of them are configured this way. Annoying. Or, if you launch a process on the server in the foreground and you want to do something else on the server, what do you do? Log in again in another terminal?

Screen can solve all these problems for you. Here is the figure of an SSH connection:


We will use screen twice. First, we will launch it on machine A. This screen instance will guarantee that the connection won’t be broken even if you are inactive. Fine. Second, we will launch screen on machine B too. This second screen will allow us to open virtual tabs on the server! For instance, you want to work with two Midnight Commanders. You start copying something that takes a long time. No problem, just open a new “tab” and continue working on the server. You don’t need to log in again!

You will need two configuration files. Here is the first one that must be copied to machine A, to your HOME folder. Here is the second one that you need to put on machine B, also in your HOME folder. Don’t forget to rename them to “.screenrc”.

Log in with SSH
For logging in I use this script:

echo "# screen closed"

For instance, you have a VPS whose IP is You installed SSH on port 2222. Then, the first line could be “screen -t my_vps ssh -p 2222 -t me@ screen“. It will start screen on localhost, but upon login it will also start screen on the remote server.

Here is a screenshot of the bottom left corner:


By default, screen commands can be activated with Ctrl-a. However, we have two screen instances, so on machine B I changed the shortcut to Ctrl-b. It’s also displayed in the corner, so there is no need to memorize it. As can be seen on the figure, on the server I have two virtual tabs open.

Use cases
Now, what can we do with all this? If you want to open a new virtual tab on the server, press “Ctrl-b c” (hold Ctrl and press b, release both and press c). To close a tab just press “Ctrl-d” (like in any terminal).

Change between tabs: Ctrl-b-b (hold Ctrl, then press b twice). Or, use the ID of the tabs: “Ctrl-b 0” (first tab), “Ctrl-b 1” (second tab), etc.

You can also detach screen, but I will write about that later.


Categories: bash Tags: ,
  1. i90rr _
    March 16, 2016 at 04:12

    Nice! Next you will discover tmux and mosh (Mobile Shell). See you there!

    • March 16, 2016 at 06:07

      Tmux is an alternative of screen, right? Why should I use tmux instead?

  2. March 16, 2016 at 17:38

    Before you dismiss tmux, take it out for a ride and see how it feels. Now that you are using screen you will be able to see the pros and cons.

    I am a screen user myself and I am making the switch to tmux.

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: