Sometimes (but not always) this doesn't work right after you reboot though been testing it and if you set -display :0 instead of -display :1, it seems to create 1 root and 2 users and then just works. Thought you needed to use -display :1 in Ubuntu 22.04 but -display :0 actually works and -display :1 does not seem to do so. The two users that show up are the same exact user that you presumably logged in with (user 1000) with different CGroup numbers. No idea why but then it just works. Still trying to get the ability to connect before you have logged in as well but not likely to be able to easily do so and have a workaround in place at least.
Using Steve's answer, I got it almost there for me using Ubuntu 22.04. Of note, I have tried this when you have not logged in yet and does not currently work. I do know that requires a different user and there are a few workarounds possible. The below only works if you are already logged in.
This, for me at least, makes x11vnc actually work with x11 (not Wayland) on Ubuntu 22.04.
Already be using SSH - Port 5900 here
sudo apt-get install x11vnc net-tools
Set a fairly low quality overall password for x11vnc
x11vnc -storepasswd
turn off wayland
cd /etc/gdm3
nano custom.conf
Uncomment Waylandnable=false
sudo nano /etc/systemd/system/x11vnc.service
to make the below file (which needs you to enter your own username where PUTLOGGEDINUSERNAMEHERE
is currently there twice for it to work).
[Unit]
Description=Start x11vnc at startup on Ubuntu 22.04 and x11 not Wayland.
After=multi-user.target
[Service]
Type=simple
ExecStart=bash -c 'sudo -u PUTLOGGEDINUSERNAMEHERE /usr/bin/x11vnc -display :0 -auth /run/user/1000/gdm/Xauthority -rfbauth /home/PUTLOGGEDINUSERNAMEHERETOO/.vnc/passwd -rfbport 5900'
ExecStop=/usr/bin/killall x11vnc
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
Then
sudo systemctl daemon-reload
systemctl start x11vnc.service
or if it is already running
sudo systemctl daemon-reload
systemctl restart x11vnc.service
then you should be able to actually connect to port 22 through SSH and actually connect to x11vnc.
Absolutely no idea why -display :0 seems to work most of the time now and -display :1 does not but would suggest -display :0 and the Ubuntu desktop and it seems to always startup. You will unfortunately need to automatically log in though you can always immediately lock the screen and the x11vnc will continue to work.
Can also check status with
systemctl status x11vnc.service
Can check the last 100 records
journalctl -u x11vnc.service -n 100
Not entirely clear why it throws
ubuntuuser x11vnc[34174]: Libgcrypt warning: missing initialization - please fix the application
But at least the entire x11vnc actually worked with Ubuntu 22.04 and it took close to a day to sort this out. Wanted to share in case anyone else is having this issue and not sure if there is anything we are not seeing here specifically either.
Note that user gdm is now user 126. Why? No idea but it's not 120 as shown in the above link.
Looking forward to try to solve why it doesn't let you actually use it before login seems to be in part because you have to run a command as root but you have to need to do so with /bin/sh -c sudo 'actual command' for ExecStart.
Plus, more importantly, the default user shows up as 1000 and in place but gdm does not which might be why it is not working to let you assign that user when you try to log in?
sudo ls -la /run/user/126/gdm
Nothing there.
sudo ls - la /run/user/1000/gdm
Shows the user at 1000 and Xauthority but nothing comes up for gdm.
Starting to get past my current understanding but doesn't seem like how it was for prior versions of Ubuntu where the gdm user would be assigned 100 or 106 or 120 which is the standards for the last few Ubuntu releases. Maybe that is relevant for why you cannot setup this to work before logging in because it has been doable before.
Sometimes, you can start the x11vnc.service and then edit the /etc/systemd/systemx11vnc.service file to change it from display :0 to display :1 and then it adds a second (technically third because you started with sudo -u) user and then it suddenly works. It presumably should be using display :1 always anyway and no idea why it is adding a second user with a new CGroup but that almost always seems to make it just work when you restart the x11vnc.service.
To clarify, using Ubuntu 22.04 which did seem to change several things with x11vnc, so please keep that in mind at least. Noticed several changes over the past few releases which are specifically problematic for x11vnc and a fair bit of code only works with this specific or a nearby release.
Lastly, also get a X cursor but only on the Ubuntu desktop and the top of windows. Move it over a terminal and it turns into a normal mouse cursor just with a dot on it.
Here is a bit more code showing it actually running if that is helpful. If you see it showing one non sudo version it does not run and just loops. If you see two, it is running and lets you connect.
x11vnc.service - Start x11vnc at startup.
Loaded: loaded (/etc/systemd/system/x11vnc.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2022-06-01 19:30:40 EDT; 2s ago
Main PID: 2734 (sudo)
Tasks: 3 (limit: 9354)
Memory: 9.1M
CPU: 88ms
CGroup: /system.slice/x11vnc.service
\u251c\u25002734 sudo -u ubuntuuser/usr/bin/x11vnc -noxrecord -noxfixes -auth /run/user/1000/gdm/Xauthority -forever -loop >
\u251c\u25002735 /usr/bin/x11vnc -noxrecord -noxfixes -auth /run/user/1000/gdm/Xauthority -forever -loop -noxdamage -rfbaut>
\u2514\u25002736 /usr/bin/x11vnc -noxrecord -noxfixes -auth /run/user/1000/gdm/Xauthority -forever -loop -noxdamage -rfbaut>
Jun 01 19:30:41 ubuntuuser bash[2736]: The VNC desktop is: ubuntuuser:5900
Jun 01 19:30:41 ubuntuuser bash[2736]: 01/06/2022 19:30:41 possible alias: ubuntuuser::5900
Jun 01 19:30:41 ubuntuuser bash[2736]: PORT=5900
Jun 01 19:30:41 ubuntuuser bash[2736]: ******************************************************************************
Sometimes though not completely sure how to best reproduce it, you try to restart x11vnc.service while you are connected, it can just hang the terminal and stops responding. It then drops the sudo and one of the two CGroup users when you exit but then one of them stays and the x11vnc.service goes from green to white. Restarting the service restores the 3 in total users and you can just connect again. Also, strangely, if you set the -display :0 setting, it often times still works. Was under the impression it needed to be -display :1 but who knows? More testing seems to indicate that -display :0 seems more likely to make it just work on bootup.
Tried adding a second version of the x11vncA.service to run on startup and open a different port or a different display but it seems to consistently fail and go into Loaded: loaded mode but then inactive. Despite the other one working. Tried a host of different points at which it should start (graphical.target instead of multi-user.target) but left it at multi-user.target because that works for some reason. Why that starts this up? Guessing it has something to do if it starts at -display :0 and then somehow needs to run at -display :1 then it winds up still starting up anyway though not sure that is quite how it is supposed to be working. Just want some kind of Gnome based :1 desktop that works!
Here are some potential starting points to setup something similar on login though that does not currently work but at least you can now use x11vnc with Ubuntu 22.04 at least.
See also Ubuntu 18.04 LTS x11vnc no longer works.
Create x11vnc.service as it seems to be all you need to make this work assuming you automatically login though you do seem to need to try something else to work before you have started the GUI and actual desktop.