linux解决xhost: unable to open display ""

实用技巧:在Linux下设置xhost方法步骤

第一步:用root登陆linux,启动vnc服务;

第二步:根据vnc起来的端口,设置export DISPLAY=localhost:1(1表示vnc在第一个tty上启动的),vnc的启动信息见附件1;

第三步:执行xhost +,并且提示“access control disabled, clients can connect from any host”才正确。

linux下的操作日志如下:

[root@localhost ~]# vncserver

New ‘localhost:1 (root)’ desktop is localhost:1

Starting applications specified in /root/.vnc/xstartupLog file is /root/.vnc/localhost:1.log

[root@localhost ~]# export DISPLAY=localhost:1 ;必须以root身份

[root@localhost ~]# xhost +

access control disabled, clients can connect from any host

在设置xhost时,出现了unable的问题。使用上诉方法就可以搞定。

centos 6安装vncserver

yum -y install tigervnc-server
首次启动

vncserver
会提示输入两次密码

然后编辑/root/.vnc/xstartup,将最后一行twm替换为gnome-session或者startkde
懒人可以直接用以下语句直接替换(执行任意一条即可,建议选择第一条稳定,但是占用内存较多)

sed -i ‘s/twm/gnome-session/g’ /root/.vnc/xstartup
sed -i ‘s/twm/startkde/g’ /root/.vnc/xstartup

重启vncserver

vncserver
客户端连接

vncclient里输入 vncserver-IP:1

需要在Linux上安装图形界面服务端程序X Window

yum groupinstall “X Window System” #安装X Window

[root@powerlong4 database]# yum groupinstall “X Window System” “Desktop Platform” Desktop

yum install vnc

[root@powerlong4 database]# export DISPLAY=192.168.121.217:0.0

[root@powerlong4 database]# xhost +

access control disabled, clients can connect from any host

[root@powerlong4 database]#

启动vncserver(服务器端)
vncserver :n
这里的n是sessionnumber,不指定默认为1,也可以是2、3等等。第一次会提示输入密码,以后可以使用vncpasswd命令修改密码。
VNC的默认端口是5900,而远程桌面连接端口则是5900+n(n是vncserver命令指定的)。如果使用“vncserver :1”命令启动VNC Server,那么下面的端口应该是5901。

关闭vncserver
vncserver -kill :n
查看连接
vncserver -list

原文地址:https://www.cnblogs.com/lllini/p/11955245.html