Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I connect from machine A to machine B. In machine B, I'd like to print A's hostname in bash's command prompt.

I tried to get this using who -m and doing some cuting, put output differs between different unix'es.

I could not find any direct way to get this hostname.

I see two possibilites:

  1. Somehow passing it when connecting using ssh (I don't think I will ever use something else to connect remotely)
  2. Processing who's output in multiple ways and hoping there aren't too much versions around

Possibility (1) is preferred, because I can send the hostname I've assigned to a host and not my carrier's one, but I couldn't find any way to archive this in man ssh_config.

share|improve this question

1 Answer 1

Accessing the IP address of the client is pretty easy. In my .profile, I have the following snippet:

PS1="\T [\h:\w]\n(from ${SSH_CLIENT%% *}) $ "

which produces a two line prompt that looks like this:

08:25:14 [fluorine:~]
(from 72.69.85.67) $ 

If getting the client hostname is really important to you, then you could use that IP address and do a reverse DNS lookup on the extracted IP address (assuming dig or something similar is installed on the server).

Or, as you suggest, you can send environment variables to the server assuming you have the ssh server configured to do so. You can refer to this answer for help in doing that.

share|improve this answer
    
I already found $SSH_CONNECTION in env, but I'd prefer to have the configured hostname, not the one I can get from DNS. The link you provided looks more promising, will have a look into that. –  Jens Erat Oct 29 '11 at 0:45

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.