Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2221640
  • 博文数量: 533
  • 博客积分: 8689
  • 博客等级: 中将
  • 技术积分: 7046
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-26 10:00
文章分类

全部博文(533)

文章存档

2024年(2)

2023年(4)

2022年(16)

2014年(90)

2013年(76)

2012年(125)

2011年(184)

2010年(37)

分类: 系统运维

2022-03-24 17:46:04



https://blog.csdn.net/qxqxqzzz/article/details/103974974

Ubuntu VNC 安装/使用/故障解决 

Vnc viewer远程桌面Xfce连接ubuntu16.04 使用及问题汇总

目录


一、 Ubuntu 安装 VNC 步骤


二、Windowns 10 安装VNC Viewer客户端 并设置连接


三、指定端口连接:端口转发的情况下如何使用VNC?


四、常见问题及解决:


如果sudo shutdown -r 等远程关机或意外情况 导致vnc viewer关闭之后,需要重新开启vncserver服务后方能使用。


灰屏等部分问题可能是因为当前账户权限不足(如2.1, 2.3等),如果以下步骤都通过root账户安装配置,可能会减少问题的出现。


所有出现的问题都可以在日志,如 /home/user_name/.vnc/pc_name:2.log (这个文件路径会在第3步之后显示) 中查看。
 


一、 Ubuntu 安装 VNC 步骤
强烈建议以下内容都使用 aptitude 安装,避免依赖冲突!!!


# 1. 安装必要的软件
# 安装xrdp
$ sudo apt-get update
$ sudo apt-get install xrdp
# 安装xfce4
$ sudo apt-get install xfce4 # 选lightdm
 
# 注释的部分可以跳过,仅供遇到问题时参考
# $ sudo apt-get install xubuntu-desktop # 中间遇到 *** libreoffice.sh (Y/I/N/O/D/Z) [default=N] ? 直接回车
 
# 安装其他必要的软件
# $ sudo apt-get install gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal
 
# 或者加 --fix-missing 修复依赖的方式进行安装,即:
# $ sudo apt-get install gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal --fix-missing)
 
# --no-install-recommends 只安装相关的必要的包,不安装非必要的
$ sudo apt-get install --no-install-recommends ubuntu-desktop gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal
 
# 配置xfce4
$ echo "xfce4-session">~/.xsession
$ sudo vi /etc/xrdp/startwm.sh
# 在./etc/X11/Xsession前一行插入
xfce4-session
# 重启xrdp
$ sudo service xrdp restart
# 安装vnc4server
$ sudo apt-get install vnc4server
# 设置vncserver的密码,限制8位
$ vncpasswd
 
# 2. 设置配置文件
 
# 2.1 修改xstartup
$ sudo vi ~/.vnc/xstartup
# 复制粘贴以下内容,替换原文件内容:
 
################### 配置一 #####################
#!/bin/sh
# Uncomment the following two lines for normal desktop:
unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc
unset DBUS_SESSION_BUS_ADDRESS
 
[ -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 &
 
# export XKL_XMODMAP_DISABLE=1
 
gnome-panel &
gnmoe-settings-daemon &
metacity &
nautilus &
gnome-terminal &
################################################
 
# 2.2 
# !!设置权限,否则你可能最后一直都是灰色,鼠标为叉
# 感谢:nickcrabtree @
$ sudo chmod +x ~/.vnc/xstartup
 
# 2.3 创建必要的文件夹,否则后续可能打开桌面时会报错 oops之类的
$ sudo mkdir ~/.config/nautilus
 
# 3. 以指定的分辨率(1920x1080)、指定的端口(X,比如 :2)启动远程桌面
$ vncserver -geometry 1920x1080 :2


二、Windowns 10 安装VNC Viewer客户端 并设置连接
# 4. Win10官网下载安装Vnc Viewer
File -> New connection -> Vnc server填 服务器IP地址:端口号,如 xxx.xx.xx.xxx:5902
# 注意端口号是 5900 + X
其他复选框全部勾选
 
# 这样打开的界面是基于Xfce4的。
灰色和蓝色为主。
 
# 4.打不开终端的解决
左上角Applications -> 设置 -> 首选应用程序 -> Utilities -> Terminal Emulator -> 选择 Xfce终端
# 终端打开后显示在远程服务器的本地桌面上(就是机房里的服务器的桌面) 而不是目前你本地的桌面
左上角Applications -> 
 
# 注:
# 1、关闭指定的vncserver服务:
$ vncserver -kill :2


三、指定端口连接:端口转发的情况下如何使用VNC?
解决:


例如转发后的端口是8001,则起VNC进程的端口号应该为 8001 - 5900 = 2101


$ vncserver -geometry 1920x1080 :2101
感谢:Setting VNC for remote access (with port forwarding)


四、常见问题及解决:
1) 只是鼠标变成X,没有灰屏或不显示图标的问题,可能是分辨率导致,需要调整启动vncserver时的分辨率


2) 如果出现 vnc4server+Gnome灰屏、仅背景无图标,鼠标变成X,可能是:必要的软件包没有安装/或者xstartup设置有误/或者将xstartup改成下面这样:


################## 配置二 #################
#!/bin/sh
 
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
 
xsetroot -solid grey
vncconfig -iconic &
 
gnome-panel &
gnmoe-settings-daemon &
metacity &
nautilus &
gnome-terminal &
###################################


这一步可能解决了没有桌面的问题,但是会出现没有菜单栏的问题。也有可能到这一步就能有ubuntu gnome桌面可以使用了!


3) 如果显示正常,只是没有顶部菜单栏。则xstartup设置有误,请检查设置。如果还不行:


$ apt-get install gnome-core xfce4 firefox nano -y --force-yes
# 如果有装不上的就加上 --fix-missing: apt-get install gnome-core xfce4 firefox nano -y --force-yes --fix-missing
 
 
中间如果弹出对话框,选择lightdm。
如果选择了gdm然后按照下面的xstartup配置、在打开vnc远程桌面时,遇到Unable to load a failsafe session的对话框报错。请参考后文解决方法。
 
$ sudo vi ~/.vnc/xstartup
 
################## 配置三 #################
#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
startxfce4 &
 
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
#########################################
感谢:Sinscary @


其他解决参考:


如果到这一步(按照上述配置修改了你的配置)还不能解决你的问题,那你可能遇到了 8)中描述的问题:详见 8)


报错:Unable to load a failsafe session
 
解决:
# (前提是你在sudo用户组中,如果不知道怎么加入这个组,请参考:https://blog.csdn.net/qxqxqzzz/article/details/105244982)
 
$ sudo chown -R your_user_name:sudo /home/your_user_name/ # 实测有效
$ vncserver -kill :2
$ vncserver -geometry 1920x1080 :2
 
感谢:Nemesis1963 @
如果还是没有菜单栏,请尝试在终端输入:


$ gnome-panel
看看桌面左上角是不是显示出了 Applications


4) 不能使用本地的剪贴板:


方法一:


在远程桌面终端中输入:
$ vncconfig -nowin& # 没有弹出窗口。运行完这个命令之后,可以关闭当前终端,重新打开一个新终端再尝试复制粘贴

$ vncconfig & # 需要保持弹出的窗口一直打开
即可。参考:https://blog.csdn.net/dongfangxiaozi_/article/details/72621974


方法二:
如果以上方法还不能解决,比如还是报:$ No VNC extension on display 的错误


(出现这个错误的原因是因为你在错误的地方输入了$ vncconfig -nowin&, 应该在打开远程VNC桌面之后的桌面上的终端内输入(注意不是在本地SCH的终端中输入)


$ sudo apt install autocutsel
打开VNC,在VNC桌面终端中(注意不是在本地SCH终端)输入:


$ autocutsel -f
命令没有任何输出。然后关掉当前终端,再打开一个终端。这时候你可以在本地和VNC桌面之间使用剪贴板了。中文可能会粘贴成乱码。解决办法下回分解。


参考:一行命令开启VNC 和windows之间复制粘贴功能_Double_V的博客-CSDN博客_vnc复制粘贴权限设置


方法三:


如果还是无法复制粘贴,可能是权限问题,你当前的用户和开启vnc进程的用户不是同一个用户。需要保持:运行上述命令的用户 = 开启vnc进程的用户 = 当前使用终端的用户。否则会报如“X11: Invalid MIT-MAGIC-COOKIE-1 keyError: Can't open display: :0 on Ubuntu” 之类奇怪的错误。


在保证用户相同之后,还是无法粘贴?尝试使用鼠标中键粘贴。右键paste菜单灰色但是中键也许可以使用!


方法四:


还是不行????使用terminator吧!ubuntu终端 terminator_Hello Word!-CSDN博客




5) 不能使用tab自动补全:
左上角Applications -> 设置 -> 窗口管理器 -> Keyboard -> switch window for same application -> 双击清除之前的快捷键设置 即可。参考:https://blog.csdn.net/xiaobaiing/article/details/46236163


# 参考: 
# https://blog.csdn.net/weixin_41803874/article/details/81042115
# https://blog.csdn.net/shaopengf/article/details/53186447
#


6)VNC启动不了也关不掉:A VNC server is already running as :2 / Can't find file /home/user1/.vnc/xxxpc:2.pid


$ vncserver -geometry 1920x1080 :2
 
Warning: lthpc:2 is taken because of /tmp/.X2-lock
Remove this file if there is no X server lthpc:2
A VNC server is already running as :2
 
$ vncserver -kill :2
 
Can't find file /home/user1/.vnc/xxxpc:2.pid
You'll have to kill the Xtightvnc process manually
解决:
$ vncserver :2
 
Warning: lthpc:2 is taken because of /tmp/.X2-lock
Remove this file if there is no X server lthpc:2
A VNC server is already running as :2
 
$ sudo rm /tmp/.X2-lock
 
$ vncserver -geometry 1920x1080 :2
 
Warning: lthpc:2 is taken because of /tmp/.X11-unix/X2
Remove this file if there is no X server lthpc:2
A VNC server is already running as :2
 
$ rm /tmp/.X11-unix/X2
 
# 此时启动成功
$ vncserver -geometry 1920x1080 :2
 
# 还不行?找到进程杀死他!
$ sudo netstat -anp|grep 5902
 
# tcp6       0      0 :::5902                 :::*                    LISTEN      98850/Xvnc4       
# unix  3      [ ]         STREAM     CONNECTED     995902   1613/dbus-daemon     /run/dbus/system_bus_socket
 
$ sudo kill 98850
参考:Cancelor@solaris 10 - Getting Error with VNC: Session is already running - Stack Overflow


7) xfce安装失败:


$ sudo apt install xfce4
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
 
The following packages have unmet dependencies:
 gnome-session-common : Breaks: eog (< 3.36.0) but 3.18.2-1ubuntu2.1 is to be installed
 gsettings-desktop-schemas : Breaks: gnome-settings-daemon (< 3.19.92) but 3.18.2-0ubuntu3.1 is to be installed
 libmutter-6-0 : Breaks: gnome-settings-daemon (< 3.31.91) but 3.18.2-0ubuntu3.1 is to be installed
 unity-settings-daemon-schemas : Breaks: gnome-settings-daemon (< 3.20.1-2ubuntu1~) but 3.18.2-0ubuntu3.1 is to be installed
E: Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages.


尝试(参考:Ubuntu安装eog遇到的坑及解决方案_Seven-CSDN博客):


$ sudo apt-get install aptitude
$ sudo aptitude install xfce4
还是报错。。。


Ign focal-updates/main amd64 cups-common all 2.3.1-9ubuntu1.1
Err focal-updates/main amd64 cups-server-common all 2.3.1-9ubuntu1.1
  Could not handshake: An unexpected TLS packet was received. [IP: 10.167.32.133 8080]
Err focal-updates/main amd64 cups-common all 2.3.1-9ubuntu1.1
  Could not handshake: An unexpected TLS packet was received. [IP: 10.167.32.133 8080]
0% [Working]E: Failed to fetch /pool/main/p/python3.8/libpython3.8-minimal_3.8.2-1ubuntu1.1_amd64.deb: Could not handshake: An unexpected TLS packet was received. [IP: 10.167.32.133 8080]
E: Unable to correct for unavailable packages
最终自己解决了!正解:安装xfce4报上述错误的时候,可能是服务器上已经有人安装了vnc了,这时候你只需要简单配置一下,就可以直接使用了!如下:


# xfce4安装失败,就试试xubuntu-desktop !
$ sudo apt-get install xubuntu-desktop
# 查看服务器上之前是不是装过了vnc
$ vncserver
$ echo "xfce4-session">~/.xsession
# 此步可能会fail,因此可能是非必须
$ sudo service xrdp restart
# 如果上面发现服务器装过vnc了,这时候只需要配置一下就可以直接用了!设置自己的vnc密码
$ vncpasswd
$ sudo chmod +x ~/.vnc/xstartup
$ sudo mkdir ~/.config/nautilus
# 参考前文粘贴配置。第一个不行就后面一个。我xfce4安装失败,改成 3)中的配置就可以了
$ sudo vi ~/.vnc/xstartup
$ vncserver -geometry 1920x1080 :2
其他方法:Jetson nano VNC远程桌面配置 - Michael云擎 - 博客园 (可能只适用于Nvidia jetson)


unable to load a failsafe session
可以先参考问题 3)


8)用着好好的,某一天又灰屏了,而且报错:


unable to contact settings server...failed to connect to socket...


unable to load a failsafe session...unbale to determine failsafe session...






原因:也不知道是什么鬼原因,之前用着好好的,现在要改为dbus来启动xfce4了


解决:将配置三改为:


$ sudo vi ~/.vnc/xstartup
 
 
改为:
 
############# 配置四 #################
#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
 
# 仅仅改了这一句
dbus-launch startxfce4
 
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
###################################


感谢:Archpaca @ VNC with Xfce problem / Applications & Desktop Environments / Arch Linux Forums #5


如果还不行,


$ vncserver -kill :2
$ sudo mv ~/.config ~/.config-old
$ sudo mv ~/.cache ~/.cache-old
$ vncserver -geometry 1920x1080 :2
感谢:Xfce 4 Desktop Error xfconfd isn’t running | HostOnNet.com


9)窗口尺寸大小不能拖拽调节


查看调节窗口大小快捷键:左上角applications-settings-window manager-keyboard


10) 不能打开带GIU的程序,报错:


$ sudo gedit xxx.txt
No protocol specified
Unable to init server: Could not connect: Connection refused


(gedit:159488): Gtk-WARNING **: 14:21:19.189: cannot open display: :2.0


解决:


$ sudo vim ~/.bashrc
加入:
xhost +
 


————————————————
版权声明:本文为CSDN博主「城俊BLOG」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qxqxqzzz/article/details/103974974
阅读(4339) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~