Automatically change Terminal colors on remote connection?

12,913

Solution 1

Srigelsford from the Ubuntu forums recommends this:

I created several profiles in gnome-terminal with the settings I wanted, then created aliases like the below example to ssh to ares. The Ares profile has a red background.

alias -p ares='gnome-terminal --window-with-profile=Ares -x bash -c "ssh ares"; exit'

The pitfall of this is that it cannot be done in your existing terminal window, it launches another. My alias closes the existing one after launching the new one, but you can stop that by removing the ; exit from the end.

Just create as many profiles as you like, and associate each server to a profile in an alias. To launch just type the name of the alias $ares

Solution 2

You could change the remote system shell profile to use colorfull prompts, check the following link for details: http://www.funtoo.org/en/articles/linux/tips/prompt/ .

Solution 3

The following works for gnome3 terminals. It uses xdotool to automate interaction with the gnome terminal menu, since the available keyboard shortcuts are currently limited.

I have created two profiles. The top one in the Terminal->Profiles menu is my default, and indicates a local machine. The second one is my color scheme for a remote connection to machine66.

I then defined the following aliases in my bashrc:

alias first_profile="xdotool key F10 Right Right Right Right Down Right Return"
alias second_profile="xdotool key F10 Right Right Right Right Down Right Down Return"
alias go66="second_profile; ssh remotemachine66; first_profile"

The first_profile and second_profile just use xdotools to click f10 (open the menu), and then traverse right to terminal, down 1 to profiles selection, and then select the first or second profile.

go66 first calls second_profile to set the colors, ssh's into the remote machine. On exiting ssh first_profile is called, returning the profile to the local profile.

It's clunky, but it works. I'm happy to hear refinement suggestions.

Share:
12,913

Related videos on Youtube

Jeremy Kauffman
Author by

Jeremy Kauffman

Founder of LBRY and TopScore; flounder through life. Sometimes I post things here or here.

Updated on September 17, 2022

Comments

  • Jeremy Kauffman
    Jeremy Kauffman over 1 year

    Is there a way to automatically apply a terminal profile when connected to a remote machine via SSH?

    I'd like more obvious visual cues that I'm connected to a remote machine than the user/host displayed on each terminal line. I spend most of my day connected to multiple machines and it can get confusing.

    • Ramón
      Ramón over 13 years
      What I do now is have separate gnome-terminal launchers for each machine I connect to. Each launcher opens with a different profile and window size. If this could be somehow be made to automagically change when I connect to a server from any terminal it would be awesome. Hope you find a solution.
  • Jeremy Kauffman
    Jeremy Kauffman over 13 years
    Thanks. This is an option, but since I connect to a lot of different servers it's a little annoying. Perhaps I could write a script to copy a local settings file.
  • groovehunter
    groovehunter over 13 years
  • WinEunuuchs2Unix
    WinEunuuchs2Unix almost 4 years
    +1 and I might add your Screen Name groovehunter will meet it's objective (after a decade or 9 years and 9 months) with the arrival of Ubuntu 20.10 aka "Groovy Gorilla".