0

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?

0

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=.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.