Connecting to Ubuntu 20.04 via VNC

So I needed a way to connect to a Linux VM via VNC.

There is a lot of information out there on how to do it, but most of it talks about using less resource intensive desktop (xfce4).

I wanted the full desktop experience, and as both my Linux box and my full-time desktop are both connected to a 1GB switch, I wasn't worried about latency or bandwidth. Also, I'd allocated enough resources to my Linux VM, CPU/memory weren't a concern.

Well, it was a hell of a lot more difficult than you'd think.

Turns out there is a bug in the Ubuntu Gnome desktop (forgot to save the link that describes the bug) that prevents VNC server from working correctly with the Gnome3-based window manager.

I was able to get the Ubuntu Gnome desktop to work in a VNC server, but I wasn't able to get it to work as a service, one that would work after reboot.

I finally tried Mate, which is based off of Gnome2, compared to the default Ubuntu Gnome desktop, which is based off of Gnome3.

It worked right away, and following the if it ain't broke, don't fix it train of thought, I'm sticking with Mate.

Steps to install & configure VNC on Ubuntu 20.04

These steps assuming you either installed Ubuntu Mate, or at least have the Mate desktop installed.

  • 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.
    
  • Try to connect via VNC (I use Jump Desktop), fails.

    By default, TigerVNC only listens on 127.0.0.1/localhost, so VNC connection attempts from outside this box will fail.

    This can been seen by looking at the output of netstat -an:

    netstat -an | grep 5901
    tcp        0      0 127.0.0.1:5901          0.0.0.0:*               LISTEN
    tcp6       0      0 ::1:5901                :::*                    LISTEN
    
  • Kill the existing VNC server:

    vncserver -kill :*
    
  • Now relaunch, but specifying the -localhost no option, so VNC listens on any interface:

    vncserver -localhost no
    
    netstat -an | grep 5901
    tcp        0      0 0.0.0.0:5901            0.0.0.0:*               LISTEN
    tcp6       0      0 :::5901                 :::*                    LISTEN
    
  • At this point, I could connect to the VNC server from another computer.

Add VNC as a system service

  • To add the VNC as a service, need to create /etc/systemd/system/vncserver@.service:

    [Unit]
    Description=Start TigerVNC server at startup
    After=syslog.target network.target
    
    [Service]
    Type=simple
    User=tom
    Group=tom
    WorkingDirectory=/home/tom/.vnc
    PIDFile=/home/tom/.vnc/%H:%i.pid
    ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
    ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 -localhost :%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 server, 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 the status of the VNC 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 11:35:40 MDT; 4s ago
        Process: 9097 ExecStartPre=/usr/bin/vncserver -kill :1 > /dev/null 2>&1 (code=exited, status=0/SUCCESS)
        Process: 9102 ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 -localhost :1 (code=exited, status=0/SUCCESS)
       Main PID: 9110 (Xtigervnc)
          Tasks: 176 (limit: 4610)
         Memory: 269.6M
         CGroup: /system.slice/system-vncserver.slice/vncserver@1.service
                 ├─9110 /usr/bin/Xtigervnc :1 -desktop ubuntu1:1 (tom) -auth /home/tom/.Xauthority -geometry 1280x800 -depth 24 -rfbwait 30000 -rfbauth /home/t>
                 ├─9124 /usr/bin/mate-session
                 ├─9127 dbus-launch --exit-with-session /usr/bin/mate-session
                 ├─9128 /usr/bin/dbus-daemon --syslog --fork --print-pid 5 --print-address 7 --session
                 ├─9138 /usr/libexec/at-spi-bus-launcher
                 ├─9142 /usr/libexec/gvfsd
                 ├─9147 /usr/libexec/gvfsd-fuse /home/tom/.cache/gvfs -f -o big_writes
                 ├─9156 /usr/bin/dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf --nofork --print-address 3
                 ├─9181 /usr/libexec/dconf-service
                 ├─9189 gnome-keyring-daemon --start
                 ├─9191 /usr/bin/mate-settings-daemon
                 ├─9193 /usr/libexec/at-spi2-registryd --use-gnome-session
                 ├─9209 marco
                 ├─9225 mate-panel
                 ├─9235 /usr/libexec/gvfs-udisks2-volume-monitor
                 ├─9241 /usr/libexec/gvfs-afc-volume-monitor
                 ├─9246 /usr/bin/caja
                 ├─9248 /usr/libexec/gvfs-goa-volume-monitor
                 ├─9259 /usr/lib/mate-panel/wnck-applet
                 ├─9262 /usr/libexec/gvfs-gphoto2-volume-monitor
                 ├─9269 /usr/lib/mate-indicator-applet/mate-indicator-applet-complete
                 ├─9270 /usr/lib/mate-panel/notification-area-applet
                 ├─9271 /usr/lib/mate-applets/trashapplet
                 ├─9272 /usr/lib/x86_64-linux-gnu/brisk-menu//brisk-menu
                 ├─9278 /usr/lib/x86_64-linux-gnu/indicator-sound/indicator-sound-service
                 ├─9282 /usr/bin/python3 /usr/share/system-config-printer/applet.py
                 ├─9285 /usr/libexec/gvfs-mtp-volume-monitor
                 ├─9319 /usr/lib/x86_64-linux-gnu/indicator-messages/indicator-messages-service
                 ├─9320 /usr/bin/spice-vdagent
                 ├─9336 /usr/lib/x86_64-linux-gnu/indicator-datetime/indicator-datetime-service
                 ├─9364 /usr/lib/x86_64-linux-gnu/indicator-power/indicator-power-service
                 ├─9372 update-notifier
                 ├─9375 /usr/lib/x86_64-linux-gnu/indicator-application/indicator-application-service
                 ├─9380 mate-maximus
                 ├─9383 /usr/libexec/gvfsd-trash --spawner :1.2 /org/gtk/gvfs/exec_spaw/0
                 ├─9397 /usr/libexec/gvfsd-metadata
                 ├─9398 nm-applet
                 ├─9416 /usr/lib/x86_64-linux-gnu/indicator-session/indicator-session-service
                 ├─9434 /usr/bin/mate-screensaver --no-daemon
                 └─9470 /usr/bin/python3 /usr/lib/update-notifier/apt-check
    

Verified everything worked by rebooting, and I was able to connect to the VNC server with no issues.

References

How to Install & Configure VNC Server on Ubuntu 20.04 https://bytexd.com/how-to-install-configure-vnc-server-on-ubuntu-20-04/#configuring-vnc-to-use-other-desktop-environments