Ssh – Changing displayed hostname in bash prompt based on login credentials

bashhostnamessh

I host several domains on one server.

I would like to be able to change the displayed hostname in my bash prompt to indicate which one I picked when ssh'ing into the server.

My prompt is as follows:

\u@\h

This displays as:

user@hostname

How would I change the \h to show which domain I had logged-into (blah.net, hmm.com, etc)?

Best Answer

It turns out that ssh finds lines in ssh_config(5) by string matching, so it's OK if all the IP's are the same.

What you want, then, are

  • Different lines in .ssh/config for each system, use HostName to give ssh the real host domain name
  • Have three different environment variables with different prompts in them in your local environment
  • Have each line in .ssh/config send a different variable using SendEnv, see man ssh_config.
  • Make your prompt out of all three (two will always be null)
  • Put AcceptEnv * in /etc/ssh/sshd_config and sudo kill -1 $(cat /var/run/sshd.pid)
    (AcceptEnv E1 E2 E3 should also work.)

Example.

Client .ssh/config

Host barb
        HostName deb
        SendEnv BARB

Host jane
        HostName deb
        SendEnv JANE

Host deb barb jane
        Protocol 2
        ForwardAgent yes
        Compression no

Server .bashrc

PS1='$BARB$JANE.otherstuff...'

Server /etc/ssh/sshd_config

...
AcceptEnv *