Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

share|improve this question

5 Answers 5

up vote 3 down vote accepted

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 *
share|improve this answer
2  
I would be nervous about AcceptEnv * being a security issue. In any case "don't open what you don't need" applies. –  Dennis Williamson 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. –  Andrew H 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.

share|improve this answer
    
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
  1022)
    WEBHOST="www.foo.com"
  ;;
  2022)
    WEBHOST="www.bar.com"
  ;;
  *)
    WEBHOST="www.foobar.com"
  ;;
esac

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

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

share|improve this answer
3  
Or you could do the same thing without ruby using bash: sshconn=($SSH_CONNECTION); case "${sshconn[3]}" in –  Dennis Williamson Nov 7 '09 at 19:54
    
nice, cheers :) –  Andrew H 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:

PS1="...${dest}..."

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.

share|improve this answer

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.

share|improve this answer
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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