When you're ssh'ing to a specific remote server regularly, GNU Screen is a useful tool. If you don't know it, read this article and be enlightened.

Note that I'm using 'GNU Screen' whenever possible, because otherwise this page will not be found using any of the search engines.

Real-time sharing of a terminal

To be able to have two users share one terminal, do the following:

For this to work, GNU Screen has to run setuid, i.e. as root. To enable this, do the following as root on Fedora/RedHat/CentOS:

  # chmod 4755 /usr/bin/screen
  # ls -l /usr/bin/screen
  -rwsr-xr-x  1 root root 318836 Sep 11  2004 /usr/bin/screen

For Debian (and perhaps derivatives):

  # chmod 6755 /usr/bin/screen
  # chmod 755 /var/run/screen

Notice the fourth character on the last line is an s? This indicates that GNU Screen runs with root rights. Be careful with this, there are some security implications.

Presetting windows

A useful feature of GNU Screen is to be able to preset a number of windows and give them names. To do this, create a file .screenrc in your home directory and add the following lines (adjust to taste):

  screen  -t 'Build' 1
  screen  -t 'Edit 1' 2
  screen  -t 'Edit 2' 3
  screen  -t 'JBoss log' 4
  screen  -t 'JBoss run' 5

Start GNU Screen with

  screen -rd

and use CTRL-A and then N or P to activate the Next or Previous window. Alternatively, type CTRL-A and then 1..5 to immediately jump through a particular window.


GNU Screen can show a status bar at the bottom of the terminal screen. To show all windows in a status bar, add the following line:

  caption always "%w"

The asterisk shows which window is the active one.


Another useful setting to set in your .screenrc file, is

  defscrollback 5000

This sets the size scrollback window to 5000 lines. It's accessible through CTRL-A, [ and Escape. Instead of Escape, use Enter to mark the start and the end of a selection for copy/paste.

Long lines

To make sure that long log lines aren't cut off at 80 columns, add the following to the GNU Screen configuration file:

  wrap off


Heh -- a screenshot; get it?? Type CTRL-A, H to save the current contents of the terminal to a file.

Screen logging


Yes its been a while since I have checked in. Sorry I’ve just been too busy. But I have a great tip this time. Recently I had the need to do automatic session logging. A 3rd party was going to be logging into one of my servers to check out some software glitches that were happening. I love using GNU Screen for many shell tasks so using it for monitoring was logical. Screen is great for several reasons. First you can detach from it so you can leave the office, go home and re-attach and not have lost your place. Second, you can share another screen. It can be shared input or you can just watch what someone else is doing. Finally screen can do native logging. I wanted to automattically launch a screen session when somone logged in so if I happened to be on the server I could monitor them in real time. I also wanted a log of the session in case I wanted to look over it later or if I was not able to monitor the session live.

I ended up adding the following to my .bashrc

 # -- if $STARTED_SCREEN is set, don't try it again, to avoid looping
 # if screen fails for some reason.
 if [[ "$PS1" && "${STARTED_SCREEN:-No}" = No && "${SSH_TTY:-No}" != No ]];   then
 if [ -d $HOME/log/screen-logs ]; then
 sleep 1
 screen -RR && exit 0
 # normally, execution of this rc script ends here...
 echo "Screen failed! continuing with normal bash startup"
 mkdir -p $HOME/log/screen-logs
 # [end of auto-screen snippet]

Lets go through that …..

 if [[ "$PS1" && "${STARTED_SCREEN:-No}" = No && "${SSH_TTY:-No}" != No ]]

If I have some title at my terminal and if STARTED_SCREEN is set and non-null, (expands to $STARTED_SCREEN. Otherwise, expands to No.) and if SSH_TTY is set and not null, then we can attempt to create the screen.
$SSH_TTY is set when you ssh in, it should not be tripped by scp or sftp logins either.



Here STARTED_SCREEN is set so that we dont loop on login creating a ton of screens.

 if [ -d $HOME/log/screen-logs ]; then

if the directory is present

 sleep 1
 screen -RR && exit 0
 # normally, execution of this rc script ends here...
 echo "Screen failed! continuing with normal bash startup"

Wait one second then attempt to reattach any unattached screens. If there are no screens to be attached then make one and attach to it.

And I added the following to my .screenrc

 # support color X terminals
 termcap xterm 'XT:AF=E[3%dm:AB=E[4%dm:AX'
 terminfo xterm 'XT:AF=E[3%p1%dm:AB=E[4%p1%dm:AX'
 termcapinfo xterm 'XT:AF=E[3%p1%dm:AB=E[4%p1%dm:AX:hs:ts=E]2;:fs=07:ds=E]2;screen 07'
 termcap xtermc 'XT:AF=E[3%dm:AB=E[4%dm:AX'
 terminfo xtermc 'XT:AF=E[3%p1%dm:AB=E[4%p1%dm:AX'
 termcapinfo xtermc 'XT:AF=E[3%p1%dm:AB=E[4%p1%dm:AX:hs:ts=E]2;:fs= 07:ds=E]2;screen 07'
 # detach on hangup
 autodetach on
 # no startup msg
 startup_message off
 # always use a login shell
 shell -$SHELL
 # auto-log
 logfile $HOME/log/screen-logs/%Y%m%d-%n.log
 deflog on

Most of this is self explanatory the log file for auto logging and deflog on are what give you your fun logs to look over later.

You might also want to do some logrotate on the logs or some script to expire logs that are x days old. If you forget about them over time they may try to eat your file system.

Please note i think i picked this up somewhere else a while back i just dont remember where. I modified it slightly to make it more readable but the credit goes to the original author.

Handy shortcut keys

GNU Screen has lots of shortcut keys to control the separate screens themselves besides lots of handy utilities.

Next screen CTRL-A, n
Previous screen CTRL-A, p
New screen CTRL-A, c
Lock screen CTRL-A, shift-X Very handy, now requires a password to unlock
Type CTRL-A CTRL-A, a Useful to go to the start of the (command)line if your shell uses Emacs keybindings