小刘哥的笔记本

Keep It Simple, Stupid !


  • 首页

  • 关于

  • 标签

  • 分类

  • 归档

CentOS7安装使用VNC远程桌面

发表于 2019-08-17 更新于 2019-08-19 分类于 Linux Valine:

安装使用VNC

VNC (Virtual Network Console)是虚拟网络控制台的缩写。它是一款优秀的远程控制工具软件,由著名的 AT&T 的欧洲研究实验室开发的。VNC 是在基于 UNIX 和 Linux 操作系统的免费的开源软件,远程控制能力强大,高效实用,其性能可以和 Windows 和 MAC 中的任何远程控制软件媲美。

VNC基本上是由两部分组成:一部分是客户端的应用程序(vncviewer);另外一部分是服务器端的应用程序(vncserver)。VNC的基本运行原理和一些Windows下的远程控制软件很相像。VNC的服务器端应用程序在UNIX和Linux操作系统中适应性很强,图形用户界面十分友好,看上去和Windows下的软件界面也很类似。在任何安装了客户端的应用程序(vncviewer)的Linux平台的计算机都能十分方便地和安装了服务器端的应用程序(vncserver)的计算机相互连接。另外,服务器端 (vncserver)还内建了Java Web接口,这样用户通过服务器端对其他计算机的操作就能通过Netscape显示出来了,这样的操作过程和显示方式比较直观方便。

1. 在 Linux 服务器上安装配置 VNC-server

1.1 CentOS 下安装桌面环境及 VNC 服务

vnc 服务依赖于桌面环境的运行,CentOS7 默认最小安装是不带桌面环境的,所以安装 VNC 之前需要首先安装桌面环境,GNOME 或者 KDE 均可。

我们以安装 “GNOME Desktop” 桌面环境为例安装 GNOME 桌面环境。

1
2
## 列出可安装的group
$ sudo yum grouplist
1
2
3
4
5
6
## 安装 GNOME 桌面环境
$ sudo yum groupinstall "GNOME Desktop"
## 设置默认启动方式为界面启动
$ sudo ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target
## 重启服务器
$ sudo reboot

注意:对于已经安装桌面环境的 CentOS7 可以不做处理。

确认桌面环境安装完成后, 安装 VNC 服务端。

1
2
## 安装 vnc-server
$ sudo yum install tigervnc tigervnc-server

1.2 配置 vnc-server

1.2.1 拷贝配置文件

安装完成后开始配置, 将模板配置文件拷贝至配置文件目录下 /etc/systemd/system/ , 如果是多用户,则每个用户拷贝一份,其中 @:1.service 中的数字 1 对应 VNC 服务端口 5901 , 数字 2 对应端口 5902 根据自己需求设置即可。

1
2
3
4
## 复制配置模板文件
$ sudo cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service
## 编辑配置文件
$ sudo vim /etc/systemd/system/vncserver@:1.service

配置文件(/etc/systemd/system/vncserver@:1.service)内容:

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
42
43
44
45
46
47
48
# The vncserver service unit file
#
# Quick HowTo:
# 1. Copy this file to /etc/systemd/system/vncserver@.service
# 2. Replace <USER> with the actual user name and edit vncserver
# parameters appropriately
# (ExecStart=/usr/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i"
# PIDFile=/home/<USER>/.vnc/%H%i.pid)
# 3. Run `systemctl daemon-reload`
# 4. Run `systemctl enable vncserver@:<display>.service`
#
# DO NOT RUN THIS SERVICE if your local area network is
# untrusted! For a secure way of using VNC, you should
# limit connections to the local host and then tunnel from
# the machine you want to view VNC on (host A) to the machine
# whose VNC output you want to view (host B)
#
# [user@hostA ~]$ ssh -v -C -L 590N:localhost:590M hostB
#
# this will open a connection on port 590N of your hostA to hostB's port 590M
# (in fact, it ssh-connects to hostB and then connects to localhost (on hostB).
# See the ssh man page for details on port forwarding)
#
# You can then point a VNC client on hostA at vncdisplay N of localhost and with
# the help of ssh, you end up seeing what hostB makes available on port 590M
#
# Use "-nolisten tcp" to prevent X connections to your VNC server via TCP.
#
# Use "-localhost" to prevent remote VNC clients connecting except when
# doing so through a secure tunnel. See the "-via" option in the
# `man vncviewer' manual page.


[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 <USER> -c "/usr/bin/vncserver %i"
PIDFile=/home/<USER>/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'

[Install]
WantedBy=multi-user.target

1.2.2 修改配置文件

将配置文件中 <USER> 全部替换为自己的用户名,例如用户为 sam , 则修改为:

1
2
ExecStart=/usr/sbin/runuser -l sam -c "/usr/bin/vncserver %i"
PIDFile=/home/sam/.vnc/%H%i.pid

如果是使用 root 账户登录,则修改为:

1
2
ExecStart=/usr/sbin/runuser -l root -c "/usr/bin/vncserver %i"
PIDFile=/root/.vnc/%H%i.pid

1.2.3 重新加载配置文件

1
2
## 重新加载 systemctl 文件
$ sudo systemctl daemon-reload

1.2.4 添加防火墙例外

对于是默认防火墙(firewall)的,可以使用 firewall-cmd 进行配置:

1
2
3
4
5
6
7
8
9
## 方法一:添加 VNC 服务例外
$ sudo firewall-cmd --permanent --add-service vnc-server
## 方法二: 添加 5901 端口例外
$ sudo firewall-cmd --permanent --add-port 5091-5902/tcp
## 方法三:关闭并禁用防火墙(不推荐)
$ sudo systemctl stop firewalld.service
$ sudo systemctl disable firewalld.service
## 重启防火墙(适用于方法一和二)
$ sudo systemctl restart firewalld.service

对于防护墙为 iptable 的,修改相应的配置文件,添加 VNC 例外:

1
2
## 编辑配置文件
$ sudo vim /etc/sysconfig/iptables

在合适的位置添加:

1
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5901:5902 -j ACCEPT

然后重启防火墙:

1
2
## 重启防火墙
$ sudo service iptables restart

1.2.5 修改 VNC 连接密码

修改相应账户的连接密码:

1
$ vncpasswd sam

1.2.6 启动 VNC 并设置开机启动

为了方便连接,可以设置 VNC 开机启动:

1
2
3
4
5
6
7
8
## 设置开机启动
$ sudo systemctl enable vncserver@:1.service
## 启动 VNC 服务
$ sudo systemctl start vncserver@:1.service
## 查看 VNC 服务运行情况
$ sudo systemctl status vncserver@:1.service
## 停止 VNC 服务
$ sudo systemctl stop vncserver@:1.service

VNC 配置完成并启动后就可以使用本地客户机进行远程连接了。

2. 在 Windows 下安装配置 VNC 服务

下载相应版本安装 TightVNC :

  • Installer for Windows (64-bit) (2,232,320 bytes)
  • Installer for Windows (32-bit) (2,035,712 bytes)

安装完成后按照指示进行配置即可。

3. 远程连接服务器

3.1 Windows 下远程连接

Window 下使用 TightVNC-Viewer 进行连接:

填写 IP 及 端口号:

输入配置的 VNC 连接密码即可远程控制服务器:

3.2 Mac 下远程连接

Mac OS 下有天然优势,在 safari 浏览器地址栏中输入IP地址及端口即可:vnc://192.168.3.2:5901,这里需要将 IP 地址及端口更换为自己的 IP 及端口。

3.3 Linux 下远程连接

待续~

4. 参考资料

  1. http://www.baike.com/wiki/VNC

  2. https://my.oschina.net/huhaoren/blog/497394

  3. https://www.digitalocean.com/community/questions/couldn-t-start-vnc-server

  4. CentOS7 安装桌面环境并配置 VNC 远程连接

Alipe wechat
扫码关注小刘哥订阅号!
谢谢您的赏识!
Alipe 微信支付

微信支付

Alipe 支付宝

支付宝

# Linux # VNC # SSH # CentOS7
使用Hexo搭建个人博客
使用LTR装配指数(LAI)评估基因组组装质量
  • 文章目录
  • 站点概览
Alipe

Alipe

种地,学习,撸代码!
11 日志
7 分类
38 标签
RSS
GitHub E-Mail JianShu Twitter
  1. 安装使用VNC
    1. 1. 在 Linux 服务器上安装配置 VNC-server
      1. 1.1 CentOS 下安装桌面环境及 VNC 服务
      2. 1.2 配置 vnc-server
    2. 2. 在 Windows 下安装配置 VNC 服务
    3. 3. 远程连接服务器
      1. 3.1 Windows 下远程连接
      2. 3.2 Mac 下远程连接
      3. 3.3 Linux 下远程连接
    4. 4. 参考资料
© 2019 Alipe
由 Hexo 强力驱动
|
主题 – NexT.Pisces