vnc灰屏,黑屏,鼠標黑×問題分析及解決


問題現象

centos7.6系統已經正常安裝vnc相關組件,並且通過vncclient工具可以正常連接到vncserver,但由於不知名誤操作導致vnc 工具登錄連接后,出現黑屏、灰屏、鼠標大黑叉情況。

同時,通過嘗試重啟vnc server來解決問題,重啟前通過kill -9強制停掉了Xvnc進程,強制停止進程后,發現vnc server已經不能正常重啟,重啟命令沒有報錯,重啟完查看服務狀態為inactive,同時ps發現並沒有相關的Xvnc進程。

[root@database3 .X11-unix]# systemctl start vncserver@:1.service
[root@database3 .X11-unix]# vncserver :1

New 'database3:1 (root)' desktop is database3:1

Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/database3:1.log

[root@database3 .X11-unix]# vncserver :2

New 'database3:2 (root)' desktop is database3:2

Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/database3:2.log

[root@database3 .X11-unix]# vncserver :1^C
[root@database3 .X11-unix]# ps -ef | grep -i vnc
root     19042 16877  0 19:10 pts/0    00:00:00 grep --color=auto -i vnc

分析解決

1、查看vnc service服務配置文件正確性

一般情況下,vnc安裝后特定打開窗口服務的配置文件在/etc/systemd/system/目錄下,(copy from /lib/systemd/system/vncserver@.service),名稱是vncserver@:1.service,":"后面的數字隨着開啟vnc窗口而變化,多個窗口開啟時,對應多個文件。

配置中文件中主要內容,主要關注service下面的ExecStartPre等參數的值。

[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target

[Service]
#Type=simple
Type=forking

# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
ExecStart=/usr/sbin/runuser -l root -c "/usr/bin/vncserver %i"
# 服務啟動后對應的進程文件所在的位置
PIDFile=/root/.vnc/%H%i.pid 
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'

[Install]
WantedBy=multi-user.target

2、查看vncserver啟動日志

vnc server啟動日志一般在用戶的家目錄下的隱藏文件.vnc中。如果是root用戶,那么日志在/root/.vnc/中。

當使用systemctl restart vncserver@:1.service或者systemctl start vncserver@:1.service或者vncserver :1等命令啟動vncserver時,會在/root/.vnc目錄下生成對應的啟動日志文件。 啟動日志文件名稱的格式為:“主機名:啟vnc窗口標識.log”.查看日志發現,日志中包含kill vnc進程的操操作以及不到打開顯示圖形的錯誤提示:

[root@database3 .vnc]# cat database3\:1.log

Xvnc TigerVNC 1.8.0 - built Apr  1 2020 04:46:00
Copyright (C) 1999-2017 TigerVNC Team and many others (see README.txt)
See http://www.tigervnc.org for information on TigerVNC.
Underlying X server release 12004000, The X.Org Foundation


Tue Sep 29 17:10:23 2020
 vncext:      VNC extension running!
 vncext:      Listening for VNC connections on all interface(s), port 5901
 vncext:      Listening for HTTP connections on all interface(s), port 5801
 vncext:      created VNC server for screen 0

Running without a11y support!
killing Xvnc process ID 3438
Error: cannot open Display: :1

其實,通過分析發現,上述日志中的killling進程日志和Error日志,其實是vncserver啟動操作中調用/root/.vnc/xstartup腳本產生的。原始腳本如下:

[root@database3 .vnc]# cat xstartup
#!/bin/sh

unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
/etc/X11/xinit/xinitrc
# Assume either Gnome or KDE will be started by default when installed
# We want to kill the session automatically in this case when user logs out. In case you modify
# /etc/X11/xinit/Xclients or ~/.Xclients yourself to achieve a different result, then you should
# be responsible to modify below code to avoid that your session will be automatically killed
if [ -e /usr/bin/gnome-session -o -e /usr/bin/startkde ]; then
    vncserver -kill $DISPLAY
fi

通過分析腳本可知,腳本在執行到/etc/X11/xinit/xinitrc時,“Running without a11y support!”提示語輸出,和系統輸入法相關(不影響當前文章的問題)。

腳本執行vncserver -kill $DISPLAY時,對應日志中的“killing Xvnc process ID 3438”,也就是說,vnc server在啟動時調用上述腳本時,又進入了上述腳本中的自動kill語句,這顯然是不合適的。正常啟動設置時,如果已經啟動gnome session或者kde的,ls /usr/bin/gnome-session或者/usr/bin/startkde的值均不為空,該腳本不允許啟動vnc server。我們可以直接將次判斷注釋掉。防止自動殺掉vnc進程。同時在腳本后端配置gnome-session。更改后的配置。

[root@database3 .vnc]# cat xstartup
#!/bin/sh

unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
/etc/X11/xinit/xinitrc
# Assume either Gnome or KDE will be started by default when installed
# We want to kill the session automatically in this case when user logs out. In case you modify
# /etc/X11/xinit/Xclients or ~/.Xclients yourself to achieve a different result, then you should
# be responsible to modify below code to avoid that your session will be automatically killed
#if [ -e /usr/bin/gnome-session -o -e /usr/bin/startkde ]; then
    #vncserver -kill $DISPLAY
#fi
gnome-session &

3、檢查啟動vncserver時調用的xstartup腳本的權限

/root/.vnc/xstartup腳本的執行權限必須是755.腳本名稱前面的路徑根據實際環境進行替換即可。

[root@database3 .vnc]# ll
total 24
-rw-r--r-- 1 root root 332 Aug  1 10:53 config
-rw-r--r-- 1 root root 941 Sep 29 17:10 database3:1.log
-rw-r--r-- 1 root root   6 Sep 29 17:10 database3:1.pid
-rw-r--r-- 1 root root 489 Aug  1 10:56 default.tigervnc
-rw------- 1 root root   8 Sep 29 10:58 passwd
-rwxr-xr-x 1 root root 559 Sep 29 14:50 xstartup

4、檢查是否安裝OS相關圖形包組

服務器在安裝字符界面操作系統時默認不會安裝相關圖形包組。

# yum groupinstall "GNOME Desktop Environment" (centos 5.x安裝Gnome桌面環境)
# yum groupinstall "Desktop" "X Window System" (centos 6.x安裝Gnome桌面環境)
# yum install -y gnome* (centos 7可用)

可以按照上述四項檢查項進行排查,基本能夠解決vnc連接不上、連接上黑屏、灰屏、出現黑叉鼠標的問題,如果還不行,繼續排查其他原因。


注意停止vnc進程的命令,最好不要使用kill -9,而是用vncserver -kill :1等來代替。
/root/.vnc/xstartup腳本的執行權限必須是755.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM