x11vnc as a service

working one




1. apt install x11vnc

2. x11vnc -storepasswd 

                Create a file x11vnc.sh


x11vnc -display :0 -forever -loop -noxdamage -repeat -rfbauth /home/rasitha/.vnc/passwd -rfbport 5916 -shared







make it executable & run at startup





3. sudo nano /usr/bin/startx11vnc 

       

4. #!/bin/sh


# Restart the x11vnc service

restartx11vnc () {

    /usr/bin/systemctl restart x11vnc

}


# Log a message to the system log

logtosyslog () {

    /usr/bin/logger -t startx11vnc $1

}


# Log startup

logtosyslog "x11vnc starting"


# Start up x11vnc in the background. If it exits (-loop exits) then we'll try to recover by restarting.

# The & at the end lets the first line run in the background. Restarting the service will kill it. Commands are

# chained with  ;  because we want all the commands to be executed if x11vnc exitsno matter what the return status.

# Putting the commands in the next 3 lines between parentheses allows the & at the end run all the commands in a

# subshell as a group which also allows the second part of the script to go ahead and run.

( /usr/bin/x11vnc -auth /var/run/sddm/* -loop -forever -repeat -shared -display :0 -rfbauth /home/rasitha/.vnc/passwd -rfbport 5900 -oa /var/log/x11vnc.log ; \

logtosyslog "x11vnc exited, restarting" ; \

restartx11vnc ) &


# A second thread is started because of the & at the end of the last line and this thread will wait for a new file

# to be created in /var/run/sddm. This indicates that a user has logged off and a new Xauthority has been generated

/usr/bin/inotifywait -qq -e create /var/run/sddm


# Wait to make sure the new Xauthority is fully set up

/usr/bin/sleep 2


# Log the change

logtosyslog "The Xauthority file changed, restartting x11vnc"


# Restart the x11vnc service so that the new Xauthority is used

restartx11vnc


5. sudo apt install inotify-tools

6. sudo nano /etc/systemd/system/x11vnc.service 

[Unit]

Description=Run x11vnc at startup

After=multi-user.target


[Service]

#If you start x11vnc this way, every time a user logs off he won’t be able to log back in until the service is restarted 

#ExecStart=/bin/bash -c "/usr/bin/x11vnc -auth /var/run/sddm/* -forever -loop -repeat -noxdamage -shared -display :0 -rfbauth /etc/x11vnc.pass -rfbport 5900 -o /var/log/x11vnc.log"

ExecStart=/bin/bash /usr/bin/startx11vnc


Restart=on-failure

RestartSec=10


[Install]

WantedBy=multi-user.target


7. sudo systemctl enable x11vnc.service

8. sudo systemctl daemon-reload

9. reboot

Comments