I am trying to launch Xev from console, it always gives "unable to open display", I need to launch xev from a console as another user (I use sudo -u '#1000' to do this).

I know xev has a "display" argument (xev -display) but I just can't figure out how to use it.

How do I launch xev from the console from outside of my X11 environment?

  • 1
    For all X11 programs there is an environment variable DISPLAY. For Display 0 you can do DISPLAY=:0 xev. Now you will get an error about security X11 has security to stop you doing this. You need to look into xauth. Note xhost + is dangerous. – ctrl-alt-delor Feb 19 '15 at 22:51

All¹ X11 programs open their windows on the display indicated by the environment variable DISPLAY. Thus:

sudo -u 1000 env DISPLAY=:0 xev

or for that matter, since you can run programs as a different user from the X server, just

DISPLAY=:0 xev

:NUMBER is the notation for local displays; in most scenarios, the X11 server that is running on the console is the one that's started first and ends up being number 0. You can run echo "$DISPLAY" in a terminal on that display to check whether the display number is correct. :0.0 is equivalent to :0 (a trailing .0 can be omitted).

If you run the program as a different user from the X server, and sometimes even if you run it as the same user, you may need to set the XAUTHORITY environment variable as well. This variable points to a file that contains a password (called a cookie) that applications must pass to the X server. To see the right value from XAUTHORITY, run echo $XAUTHORITY on that display; if it's unset, the default value is ~/.Xauthority where ~ represents the user's home directory.

If you need to find the values of DISPLAY and XAUTHORITY programmatically, see Open a window on a remote X display (why "Cannot open display")?

¹ At least almost all. It's technically possible for them not to, but it takes active work on the part of the programmer to make it not so, whereas a -display argument is a convention that is far from universal.

  • Thanks for your detailed answer! This worked perfectly :) – Cestarian Feb 20 '15 at 1:06

You should try:

xev --display localhost:0.0

assuming that X is actually running.

  • This did not work for me. – Cestarian Feb 19 '15 at 21:52
  • @Cestarian Have you checked that X is actually running? – Anthon Feb 19 '15 at 21:53
  • Yes of course, it's running on tty 1. – Cestarian Feb 19 '15 at 21:57
  • @Cestarian Maybe you can include some more information in your question, like how started X and other things that might seem relevant. As the other user did you run xhost + to actually allow access to the X server? – Anthon Feb 19 '15 at 21:59
  • I started X with the startx command. I did not even have xhost installed. Naturally I am not familiar with how to operate it either. – Cestarian Feb 19 '15 at 22:47

The following command is very useful:

xev | grep -A2 --line-buffered '^KeyRelease' | sed -n '/keycode /s/^.*keycode \([0-9]*\).* (.*, \(.*\)).*$/\1 \2/p'

But you need to set DISPLAY variable and it's related to your position to xev machine.

NOTE: When you apply DISPALY variable that your position is illlegal, or you don't have any local access to machine.

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.