I see that $DISPLAY
is set to localhost:0.0 if I am running over a vnc server this may not be correct, is there a way to automatically set it in my login script?

- 11,875
- 18
- 85
- 108
-
1questions like this are more about operating systems than programming and serverfault.com is where they go. – SpliFF Jun 29 '09 at 09:26
-
1It seems when vncserver started, the DISPLAY variable is automatically set in the new vnc session, at least this works for me. – Sam Liao Jun 29 '09 at 09:33
-
You should describe what machine your are logging into, and where the vnc server(s) are running. Any shells created in a vncserver environment would already have the correct DISPLAY variable. It sounds like you are remote logging into a remote host and trying to start apps on an already running X server. Your login script doesn't know which vnc server it should connect to any more than we do. – codeDr Jul 03 '09 at 03:45
5 Answers
Here's something I've just knocked up. It inspects the environment of the last-launched "gnome-session" process (DISPLAY is set correctly when VNC launches a session/window manager). Replace "gnome-session" with the name of whatever process your VNC server launches on startup.
PID=`pgrep -n -u $USER gnome-session`
if [ -n "$PID" ]; then
export DISPLAY=`awk 'BEGIN{FS="="; RS="\0"} $1=="DISPLAY" {print $2; exit}' /proc/$PID/environ`
echo "DISPLAY set to $DISPLAY"
else
echo "Could not set DISPLAY"
fi
unset PID
You should just be able to drop that in your .bashrc file.

- 7,700
- 2
- 29
- 37
-
login, switch user & login into another account, switch back to first account and "last launched gnome-session" is not the active one any more. – Dima Jan 16 '13 at 13:18
-
@Dima Good point. I've modified my answer to at least restrict processes to the current user. – Nick Feb 11 '13 at 17:06
-
I had to change your awk line to: `export DISPLAY=$(cat /proc/$PID/environ | strings | awk 'BEGIN{FS="=";} $1=="DISPLAY" {print $2; exit}')` Then it worked. – isaaclw Dec 05 '13 at 20:44
-
Also, I changed `pgrep` to `'gnome-session|xfdesktop'`, to include xfce desktops. Full command is now: `PID=$(pgrep -n -u $USER 'gnome-session|xfdesktop')` – isaaclw Dec 11 '13 at 15:54
-
2The export line can be simplified to: `export $(grep -z ^DISPLAY= /proc/$PID/environ)`. – scai Sep 03 '14 at 12:46
do you use Bash? Go to the file .bashrc in your home directory and set the variable, then export it.
DISPLAY=localhost:0.0 ; export DISPLAY
you can use /etc/bashrc if you want to do it for all the users.
You may also want to look in ~/.bash_profile and /etc/profile
EDIT:
function get_xserver ()
{
case $TERM in
xterm )
XSERVER=$(who am i | awk '{print $NF}' | tr -d ')''(' )
XSERVER=${XSERVER%%:*}
;;
aterm | rxvt)
;;
esac
}
if [ -z ${DISPLAY:=""} ]; then
get_xserver
if [[ -z ${XSERVER} || ${XSERVER} == $(hostname) || \
${XSERVER} == "unix" ]]; then
DISPLAY=":0.0" # Display on local host.
else
DISPLAY=${XSERVER}:0.0 # Display on remote host.
fi
fi
export DISPLAY

- 11,809
- 5
- 57
- 91
-
if i put this in my bashrc it wont work with vncserver because it creates other displays like localhost:1.0 etc so i need a little more generic solution. – Jun 29 '09 at 11:13
I'm guessing here, based on issues I've had in the past which I did solve:
- you're connecting to a vnc server on machine B, displaying it using a VNC client on machine A
- you're launching a console (xterm or equivalent) on machine B and using that to connect to machine C
- you want to launch an X-based application on machine C, having it display to the VNC server on machine B, so you can see it on machine A.
I ended up with two solutions. My original solution was based on using rsh. Since then, most of our servers have had ssh installed, which has made this easier.
Using rsh, I put together a table of machines vs OS vs custom options which would guide this process in perl. Bourne shell wasn't sufficient, and we don't have bash on Sun or HP machines (and didn't have bash on AIX at the time - AIX 5L wasn't out yet). Korn shell wasn't much of an option, either, since most of our Linux boxes don't have pdksh installed. But, if you don't face these limitations, you can implement the idea in ksh or bash, I think.
Anyway, I would basically run 'rsh $machine -l $user "$cmd"' where $machine, of course, was the machine I was logging in to, $user, similarly obvious (though when I was going in as "root" this had some variance as we have multiple roots on some machines for reasons I don't fully understand), and $cmd was basically "DISPLAY=$DISPLAY xterm", though if I were launching konsole, for example, $cmd would be "konsole --display=$DISPLAY". Since $DISPLAY was being evaluated locally (where it's set properly), and not being passed literally across rsh, the display would always be set correctly.
I also had to make sure that no one did anything silly like reset DISPLAY if it was already set.
Now, I just use ssh, make sure that X11Forwarding is set to yes on the server (sshd_config), and then I can just ssh to the machine, let X commands go across the wire encrypted, and it'll always go back to the right place.

- 21,664
- 5
- 41
- 68
Your vncserver have a configuration file somewher that set the display number. To do it automaticaly, one solution is to parse this file, extract the number and set it correctly. A simpler (better) is to have this display number set in a config script and use it in both your VNC server config and in your init scripts.

- 14,948
- 3
- 36
- 59
You'll need to tell your vnc client to export the correct $DISPLAY once you have logged in. How you do that will probably depend on your vnc client.

- 11,745
- 10
- 75
- 100