Ssh – DISPLAY environment variable not set

amazon ec2displayremoterhelssh

I am using a Amazon Linux 2 (a Fedora/Cent OS like distro) EC2 machine with the Deep Learning AMI (Amazon Linux 2) Version 30.1 AMI.

I want to use xclip and gedit in the remote instance. But if I type in xclip I get

Error: Can't open display: (null)

If I do gedit filename I get

(gedit:6012): Gtk-WARNING **: 00:18:40.117: cannot open display:

I have already gone through the following links:

  1. https://github.com/microsoft/WSL/issues/4933
  2. xsel: Can't open display: (null)
  3. https://askubuntu.com/questions/1035903/how-can-i-get-around-using-xclip-in-the-linux-subsystem-on-win-10
  4. https://www.cyberciti.biz/faq/error-couldnt-open-display-null-and-solution/
  5. https://superuser.com/questions/310197/how-do-i-fix-a-cannot-open-display-error-when-opening-an-x-program-after-sshi
  6. https://www.linuxquestions.org/questions/linux-software-2/xdotool-error-can%27t-open-display-null-4175527094/
  7. https://serverfault.com/questions/765606/xming-cant-open-display-error
  8. https://serverfault.com/questions/425346/cant-open-display-x11-forwarding-cent-os
  9. https://askubuntu.com/questions/305654/xclip-on-headless-server/305681#305681

And thus have,

  1. installed xauth
  2. manually set the environment variable DISPLAY to
    export DISPLAY="IP_of_remote_machine:0"
    export DISPLAY="127.0.0.1:0"
    export DISPLAY=:0.0
    export DISPLAY=localhost:0
    When I do this I get

    (gedit:6053): Gtk-WARNING **: 00:23:36.052: cannot open display: 127.0.0.1:0
    
  3. I have tried the -Y flag (-X is not an option cause of security, also -X doesn't work)
  4. I have added ForwardX11 yes in the client side's ~/.ssh/config and X11Forwarding yes in the server side's /etc/ssh/sshd_config

Nothing works. Any directions?

EDIT:

I also watched this video which says that DISPLAY should not be hardcoded. But my DISPLAY is not set automatically. When I do a echo $DISPLAY I get nothing.

So I did a xauth list which gave me

ip-x-y-x.ec2.internal/unix:10  MIT-MAGIC-COOKIE-1  c84bcd904dd50f7776f667eca7a480f5

so I did

export DISPLAY=localhost:10.0

Still I get the same error.

Best Answer

I solved this problem by simply hardcoding $DISPLAY as :1,

export DISPLAY=:1

in my .bashrc file.

I however, still don't understand why this works. Here is the output of xauth list

ip-172-31-74-230.ec2.internal:1  MIT-MAGIC-COOKIE-1  8a44b41e42cae52c6acd4747763bb985
ip-172-31-74-230.ec2.internal/unix:1  MIT-MAGIC-COOKIE-1  8a44b41e42cae52c6acd4747763bb985
ip-172-31-74-230.ec2.internal:2  MIT-MAGIC-COOKIE-1  e24f36d585762b0c0fe24010b99e448d
ip-172-31-74-230.ec2.internal/unix:2  MIT-MAGIC-COOKIE-1  e24f36d585762b0c0fe24010b99e448d
ip-172-31-74-230.ec2.internal/unix:10  MIT-MAGIC-COOKIE-1  c84bcd904dd50f7776f667eca7a480f5
Related Question