Useful bash scripts: Automatically changing terminal theme when using ssh on macOS
But we are hackers and hackers have black terminals with green font colors ~ John Nunemaker
This is the second in a series of posts on useful bash aliases and shell customisations that developers here at Talis use for their own personal productivity. In this post I describe how I configured my shell to automatically change my terminal theme when I connect to a remote machine in any of our AWS accounts.
As I’ve mentioned previously, at Talis, we run most of our infrastructure on AWS. This is spread over multiple accounts, which exist to separate our production infrastructure from development/staging infrastructure. Consequently we can find ourselves needing to SSH onto boxes across these various accounts. For me it is not uncommon to be connected to multiple machines across these accounts, and what I found myself needing was a way to quickly tell which of these were production boxes and which were servers in our development account.
All of my development work is done on a Macbook Pro running macOS. Several years ago
I started using iTerm2 as my terminal emulator instead of the built in
terminal which has always felt particularly limited. Given these constraints
the solution I came up with was to implement a wrapper around an
ssh command that
would tell iTerm2 when to switch themes so that we can use different colors for
production environments vs development.
In order to work it requires you to create three profiles in iTerm2, and for the
purposes of this each of these profiles is essentially the theme you want to use.
When creating a profile you can customise colors, fonts etc. But crucially for
each of them you need to enter a value in the
badge field. This tells iTerm2 what
to set as the badge, which is displayed as a watermark on the terminal. In this case
I wanted to use the host of the machine that I’ve connected to which I specify as
current_user_host in my script; therefore the value for the badge field needs
to be set to
When you’ve created the profiles you can add the following to your
file to ensure that the
ssh wrapper script knows which profiles to use for the
three themes it requires.
export SSH_DEFAULT_THEME=Spacemacs export SSH_DANGER_THEME=Production export SSH_WARNING_THEME=Staging
Once this is done you can use the wrapper script. Do the following:
- copy the contents of the script to
/usr/local/bin/ssh(or anywhere as long as it’s on your
- now when you issue an
sshcommand in the terminal the script captures the hostname of the machine that you are trying to connect to
- it then uses awslookup to check to see which AWS account that host resides in.
- in my case, if it’s in the production account it tells iTerm to switch to the
SSH_DANGER_THEME, and if it’s in our development account it uses the
- the terminal will then switch to the corresponding theme.
- when you exit your
sshsession the wrapper resets the theme back to your default.
For example, when I
ssh to a production server, my terminal automatically
switches to this:
And when I connect to a development server, it automatically changes to this:
As soon as I exit the
ssh session the terminal is restored to my default theme.
Whilst this is a very specific solution for macOS you can achieve similar results on Linux. Enjoy!