December 2, 2013 · cli hacks linux productivity putty raspberrypi screen shell ssh

Using Command Line like a boss with screen

Hopefully, if you are using Linux command line, you've heard of screen. And if you've heard of it, chances are, you're already using it. I've wrote about one usage of screen on my previous blog post, explaining how to use it to access your Raspberry Pi via serial connection. Now, we're going to see what else can be done with this useful tool.

Screen is a terminal multiplexer. This means that you can start screen in one terminal and open any number of terminals inside that terminal. This lets you have several applications, like mutt, lynx, python client and a couple of spare shells, all open inside a single PuTTY/SSH connection. This is a great use of screen, but the benefits don't just end there. Thanks to screen you can run this time consuming package compilation you don't want to be interrupted in case your SSH connection gets flaky for any reason. You can just start it, exit screen and your SSH connection, and return later to see the progress. You can even run applications inside screen not having to change terminals constantly, log in and out, or to worry your X crashes. Screen is just there, as if above it all. It detaches from your actual console session. There is even more - it will auto-detach if the terminal it's in crashes or you logout. You can easily re-attach it at any time on any SSH, TTY or X session. The only major thing that will erase screen session is a reboot.

Installation

Installing screen is pretty straightforward. It's already in most distribution's repositories.

On Debian based distros do:

sudo apt-get install screen

On ArchLinux:

su - pacman -S screen

Fedora/RedHat/CentOS users should do:

su - yum install screen

All in all, you know how to install things in your distribution, don't you?

[TOP]

Getting started

Start screen by simply typing the command

$ screen

at your favourite command shell. You will be greeted by a welcome message:

screen

Dismiss the message with Space or Enter and you'll be taken into an empty terminal containing a shell prompt, serving as screen window, which is pretty much what you had before you started screen. You can check that with a simple command:

user@Machine ~ $ screen -ls 
There is a screen on: 
15659.pts-0.Machine (Attached) 
1 Socket in /var/run/screen/S-user.

If you decide to run several instances of screen (you can run as many as you wish to), just run the command like this:

$ screen -S screenname

Naming screens is very useful, as we'll see in a while.

[TOP]

Screen usage

1. Opening windows within screen

To open and navigate through window instances within screen, we use keyboard shortcuts. Creating a window is done by [Ctrl]-a c key combination. First, you press <strong>[Ctrl]</strong> and hold a key, release them and press <strong>c</strong>. The <strong>[Ctrl]-a w</strong> combination lists for you window instances at the bottom of your screen:

screens

The active window is indicated with * (asterisk). In our case, it's window #4 holding zsh shell instance. After the number goes application running within the window. The previously active window is indicated by <strong>-</strong> (hyphen). There is yet another, very useful, way to create new windows. At a command line (in any window), type <strong>screen [program_name]</strong> and a new window will be invoked, running the specified program, without the need to create another shell process. Once you finish work with an application, the screen window will be automatically closed.

[TOP]

2. Window navigation

To navigate through windows, use [Ctrl]-a # where # is a number corresponding to screen instance you want to use.

[Ctrl]-a n and [Ctrl]-a p let you cycle through the window list. [Ctrl]-a p, as you might guessed, takes you back, while [Ctrl]-a n cycles forward.

One more thing: [Ctrl]-a [Ctrl]-a flips between the currently active window (marked by an asterisk) and the previously active window (marked by a hyphen).

[TOP]

3. Killing windows

There are two ways to kill a window you no longer wish to use. First one is to use [Ctrl]-a k kill-function. The function asks for confirmation before taking effect. The other way is to simply type <strong>exit</strong> within the desired window.

[TOP]

4. Exiting screen

Again, two ways of doing so. The first one is to close all the screen windows manually with exit or kill-function. The other one is to use  [Ctrl]-a \ command that will ask for confirmation to kill all windows and exit.

[TOP]

More screen voodoo

1. Detaching/reattaching sessions

Screen lets you to move from one terminal or machine to another and leave your session intact and programs running on the host where the session's been started. In fact, screen will detach itself automatically if you lose connection to the machine, preventing you from losing all the data. Once detached, you can reattach your session at any time and machine and pick up right where you left off. This is one of the most Ãœbercool things a screen can do.

To detach a session, use the key combination [Ctrl]-a d. Type screen -ls in the terminal and you'll see that your session is detached and running in background:

user@Machine ~ $ screen -ls 
There is a screen on: 17153.pts-1.Machine (Detached) 
1 Socket in /var/run/screen/S-user.

No matter if you detach manually or through a lost connection, the procedure for reattaching is the same. Just run

$ screen -r

And you'll be reattached to a session to a point where you left off. If for whatever reason screen complains and you aren't allowed to reconnect to the session, use

$ screen -DD -r

to detach and reattach the session to your new login.

[TOP]

2. Locking screen

Screen has one more very useful function. It works the same as logout on GUI session, letting you lock the screen and unlock it with your password. You don't have to detach the screen and close terminal if you go away from your machine, you can simply lock it preventing anyone from gaining physical access to your session. To do so, use key combination [Ctrl]-a x. You will see a prompt similar to this one:

Screen used by John Doe on Machine. 
Password:

The only limitation is that you will not be able to detach screen once the session is locked.

[TOP]

3. Naming/renaming screen sessions

As I've said before, sometimes, for various reasons, you might want to use several screen instances. Hence, naming them on your own, instead of using full screen name is very useful.

To create a named screen session, run:

$ screen -S mysessionname

To name (or rename) an existing screen session, do <strong>[Ctrl]-a :sessionname mySessionName</strong>. Mind the colon before sessionname - it must be there.

To rename a session without attaching to it, even from within another screen session, use the command:

$ screen -S oldSessionName -X sessionname newSessionName
```
A few words of explanation. The **`-S`** switch specifies the session name you want to edit. The `<strong>-X</strong>` switch, lets you rename the session without attaching to it.

Let's say we have two instances of screen:
```
user@Machine ~ % screen -ls 
There are screens on: 
18706.pts-1.Machine (Detached) 
18676.pts-1.Machine (Attached) 
2 Sockets in /var/run/screen/S-user.
```
As you can see, we're attached to the session named **`18676.pts-1.Machine`**, which isn't really human-friendly. To rename it, we'll use the key combination: `[Ctrl]-a` and type:
```
:sessionname session1
```
Listing the screen sessions we'll see:
```
user@Machine ~ % screen -ls 
There are screens on: 
18706.pts-1.Machine (Detached) 
18676.session1 (Attached) 
2 Sockets in /var/run/screen/S-user.
```
Now, to rename the other session, we'll use the shell command:
```
$ screen -S pts-1.Machine -X sessionname session2
```
And viola! We've renamed the sessions:
```
user@Machine ~ % screen -ls 
There are screens on: 
18706.session2 (Detached) 
18676.session1 (Attached) 
2 Sockets in /var/run/screen/S-user.
```
Why do we use that? Well, it's pretty useful when you want to attach a screen session. You don't have to provide a full session name, it's sufficient to give the name you've chosen for the session. So, instead of typing:
```
$ screen -r 18676.pts-1.Machine
```
we can simply use it's new name to invoke it:
```
$ screen -r session1
```
This is less time consuming.

<span style="color: #ff99cc;">[<span style="color: #ff99cc;">[TOP]</span>](https://blog.onetwentyseven001.com/index.php/linux-screen/#menu)</span>

#### 4. Copy and paste

You might have already noticed, that it's hard to copy/paste running only command line without any X session. Well, screen let's you do that, not only inside a window, but between windows. The copy process might sound confusing at first, but once you get used to it, it's pretty straightforward. To start copying hit `<strong>[Ctrl]-a [</strong>`. You'll see something like this in the status line:
```
Copy mode - Column 17 Line 1(+1024) (135,41)
```
At this point, move the cursor anywhere you want. To start copying, hit **ENTER** and the status line will show:
```
First mark set - Column 17 Line 1
```
Move the cursor (up, down, left or right) to make selection and hit **ENTER** to mark the end of it:
```
Copied 322 characters into buffer
```
Now move to the window and/or location of your choosing and insert the copied text with <strong>[Ctrl]-a ]</strong>.

<span style="color: #ff99cc;">[<span style="color: #ff99cc;">[TOP]</span>](https://blog.onetwentyseven001.com/index.php/linux-screen/#menu)</span>

#### 5. Renaming windows

Sometimes a process ran within a window may give itself a longish window title, which affects it's name in screen. You may also want to give windows unique names for your own convenience. This can be done with key combination <strong>C-a A</strong>. Once invoked, you'll be prompted for the new title.

<span style="color: #ff99cc;">[<span style="color: #ff99cc;">[TOP]</span>](https://blog.onetwentyseven001.com/index.php/linux-screen/#menu)</span>

#### 6. Hardstatus line

At some point, especially if you've never used `screen` before, you might get lost within screen windows. For this, we can modify `screen` configuration file to show **hardstatus line** within our `screen`. This is one of the most useful features on screen and when customised properly, it really simplifies your work. It can list your current terminal, all open terminals, your system time clock, your hostname and more. Below you'll find a simple hardstatus line that I use on my system.

![screen hardstatus](https://blog.onetwentyseven001.com/content/images/2013/12/screen2.jpg)

To use it, edit/create screen config file:
```
$ nano ~/.screenrc
```
and put the following there:
```
hardstatus alwayslastline hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c%{g}]'
```
Save and start modified `screen` instance. Talking about `.screenrc`"¦

<span style="color: #ff99cc;">[<span style="color: #ff99cc;">[TOP]</span>](https://blog.onetwentyseven001.com/index.php/linux-screen/#menu)</span>

#### 7. Configuration file

There are two types of screen configuration files (as it is with most Linux programs). The main configuration file sits at **`/usr/local/etc/screenrc`** (on some systems it's **`/etc/screenrc`**). It holds all the defaults for all users on the system, thus it's not advisable to edit it, as once a change is done, especially one which you cannot locate, every user will have the same problem. This is why every user can create their own configuration file for an application, that sits in their home directory. Instead of configuring screen on the fly using the <span style="color: #800000;">[<span style="color: #800000;"><strong>colon</strong> command</span>](https://www.gnu.org/software/screen/manual/screen.html#Colon "screen colon")</span>, you can place all your bindings in that file. You can use the default configuration file as a guide, as it contains a number of useful examples for various commands. You can set in the file just about anything, from key bindings to default programs that you want to run on startup. It's all up to you.

If you need more help on the use of screen, head to the documentation page: <span style="color: #800000;">**[<span style="color: #800000;">Screen User's Manual</span>](https://www.gnu.org/software/screen/manual/screen.html "Screen User's Manual")**</span>.

<span style="color: #ff99cc;">[<span style="color: #ff99cc;">[TOP]</span>](https://blog.onetwentyseven001.com/index.php/linux-screen/#menu)</span>

### Default key bindings

The following table shows the default key bindings:

<table><tbody><tr><th>Keystrokes</th><th>C-a : Command</th><th>Action</th></tr><tr><td>Ctrl-a 0</td><td>select 0</td><td>Switch to window 0</td></tr><tr><td>Ctrl-a 9</td><td>select 9</td><td>Switch to window 9</td></tr><tr><td>Ctrl-a Ctrl-a</td><td>other</td><td>Toggle to the window displayed previously.</td></tr><tr><td>Ctrl-a a</td><td>meta</td><td>Send the command character (C-a) to window. See escape command.</td></tr><tr><td>Ctrl-a A</td><td>title</td><td>Allow the user to enter a name for the current window.</td></tr><tr><td>Ctrl-a c</td><td>screen</td><td>Create a new window with a shell and switch to that window.</td></tr><tr><td>Ctrl-a Ctrl-c</td><td>screen</td><td>Create a new window with a shell and switch to that window.</td></tr><tr><td>Ctrl-a C</td><td>clear</td><td>Clear the screen.</td></tr><tr><td>Ctrl-a d</td><td>detach</td><td>Detach screen from this terminal.</td></tr><tr><td>Ctrl-a Ctrl-d</td><td>detach</td><td>Detach screen from this terminal.</td></tr><tr><td>Ctrl-a D D</td><td>pow_detach</td><td>Detach and logout.</td></tr><tr><td>Ctrl-a k</td><td>kill</td><td>Destroy current window.</td></tr><tr><td>Ctrl-a Ctrl-k</td><td>kill</td><td>Destroy current window.</td></tr><tr><td>Ctrl-a Space</td><td>next</td><td>Switch to the next window.</td></tr><tr><td>Ctrl-a n</td><td>next</td><td>Switch to the next window.</td></tr><tr><td>Ctrl-a Ctrl-n</td><td>next</td><td>Switch to the next window.</td></tr><tr><td>Ctrl-a x</td><td>lockscreen</td><td>Lock this terminal.</td></tr><tr><td>Ctrl-a Ctrl-x</td><td>lockscreen</td><td>Lock this terminal.</td></tr><tr><td>Ctrl-a w</td><td>windows</td><td>Show a list of window.</td></tr><tr><td>Ctrl-a Ctrl-w</td><td>windows</td><td>Show a list of window.</td></tr><tr><td>Ctrl-a ?</td><td>help</td><td>Show key bindings.</td></tr></tbody></table>Take a moment to look over those commands. They should tell you how to basically operate screen, especially the last one. There are plenty more, use **`[Ctrl]-a c`** and then type <strong>man screen</strong> for the full list.

<span style="color: #ff99cc;">[<span style="color: #ff99cc;">[TOP]</span>](https://blog.onetwentyseven001.com/index.php/linux-screen/#menu)</span>


  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket

Contact