VNC Viewer 请求超时

问题

连接时提示:Timed out waiting for a response from the computer

vnc连接centos7 VNC连接超时_运维

在通过VNC Viewer连接虚拟桌面时,出现了连接超时的现象,一般出现这种问题,首先考虑一下是不是服务器IPVNC端口号 输入错误导致。
经过排查后,确认IP和端口号都是正确的,尝试各种方法后发现问题所在:

服务器可能打开了防火墙,通过服务器外部IP无法访问某些服务,导致连接超时。

解决方案

只需要在服务器中手动开启相应的端口号,VNC Viewer就能够访问了。

首先查看当前防火墙的规则:

#查看当前的规则和对应的编号,需要使用root权限
sudo iptables -L -n --line-number

vnc连接centos7 VNC连接超时_服务器_02


下面还有很多规则,图中的两个端口是与VNC相关的端口,一个是52号桌面的端口,一个是9号桌面的端口,之前的规则中只有对09的规则,图中的52是后来添加的。

由于之前的规则中并没有开放52号桌面的端口,所以手动进行开启。
首先要获取52号桌面的端口号,VNC的默认端口是从5901开始的,即1号桌面的端口为5901,相应的52号桌面的端口号为5952,如果不确定或为了稳妥,可以使用下面的命令查看个人用户下所有VNC的端口号。

netstat -lp|grep -i vnc

vnc连接centos7 VNC连接超时_ubuntu_03


可以得到,52号桌面的端口号为:5952

然后,手动将开启52端口的规则,加入防火墙规则的命令如下:

sudo iptables -I INPUT -p tcp --dport 5952 -j ACCEPT

开启端口后VNC Viewer就可以访问端口52对应的VNC了。

gnome桌面灰屏

注:在gnome环境下的安装不一定能够成功,如果你的系统是16.04或18.04,直接向后翻,看xfce4的方案。

打开桌面后为灰屏状态,鼠标为 ‘X’ 状,无法进行任何操作。

vnc连接centos7 VNC连接超时_运维_04


如果你之前已经配置了xstartup文件,且运行正常,可以尝试一下命令,安装gnome需要的包,或对这些包进行更新。

sudo apt-get install gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal

在安装或更新后,如果还是灰屏,说明是xstartup的问题。

我们打开 amax-sys:52.log 文件,查看桌面的日志:

Xvnc Free Edition 4.1.1 - built Feb 25 2015 23:02:21
Copyright (C) 2002-2005 RealVNC Ltd.
See http://www.realvnc.com for information on VNC.
Underlying X server release 40300000, The XFree86 Project, Inc


Mon Nov  8 14:25:29 2021
 vncext:      VNC extension running!
 vncext:      Listening for VNC connections on port 5952
 vncext:      created VNC server for screen 0
error opening security policy file /etc/X11/xserver/SecurityPolicy
Could not init font path element /usr/X11R6/lib/X11/fonts/Type1/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/Speedo/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/misc/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/75dpi/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/100dpi/, removing from list!
Could not init font path element /usr/share/fonts/X11/75dpi/, removing from list!
Could not init font path element /usr/share/fonts/X11/100dpi/, removing from list!
sh: /home/liushuo/.vnc/xstartup: Permission denied

从最后一句中,得到xstartup文件的权限不足,查看xstartup的权限:

vnc连接centos7 VNC连接超时_ubuntu_05

可以看到xstartup只有读写权限,因此我们为xstartup添加执行权限。

chmod +x .vnc/xstartup

vnc连接centos7 VNC连接超时_ubuntu_06


然后重新生成52号桌面

vncserver -kill :52
vncserver -geometry 1920x1080 :52

打开桌面后,菜单栏可以正常显示,程序和终端都可以正常运行,但是桌面和图标无法显示,且程序运行很慢。

vnc连接centos7 VNC连接超时_vnc连接centos7_07

查看日志文件:

Xvnc Free Edition 4.1.1 - built Feb 25 2015 23:02:21
Copyright (C) 2002-2005 RealVNC Ltd.
See http://www.realvnc.com for information on VNC.
Underlying X server release 40300000, The XFree86 Project, Inc


Mon Nov  8 17:17:22 2021
 vncext:      VNC extension running!
 vncext:      Listening for VNC connections on port 5951
 vncext:      created VNC server for screen 0
error opening security policy file /etc/X11/xserver/SecurityPolicy
Could not init font path element /usr/X11R6/lib/X11/fonts/Type1/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/Speedo/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/misc/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/75dpi/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/100dpi/, removing from list!
Could not init font path element /usr/share/fonts/X11/75dpi/, removing from list!
Could not init font path element /usr/share/fonts/X11/100dpi/, removing from list!
/home/liushuo/.vnc/xstartup: line 16: gnome-settings-daemon: command not found
metacity-Message: 17:17:25.351: could not find XKB extension.

原因是没有 gnome-settings-daemon 命令,也就是gnome-settings-daemon没有安装成功。

我通过 apt-get 安装没有出现问题,但是通过 ps -A | grep gnome 查找不到gnome-settings-daemon的信息。

vnc连接centos7 VNC连接超时_ubuntu_08


搞了半天不知道问题出在哪里,知道解决方案的大佬可以交流一下。

我的xstartup内容如下:

#!/bin/sh

# Uncomment the following two lines for normal desktop:
# unset SESSION_MANAGER
# exec /etc/X11/xinit/xinitrc

# dbus-launch startxfce4
 
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
 
xsetroot -solid grey
vncconfig -iconic &
 
gnome-panel &
gnome-settings-daemon &
metacity &
nautilus &
gnome-terminal &

如果你的gnome-settings-daemon正常,修改xstartup的权限后,桌面应该就出来了。
如果你和我出现一样的问题,且Ubuntu系统为16.04或18.04,可以选择安装另一种桌面环境:Xfce4。

xfce4方案

安装方式也非常简单,此处默认系统已经安装了vncserver,如果没有安装,使用如下命令进行安装:

sudo apt-get install vnc4server

然后安装xfce

sudo apt-get install xfce4

可以使用如下命令,查看是否安装成功:

ps -A | grep xfce4

vnc连接centos7 VNC连接超时_vnc连接centos7_09


修改配置文件xstartup为:

#!/bin/sh

# Uncomment the following two lines for normal desktop:
# unset SESSION_MANAGER
# exec /etc/X11/xinit/xinitrc

dbus-launch startxfce4

[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
x-window-manager &
xrdb $HOME/.Xresources

然后重新生成52号桌面即可

vncserver -kill :52
vncserver -geometry 1920x1080 :52

vnc连接centos7 VNC连接超时_ubuntu_10


附当前正常使用的VNC的log文件:

Xvnc Free Edition 4.1.1 - built Feb 25 2015 23:02:21
Copyright (C) 2002-2005 RealVNC Ltd.
See http://www.realvnc.com for information on VNC.
Underlying X server release 40300000, The XFree86 Project, Inc


Mon Nov  8 19:08:30 2021
 vncext:      VNC extension running!
 vncext:      Listening for VNC connections on port 5951
 vncext:      created VNC server for screen 0
error opening security policy file /etc/X11/xserver/SecurityPolicy
Could not init font path element /usr/X11R6/lib/X11/fonts/Type1/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/Speedo/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/misc/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/75dpi/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/100dpi/, removing from list!
Could not init font path element /usr/share/fonts/X11/75dpi/, removing from list!
Could not init font path element /usr/share/fonts/X11/100dpi/, removing from list!
/usr/bin/startxfce4: X server already running on display :51
gpg-agent[42352]: WARNING: "--write-env-file" is an obsolete option - it has no effect
gpg-agent: a gpg-agent is already running - not starting a new one

(xfce4-session:42337): xfce4-session-WARNING **: 19:08:34.025: gpg-agent returned no PID in the variables

(xfce4-session:42337): xfce4-session-WARNING **: 19:08:34.026: xfsm_manager_load_session: Something wrong with /home/liushuo/.cache/sessions/xfce4-session-amax-sys:51, Does it exist? Permissions issue?
vmware-user: could not open /proc/fs/vmblock/dev

(xfwm4:42355): xfwm4-WARNING **: 19:08:34.063: The display does not support the XRender extension.

(xfwm4:42355): xfwm4-WARNING **: 19:08:34.063: The display does not support the XComposite extension.

(xfwm4:42355): xfwm4-WARNING **: 19:08:34.063: The display does not support the XDamage extension.

(xfwm4:42355): xfwm4-WARNING **: 19:08:34.064: The display does not support the XFixes extension.

(xfwm4:42355): xfwm4-WARNING **: 19:08:34.064: Compositing manager disabled.

(xfsettingsd:42371): xfsettingsd-CRITICAL **: 19:08:34.076: RANDR extension is too old, version 1.1. Display settings won't be applied.
Xlib:  extension "XInputExtension" missing on display ":51.0".

(xfsettingsd:42371): xfsettingsd-CRITICAL **: 19:08:34.076: XI is not present.

(xfsettingsd:42371): xfsettingsd-CRITICAL **: 19:08:34.076: Failed to initialize the Xkb extension.

(xfsettingsd:42371): xfsettingsd-CRITICAL **: 19:08:34.076: Failed to initialize the Accessibility extension.

** (update-notifier:42372): WARNING **: 19:08:34.100: already running?
xfwm4-Message: 19:08:34.112: Unsupported keyboard modifier '<Super>Tab'

(polkit-gnome-authentication-agent-1:42374): GLib-CRITICAL **: 19:08:34.115: g_variant_new_string: assertion 'string != NULL' failed

(polkit-gnome-authentication-agent-1:42374): polkit-gnome-1-WARNING **: 19:08:34.116: Failed to register client: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.SessionManager was not provided by any .service files

(xfwm4:42355): xfwm4-WARNING **: 19:08:34.117: Cannot find visual format on screen 0
xfwm4-Message: 19:08:34.160: Unsupported keyboard modifier '<Super>Tab'

(nm-applet:42367): nm-applet-WARNING **: 19:08:34.220: GDBus.Error:org.freedesktop.NetworkManager.AgentManager.PermissionDenied: An agent with this ID is already registered for this user.

(nm-applet:42367): Gtk-WARNING **: 19:08:34.255: Can't set a parent on widget which has a parent
Failure: Module initialization failed

** (xfdesktop:42363): WARNING **: 19:08:34.472: Failed to set the background '/usr/share/backgrounds/xfce/xfce-teal.jpg': GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: No such interface 'org.freedesktop.DisplayManager.AccountsService'

仍有一些警告,但桌面已经可以正常使用。