转发X11失败:网络错误:连接被拒绝。

38
我有一个VPS,其操作系统是CentOS6.3。我想通过PuTTY和Xming运行startx。但是,它会产生以下错误:
PuTTY X11 proxy: unable to connect to forwarded X server: Network error: Connection refused

整个条件:
Using username "root".
Authenticating with public key "rsa-key-20150906" from agent
Last login: Thu Jan 21 13:53:40 2016 from 222.222.150.82
[root@mairo ~]# xhost +
PuTTY X11 proxy: unable to connect to forwarded X server: Network error: Connection refused
xhost:  unable to open display "localhost:10.0"
[root@mairo ~]# echo $DISPLAY
localhost:10.0
[root@mairo ~]# gedit
PuTTY X11 proxy: unable to connect to forwarded X server: Network error: Connection refused
(gedit:6287): Gtk-WARNING **: cannot open display: localhost:10.0
[root@mairo ~]#

这是Xming日志:

Welcome to the Xming X Server
Vendor: Colin Harrison
Release: 6.9.0.31
FreeType2: 2.3.4
Contact: http://sourceforge.net/forum/?group_id=156984

Xming :10 -multiwindow -clipboard 

XdmcpRegisterConnection: newAddress 192.168.139.1
winAdjustVideoModeShadowGDI - Using Windows display depth of 32 bits per pixel
winAllocateFBShadowGDI - Creating DIB with width: 1366 height: 768 depth: 32
winInitVisualsShadowGDI - Masks 00ff0000 0000ff00 000000ff BPRGB 8 d 24 bpp 32
glWinInitVisuals:1596: glWinInitVisuals
glWinInitVisualConfigs:1503: glWinInitVisualConfigs glWinSetVisualConfigs:1581: glWinSetVisualConfigs
init_visuals:1055: init_visuals
null screen fn ReparentWindow
null screen fn RestackWindow
InitQueue - Calling pthread_mutex_init
InitQueue - pthread_mutex_init returned
InitQueue - Calling pthread_cond_init
InitQueue - pthread_cond_init returned
winInitMultiWindowWM - Hello
winInitMultiWindowWM - Calling pthread_mutex_lock ()
winMultiWindowXMsgProc - Hello
winMultiWindowXMsgProc - Calling pthread_mutex_lock ()
glWinScreenProbe:1390: glWinScreenProbe
fixup_visuals:1303: fixup_visuals
init_screen_visuals:1336: init_screen_visuals
(--) 5 mouse buttons found
(--) Setting autorepeat to delay=500, rate=31
(--) winConfigKeyboard - Layout: "00000804" (00000804) 
(EE) Keyboardlayout "Chinese (Simplified) - US Keyboard" (00000804) is unknown
Could not init font path element D:\Program Files (x86)\Xming/fonts/misc/, removing from list!
Could not init font path element D:\Program Files (x86)\Xming/fonts/TTF/, removing from list!
Could not init font path element D:\Program Files (x86)\Xming/fonts/Type1/, removing from list!
Could not init font path element D:\Program Files (x86)\Xming/fonts/75dpi/, removing from list!
Could not init font path element D:\Program Files (x86)\Xming/fonts/100dpi/, removing from list!
Could not init font path element C:\Program Files\Xming\fonts\dejavu, removing from list!
Could not init font path element C:\Program Files\Xming\fonts\cyrillic, removing from list!
Could not init font path element C:\WINDOWS\Fonts, removing from list!
winInitMultiWindowWM - pthread_mutex_lock () returned.
winInitMultiWindowWM - pthread_mutex_unlock () returned.
winInitMultiWindowWM - DISPLAY=127.0.0.1:10.0
winMultiWindowXMsgProc - pthread_mutex_lock () returned.
winMultiWindowXMsgProc - pthread_mutex_unlock () returned.
winMultiWindowXMsgProc - DISPLAY=127.0.0.1:10.0
winProcEstablishConnection - Hello
winInitClipboard ()
winProcEstablishConnection - winInitClipboard returned.
winClipboardProc - Hello
DetectUnicodeSupport - Windows Vista
winClipboardProc - DISPLAY=127.0.0.1:10.0
winInitMultiWindowWM - XOpenDisplay () returned and successfully opened the display.
winMultiWindowXMsgProc - XOpenDisplay () returned and successfully opened the display.
winClipboardProc - XOpenDisplay () returned and successfully opened the display.

这是我在VPS上的sshd_config

#   $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options change a
# default value.

#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

# Disable legacy (protocol version 1) support in the server for new
# installations. In future the default will change to require explicit
# activation of protocol 1
Protocol 2

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key

# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 1024

# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile .ssh/authorized_keys
#AuthorizedKeysCommand none
#AuthorizedKeysCommandRunAs nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes

# GSSAPI options
#GSSAPIAuthentication no
GSSAPIAuthentication yes
#GSSAPICleanupCredentials yes
GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no

# Set this to 'yes' to enable PAM authentication, account processing, 
# and session processing. If this is enabled, PAM authentication will 
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
#UsePAM no
UsePAM yes

# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS

#AllowAgentForwarding yes
#AllowTcpForwarding yes
GatewayPorts yes
#X11Forwarding no
X11Forwarding yes
#X11DisplayOffset 10
X11UseLocalhost yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none

# no default banner path
#Banner none

# override default of no subsystems
Subsystem   sftp    /usr/libexec/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#   X11Forwarding no
#   AllowTcpForwarding no
#   ForceCommand cvs server

我已启用 X11 转发

enter image description here

上面给出的错误是什么引起的?


新增细节

根据 @lilydjwg 的回答,我已经填写了“X显示位置”,然后再次尝试,但仍然不正确:

enter image description here

Using username "root".
Authenticating with public key "rsa-key-20150906" from agent
Last login: Thu Jan 21 22:24:57 2016 from 222.222.150.82
[root@mairo ~]# echo $DISPLAY
localhost:10.0
[root@mairo ~]# gedit
process 6968: D-Bus library appears to be incorrectly set up; failed to read machine uuid: Failed to open"/var/lib/dbus/machine-id": No such file or directory
See the manual page for dbus-uuidgen to correct this issue.
  D-Bus not built with -rdynamic so unable to print a backtrace
Aborted
[root@mairo ~]#

不行,你不能在Putty上启动startx - Jakuje
@Jakuje 不仅仅是startx,当我运行gedit时也会出错。 - TJM
我认为你的gedit问题与X连接无关。它无法找到D-Bus连接,这通常是由窗口管理器启动的。尝试一些基本的东西,比如“xclock”,看看是否可以工作。 - JvO
@JvO 是的,它在一分钟后就能工作。但仍然会出现一个警告 警告:字符串到字体集转换中缺少字符集 - TJM
所以你的X连接已经工作了,你只需要一些额外的组件,比如D-Bus守护进程。警告可能是由于缺少字体或区域设置引起的。顺便说一句,如果'xclock'需要一分钟才能启动,你的X连接将会非常慢... - JvO
14个回答

72

其他答案已经过时、不完整或根本无法使用。

您还需要在主机上指定一个X-11服务器来处理GUId程序的启动。如果客户端是Windows机器,则安装Xming。如果客户端是Linux机器,则安装XQuartz。

现在假设这是Windows连接到Linux。为了能够通过putty启动X11程序,执行以下操作:

- Launch XMing on Windows client
- Launch Putty
    * Fill in basic options as you know in session category
    * Connection -> SSH -> X11
        -> Enable X11 forwarding
        -> X display location = :0.0
        -> MIT-Magic-Cookie-1
        -> X authority file for local display = point to the Xming.exe executable

当然,ssh服务器应该允许桌面共享“允许其他用户查看您的桌面”。

MobaXterm和其他完整的远程桌面程序也可以使用。


1
截至2019年12月,这应该是被认可的答案。 - JoeCrash
1
这对我也完美地起作用,请接受这个答案! - Nam Nguyen
在远程机器上运行Firefox时,我收到[GFX1-]: glxtest: libEGL初始化失败的消息。在主机上,我的浏览器是空白的。 - Yann Greder
在Windows下,使用Xming作为本地显示器会更好。网络上有很多答案都提到了手动设置DISPLAY环境变量,这可能是假定用户总是在GNU/Linux下的情况。但实际情况并非如此。 - Taro
经过大量的寻找答案,这个解决方案解决了问题!非常感谢!这真的需要成为被接受的答案! - LtMuffin
绝对的救命稻草,+1 百万次。 - o_in25

27

PuTTY无法找到您的X服务器,因为您没有告诉它。(Linux上的ssh没有这个问题,因为它在X下运行,所以它只使用X。)请在“X显示位置”后面的空白框中填写您的Xming服务器地址。

或者,尝试MobaXterm。它内置了一个X服务器。


4
但要填什么?是填写 localhost:10 吗? - TJM
@TJM gedit正在使用D-Bus,所以您也应该将其转发。实际上,有些应用程序即使您已经转发了D-Bus,也无法正常工作,例如evince。 - lilydjwg
@lilyjwg 那我该怎么办呢?有没有像pac一样的东西可以指定哪些进程前进D-Bus,哪些不前进? - TJM
@TJM 不,D-Bus 不使用 JavaScript。你只需要一个 D-Bus,无论是本地的还是远程的。 - lilydjwg
感谢使用“xming”这个术语。它提醒了我们,xterm需要一个显示服务器! - Neetigya Chahar

2

填写“X显示位置”对我没有用。 但是安装MobaXterm就解决了问题。


1
我曾经遇到过同样的问题,但现在已经解决了。 最终,Putty与Cigwin-X可以一起使用,而Xming不是MS-Windows X服务器的必需应用程序。
现在控制X窗口运行的是xlaunch。 当然,xlaunch.exe必须安装在Cigwin中。 在交互模式下运行时,它会要求进行“额外设置”。 您应该在附加参数字段中添加“-listen tcp”,因为Cigwin-X默认不侦听TCP。
为了避免重复这些步骤,您可以将设置保存到文件中。并通过其快捷方式运行xlaunch.exe,并在其中修改CLI。类似于:
C:\cygwin64\bin\xlaunch.exe -run C:\cygwin64\config.xlaunch

1
D-Bus错误可以通过dbus-launch进行修复:
dbus-launch command

0

我遇到了相同的错误,在我的情况下,我只是将显示编号设置为0,然后就可以成功启动xeyes。


0

X显示位置:localhost:0 对我有效 :)


0
对我来说,这是一个简单的错误:Xming没有运行。确保你的Xming进程在后台运行,否则它将无法工作。例如,如果你重新启动计算机,而没有将Xming设置为自动运行。

0
PuTTY找不到您的X服务器,因为您没有告诉它。(在Linux上的ssh没有这个问题,因为它在X下运行,所以它只使用那个。)在“X显示位置”后面的空白框中填写您的Xming服务器地址。 或者,尝试MobaXterm。它内置了一个X服务器。

0

不要以root用户身份登录,尝试使用具有sudo权限的其他用户。


我认为这是由于服务器管理员在sshd_config中禁用了PermitRootLogin导致的。如果已禁用,您将无法作为root登录该服务器。但是,根据上面的问题,我已经使用root登录。因此,使用root不是X11转发失败的原因。 - TJM
无论是否启用PermitRootLogin,连接到服务器通过SSH都是可以的,无论X11Forwarding如何。再次强调,以root用户身份连接将会给您带来安全问题,不应该这样做。我建议您使用Cygwin / X,它更快、更完整,但不要使用root用户身份。我正在使用google-chrome进行测试,效果很好(有点慢),虽然会给我一些GPU和OpenGL的错误(与Xming相同),但我无法解决。也许这是一个Ubuntu的问题。 - Frank Mascarell
对不起,我以为我在西班牙语的stackoverflow网站上,那里我有更高的声望和评论权限。我只是想要帮忙。我的问题类似,我想最好还是提出一个新问题。 - Frank Mascarell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接