I know this thread is old, but just in case someone is wondering. I was getting exactly the same problem:
unable to open display ":0"
I was trying to turn on two of my displays via SSH, but it failed with the message from above. Other thing I had to say is that I wasn't logged on the console of the physical PC, so, only the logon screen was there, but the displays were off because of the idle settings.
So, on my case, I had to add also the XAUTHORITY variable as follows:
#Turn on the main display
sudo DISPLAY=:0 XAUTHORITY=/run/user/121/gdm/Xauthority xrandr --output HDMI-0 --off
sudo DISPLAY=:0 XAUTHORITY=/run/user/121/gdm/Xauthority xrandr --output HDMI-0 --auto
#Turn on the second display
sudo DISPLAY=:0 XAUTHORITY=/run/user/121/gdm/Xauthority xrandr --output DP-4 --right-of HDMI-0 --off
sudo DISPLAY=:0 XAUTHORITY=/run/user/121/gdm/Xauthority xrandr --output DP-4 --right-of HDMI-0 --auto
Here I have to mention that I'm using Ubuntu 18 with gdm3 as display manager. You should check the path:
/run/user/121/gdm/Xauthority
It maybe different on your system. Also check your xorg.conf configuration. There I defined two screens as follows:
Screen 0 "Screen0"
Screen 1 "Screen1" RightOf "Screen0"
That's why I'm using the "--right-of" option.
Finally check the names of your ouputs by running:
sudo DISPLAY=:0 XAUTHORITY=/run/user/121/gdm/Xauthority xrandr -q
There you will get the right names. On my case: "HDMI-0" (main monitor) and "DP-4" (second monitor).