Say I'm on machine local and log into machine remote with ssh, using X11 forwarding. Is there any way for programs running within the ssh session on remote can know they are being displayed on local? Ideally, I'd like to know the hostname of the computer that the X server is running on. My goal is to cause different behavior in a program (emacs) based on which machine it's displayed on.

  • From what I understand, this is not possible, all you are doing is forwarding what would have been displayed on the remote machine on your machine. The configuration files necessary to have all of your settings while editing files in emacs wouldn't exist. – Eric Jul 29 '13 at 22:58
  • The location of where your "display" is is set in the DISPLAY environment variable. You should be able to programmatically do something with that. Another means would be export tty=$(tty|sed '@/dev/@@') ; w | grep $tty | awk '{print $3}' – Drav Sloan Jul 29 '13 at 23:14
  • (just to add to that, ssh by default on remote offsets the display to :10 rather than :1 that local would be using). – Drav Sloan Jul 29 '13 at 23:35

I don't think the X server reports its host name to clients. The clients are supposed to know the host name that they're connecting to, but over an SSH connection, that's localhost, with ssh doing the forwarding.

The OpenSSH server sets the SSH_CLIENT environment variable; you can read the client's IP address from there. That's the address as seen by the server, so you can't distinguish machines behind the same NAT. This is the address of the X server, provided that nothing else is going on (SSH forwarding, remote X server on the client, …). Note that you'll only get the environment variable in Emacs if it was started from within that SSH connection, not if you created a frame in a running Emacs with emacsclient.

You can get at some properties of the display (dimensions, color depth, X vendor, …) in Emacs with the functions x-server-XXX, x-display-XXX and display-XXX.


A hacky way to get the server name:

  • When the server starts, arrange for it to put its own name in a root property:

    xprop -root -f __SERVER_NAME__ 8s -set __SERVER_NAME__ "$(hostname -s)"
  • You can then use xprop -root __SERVER_NAME__ to get the name.

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.