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.
2 Answers
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.
DISPLAYenvironment variable. You should be able to programmatically do something with that. Another means would beexport tty=$(tty|sed '@/dev/@@') ; w | grep $tty | awk '{print $3}'remoteoffsets the display to :10 rather than :1 thatlocalwould be using).