Posts Tagged ‘ssh’

Improve your SSH with GNU Screen

Friday, September 11th, 2009

SSH is a wonderful thing. You can log in to a terminal remotely and securely as well as tunnel traffic through that connection. However, one of the biggest problems with SSH is that as soon as you close the session (or are disconnected), any processes running are killed, which can lead to things like loss of work or inconsistent files. It also means that you can’t leave things encoding overnight because as soon as you disconnect, it stops encoding. This is where GNU Screen is useful.

When screen is open, you can disconnect and reconnect to get the same screen back again. You can also have multiple screens open at once and tab between them. Ctrl+a then c creates a new screen, Ctrl+a then n tabs to the next one and Ctrl+a then k kills the selected window.

This also means that you can resize windows and even the previous text is also resized if it was truncated or wrapped before.

Just add

screen -R

to your ~/.profile to have it re-open the same screen every time you connect to SSH or, if there isn’t one running, start one.

You can also add a line to the bottom of your SSH session that tells you which tab you are currently looking at, the date, time and other similar details. This is called the hardstatus line.

I use the following hardstatus line in my ~/.screenrc

hardstatus alwayslastline
hardstatus string "%{= kw}[%{= kg}%H%{= kw}]%-=%-w%{= kr}{%{= kw}%n %t%{= kr}}%{= kw}%+w%-=[%{= kg}%Y-%m-%d %c%{= kw}]"

To explain what each part does, I will first give the uncoloured version. %{= __} sets a colour. For example, “kg” is black background and green foreground.

If we remove all of these we get

"[%H]%-=%-w{%n %t}%+w%-=[%Y-%m-%d %c]"

%H is a host name
%-= is padding
%-w is all of the windows before the current one
%n %t is the current window number and name
%+w is all of the windows after the current one
%Y-%m-%d is the date
%c is the time

Here’s how it looks (as always, click to enlarge):
screen

The downside is that scrolling doesn’t seem to work at all. I don’t know if it has just been replaced by something internal to screen but I generally use something like shift+page up to scroll up or the scroll bar in PuTTY.