Tuesday, October 23, 2007

Starting x11vnc Easily

I had a friend recently ask me how I share out my desktop in Linux so that I can log in and run things remotely. I told him that I use a tool called "x11vnc" which allows you to share out an X-Windows session that has all ready been started. This is different than the normal VNC server functionality for Linux, which is to start an all new X-Windows session for remote access.

Using your default display allows you to get back to whatever you left running at the house. Perhaps a web page you were on at the time, a few bit-torrents that you want to check up on, etc.

In trying to share my method I had to confess that I really don't know how I made it all work. I was up late one night when I finally string together a command which got the desktop shared. Since then, I had been copying and pasting commands from a text file that I had saved away. The text file has now perished in a re-installation so I finally took the time to figure this all out again. This time, I have written a sensible shell script which I will document here for my friend Rob, and for anyone else who might be interested.

#!/bin/bash

# Set this to your username
USERNAME="steve"
XAUTH=`/usr/bin/ls /var/run/xauth`

# Are we root, or just some schmuck?
if [ $(whoami) = "root" ]
then
# Now, do we have a password file created?
if [ -e /home/$USERNAME/.vnc/passwd ]
then
x11vnc -shared -display :0 -auth /var/run/xauth/$XAUTH -usepw -forever
else
echo -e "You have not set a password yet, run: x11vnc -storepasswd as $USERNAME."
exit 1
fi
else
echo "You need to be root to do this (or use sudo -b ./startx11vnc)."
exit 1
fi

exit 1


This script requires root permissions, and does the usual griping if it's run as a regular user. It then goes out to see if the person running it has all ready created a VNC connection password. If not, it gripes some more but drops a hint on how to create one. If running this as root, and you have a password file created, we then go out and find your xauth file. This file is needed to point x11vnc to the active running session. With that, we finally launch x11vnc and begin sharing the desktop.

Notice that to run x11vnc, the script suggests using "-b" which will allow the sudo command to ask for the password, and then politely launch the program into the background. In doing so, you can safely close the terminal window that you launched this command from.

Although wouldn't it be nice if this ran every time you logged on? Oh, I will have to work on that now.