Running VNC on Ubuntu 20.04 with default window manager

I wrote previously about my frustration with trying to get the default Ubuntu desktop to run in a VNC session, one that would install as a service, and work after a reboot.

After spending more time than I'd like to admit, I threw in the towel, and went with the Mate desktop (Gnome 2 based), which worked without a hitch.

It still bugged me that I couldn't figure this out, so when I had a couple of free minutes, I looked further and was finally able to get it working.

The tl;dr is that Ubuntu 20.04 doesn't have a /etc/vnc/xstartup file.

Steps to install VNC on Ubuntu 20.04, using the default window manager

  • Create the /etc/vnc directory:

    sudo mkdir /etc/vnc
    
  • Create the non-existent /etc/vnc/xstartup file, adding these lines:

    #!/bin/sh
    
    test x"$SHELL" = x"" && SHELL=/bin/bash
    test x"$1"     = x"" && set -- default
    
    unset SESSION_MANAGER
    unset DBUS_SESSION_BUS_ADDRESS
    
    vncconfig -iconic &
    "$SHELL" -l << EOF
    export XDG_SESSION_TYPE=x11
    export GNOME_SHELL_SESSION_MODE=ubuntu
    dbus-launch --exit-with-session gnome-session --session=ubuntu
    EOF
    vncserver -kill $DISPLAY
    
  • Make the /etc/vnc/xstartup file executable:

    sudo chmod +x /etc/vnc/xstartup
    
  • Install the TigerVNC server:

    sudo apt install tigervnc-standalone-server -y
    
  • Run VNC server for the first time, which will ask for a (6 character minimum) password and if a read-only password is desired:

    vncserver
    
    You will require a password to access your desktops.
    
    Password:
    Verify:
    Would you like to enter a view-only password (y/n)? n
    
    New 'ubuntu1:1 (tom)' desktop at :1 on machine ubuntu1
    
    Starting applications specified in /etc/X11/Xvnc-session
    Log file is /home/tom/.vnc/ubuntu1:1.log
    
    Use xtigervncviewer -SecurityTypes VncAuth -passwd /home/tom/.vnc/passwd :1 to connect to the VNC server.
    
  • Create/add this to the much-simpler ~/.vnc/xstartup file:

    #!/bin/sh
    [ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
    [ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
    
  • Make the ~/.vnc/xstartup file executable:

    chmod +x ~/.vnc/xstartup
    
  • Create/add this to the /etc/systemd/system/vncserver@.service file:

    [Unit]
    Description=Start TigerVNC server at startup
    After=syslog.target network.target
    
    [Service]
    Type=simple
    User=tom
    PAMName=login
    PIDFile=/home/tom/.vnc/%H:%i.pid
    ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
    ExecStart=/usr/bin/vncserver -fg -depth 24 -geometry 1920x1080 -localhost no :%i
    ExecStop=/usr/bin/vncserver -kill :%i
    [Install]
    WantedBy=multi-user.target
    
  • Reload the daemons, so it can load the new VNC service:

    sudo systemctl daemon-reload
    
  • Enable the VNC service, so it loads on boot:

    sudo systemctl enable vncserver@1.service
    Created symlink /etc/systemd/system/multi-user.target.wants/vncserver@1.service → /etc/systemd/system/vncserver@.service.
    
  • Kill any existing VNC servers:

    vncserver -kill :*
    
  • Start the VNC service:

    sudo systemctl start vncserver@1
    
  • Check on the status of the vncserver service:

    sudo systemctl status vncserver@1
    ● vncserver@1.service - Start TigerVNC server at startup
         Loaded: loaded (/etc/systemd/system/vncserver@.service; enabled; vendor preset: enabled)
         Active: active (running) since Tue 2021-09-14 17:10:18 MDT; 9min ago
        Process: 2575 ExecStartPre=/usr/bin/vncserver -kill :1 > /dev/null 2>&1 (code=exited, status=0/SUCCESS)
       Main PID: 2581 (vncserver)
          Tasks: 0 (limit: 4650)
         Memory: 688.0K
         CGroup: /system.slice/system-vncserver.slice/vncserver@1.service
    2581 /usr/bin/perl /usr/bin/vncserver -fg -depth 24 -geometry 1920x1080 -localhost no :1
    
    Sep 14 17:10:18 ubuntu1 systemd[1]: Starting Start TigerVNC server at startup...
    Sep 14 17:10:18 ubuntu1 systemd[2575]: pam_unix(login:session): session opened for user tom by (uid=0)
    Sep 14 17:10:18 ubuntu1 systemd[1]: Started Start TigerVNC server at startup.
    Sep 14 17:10:18 ubuntu1 systemd[2581]: pam_unix(login:session): session opened for user tom by (uid=0)
    

At this point, I could connect to the VNC server, and it still worked, even after a reboot.

Screenshot of VNC connection using Jump Desktop application

References

Gnome-shell > Issues > #3038 https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/3038