Bash – Using BASH to determine if X11 forwarding is enabled from SSH

bashsshx11

After logging into a network via SSH, the network's default shell being used is BASH.
It is then possible to run applications which utilize graphics only if X11 forwarding has been enabled which I usually do via the -X option while logging in:

ssh -X user@network.to.connect.to

I notice that when it is enabled the DISPLAY environmental variable exists and is set as such DISPLAY=localhost:11.0, however there is nothing stopping the user from exporting an environmental variable named the same thing, making it look as if it has been enabled.

Is there a way from the bash shell or a bash script to determine if X11 Forwarding is enabled?

Best Answer

First, why? Why should a user fake DISPLAY? Nevertheless, I see two workarounds:

  1. readonly DISPLAY in bashrc/profile. If executed right at login, the user won't be able to set DISPLAY afterwards.
  2. xset q. Queries some X11 infos. Returns an error if no X11 session exists
Related Question