Custom Color-Coded SSH Terminal Prompts for Multi-Server Management


2 views

Every seasoned sysadmin knows the moment of panic when you realize you just ran rm -rf /tmp/* on your production database server instead of the staging environment. The solution? Color-coded terminal prompts that instantly identify which machine you're working on.

Here's how to implement machine-specific prompt coloring in your ~/.bashrc or ~/.bash_profile:

# Color definitions
BLUE='$$\e[0;34m$$'
PURPLE='$$\e[0;35m$$'
RED='$$\e[0;31m$$'
GREEN='$$\e[0;32m$$'
YELLOW='$$\e[0;33m$$'
RESET='$$\e[0m$$'

# Machine-specific prompts
case $(hostname) in
  desktop1*) 
    PS1="${BLUE}[\u@\h \W]\$${RESET} "
    ;;
  laptop*) 
    PS1="${PURPLE}[\u@\h \W]\$${RESET} "
    ;;
  db-server*)
    PS1="${RED}[\u@\h \W]\$${RESET} "
    ;;
  *)
    PS1="${GREEN}[\u@\h \W]\$${RESET} "
    ;;
esac

For environments where you frequently SSH between machines, this enhanced version detects remote sessions:

if [ -n "$SSH_CONNECTION" ]; then
    PS1="${RED}[SSH:\h \W]\$${RESET} "
else
    PS1="${GREEN}[\u@\h \W]\$${RESET} "
fi

Combine colors with other visual indicators for maximum effect:

# Add emoji indicators
PROD_EMOJI=$'\U26A0'  # Warning symbol
DEV_EMOJI=$'\U1F41B'  # Bug symbol

case $(hostname) in
  prod*)
    PS1="${RED}${PROD_EMOJI} [\u@\h \W]\$${RESET} "
    ;;
  dev*)
    PS1="${YELLOW}${DEV_EMOJI} [\u@\h \W]\$${RESET} "
    ;;
esac

For tmux users, set window titles along with colors:

# In ~/.tmux.conf
set -g window-status-format "#I:#W#F"
set -g window-status-current-format "#[fg=red]#I:#W#F"

Remember that:

  • Use high-contrast colors that are visible in both light and dark terminal themes
  • Consider colorblind-friendly palettes (avoid red/green combinations)
  • Test your colors with echo -e "\e[0;31mTest\e[0m"

To make these changes permanent:

# For all users (system-wide):
sudo nano /etc/bash.bashrc

# For current user:
nano ~/.bashrc
source ~/.bashrc

Working with multiple remote servers via SSH can quickly become confusing. When you have terminal tabs or windows open to different machines, it's frighteningly easy to execute commands on the wrong server - potentially with disastrous consequences. The standard monochrome prompts don't help distinguish between environments.

Here's a robust solution using your shell's PS1 variable and SSH configuration to give each server a distinct colored prompt. We'll implement this for bash (works similarly for zsh):

# In your ~/.bashrc or server's /etc/bashrc:
function set_ssh_prompt() {
    case $(hostname) in
        desktop1*)
            PS1="$$\e[1;34m$$[\u@\h \W]\$ $$\e[0m$$"  # Blue
            ;;
        laptop*)
            PS1="$$\e[1;35m$$[\u@\h \W]\$ $$\e[0m$$"  # Purple
            ;;
        server*)
            PS1="$$\e[1;31m$$[\u@\h \W]\$ $$\e[0m$$"  # Red
            ;;
        *)
            PS1="$$\e[1;32m$$[\u@\h \W]\$ $$\e[0m$$"  # Default green
            ;;
    esac
}

PROMPT_COMMAND=set_ssh_prompt

For this to work when SSHing into machines, we need to ensure the remote server executes our bashrc. Add this to your local SSH config (~/.ssh/config):

Host desktop1
    HostName desktop1.example.com
    User yourusername
    RequestTTY yes
    RemoteCommand bash --login

Host laptop
    HostName 192.168.1.100
    User devuser
    RequestTTY yes
    RemoteCommand bash --login

For even better visual distinction, combine colors with these techniques:

# Environment-specific emoji in prompt
PS1="$$\e[1;31m$$?️ [\u@\h \W]\$ $$\e[0m$$"  # Server
PS1="$$\e[1;34m$$? [\u@\h \W]\$ $$\e[0m$$"  # Laptop

# Background colors for immediate recognition
PS1="$$\e[30;47m$$[PROD] $$\e[0m$$$$\e[1;31m$$[\u@\h \W]\$ $$\e[0m$$"

While we're improving our terminal experience:

# Colorized man pages (add to ~/.bashrc or ~/.zshrc)
export LESS_TERMCAP_mb=$'\e[1;32m'
export LESS_TERMCAP_md=$'\e[1;32m'
export LESS_TERMCAP_me=$'\e[0m'
export LESS_TERMCAP_se=$'\e[0m'
export LESS_TERMCAP_so=$'\e[01;33m'
export LESS_TERMCAP_ue=$'\e[0m'
export LESS_TERMCAP_us=$'\e[1;4;31m'

# Immediate visual feedback for dangerous commands
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

If colors aren't appearing:

  • Ensure your terminal supports ANSI colors (most modern terminals do)
  • Check that your remote shell is actually loading the rc file (bash --login helps)
  • Verify the hostname matches your case statements exactly