GNU Screen Tips

These are some common commands I use. In the examples, C-a means push the Control + A buttons at the same time.

Send command to all windows

Enter command line mode with C-a then :. This will send ls -l to all windows:

at "#" stuff "ls -l^M"

Automatic window naming

When I connect to hosts with SSH I like to have the window names set automatically to the hostname of the server I am connected to. This can be done with a simple SSH local command.

  1. Edit your SSH client configuration file, ~/.ssh/config. A host definition needs to be added along with the local command to execute. In this case I apply this local command to all hosts I connect to, so the content is like this:
# Set default options for all other sessions
Host *
  # Default user = root unless you specify a username when connecting or in a seperate host definition
  User root
  # Run local command to set the screen window name to the hostname of the server that you are connecting to
  LocalCommand ~/.ssh-hostname %h
  # Turn on compression
  Compression yes
  1. Create the file ~/.ssh-hostname. This will be executed when connecting to hosts and it is what will set the window name. The content of the file needs to be set to this:
#!/bin/bash
echo -ne "\033k$1\033\\"
  1. Make the file created in step to executable:
chmod +x ~/.ssh-hostname

Attempt to SSH to a host when using screen to verify it works.

Leave a Reply

Your email address will not be published.