Keeping track of ssh connections the colourful way

At any given time, I usually have one or two Terminal windows open for the local machine and one or two for each server I’m on. This means I have as many as 6-7 terminals open connecting me to 3-4 different hosts. Keeping track of which server the current window is connected to can be difficult, so I’ve been looking for something to make it easier.

This guy has each Mac OS X Terminal window with a custom background, but it would mean I would have to open ssh connections using saved sessions, which I don’t want to do. As well, I suspect that after you log out, the window background will still be set for the remote server. I like the idea, but not the implementation. If there was a way to manipulate the current Terminal window from the command line, then I’d just write a wrapper script for ssh that automagically changes the background to and from the server being connected to (but I haven’t found a way of doing this).

So what was my solution? I have each server’s .bashrc change the command prompt “user@hostname” to a colour specific to that server. Not as impressive as you’d like, perhaps, but hopefully it’ll stop me from typing rm -rf * in the wrong window (again). More detailed instructions can be found here, but the summary is this:

  1. Open up .bashrc with your favourite text editor and find something that looks like the following line:
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
  2. To change the colour, look for something like \[033[01;32m\] that shows up just before \u@\h (user@hostname); the colour is the third number in that section, in this case 32. Colours you can change it to are:
    • 30: black
    • 31: red
    • 32: green
    • 33: yellow
    • 34: blue
    • 35: purple
    • 36: cyan
    • 37: red
  3. Save the file, log out, and log back in.

Yes, Drew, this is why turbine now has a red prompt.

Leave a Reply

:mrgreen: :neutral: :twisted: :shock: :smile: :???: :cool: :evil: :grin: :oops: :razz: :roll: :wink: :cry: :eek: :lol: :mad: :sad: