How to access an ubuntu machine via VNC from the login screen?

36,597

Solution 1

Your best bet is to install xrdp Install xrdp. After installation, you can use an RDP client to connect to the machine - you will then be prompted for your credentials as you would be on the login screen.

Solution 2

Overall, I'd recommend x11vnc.

TL;DR

apt-get -y install x11vnc
x11vnc -storepasswd

Enter your password, it's saved by default in ~/.vnc/passwd in INSECURE encrypted form. It can be decrypted because the key is known... protect it with filesystem permissions)

chmod 600 ~/.vnc/passwd

Save my helper script locally:

mkdir ~/bin/
curl https://gist.githubusercontent.com/trinitronx/76d2bf98489e5e3e84fa/raw/53885d87f91320b574ca4f7d609e4bb268274f68/start_x11vnc.sh  > ~/bin/start_x11vnc.sh && chmod +x ~/bin/start_x11vnc.sh

From your VNC Client host:

ssh -f  -L 5900:127.0.0.1:5900 -p 22 [email protected] '~/bin/start_x11vnc.sh && sleep 10'

Or, from your VNC Server host, run :

~/bin/start_x11vnc.sh

via a terminal (or start it as a daemon with -forever as an init.d service, upstart service, systemd unit, or however you wish)

Now run your VNC Client of choice from your Client Host, point it at: 127.0.0.1:5900, login with password saved above.

Use the X11 "Magic Cookie"

Most X display managers (like GDM, XDM, KDM) start an initial X11 server and authenticate to it with an MIT Magic Cookie. Depending on your display manager, the magic cookie will be found in one of various locations.

I've had good luck getting a VNC session open on the Ubuntu GDM login screen *NOTE1 by finding the magic cookie with this script:

#!/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

I can start this script (I called it start_x11vnc.sh) anytime via SSH... even before login via the gdm login screen. It launches an x11vnc server which I can then connect to over SSH tunnel. (Use ssh -L 5900:127.0.0.1:5900 or add LocalForward 5900 127.0.0.1:5900 to your host's entry in ~/.ssh/config).

NOTE1: In some new distro releases such as Ubuntu >= 17.10, the GDM login X session display is completely separate from the logged in user's X session display. Therefore, it is necessary to first connect to the GDM X session, login... and finally disconnect and re-connect to the newly started X session. Why they now do it this way is a mystery, but it broke the old version of this script.

Solution 3

To enable GDM login over a VNC ssh remote connection try with X11vnc. See also this answer.

Share:
36,597

Related videos on Youtube

karthick87
Author by

karthick87

Updated on September 18, 2022

Comments

  • karthick87
    karthick87 over 1 year

    I want to access an Ubuntu machine remotely via VNC. But I can access the Ubuntu machine only if the user is logged in. I want to access the ubuntu machine via VNC from the login screen itself. We can access all windows machines from the login screen. However we were not able to access the Ubuntu machine from the login screen. There is some way but I am not aware of the details. Can someone give a solution to this problem?

    • Robin Green
      Robin Green about 13 years
      If the other machine is on the same LAN it would be feasible to just use XDMCP instead of VNC. If it's not on the same LAN, unaccelerated X over the network might be too slow.
  • karthick87
    karthick87 almost 13 years
    I have installed xrdp but still i am unable to access the ubuntu machine from the login screen.
  • Nathan Osman
    Nathan Osman almost 13 years
    @karthick: Are you connecting to the machine with VNC or using an RDP client?
  • karthick87
    karthick87 almost 13 years
    I dont find RDP client in my machine. Where it will be located ?
  • Nathan Osman
    Nathan Osman almost 13 years
    @karthick: Are you using Ubuntu as your client? If so, the program to use is called "Terminal Server Client". On Windows, it's called "Remote Desktop" IIRC.
  • karthick87
    karthick87 almost 13 years
    Yeah thank you. Is it possible to create a launcher for Terminal Server Client ?
  • Nathan Osman
    Nathan Osman almost 13 years
    @karthick: That would make a great question to ask here :)
  • karthick87
    karthick87 almost 13 years
    Is it possible to create a xrdp launcher ?
  • Nathan Osman
    Nathan Osman almost 13 years
    @kar: xrdp is a daemon.
  • Kris Erickson
    Kris Erickson almost 13 years
    Wow, xrdp is awesome, I had spent hours trying to get xvncserver to work remotely and five seconds after sudo apt-get install xrpd I was at the desktop!
  • karthick87
    karthick87 almost 13 years
    Do you want me to install xrdp in all hosts?
  • karthick87
    karthick87 almost 13 years
    Thankyou i have installed xrdp in all hosts and now i am able to access the ubuntu machine from the login screen.
  • Dims
    Dims over 6 years
    Doesn't work. RDP client shows empty XWindow desktop with cross-like mouse cursor, then closes w/o any error message
  • Dims
    Dims over 6 years
    Does not work. VNC client connects then closes immediately
  • Gokul NC
    Gokul NC over 6 years
    @Dims Use the command: vncviewer <remote_ip>
  • Ben Middleton
    Ben Middleton about 6 years
    script was broken for new versions of Ubuntu... I updated it with a hack to enable it to work again. It seems now gdm display session is now separate from the logged in session, so if you are using this script on Ubuntu, it will first take you to GDM login screen, then when you login this X session display goes blank. Now you need to stop the first x11vnc process and re-run the script again to re-attach to the separate logged in X session & display.
  • mace
    mace about 6 years
    This didn't work.
  • Didier A.
    Didier A. about 6 years
    @mace Interesting, it had worked for me, maybe its dependent on the Ubuntu version. Unfortunately, I don't remember what version my Ubuntu was when I had done this procedure.