hunkier

学习笔记,开源代码,技术分享

CentOS 7上部署vnc服务器并实现远程桌面

一、安装X Window System

注:若已经安装GUI则可跳过

1、切换到root用户,执行 yum groupinstall "X Window System"
2、执行 yum install gnome-classic-session gnome-terminal nautilus-open-terminal control-center liberation-mono-fonts dejavu-lgc-sans-fonts -y 安装相关组件
3、设置默认启动图形界面

1
yum groupinstall -y "Fonts"

4、设置默认启动图形界面

1
2
# unlink /etc/systemd/system/default.target
# ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target

5、重启系统生效

1
# reboot

二、安装vnc服务

1、执行yum install tigervnc-server -y安装VNC服务器软件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# yum install tigervnc-server -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirror.lzu.edu.cn
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package tigervnc-server.x86_64 0:1.8.0-13.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===================================================================================================================================================================================================================
Package Arch Version Repository Size
===================================================================================================================================================================================================================
Installing:
tigervnc-server x86_64 1.8.0-13.el7 base 215 k

Transaction Summary
===================================================================================================================================================================================================================
Install 1 Package

Total download size: 215 k
Installed size: 509 k
Downloading packages:
tigervnc-server-1.8.0-13.el7.x86_64.rpm | 215 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : tigervnc-server-1.8.0-13.el7.x86_64 1/1
Verifying : tigervnc-server-1.8.0-13.el7.x86_64 1/1

Installed:
tigervnc-server.x86_64 0:1.8.0-13.el7

Complete!

2、配置VNC

在/etc/systemd/system目录里创建一个配置文件(可以将/lib/systemd/system/vncserver@.service拷贝一份配置文件范例过来)

1
cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service

使用文本编辑器打开 /etc/systemd/system/vncserver@:1.service ,找到下面这几行,用自己的用户名替换掉 <USER>

1
2
3
4
5
User=<USER>
PIDFile=/home/<USER>/.vnc/%H%i.pid
替换成
User=root
PIDFile=/home/root/.vnc/%H%i.pid

重启systemd

1
systemctl daemon-reload

vncpasswd设置VNC登录密码

1
vncpasswd

执行vncserver开启一个VNC窗口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# vncserver

You will require a password to access your desktops.

Password:
Verify:
Would you like to enter a view-only password (y/n)? n
A view-only password is not used
xauth: file /root/.Xauthority does not exist

New 'server:1 (root)' desktop is server:1

Creating default startup script /root/.vnc/xstartup
Creating default config /root/.vnc/config
Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/server:1.log

开启远程端口

1
2
3
4
5
6
7
8
# vncserver :1
A VNC server is already running as :1

New 'server:2 (root)' desktop is server:2

Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/server:2.log

设置防火墙规则,允许访问VNC-SERVER的流量通过,并重启firewall服务使之生效

1
2
3
# firewall-cmd --permanent --add-service vnc-server

# systemctl restart firewalld.service

三、客户端连接 VNC server

客户端可以使用官方,直接去 官网 下载,推荐使用 MobaXterm,集成常用 ssh,sftp 等功能。

地址栏填写:ip::5901。例如:172.20.8.31::5901

我们开启的是vncserver :1,而 vncserver 默认从5900开始,所以我们使用 5900+1 = 5901,注意需要两个 ::

接下来输入服务端设置的 vncserver 的密码就可以了。

注意关闭防火墙和selinux

其他设置参考:https://blog.csdn.net/yxc2959/article/details/79100724

谢谢你请我喝牛奶

欢迎关注我的其它发布渠道