Skip to content

Instantly share code, notes, and snippets.

@trinitronx
Last active December 11, 2022 13:33
Show Gist options
  • Save trinitronx/76d2bf98489e5e3e84fa to your computer and use it in GitHub Desktop.
Save trinitronx/76d2bf98489e5e3e84fa to your computer and use it in GitHub Desktop.
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.
#!/bin/bash
DEFAULT_DISPLAY=:0
X11VNC_DISPLAY="$DEFAULT_DISPLAY"
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
EXIT_CODE=$?
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`
if [ -z "$MAGIC_COOKIE_FILE" -a "$NUM_MAGIC_COOKIE_FILE_SESSIONS" -gt 1 ]; then
# Find the current user's session
MAGIC_COOKIE_FILE=`sudo find /run/user/$(id -u) -iwholename '*/gdm/*' -iname '*Xauthority'`
X11VNC_DISPLAY=":1"
else
# 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`
fi
# 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'
[ -n "$MAGIC_COOKIE_FILE" -a -z "$GUESS_FLAG" ] && AUTH_COOKIE_FLAG="-auth $MAGIC_COOKIE_FILE"
[ ! -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}
fi
fi
@sda1969
Copy link

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