For zsh, I decided I wanted to simply add a line to my zshrc file to set the window title for iTerm2 to $(hostname) of the box I was currently logged in to:

echo -ne "\e]1;${(hostname)}\a"

Yes, its that easy:)

Looking a little further into the superuser question. it appears theres another way that works for both bash and zsh and, by the author’s note:

This way you can immediately see what host you’re connected to in what window, and the window title for each tab shows user & CWD.

# $1 = type; 0 - both, 1 - tab, 2 - title
# rest = text
setTerminalText () {
    # echo works in bash & zsh
    local mode=$1 ; shift
    echo -ne "\033]$mode;$@\007"
}
stt_both  () { setTerminalText 0 $@; }
stt_tab   () { setTerminalText 1 $@; }
stt_title () { setTerminalText 2 $@; }

I think thats probably one of the best comments I’ve seen on superuser ;P

Mario Loria is a builder of diverse infrastructure with modern workloads on both bare-metal and cloud platforms. He's traversed roles in system administration, network engineering, and DevOps. You can learn more about him here.