Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A script to start x11vnc server and retry with magic cookie if a desktop session is not created. This allows one to SSH into a box with only a gdm or login screen but no existing desktop session and start x11vnc to login the first time.
if [ -x /usr/bin/x11vnc ]; then
[ "$1" == '-nocache' ] && CACHE_FLAG='-noncache' || CACHE_FLAG='-noncache'
[ "$2" == '-guess' ] && GUESS_FLAG='-auth guess' || GUESS_FLAG=''
[ -f /root/.vnc/passwd ] && PASSWORD="/root/.vnc/passwd"
[ -f $HOME/.vnc/passwd ] && PASSWORD="$HOME/.vnc/passwd"
[ ! -z "$PASSWORD" ] && x11vnc -display $X11VNC_DISPLAY -xkb -rfbauth $PASSWORD -rfbport 5900 -shared -forever -nowf -norc -notruecolor -bg $GUESS_FLAG $CACHE_FLAG -noxdamage
if [ $EXIT_CODE -ne 0 ]; then
echo "\n*********************************************************************"
echo "*** Could not start x11vnc! Trying again with gdm MAGIC_COOKIE! ***"
echo "*********************************************************************\n"
# Old GDM location for Ubuntu <= 17.10
MAGIC_COOKIE_FILE=`sudo find /var/run/gdm/ -iname database | grep for-gdm`
# New GDM location for Ubuntu >= 17.10
[ -z "$MAGIC_COOKIE_FILE" ] && NUM_MAGIC_COOKIE_FILE_SESSIONS=`sudo find /run/user/ -iwholename '*/gdm/*' -iname '*Xauthority' 2>/dev/null | wc -l`
# Find the current user's session
MAGIC_COOKIE_FILE=`sudo find /run/user/$(id -u) -iwholename '*/gdm/*' -iname '*Xauthority'`
# Find the GDM user's session (or whichever shows up first in ps list)
# This should pick up the original gdm session which grabs :0
# If you login after gdm login screen, your Xorg server may end up on another display!
# Workaround for now is to restart x11vnc on that display number
[ -z "$MAGIC_COOKIE_FILE" ] && MAGIC_COOKIE_FILE=`sudo find /run/user/ -iwholename '*/gdm/*' -iname '*Xauthority' | head -n1`
# Old lightdm location for Ubuntu <= 17.10
[ -z "$MAGIC_COOKIE_FILE" ] && MAGIC_COOKIE_FILE=`sudo find /var/lib -name '.Xauthority' -o -wholename '/var/run/lightdm/root/:0' | head -n1`
#sudo bash -c "[ -z \"$MAGIC_COOKIE_FILE\" -a -e /var/run/lightdm/root/:0 ]" && MAGIC_COOKIE_FILE='/var/run/lightdm/root/:0'
[ ! -z "$PASSWORD" ] && sudo x11vnc -display $X11VNC_DISPLAY -xkb -rfbauth $PASSWORD -rfbport 5900 -shared -forever -nowf -norc -notruecolor -bg $GUESS_FLAG $CACHE_FLAG -noxdamage ${AUTH_COOKIE_FLAG}

This comment has been minimized.

Copy link

@sda1969 sda1969 commented Jan 11, 2019

Thanks.Works great for me. A few comments related to Ubuntu 18.10:

  1. By default wayland is used instead of Xorg and Xauthority file is not generated. To get your script work I had to fix /etc/gdm3/custom.conf file: uncomment the WaylandEnable=false line and reboot. May be it is a good idea to check custom.conf file prior to search for Xauthority and warn user to enable Xorg.

  2. I replaced key -forever for -once in the second invocation of x11vnc in line 38.
    My usage scenario is as follows:

  • remote server is powered on (there is no user login possible)
  • ssh session is established
  • your script is executed first time
  • vncviewer to:0 display login first time
  • gdm authorization passed, black screen after it
  • disconnect vncviewer (x11vnc is killed now due to -once key)
  • your script is executed second time
  • vncviewer to:0 display login second time and get happy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment