# How to create a vnc-server service and how to enable it when the system start



## ziomario (Sep 17, 2021)

Hello to everyone.

I'm trying to create a vnc-server service that should be enabled when the system start. I've created the script below that I've placed inside the folder:  /usr/local/etc/rc.d


```
#!/bin/sh

# PROVIDE: vnc-server
#
# Add the following line to /etc/rc.conf[.local] to enable vnc-server.
#
# vnc-server_enable (bool):       Set to 'YES' to enable
#                               Default: NO
#
# vnc-server_flags (str):         Custom additional arguments to be passed to vnc-server.
#                                              For example, to make it listen on TCP ports, set it
#                                              to "--listen".
#                                              Default: ""
#

. /etc/rc.subr

name=vnc-server
rcvar=vnc-server_enable

load_rc_config $name

: ${vnc-server_enable:="YES"}
: ${vnc-server_flags:="-geometry"}

command=/usr/local/bin/vncserver
pidfile=/var/run/${name}.pid

command_args="--daemon --pid-file=${pidfile}"

PATH="${PATH}:/usr/local/bin"

run_rc_command "$1"
```


and inside the file /etc/rc.conf , I have included :


```
vnc-server="YES"
vnc-server_flags="1920x1080"
```

unfortunately it does not work. On the boot messages I see these errors :


```
/etc/rc.conf: vnc-server=YES: not found
/etc/rc.conf: vnc-server_flag=1920x1080: not found
/etc/rc.conf : WARNING: $vnc-server_enable is not set properly
```

can someone help me to understand where is the error ?


----------



## eternal_noob (Sep 17, 2021)

ziomario said:


> vnc-server="YES"


This is wrong. See https://sosheskaz.github.io/tutorial/2017/03/28/FreeBSD-rcd-Setup.html


----------



## ziomario (Sep 17, 2021)

good tutorial. I can see it at a first sight.


----------



## ziomario (Sep 19, 2021)

I've created the vnc_server service that I've placed inside the folder /usr/local/etc/rc.d

. /etc/rc.subr
name=vnc_server
rcvar="${name}_enable"
load_rc_config $name
: ${vnc_server_enable:=""}
: ${vnc_server_flags:=""}
command=/usr/local/bin/vncserver
pidfile=/home/${USER}/.vnc/${name}.pid
PATH="${PATH}:/usr/local/bin"
run_rc_command "$1"

it seems that it works when I start the service :


root@marietto:/usr/local/etc/rc.d # service vnc_server start

Starting vnc_server.
Warning: marietto:1 is taken because of /tmp/.X1-lock
Remove this file if there is no X server marietto:1 xauth: (stdin):1:  bad display name "marietto:2" in "add" command
New 'marietto:2 ()' desktop is marietto:2
Starting applications specified in //.vnc/xstartup
Log file is //.vnc/marietto:2.log

when I restart the system,from the boot messages I see that the vncserver is running,but after that KDE is ready to go,I don't see the vnc server on the list of the running process anymore. This is what I've added to rc.conf :

vnc_server_enable=YES
vnc_server_flags="-geometry 1920x1080"


----------



## Tieks (Sep 19, 2021)

ziomario said:


> but when I restart the system,the vncserver is not running.


I see you sourced /etc/rc.subr, so a restart is basically a stop/start sequence. What exactly does it say when you enter `service vnc_server restart`? And your .pid file, is it indeed there and does it contain the correct process id?


----------



## ziomario (Sep 19, 2021)

When I restart the system,from the boot messages I see that the vncserver is running,but after that KDE has been fully loaded,I don't see the vnc server on the list of the running processes anymore,giving the classic :


```
ps ax | grep vnc
```

I've also used another method. I've added this script inside the automatic applications and scripts tab included in KDE as a startup script :


```
sleep 5
/usr/local/bin/vncserver -geometry 1920x1080
```

and I've rebooted again. And again,when KDE is ready,I do : *ps ax | grep vnc* (and I have also checked the running processes one by one),I don't see the vncserver running between the processes.


----------



## Tieks (Sep 19, 2021)

ziomario said:


> from the boot messages I see that the vncserver is running,but after that KDE has been fully loaded,I don't see the vnc server on the list of the running processes anymore


It does start indeed, message 'Starting vnc_server.' comes from line 1109 in /etc/rc.subr. If you don't see vnc in `ps -ax` after that, vnc just stops or dumps. You will have to look into that first.
Btw, you don't need to reboot after changing /usr/local/etc/rc.d/vnc_server. Command `service vnc_server start/stop/restart/status` will pick it up immediately.


----------



## ziomario (Sep 19, 2021)

vncserver is giving to me a lot of problems. KDE is not able to load correctly. Sometimes it freezes as soon it starts,some others it won't start at all,telling that there is a timeout during the loading of the .Xauthority file. If I remove all the .Xauthorities files from the folder /home/user,everything come back to work as it should (except for vncserver)

I have removed the vncserver process from the KDE tab and I've also removed these lines from the rc.conf :

vnc_server_enable=YES
vnc_server_flags="-geometry 1920x1080"

these methods aren't the best ones. There should be a different and better method to lets the vncserver restarts at each reboot without problems.


----------



## Emrion (Sep 19, 2021)

I don't know what you're trying to achieve but if you just want a vnc server which allows to remotely see your KDE session, give a try to net/x11vnc.


----------



## ziomario (Sep 19, 2021)

I want to achieve an easy goal : when I restart FreeBSD I want to see that the vncserver is running and when I try to connect to  it using the viewer,I would like to see that a desktop manager like xfce4 can be loaded. And for sure,I don't want that when I restart the system,it won't load KDE because it goes in timeout trying to read the .Xauthority file. Can u post here your xstartup file ? thanks.


----------



## ziomario (Sep 19, 2021)

Emrion said:


> I don't know what you're trying to achieve but if you just want a vnc server which allows to remotely see your KDE session, give a try to net/x11vnc.



what's the difference between x11vnc and tigervnc-server ? actually I've installed this tiger,but it gives the errors that I've explained. Can u suggest a tutorial that explains how to configure correctly x11vnc ?


----------



## Emrion (Sep 19, 2021)

The classic vnc-servers try to open a new display when starting while x11vnc uses an already open display.

It's exactly what you get with a vnc-server under a Windows OS.

Edit: I use the autostart feature included in LXDE (you surely have the same in KDE). It runs:`x11vnc -display :0 -forever -no6 -passwdfile /home/myuser/passvnc`
Where passvnc is a simple text file that only contains the vnc password.


----------



## ziomario (Sep 19, 2021)

that's nice. I've started the server with this command :

*x11vnc -rfbauth /home/marietto/.vnc/passwd -ncache 10*

actually I have these problems to fix :

1) when the viewer exits,the server disconnects :


```
19/09/2021 14:56:16 viewer exited.
19/09/2021 14:56:16 deleted 60 tile_row polling images.
```

2) I would like that the server will restart when I reboot FreeBSD (im using KDE with the automatic insertion of username and password every time the system restarts)

NB : I've fixed the first problem with this command :

*x11vnc -xkb -forever -rfbauth /home/marietto/.vnc/passwd -ncache 10*

2) still unfixed.


----------



## ziomario (Sep 19, 2021)

I've understood where it was the mistake : I tend to forget to add the sbebang at the beginning of the script 

it works like this :

*#!/bin/sh
x11vnc -xkb -forever -rfbauth /home/marietto/.vnc/passwd -ncache 10*


----------



## ziomario (Sep 19, 2021)

For learning purposes,I'm trying to enable the previous command (x11vnc -xkb -forever -rfbauth /home/marietto/.vnc/passwd -ncache 10) not using the auto start applications tab on KDE,but I wrote a script called vnc_server that I've saved inside the folder /usr/local/etc/rc.d


```
. /etc/rc.subr

name=vnc_server
rcvar="${name}_enable"

load_rc_config $name

: ${vnc_server_enable:=""}
: ${vnc_server_flags:=""}

command=/usr/local/bin/x11vnc
#pidfile=/home/${USER}/.vnc/${name}.pid

#command_args="--daemon --pid-file=${pidfile}"
#command_args="--pid-file=${pidfile}"

PATH="${PATH}:/usr/local/bin"

run_rc_command "$1"
```

and /etc/rc.conf


```
vnc_server_enable="YES"
vnc_server_flags="-xkb -forever -rfbauth /home/marietto/.vnc/passwd -ncache 10"
```

when the system restarts and KDE is ready (it does not ask for the username and password,because I've activated the automatic logon),I don't see any x11vnc process active.


```
root@marietto:/home/marietto # service vnc_server start

Starting vnc_server.
19/09/2021 17:34:45 passing arg to libvncserver: -rfbauth
19/09/2021 17:34:45 passing arg to libvncserver: /home/marietto/.vnc/passwd
19/09/2021 17:34:45 x11vnc version: 0.9.16 lastmod: 2019-01-05  pid: 1883
19/09/2021 17:34:45 XOpenDisplay("") failed.
19/09/2021 17:34:45 Trying again with XAUTHLOCALHOSTNAME=localhost ...
19/09/2021 17:34:45
19/09/2021 17:34:45 *** XOpenDisplay failed. No -display or DISPLAY.
19/09/2021 17:34:45 *** Trying ":0" in 4 seconds.  Press Ctrl-C to abort.
19/09/2021 17:34:45 *** 1 2 3 4
No protocol specified
19/09/2021 17:34:49 XOpenDisplay(":0") failed.
19/09/2021 17:34:49 Trying again with XAUTHLOCALHOSTNAME=localhost ...
No protocol specified
19/09/2021 17:34:49 XOpenDisplay(":0") failed.
19/09/2021 17:34:49 Trying again with unset XAUTHLOCALHOSTNAME ...
No protocol specified
19/09/2021 17:34:49

19/09/2021 17:34:49 ***************************************
19/09/2021 17:34:49 *** XOpenDisplay failed (:0)

*** x11vnc was unable to open the X DISPLAY: ":0", it cannot continue.
*** There may be "Xlib:" error messages above with details about the failure.

/usr/local/etc/rc.d/vnc_server: WARNING: failed to start vnc_server
```

same error adding -display :0,like this : *vnc_server_flags="-xkb -forever -display :0 -rfbauth /home/marietto/.vnc/passwd -ncache 10"*


----------



## Emrion (Sep 19, 2021)

Emrion said:


> The classic vnc-servers try to open a new display when starting while x11vnc uses an *already open display*.


So, Xorg needs to be launched before x11vnc.

Anyway, it seems you want to open new displays to let a remote user uses a different X session than yours. x11vnc isn't suited for that. tigervnc and the likes are the softwares you need.


----------



## ziomario (Sep 19, 2021)

I know,I've already tried tigervnc,but I haven't been satisfied. I've seen some bad behavior that I haven't been able to fix. You can see here :









						How to create a vnc-server service and how to enable it when the system start
					

Hello to everyone.  I'm trying to create a vnc-server service that should be enabled when the system start. I've created the script below that I've placed inside the folder:  /usr/local/etc/rc.d  #!/bin/sh  # PROVIDE: vnc-server # # Add the following line to /etc/rc.conf[.local] to enable...




					forums.freebsd.org
				




And both tiger and x11vnc don't start using the script placed inside the rc.d folder. I would like to achieve this because I find very useful for the future,to be able to understand how the rc.d method work.


----------



## Tieks (Sep 19, 2021)

ziomario said:


> command=/usr/local/bin/x11vnc


If this is an interpreted script (first line contains a shebang like #!/bin/sh), then you need an additional var _command_interpreter="/bin/sh"_ in it. Else 'service vnc_server start' will work, but stop/status/restart won't work. That's because the process id is tied to the interpreter. See function _find_processes in /etc/rc.subr for details.


----------



## ziomario (Sep 19, 2021)

Are u talking about the vnc_server script ? yes,it starts with the shebang...


```
#!/bin/sh

. /etc/rc.subr

name=vnc_server
rcvar="${name}_enable"

load_rc_config $name

: ${vnc_server_enable:=""}
: ${vnc_server_flags:=""}

command=/usr/local/bin/x11vnc
#pidfile=/home/${USER}/.vnc/${name}.pid

#command_args="--daemon --pid-file=${pidfile}"
#command_args="--pid-file=${pidfile}"

PATH="${PATH}:/usr/local/bin"

run_rc_command "$1"
```


----------



## Tieks (Sep 19, 2021)

ziomario said:


> Are u talking about the vnc_server script ?


No, it's about /usr/local/bin/x11vnc. If that is some script beginning with a shebang, you need a line 'command_interpreter=...' in /usr/local/etc/rc.d/vnc_server. You wrote earlier you forgot a shebang, so I felt it could be a script.


----------



## ziomario (Sep 19, 2021)

x11vnc is an executable,not a script.


----------

