[Ubuntu 20.04] How to fix “No protocol specified .. Connection refused” error while running GUI apps on a VNC connection

Running GUI apps on a VNC connection usually requires the presence the presence of an .Xauthority file in the user’s home folder (example: /home/username/). If this file is not present/not configured correctly, we get errors like these.

error
error

If this file is present someone where else on the system (e.g. on some other user’s home folder) and you have access to that location, you can fix it by just copying it to your home folder.

Fixed
Fixed

If this file is missing, then you can generate it with these steps (source: Ton’s answer on Superuser.com)

cd
touch ~/.Xauthority
xauth generate :1 . trusted 
xauth add ${HOST}:1 . $(xxd -l 16 -p /dev/urandom)

Replace :1 with the name of the display created by your VNC server. Usually the $DISPLAY environment should have the display name, but sometimes that might not be the case. You can check it with

echo $DISPLAY

Display names should usually be in the $HOST:$DISPLAY format but in some cases it can also be something like $HOST/unix:$DISPLAY