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:


This displays as:


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


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.)


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


Server /etc/ssh/sshd_config

AcceptEnv *
  • 2
    I would be nervous about AcceptEnv * being a security issue. In any case "don't open what you don't need" applies. – Paused until further notice. Nov 8 '09 at 0:48
  • Dammit I knew there must have been something like SendEnv...one must remember to read manpage before posting :) Great answer. – Cawflands Nov 9 '09 at 14:18

Do your domains all have different IPs?

If so, you can use the variable $SSH_CONNECTION once you are logged in.

If not, then it is impossible to differentiate, all the server sees is the IP address, there is no such thing as name-based ssh.

  • no - all the same IP.. they're fairly low-traffic domains on this server – warren Nov 7 '09 at 17:02

One way to do this could be to make entries for each of your hostnames in your local ~/.ssh/config, configured to connect to different ports on your server.

Host foo
  HostName www.foo.com
  Port 1022

Host bar HostName www.bar.com Port 2022

Then your ~/.bashrc on the server can parse the SSH_CONNECTION environment variable and pick out the port you've connected to, and change the prompt accordingly.

case $(ruby -e 'puts ENV["SSH_CONNECTION"].split[-1]') in

PS1="\n\u@$WEBHOST \w\n$?> "

Obviously you would need ruby in your path for this, but you see what I mean.

  • 3
    Or you could do the same thing without ruby using bash: sshconn=($SSH_CONNECTION); case "${sshconn[3]}" in – Paused until further notice. Nov 7 '09 at 19:54
  • nice, cheers :) – Cawflands Nov 9 '09 at 14:17

You might be able to use the SendEnv directive (protocol 2 is required and sshd on the remote machine must be configured for AcceptEnv to include the name of the variable you want to use):

On the local machine:

export dest=example.com; ssh -o "SendEnv dest" username@$dest

On the remote machine, in ~/.bashrc:


Where the ellipses represent the rest of the stuff in your prompt.

If you edit /etc/ssh/sshd_config to add a variable you may have to send SIGHUP to sshd.


Or, if you already use bash on these domains, edit the .bashrc file to add the line:

PS1='\u@\h:\w\$ '

So that when you login and switch to bash the username and hostname are displayed.

  • 1
    thanks for that .. but my prompt already does this. the "h" tells the prompt to use the results of hostname in the display. hostname does not change based on which incoming domain name you used, but uses the canonical name of the device – warren Mar 31 '11 at 12:26

What you have to do is to specify prompt (PS1) with correct hostname during connection to a server. Assuming you use ssh client on Linux machine you can do it by running command:

ssh -t username@hostname-001 "export PS1='\u@hostname-001'; bash --login"

I found this technique very useful while managing connections to many servers without proper hostname set (like in EC2 amazon default hostnames). I wrote a small bash helper function, used bash aliases and put it in .bashrc file.

function __ssh_helper {
    local user=$1
    local host=$2
    local txtgrn='\e[0;32m'
    local txtblu='\e[0;34m'
    local txtcyn='\e[0;36m'
    local txtrst='\e[0m'
    ssh -t ${user}@${host} "export PS1='[${txtcyn}\u${txtrst}@${txtgrn}${host}${txtrst} \W]\$ '; bash --login"

alias hostname-001.prod='__ssh_helper username hostname-001.prod'
alias hostname-002.prod='__ssh_helper username hostname-002.prod'

I used /etc/hosts file to point hostname-00(1|2).prod to ip addresses. To add new ssh connection simply: 1. add hostname to /etc/hosts file; 2. add an alias to .bashrc; 3. pick up changes by sourcing .bashrc file. Hope it helps someone :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.