Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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 $ 

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .