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:
screen -t SESSION_TITLE ssh -p REMOTE_PORT -t USERNAME@REMOTE_HOST screen clear echo "# screen closed"
For instance, you have a VPS whose IP is 18.104.22.168. You installed SSH on port 2222. Then, the first line could be “
screen -t my_vps ssh -p 2222 -t email@example.com 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.
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.
- How To Use Linux Screen
- A quick tutorial on screen
- screen: Keep Your Processes Running Despite A Dropped Connection
- GNU Screen @ ArchWiki
- screen shortcuts
- 10 Screen Command Examples to Manage Linux Terminals