You are not logged in.

#1 2013-08-24 12:04:54

Markus.N2
Member
From: Germany
Registered: 2013-08-22
Posts: 99

how to enable x11vnc at startup using systemd ?

Hi everyone,

This is what i tried:

  • copy /usr/lib/systemd/system/x11vnc.service to /etc/systemd/system/x11vnc.service

  • edit /etc/systemd/system/x11vnc.service, added the Install section and changed ExecStart

    [Unit]
    Description=VNC Server for X11
    Requires=graphical.target
    After=graphical.target
    
    [Service]
    Type=forking
    ExecStart=/usr/bin/x11vnc -rfbauth /etc/x11vnc.pass -rfbport 5900 -display :0 -auth /var/run/lxdm/lxdm-\:0.auth \
     -forever -bg -o /var/log/x11vnc.log -xkb -noxrecord -noxfixes -noxdamage -nomodtweak
    
    [Install]
    WantedBy=multi-user.target
  • systemctl daemon-reload

  • systemctl enable x11vnc

But after reboot, x11vnc was not started automatically, while starting the x11vnc service manually afterwards worked. It turned out I had produced a dependency cycle.

root@archlinux:~# journalctl --this-boot | fgrep cycle
Aug 24 13:09:21 archlinux systemd[1]: Found ordering cycle on graphical.target/start
Aug 24 13:09:21 archlinux systemd[1]: Walked on cycle path to multi-user.target/start
Aug 24 13:09:21 archlinux systemd[1]: Walked on cycle path to x11vnc.service/start
Aug 24 13:09:21 archlinux systemd[1]: Walked on cycle path to graphical.target/start
Aug 24 13:09:21 archlinux systemd[1]: Breaking ordering cycle by deleting job x11vnc.service/start
Aug 24 13:09:21 archlinux systemd[1]: Job x11vnc.service/start deleted to break ordering cycle starting with graphical.target/start

But I am unsure how I can resolve the cycle. Here are the other units envolved in the cycle (both unchanged):

/usr/lib/systemd/system/multi-user.target:

[Unit]
Description=Multi-User System
Documentation=man:systemd.special(7)
Requires=basic.target
Conflicts=rescue.service rescue.target
After=basic.target rescue.service rescue.target
AllowIsolate=yes

[Install]
Alias=default.target

/usr/lib/systemd/system/graphical.target:

Description=Graphical Interface
Documentation=man:systemd.special(7)
Requires=multi-user.target
After=multi-user.target
Conflicts=rescue.target
Wants=display-manager.service
AllowIsolate=yes

[Install]
Alias=default.target

My intention was to start x11vnc.service after graphical.target is reached. How can I achieve this ?

Altering my Install section to

[Install]
Alias=default.target

screws up the system and leads me to an emergency shell at next boot because /etc/systemd/system/default.target was symlinked to /etc/systemd/system/x11vnc.service by systemctl enable x11vnc.

Any ideas / suggestins ?
Thanx

Last edited by Markus.N2 (2013-08-24 16:15:48)

Offline

#2 2013-12-30 12:54:00

ArgelErx
Member
Registered: 2013-12-30
Posts: 13

Re: how to enable x11vnc at startup using systemd ?

Markus.N2 wrote:

[...]
My intention was to start x11vnc.service after graphical.target is reached. How can I achieve this ?
[...]
Any ideas / suggestins ?

This is what I did (systemd 208):

1. sudo cp /usr/lib/systemd/system/x11vnc.service /etc/systemd/system/

[Unit]
Description=VNC Server for X11
Requires=display-manager.service
After=display-manager.service

[Service]
Type=forking
ExecStart=/usr/bin/x11vnc -norc -forever -shared -bg -rfbauth /etc/x11vnc.pass -allow 192.168.1. -autoport 5900 -o /var/log/x11vnc.log

2. sudo cp /usr/lib/systemd/system/graphical.target /etc/systemd/system/

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Graphical Interface
Documentation=man:systemd.special(7)
Requires=multi-user.target
After=multi-user.target
Conflicts=rescue.target
Wants=display-manager.service
Wants=x11vnc.service
AllowIsolate=yes

[Install]
Alias=default.target

3. sudo systemctl enable graphical.target

Will create symlink /etc/systemd/system/default.target -> /etc/systemd/system/graphical.target

4. Reboot

Offline

#3 2013-12-30 20:17:28

mcloaked
Member
From: Yorkshire, UK
Registered: 2012-02-02
Posts: 1,273

Re: how to enable x11vnc at startup using systemd ?

I guess something analogous can be done with tigervnc?


Mike C

Offline

#4 2014-02-03 15:01:33

Markus.N2
Member
From: Germany
Registered: 2013-08-22
Posts: 99

Re: how to enable x11vnc at startup using systemd ?

@ArgelErx
Thanx, it worked this way. My Requires= and After= values were wrong.

@mcloaked
I never used tigervnc, but as long as it can be started from a terminal using command line parameters, it should be possible to use the same configuration, just replace the ExecStart= value in the x11vnc.service example above.
Maybe it is a good idea to name the file tigervnc.service (and adapt the Wants= value in /etc/systemd/system/graphical.target).

On the other hand, the dependency to display-manager.service is only necessary if tigervnc shares a main property of x11vnc: it allows to connect to an existing x11 session. Most other VNC servers don't do this. Instead, they open a new x11 session that can only be accessed via network (no local display).

Last edited by Markus.N2 (2014-02-03 15:08:02)

Offline

#5 2014-02-03 15:09:11

Gusar
Member
Registered: 2009-08-25
Posts: 3,605

Re: how to enable x11vnc at startup using systemd ?

Tigervnc can export an existing x11 session. It's in the wiki, marked "dirty hack" for who knows what reason, it's not a hack, it's part of Tigervnc's functionality: https://wiki.archlinux.org/index.php/Ti … mmended.29

Offline

#6 2014-03-05 12:10:10

Markus.N2
Member
From: Germany
Registered: 2013-08-22
Posts: 99

Re: how to enable x11vnc at startup using systemd ?

Looks like the above service file is not the complete solution.

It only works if a client connects within 5 minutes after starting the service. Otherwise the x11vnc process gets killed. In /var/log/x11vnc.log, it says it caught signal 15.
When I test exactly the same command from ExecStart in a command line, it works, even when the first client connection is later than within 5 minutes. So, there must be something in systemd that mistakes the processes' state and kills it.

I don't understand this. I have set Type=forking and I added the -bg (background) parameter to x11vnc. I thought this shoould be it.

Another testcase:
When I start the service from command line (systemd start x11vnc), the command hangs until a clinet connects or the timeout occurs. How can systemd determine the state of x11vnc ? The process is there.

Changing the type to "oneshot" also did not help.

Offline

#7 2014-05-13 14:17:24

vinadoros
Member
Registered: 2014-03-12
Posts: 9

Re: how to enable x11vnc at startup using systemd ?

I believe I may have a solution to the issue in this thread. I have changed the vnc service slightly, which allows systemd to adequately deal with x11vnc if it dies unexpectedly. Here is my vnc.service file:

[Unit]
Description=VNC Server for X11
Requires=display-manager.service
After=display-manager.service

[Service]
Type=forking
ExecStart=/usr/bin/x11vnc -xkb -auth /var/run/lightdm/root/:0 -noxrecord -noxfixes -noxdamage -rfbauth /etc/x11vnc.pass -forever -bg -rfbport 5900 -o /var/log/x11vnc.log
Restart=on-failure
RestartSec=10

The same graphical target service as above was used. This also assumes you are using lightdm as the display manager. I had an issue when I log off and log on, x11vnc no longer worked. By using this code, if x11vnc fails, it will restart it after 10 seconds.

Offline

#8 2014-05-29 14:03:53

hifi25nl
Member
Registered: 2011-05-07
Posts: 263

Re: how to enable x11vnc at startup using systemd ?

I have lost some hours on this. I think it is not a good idea to start x11vnc as root. This x11vnc@.service

[Unit]
Description=VNC Server for X11
Requires=display-manager.service
After=display-manager.service

[Service]
Type=forking
ExecStart=/usr/bin/x11vnc -rfbauth /home/user/.vnc/passwd -forever -shared -bg -display :0
Restart=on-failure
RestartSec=10
User=%I

[Install]
WantedBy=multi-user.target

can be started with

systemctl start x11vnc@user.service

where you can change "user" to your user.
and it will never fail (maybe the lines about restart can be omitted).

Offline

#9 2014-06-03 11:33:59

vinadoros
Member
Registered: 2014-03-12
Posts: 9

Re: how to enable x11vnc at startup using systemd ?

hifi25nl wrote:

I have lost some hours on this. I think it is not a good idea to start x11vnc as root. This x11vnc@.service

First off, thanks for letting us know about this method, and for spending the time on it. However, I have noticed one drawback of this during my testing, which is that x11vnc will not work at the login screen with the display manager this way. It will work fine after logging in. This is most likely the reason we have all been getting this to work as root, since that method does reliably allow us to login using x11vnc, and use it as the logged in user as well.

Unfortunately, doing a "systemctl start x11vnc@root" seems to cause the same problems as mentioned earlier in this thread. I am assuming the drawback above isn't important though, since in your use case it seems like running without root might be more important...

Offline

#10 2014-06-03 15:11:45

mcloaked
Member
From: Yorkshire, UK
Registered: 2012-02-02
Posts: 1,273

Re: how to enable x11vnc at startup using systemd ?

If you are not set completely on using x11vnc but want vnc to run as a server so that you can remotely access a machine even before login, then one way which I used to use regularly is to start the vncserver from the tigervnc package by adding a section to the xorg.conf file. This needs tigervnc to be installed on both local and remote machine.  It is a while since I have used this method but when I did so I used to put something like the following in the xorg.conf file:

Section "Module"
        Load  "vnc"
EndSection

and later in the file:

Section "Screen"
        Identifier "Screen0"
        Device     "Videocard0"
        DefaultDepth     24
        SubSection "Display"
                Viewport   0 0
                Depth     24
                Option "SecurityTypes" "VncAuth"
                Option "UserPasswdVerifier" "VncAuth"
                Option      "passwordFile" "/opt/Local/etc/vnc/passwd"
        EndSubSection
EndSection

If you already have an xorg.conf file then you only need to add the load "vnc " and Option  "passwordFile" to the existing xorg.conf. If not, then you can create a minimal xorg.conf to get the vnc module loaded (xorg module not kernel module just to be clear)

Then use the vncpasswd command to create a password file, and have this accessible at /opt/Local/etc/passwd or whathever path and name for the file is suitable for your machine.

Then reboot the computer and the vnc module should be running on the "remote" machine. Check the org log to see if that is the case.

The most secure way to then access the machine remotely is to ssh to the machine from the client in a first terminal window, port forwarding say local port 55900 to the default remote port at 5900 which is the default port on which the vncserver is listening on the remote end.

After making sure you have a copy of the password file on the local client machine that wants access to the remote machine, then in a second terminal window run the usual vnc command "vncviewer --passwd path/to/password-file localhost:55900" to access the remote machine using the local port in this case at 55900 which will tunnel the vnc traffic down the ssh tunnel. 

If this is set up correctly then even from a remote reboot if the remote machine has booted up and has a graphical login screen awaiting the username and password, making the vnc connection as above should allow you to "see" the remote machine screen on the local client, and if the window focus is in this vnc window you should be able to login and use the remote machine in the same way as if you were accessing the remote machine with its mouse and keyboard. If the remote machine is already running a user session then you should see a copy of the remote screen and have mouse and keyboard control remotely.

I ran this technique for some years using different flavours of linux including arch linux but I have not needed to use it for about 9 months now so I have not existing setup with a current arch linux to check that this still works, but I would be surprised if this does not work with arch with up to date packages.

One important note though is that if anyone has the address of the machine and the vnc password file then essentially they can log in and do what they like at the other end within the privileges of the logged in user at the remote machine - so care is needed to ensure that nobody has access to the password file that you would not trust making the connection! Of course also once the vnc server is running at the remote end then the machine is continuously accessible with the above method since it is running without reference to a specific user login.

I hope this helps.

Last edited by mcloaked (2014-06-03 15:21:18)


Mike C

Offline

#11 2014-06-03 16:53:52

Gusar
Member
Registered: 2009-08-25
Posts: 3,605

Re: how to enable x11vnc at startup using systemd ?

mcloaked wrote:

This needs tigervnc to be installed on both local and remote machine.

Nope, only the server (the machine you'll be connecting to) needs tigervnc. You can use any vnc client you want on the local machine. And you can use a simpler conf file, I have this as /etc/X11/xorg.conf.d/40-vnc.conf

Section "Module"
	Load		"vnc"
EndSection

Section "Screen"
	Identifier	"Default Screen"
	Option		"PasswordFile"		"/root/.vnc/passwd"
EndSection

This is described at the link I posted earlier in this thread.

mcloaked wrote:

One important note though is that if anyone has the address of the machine and the vnc password file then essentially they can log in and do what they like at the other end within the privileges of the logged in user at the remote machine

A very simple way to fix this is to add this to the Screen section in the above config

	Option		"localhost"		"true"

With this option the server will only listen on localhost, and if you want to connect remotely you need to use a ssh tunnel. Not opening the vnc port (5900) in the firewall is also a good security measure, that way a ssh tunnel is again the only way to reach the vnc server.

Last edited by Gusar (2014-06-03 18:16:16)

Offline

#12 2014-06-03 17:33:47

mcloaked
Member
From: Yorkshire, UK
Registered: 2012-02-02
Posts: 1,273

Re: how to enable x11vnc at startup using systemd ?

Gusar wrote:
mcloaked wrote:

This needs tigervnc to be installed on both local and remote machine.

Nope, only the server (the machine you'll be connecting to) needs tigervnc. You can use any vnc client you want on the local machine.

Yes you are right - any vnc client can be used to connect - the primary consideration is setting up the server correctly. Thanks too for the additional useful points. Hope the OP will be able to set up his/her machines to enable the facilities that were asked in the first post in this thread.


Mike C

Offline

#13 2022-02-16 16:30:07

PenguinJeff
Member
Registered: 2022-02-16
Posts: 1

Re: how to enable x11vnc at startup using systemd ?

My work around is to create a simple script that sleeps for 30 seconds the runs /usr/bin/systemctl start x11vnc then put it in crontab.
cat /root/delayed_x11vnc.sh

sleep 30
/usr/bin/systemctl start x11vnc

crontab -l

# m h dom m dow program
@reboot /root/delayed_x11vnc.sh 2>/tmp/x11vnc.cron.err >/tmp/x11vnc.cron.out

Offline

#14 2022-02-17 16:05:21

2ManyDogs
Forum Fellow
Registered: 2012-01-15
Posts: 4,645

Re: how to enable x11vnc at startup using systemd ?

Thanks for the contribution, but please pay attention to the dates on threads and do not necrobump.

Closing.

Offline

Board footer

Powered by FluxBB