How to send an `xmessage` as root remotely to regular user logged in with ‘x’ session

bashcommand line

I'm using BASH on the command line, logged into another user's PC as root, via SSH.

I wish to send the user currently logged in running an X-session, a popup message, but this is the result:

root@i5:~$ xmessage now                           
Invalid MIT-MAGIC-COOKIE-1 keyInvalid MIT-MAGIC-COOKIE-1 keyError: Can't open display: :0  

The expected result was a popup message on remote machine, saying "now".

I can replicate this on my local machine by starting a local x-session, then in a terminal if I type xmessage now it works, but if in the terminal I su- so I'm root and I try again, I get the same error as above.

I'm also wishing to use this in a script which needs to be run as root by cron, on my local machine and the script in certain instances, will alert me and request that I choose one of two buttons giving feedback to the script.

Best Answer

Opening a popup window on another user's desktop session is a security issue, therefore it is not allowed. Otherwise it could easily be abused for phishing or other malicious tasks. Without special permissions (like superuser access) there is no way to accomplish what you want.

Even with root access on the machine it still is questionable whether you should open a popup window. You have no idea in what situation your popup will appear, or even whether it will appear. Normally, the window manager is in task to handle newly appearing windows. It can place it somewhere on an inactive viewport or similar, so it would not even become visible, albeit open.

There is, however, a standard way of contacting the user on the machine. That is wall. This tool still requires root permissions (to prevent spamming) but it then writes to all open TTYs, effectively notifying the user running an X session. You might want to consider using this instead of a popup window.

Related Question