centos7.2部署vnc服務記錄

散盡浮華發表於2016-08-30

 

不做過多介紹了,下面直接記錄下centos7系統下安裝配置vncserver的操作記錄

0)更改為啟動桌面或命令列模式

獲取當前系統啟動模式
[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)關閉防火牆
centos的防火牆是firewalld,關閉防火牆的命令
[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 <USER> -c "/usr/bin/vncserver %i"
PIDFile=/home/<USER>/.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設密碼(比如密碼設定為123456)
[root@localhost ~]# vncpasswd
Password:
Verify:
Would you like to enter a view-only password (y/n)? n             #注意表示"是否輸入一個只能檢視的密碼,選擇否",否則連線vnc會出現黑屏
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

注意,此處關閉了防火牆
如果防火牆開了,需要開通一下規則:
[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

關閉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服務端的地址):
VNC Server: 112.112.113.56:5901
Encrytion:Let VNC Server choose
   
然後輸入vncpasswd的密碼即可完成VNC遠端連線!

相關文章