Change terminal color when SSH

For happy devops :)

(λx.x)eranga
Effectz.AI
3 min readAug 21, 2018

--

Happy SSH :)

When doing devops we have to SSH into different servers. For an example we may SSH into production servers, staging servers, amazon boxes, local test servers etc. Usual way of doing this is to open new terminal tab and starting SSH session. Sometimes we may keep multiple SSH sessions in different terminal tabs simultaneously. In this situations its bit confuse to identify which server currently I’m in. To solve this confusion we can use different terminal themes(terminal colors) for different SSH sessions.

iTerm and oh-my-zsh, Marriage made in heaven :)

I’m using iTerm terminal on mac and oh-my-zsh with zsh shell. Different iTerm profiles are using with different themes(colors). You can use iTerm themes to obtain different terminal colors. These terminal profiles can be change(via custom oh-my-zsh command) when when SSH into different server environments.

Change iTerm profile when SSH

To change iTerm profile when SSH, we can use custom oh-my-zsh command. Custom oh-my-zsh command can be written to change the iTerm profile based on the SSH’ing server environment. Following are the steps to do that.

1. Create iTerm terminal profiles

Goto iTerm preferences and create profiles according to your SSH environments. You can choose different terminal colors for different profiles. In my scenario I have 4 iTerm profiles.

  1. Default terminal session - black color
  2. Production SSH session - dark red color
  3. Staging SSH session - dark purple color
  4. Other SSH session - dark blue color

2. Create custom shell script

Goto ~/.oh-my-zsh/custom directory and create a file iTrem2-ssh.zsh. This directory contains example.zsh file as well.

Add following content into iTrem2-ssh.zsh file. This is the script we are using to change the terminal profile based on SSH’ing server.

In here colorssh() function selecting different terminal profile according to SSH’ing host. tabc <profile-name> do the profile change. tab-reset() function reset the terminal profile to Default when exit from the SSH session. Finally alias ssh=”colorssh” creates an alias to ssh command(when execute ssh command from the terminal it calls to colorssh function).

Example terminal session

Following are the example terminal sessions of my computer. It shows how terminal colors are changing when SSH’ing to different servers Production, Staging and Test.

1. Default terminal

2. SSH into production server

3. SSH into staging server

4. SSH into test server

--

--