Can I have my terminal background change based on hostname?

I'd like to have my terminal background color change based on hostname. My reasoning for this is mainly to have and extra way to remind myself what server I am on, so I don't do something stupid.

Is there someway I can have it use the standard aubergine background if I am on my local machine, then change red if I ssh into a production web server, or other hosts that I could specify?


Solution 1:

You can use ssh/config's localcommand option to run a command whenever an alias is used. I use

host hostname
    user myusername
    localcommand xtermcontrol --bg '#abc'

This depends on xtermcontrol and your term being xterm. Presumably there are other apps for other terms.

The only problem with this approach is that it happens when you call ssh. There's nothing to undo the color change. I've done it by wrapping a function around ssh, but that has its drawbacks too.

function ssh() {
  FG=$(xtermcontrol --get-fg)
  BG=$(xtermcontrol --get-bg)
  $(which ssh) "$@"
  xtermcontrol --fg="$FG"
  xtermcontrol --bg="$BG"
}

Solution 2:

It does not seem that there is any functionality in gnome-terminal to add a new tab to an existing window from the command-line. But there are a few options to accomplish what you want.

Per Command Profiles

Create a new gnome-terminal profile for each host you will SSH into. If you only have a few hosts that you regularly connect to, this might be the simplest. Each profile can have a different title, foreground color, background color, custom command and other settings defined. Then you can use File -> Open Tab to open a new tab with the selected profile.

Reuseable Profile

Create a new gnome-terminal profile that will be used to open a new window each time you want to connect to a different SSH host (based on this AskUbuntu answer that Stefano pointed out). This would work good if you connect to many different hosts frequently. This will not allow you to distinguish between different gnome-terminal windows where you are connected to different hosts solely on the background/foreground colors, but you will have a different title per window.

  1. Create a new gnome-terminal profile (File -> New Profile) based on the Default profile and call it "RemoteHost" (note, no spaces in "RemoteHost" to make commands easier).
  2. Under the Title and Command tab, change:
    1. Initial title: to "Remote Host"
    2. When terminal commands set their own titles: to Replace initial title
  3. Under the Colors tab, change:
    1. Uncheck Use colors from system theme
    2. Build-in schemes: to Custom
    3. Text color: and Background color: to colors of your choosing. Keep in mind that some commands (like ls) use colors for their output and you don't want to pick colors that will make it difficult to read the output.
  4. Click on the Close button to save your new profile.
  5. Now you can open a new gnome-terminal window for each remote SSH host using the command gnome-terminal --window-with-profile=RemoteHost -t "Some Remote SSH Host" -x ssh user@somehost. The -t option sets the gnome-terminal window title and the -x option executes the rest of the command line in the terminal. You could even make an alias to shorten total keystrokes.

Command-Line

I found this blog entry with the following script that uses the xdotool and wmctrl commands (they weren't installed by default on Ubuntu, so you might need to install them first) to use the gnome-terminal Ctrl + Shift + t keyboard shortcut to open a new tab in the current gnome-terminal window. It could be modified to open a new tab with a specific profile and execute some command for you.

#!/bin/bash
# Path: /usr/local/bin/gnome-terminal
if [ "x$*" != "x" ]; then
  /usr/bin/gnome-terminal "$@"
else
  pgrep -u "$USER" gnome-terminal | grep -qv "$$"
  if [ "$?" == "0" ]; then
    WID=`xdotool search --class "gnome-terminal" | head -1`
    xdotool windowfocus $WID
    xdotool key ctrl+shift+t
    wmctrl -i -a $WID
  else
    /usr/bin/gnome-terminal
  fi
fi

Other

You could get creative and try some other things.

This SuperUser answer basically uses a bit of "script-fu" acrobats to create a temporary gnome-terminal profile that is used to open a new window. It may be modified for your use.

You could probably use this StackOverflow Q&A and more "script-fu" acrobats to dynamically change the gnome-terminal title whenever you SSH to a remote host. It would not be as prominent as background/foreground color changes, but it would be better than a standard Terminal title all the time.

Solution 3:

This will work in Gnome, IF you're willing to use a new gnome-terminal window for each ssh session.

  1. Create a new profile (with different background colour) called "Remote"

  2. Insert the following into .bash_aliases, or .bashrc

    ### add to .bash_aliases, for differentiating between local and remote hosts 
    sshhelper() {
       gnome-terminal --window-with-profile=Remote -x bash -c "ssh $1";
    }
    alias sshc=sshhelper
    

Now sshc remote-machine opens a new gnome-terminal session with the "Remote" profile. this will differentiate between local and remote profiles.

To accommodate multiple profiles/hosts, create multiple profiles and place something like this in .bash_aliases instead :

### add to .bash_aliases, for differentiating between multiple remote hosts
sshhelper() {
    HOST=`echo $1 | cut -d'@' -f2`

    case $HOST in
        Production )  PROFILE="Red" ;;
        Test )        PROFILE="Green" ;;
        # ... if you have more cases ...
        *)           PROFILE="Default" ;;
    esac

    gnome-terminal --window-with-profile=$PROFILE -x bash -c "ssh $1";
}

# alias ssh=sshhelper    # this will "override" the ssh command, but may break other stuff!
alias sshc=sshhelper

Now sshc Production will open a new session window using the "Red" profile, sshc Test will open a new session window using the "Green" profile, and other hosts will use the "Default" Profile.

Solution 4:

Gathering all information from stack*** sites here is the simplest solution I've found for mint mate + mate terminal (same as for gnome).

  1. In mate terminal define second profile, with different background.
  2. Check if default keys work Alt+PageDown / Alt+PageUp should change the profile, and the background as well.
  3. sudo apt get install xdotool

Now you can modify /etc/ssh/ssh_config, so that it can change the profile after each ssh session is established:

Host *
  PermitLocalCommand yes
  LocalCommand xdotool key Alt+Page_Down

Now, after each ssh connection is established in the terminal, the profile will be changed and you will never put unwanted sql update to production database again :)

However, there's no easy solution to achieve back to default profile after the ssh connection is closed. But this looks enough for me, and I didn't dig for more info.