X programs know which X session they're supposed to talk to by looking at the DISPLAY
environment variable.
First you'll need to find out what DISPLAY
is set to in the X session where you want your program to run. Do this by issuing the command echo $DISPLAY
inside an X-terminal in that X session. Typically get something like :0.0
is outputted.
Lets say you want to start the excellent program xclock
program in your X session where DISPLAY
is :0.0
, but you want to do this at the console. You then press Alt-Ctrl-F1 to shift to that console, and then type:
DISPLAY=:0.0 xclock # variables set w/ a command are local to that command
Then you go back to X by pressing Alt-Ctrl-F7 or Alt-Ctrl-F8 (or maybe some other F key, depending on your Linux flavor) and watch the new pretty wallclock you have there.
In fact you could've executed the above command in any shell on that same machine (via SSH, in any X session, on any console etc.) – the fact that you specified DISPLAY=:0.0
before the command means that the xclock
window will always pop up in the same X session.
In fact – apart from the fact that X programs look at it – there is nothing special about the DISPLAY
variable. Normally when you're starting programs from an X terminal they start in the right place because DISPLAY
has already been set for you.
Instead of using the above command, you can also split it into two by first setting DISPLAY
, and then running xclock
. In this case the DISPLAY
setting will persist until you close that shell, and all subsequent X programs started from there will open in the same X session as xclock
.
# variables set w/o a command are local to the shell,
export DISPLAY=:0.0 # unless exported
xclock