37

Without being the person logged in at the console, how do I run an X application and have it display on that X session? Assume I am either root, or I am the same user who logged in, so in principle I have persmission to do this. But how do I convince X of this?

Some examples of situations like this:

  • Log in with SSH and run a program that displays on the remote computer's screen (not tunneled through SSH—that is totally different)
  • A cron job to take a screenshot of the X session via ImageMagick's import command
  • Running a keystroke logger for audit purposes

This is a simpler version of Launch OpenGL app straight from a windowless Linux Terminal

Community
  • 1
  • 1
JasonSmith
  • 72,674
  • 22
  • 123
  • 149

2 Answers2

44

The short answer is that you have to set the DISPLAY environment variable, and then the app will run.

The long answer is that we've got Xauth, and unless you're running as the same user on the same machine that's probably not going to work unless you export the Xauth credential from the account running the X server to the account running the X client. ssh -X handles this for you, which is why it's awesome, but the manual procedure involves running xauth extract - $DISPLAY on the X server account and feeding that data into xauth merge - on the client account. (Warning: the data is binary.)

On modern Linux systems, there is one X session at :0 and the X11 authority data file is always $HOME/.Xauthority so you can most often set two environment variables, for example, in Bash:

export XAUTHORITY=/home/$your_username/.Xauthority
export DISPLAY=':0'
JasonSmith
  • 72,674
  • 22
  • 123
  • 149
hobbs
  • 223,387
  • 19
  • 210
  • 288
  • I asked this in order to answer it myself just to have it on record somewhere easy to find. Since I'm GMT+7 I didn't expect such a quick reply! Tell you what: would you mind pasting in my command-line examples? That should make yours the most complete answer, and then I'll accept yours. – JasonSmith Oct 18 '09 at 09:17
  • Hobbs, you there? I'd love to accept your answer if you would make it apply a bit more to running on a local machine. Thanks! – JasonSmith Oct 19 '09 at 03:53
  • Bwahaha, I have enough rep to edit your answer! :) I'll just add some command-line examples and accept you. – JasonSmith Dec 06 '09 at 11:03
  • You may also need to define the XAUTHLOCALHOSTNAME variable: `export XAUTHLOCALHOSTNAME=localhost` – palswim Jan 30 '12 at 21:51
  • 4
    Am I insane, or has that actually not answered the question? In both answers you only clarify prerequisites to run a program in an X-Session on a machine you SSH to. I am missing the acutal answer of how to start a program inside X (eg.: LibreOffice) on a remote machine. I would be delighted, if someone could further clarify that. Thanks, Regards. – atripes Jul 26 '13 at 17:13
  • 1
    Sorry, but after a lot of experience with this, I must tell you that *it is very much incomplete*. Many programs expect other session environment variables to be set. E.g. common KDE programs will have the wrong theme / color scheme, and some programs won’t run at all without their dbus session. The only two solutions I found, were 1. if the program has a dbus interface, then `sudo -u "$user" -n qdbus …` is the better choice. 2. if not, then you must somehow grab ALL the environment variables (`set`) from the target session, and export them *inside* the `sudo`, before running the commmand. – Evi1M4chine Jan 19 '18 at 03:32
17

The upshot is that you have to know the X display (placed in the DISPLAY environment variable) and the magic cookie (placed in a file, with the filename in the XAUTHORITY environment variable).

The quick-and-dirty way

On the system running X, if you are root or you are the same user who logged in to X, just assume the most common display and cookie files (works on almost any standard desktop install of any distro).

env DISPLAY=:0 XAUTHORITY=/home/whoever/.Xauthority /path/to/my/X/program

The more surefire way

Find them from the environment of an already-running X program. Again, if you are root or the same user who is logged in, this will tell you (if the user is using GNOME):

cat /proc/`pgrep -f ^x-session-manager`/environ \
  | ruby -ne 'puts $_.split("\0").select { |e| e =~ /^(DISPLAY|XAUTHORITY)=/ }'
JasonSmith
  • 72,674
  • 22
  • 123
  • 149
  • Hey, Is there any way I can change the display environment variable inside the java program so that the actions done by the java program are in corresponding environment. – raju Oct 28 '13 at 09:28