If you have access to production, or even a staging environment, you may sometimes get a wave of paranoia when you are executing commands in your terminal. Or worse, you may have accidentally run something you should not in your production environment.
rm -rf / anyone?
I use iTerm for my Mac, and it has profile support so I can change the background colour of my terminal. However, I don’t want to have to remember to set the profile every time I SSH into a production box. Actually, more to the point, I know I will forget. Luckily, iTerm has a feature that lets you set the profile by sending an escaped string to it from the connected terminal.
First of all you need to add a profile. Here the steps I followed:
- In iTerm, go to menu item Profiles > Open Profiles…
- Click Edit Profiles
- Click the + icon in the bottom left to add a new profile
- Give the new profile a name like ‘Production’ and click on Colors tab
- Click the Background colour selector
- Choose a colour for the background of the terminal. I used a hex colour of #FFECEC which gives a subtle red/pink background (but you might want something more in-your-face!)
- Close the preferences window and the profiles window
Next you need to tell Iterm to use this profile when you log into a particular server. As mentioned above, to do this you need to get your server to print an escaped string of characters when you log in. To do this, simply add this snippet to your .bashrc or .bash_profile file:
if [ "$SSH_TTY" ] then echo -e "\033]50;SetProfile=Production\a" fi
You may need to change Production to the name of your profile. The last thing you need to do is get the server to reset the profile when you log out. This prevents your terminal looking like production when you’re actually on your local machine! To do this, add this snippet to the .bash_logout file on the server:
if [ "$SSH_TTY" ] then echo -e "\033]50;SetProfile=\a" fi
All done. Enjoy your colourful consoles!