1

I'd like to set up VNC server on a Debian machine to run as a service so I can turn on a remote machine and access it without the need to be physically present.

One option is to set it up in the user-bus:

$ systemctl --user cat vnc.service
# /home/stew/.config/systemd/user/vnc.service
[Unit]
Description=VNC Server
After=default.target

[Service]
ExecStart=x11vnc -nevershared -forever -nopw

[Install]
WantedBy=default.target

This works, but still requires the me to physically sit at the terminal to log into gdm3 to get XAUTHORITY before I can do any remote work. If I ssh in before logging in with gdm3, the service fails. I work around this by using AutomaticLoginEnable=True and AutomaticLogin=stew in /etc/gdm3/daemon.conf.

Instead, I'd like to be able to use VNC without the need to log in as a specific user first (similar to RDP). I think the best way to do this is to use -nopw to get to a gdm3 screen.

I tried to do that with:

$ systemctl cat vnc.service
# /etc/systemd/system/vnc.service
[Unit]
Description=VNC Service (system-wide)
After=graphical.target

[Service]
ExecStart=x11vnc -auth /run/user/116/gdm/Xauthority -display :0 -nopw

[Install]
WantedBy=graphical.target

I found the XAUTHORITY path with this command which revealed the path is owned by UID 116 (system user: Debian-gdm).

stew ~ $ ps wwwwaux | grep auth
root        1033  0.1  0.5 189548 63596 tty1     Sl+  14:32   0:00 /usr/lib/xorg/Xorg vt1 -displayfd 3 -auth /run/user/116/gdm/Xauthority -nolisten tcp -background none -noreset -keeptty -novtswitch -verbose 3

I also need to set WaylandEnable=false in /etc/gdm3/daemon.conf because VNC doesn't seem to work with Wayland.

At first, this seems to work well. I get the gdm login screen. But, when I try to log in as a user, the auth gets transferred to another user and I am disconnected.

Is there a way to set up VNC so I can log in via gdm?

3 Answers 3

1

It's not seamless but here's a solution.

We start with a service that runs VNC and connects us to the GDM login screen

$ systemctl cat vnc-gdm.service 
# /etc/systemd/system/vnc-gdm.service
[Unit]
Description=VNC Server (gdm)
After=graphical.target

[Service]
ExecStart=bash -c 'x11vnc -auth /run/user/$(id -u Debian-gdm)/gdm/Xauthority -display :0 -nopw'
Restart=on-failure
RestartSec=3

[Install]
WantedBy=graphical.target

Then for each user we want to support, we add another VNC service. This one will poll every 2 sec in ExecStartPre= to see if this user has an Xorg instance. When that happens, it'll stop vnc-gdm.service to free up port 5900 so its own instance of x11vnc can bind to it.

Then it runs x11vnc.

$ systemctl cat vnc-stew.service
# /etc/systemd/system/vnc-stew.service
[Unit]
Description=VNC Server (stew)
After=graphical.target

[Service]
User=stew
ExecStartPre=sh -c 'while ! pgrep -U stew Xorg; do sleep 2; done'
ExecStartPre=+systemctl stop vnc-gdm.service
ExecStart=x11vnc -many -shared -display :1 -auth /home/stew/.Xauthority -rfbauth /home/stew/.vnc/passwd
Restart=on-failure
RestartSec=3

[Install]
WantedBy=graphical.target

I say it's not seamless because once you login via gdm: your connection gets cut and you need to reconnect to your new display. I also haven't tested this with multiple users. Also, if I log-out of the X-session, I'm pretty sure I won't be able to get back in.

I also don't like how we use systemctl stop in the ExecStartPre=.

0

On Debian 11 (Bullseye) running the Gnome desktop, Stewart's service definition for running x11vnc on the gdm greeter session (Stewart's first-above code block) almost worked for me, but it failed with x11nvc complaining about a failure to access a private resource. I fixed this problem by inserting "sudo -u Debian-gdm", resulting in the line :

    ...
    ExecStart=bash -c 'sudo -u Debian-gdm x11vnc -auth /run/user/$(id -u Debian-gdm)/gdm/Xauthority -display :0 -nopw'
    ...
0

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.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .