原文鏈接:https://www.cnblogs.com/kevingrace/p/5821450.html
?
不做過多介紹了,下面直接記錄下centos7系統(tǒng)下安裝配置vncserver的操作記錄
0)更改為啟動桌面或命令行模式
獲取當前系統(tǒng)啟動模式
[root@localhost ~]# systemctl get-default
multi-user.target
查看配置文件
[root@localhost ~]# cat /etc/inittab
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3????????? //命令行模式
# graphical.target: analogous to runlevel 5????????? //圖形界面模式
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
#
[root@localhost ~]# systemctl set-default graphical.target?????????? //由命令行模式更改為圖形界面模式
[root@localhost ~]# systemctl set-default multi-user.target????????? //由圖形界面模式更改為命令行模式
[root@localhost ~]# systemctl get-default
graphical.target
1)關(guān)閉防火墻
centos的防火墻是firewalld,關(guān)閉防火墻的命令
[root@localhost ~]# systemctl stop firewalld.service #停止firewall
[root@localhost ~]# systemctl disable firewalld.service #禁止firewall開機啟動
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
[root@localhost ~]# cat /etc/sysconfig/selinux
SELINUX=disabled
SELINUXTYPE=targeted
2)安裝軟件:
[root@localhost ~]# yum update
[root@localhost ~]# yum groupinstall "GNOME Desktop" "X Window System" "Desktop"
[root@localhost ~]# yum install tigervnc-server tigervnc vnc vnc-server
3)配置vnc連接
[root@localhost ~]# cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service
修改/etc/systemd/system/vncserver@:1.service
找到這一行
ExecStart=/sbin/runuser -l -c "/usr/bin/vncserver %i"
PIDFile=/home//.vnc/%H%i.pid
這里直接用root 用戶登錄,所以我替換成
ExecStart=/sbin/runuser -l root -c "/usr/bin/vncserver %i"
PIDFile=/root/.vnc/%H%i.pid
如果是其他用戶的話比如john替換如下
ExecStart=/sbin/runuser -l john -c "/usr/bin/vncserver %i"
PIDFile=/home/john/.vnc/%H%i.pid
由于直接root用戶登錄,所以配置如下:
[root@localhost ~]# cat /etc/systemd/system/vncserver@:1.service
.........
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[Service]
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
為VNC設(shè)密碼(比如密碼設(shè)置為123456)
[root@localhost ~]# vncpasswd
Password:
Verify:
Would you like to enter a view-only password (y/n)??n#注意表示"是否輸入一個只能查看的密碼,選擇否",否則連接vnc會出現(xiàn)黑屏
A view-only password is not used
[root@localhost ~]# vim /etc/libvirt/qemu.conf
vnc_password = "123456"
vnc_listen = "0.0.0.0"
重加載 systemd
[root@localhost ~]# systemctl daemon-reload
啟動vnc
[root@localhost ~]# systemctl enable vncserver@:1.service
[root@localhost ~]# systemctl start vncserver@:1.service
注意,此處關(guān)閉了防火墻
如果防火墻開了,需要開通一下規(guī)則:
[root@localhost ~]# firewall-cmd --permanent --add-service vnc-server
[root@localhost ~]# systemctl restart firewalld.service
如果是iptable,則需要在/etc/sysconfig/iptables里添加:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5900:5903 -j ACCEPT
關(guān)閉vnc連接
[root@localhost ~]# /usr/bin/vncserver -kill :1
測試vnc連接:
[root@localhost ~]# novnc_server --vnc 192.168.1.8:5901 --listen 6081
Warning: could not find self.pem
Starting webserver and WebSockets proxy on port 6081
WebSocket server settings:
- Listen on :6081
- Flash security policy server
- Web server. Web root: /usr/share/novnc
- No SSL/TLS support (no cert file)
- proxying from :6081 to 192.168.1.8:5901
Navigate to this URL:
http://kvm-server:6081/vnc.html?host=kvm-server&port=6081 #http訪問方式
Press Ctrl-C to exit
由于kvm-server的主機名對于ip是112.112.113.56,所以在瀏覽器里輸入:
http://112.112.113.56:6081/vnc.html?host=112.112.113.56&port=6081


也可以在本地windows機器上安裝vnc viewer,遠程訪問
在windows下安裝vnc客戶端,
下載地址:https://pan.baidu.com/s/1hrSIr4K
提取密碼:dqdt
VNC遠程連接信息(下面ip是VNC服務(wù)端的地址):
VNC Server: 112.112.113.56:5901
Encrytion:Let VNC Server choose

然后輸入vncpasswd的密碼即可完成VNC遠程連接!
問題:Could not make bus activated clients aware of XDG_CURRENT_DESKTOP=GNOME environment variable:
Could not connect: Connection refused
[root@kevin ~]# cat /root/.vnc/kevin:1.log
...........
...........
(imsettings-check:31898): GLib-GIO-CRITICAL **: 21:56:03.842: g_dbus_proxy_call_sync_internal: assertion?'G_IS_DBUS_PROXY (proxy)'?failed
GLib-GIO-Message: 21:56:03.854: Using the?'memory'?GSettings backend.? Your settings will not be saved or shared with other applications.
** (process:31798): WARNING **: 21:56:03.861: Could not?make?bus activated clients aware of XDG_CURRENT_DESKTOP=GNOME environment variable:
Could not connect: Connection refused
原因:dbus-daemon存在沖突。
因為root系統(tǒng)環(huán)境中裝有anaconda,它的bin目錄中的dbus-daemon會與系統(tǒng)自帶的dbus-daemon沖突。
[root@kevin ~]# find / -name "dbus-daemon"
/usr/bin/dbus-daemon
/data/anaconda3/bin/dbus-daemon
/data/anaconda3/pkgs/dbus-1.13.6-h746ee38_0/bin/dbus-daemon
[root@kevin ~]# which dbus-daemon
/data/anaconda3/bin/dbus-daemon
解決辦法:使用非root用戶啟動vncserver
[root@kevin ~]# useradd vncuser
[root@kevin ~]# echo "vncuser@123"|passwd --stdin vncuser
[root@kevin ~]# vim /etc/sudoers
vncuser ALL=(ALL)?????? NOPASSWD: ALL
修改vncserver使用vncuser這個非root用戶啟動
[root@kevin ~]# cat /etc/systemd/system/vncserver@:1.service
..........
..........
ExecStart=/usr/sbin/runuser?-l vncuser -c?"/usr/bin/vncserver %i"
PIDFile=/root/.vnc/%H%i.pid
接著切入到非root用戶vncuser下啟動vncserver
[root@kevin ~]# su - vncuser
Last login: Tue Jul? 2 22:05:38 CST 2019 on pts/2
設(shè)置vnc登錄密碼
[vncuser@kevin ~]$ vncpasswd
啟動vnc
[vncuser@kevin ~]$ vncserver
查看vnc日志
[vncuser@kevin ~]$?cd?.vnc/
[vncuser@kevin .vnc]$ ll
total 20
-rw-r--r-- 1 vncuser vncuser? 332 Jul? 2 22:06 config
-rw-rw-r-- 1 vncuser vncuser 1046 Jul? 2 22:10 kevin:1.log
-rw-rw-r-- 1 vncuser vncuser??? 5 Jul? 2 22:06 kevin:1.pid
-rw------- 1 vncuser vncuser??? 8 Jul? 2 22:06?passwd
-rwxr-xr-x 1 vncuser vncuser? 112 Jul? 2 22:06 xstartup
[vncuser@kevin .vnc]$?cat?kevin\:1.log
Xvnc TigerVNC 1.8.0 - built Nov? 2 2018 19:05:14
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 12001000, The X.Org Foundation
Tue Jul? 2 22:06:26 2019
?vncext:????? VNC extension running!
?vncext:????? Listening?for?VNC connections on all interface(s), port 5901
?vncext:????? created VNC server?for?screen?0
touch: cannot?touch?‘/home/vncuser/.cache/imsettings/log’: No such?file?or directory
Tue Jul? 2 22:06:30 2019
?ComparingUpdateTracker: 0 pixels?in?/ 0 pixels out
?ComparingUpdateTracker: (1:-nan ratio)
Tue Jul? 2 22:10:22 2019
?Connections: accepted: 192.168.1.200::56162
Tue Jul? 2 22:10:23 2019
?Connections: closed: 192.168.1.200::56162 (reading version failed: not an RFB
??????????????client?)
?EncodeManager: Framebuffer updates: 0
?EncodeManager:?? Total: 0 rects, 0 pixels
?EncodeManager:????????? 0 B (1:-nan ratio)
?ComparingUpdateTracker: 0 pixels?in?/ 0 pixels out
?ComparingUpdateTracker: (1:-nan ratio)