无法将 x11vnc 作为服务运行

无法将 x11vnc 作为服务运行

我正在尝试在 Ubuntu 20.04 上配置 x11vnc 服务器。当我使用以下命令从终端启动它时:

x11vnc -noxdamage -display :0 -rfbport 5900 -rfbauth /home/km/.vnc/passwd -forever -loop

这没问题,服务器启动了,我可以从另一台机器连接。

在这种情况下,我想在系统启动时启动此服务,因此我创建了一个/lib/systems/system/x1vnc.service包含以下内容的文件:

[Unit].
Description=x11vnc service
After=display-manager.service network.target syslog.target

[Service]
Type=simple
ExecStart=/usr/bin/x11vnc -noxdamage -display :0 -rfbport 5900 -rfbauth /home/km/.vnc/passwd -forever -loop
ExecStop=/usr/bin/killall x11vnc
Restart=on-failure

[Install]
WantedBy=multi-user.target

但是当我通过启动此服务时,sudo systemctl start x11vnc.service我可以看到服务器正在尝试循环启动而不停止,而我无法连接到它,示例状态输出:

km@km-Z97-HD3:~$ sudo systemctl status x11vnc.service
● x11vnc.service - x11vnc service
     Loaded: loaded (/lib/systemd/system/x11vnc.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-08-24 11:24:22 CEST; 19min ago
   Main PID: 71782 (x11vnc)
      Tasks: 1 (limit: 38269)
     Memory: 1.2M
     CGroup: /system.slice/x11vnc.service
             └─71782 /usr/bin/x11vnc -noxdamage -display :0 -rfbport 5900 -rfbauth /home/km/.vnc/passwd -forever -loop

sie 24 11:43:27 km-Z97-HD3 x11vnc[72438]:    Sometimes the command "ps wwwwaux | grep auth" can reveal the file location.
sie 24 11:43:27 km-Z97-HD3 x11vnc[72438]:    Starting with x11vnc 0.9.9 you can have it try to guess by using:
sie 24 11:43:27 km-Z97-HD3 x11vnc[72438]: -auth guess
sie 24 11:43:27 km-Z97-HD3 x11vnc[72438]: (see also the x11vnc -findauth option.).
sie 24 11:43:27 km-Z97-HD3 x11vnc[72438]:    Only root will have read permission for the file, and so x11vnc must be run
sie 24 11:43:27 km-Z97-HD3 x11vnc[72438]: as root (or copy it).  The random characters in the filenames will of course
sie 24 11:43:27 km-Z97-HD3 x11vnc[72438]: change and the directory the cookie file resides in is system dependent.
sie 24 11:43:27 km-Z97-HD3 x11vnc[72438]: See also: http://www.karlrunge.com/x11vnc/faq.html
sie 24 11:43:27 km-Z97-HD3 x11vnc[71782]: --- x11vnc loop: sleeping 2000 ms ---.
sie 24 11:43:29 km-Z97-HD3 x11vnc[71782]: --- x11vnc loop: 457 ---

如何处理这个问题?

答案1

问题在于 Xauthority。您是否尝试过输出中提到的 -auth guess。我猜那也不会起作用。我正在运行 Kubuntu 22.04,并且有一个适用于它但可能不适用于 Gnome 的解决方案。我从一个 bash 脚本开始:

#!/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 /etc/x11vnc.pass -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

我的服务文件如下所示:

[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

/var/run/sddm/ 可能位于 Xauthority 所在的位置,也可能不位于你的启动欢迎界面。它适用于 KDE 和 SDDM。脚本中的其余代码负责处理注销时 Xauthority 文件发生更改以及必须重新加载 x11vnc 才能使用新令牌的情况。哦,您还需要安装 inotify-tools,sudo apt 安装 inotify-tools检测文件更改。祝你好运!

相关内容