1. 安装 X-Window(X-Window版本,后面还有GNOME版本)

首先我们需要安装 X-Window,在终端中运行下面的命令,安装会花费一点时间。

# yum check-update# yum groupinstall "X Window System"

installing x windows

installing x windows

#yum install gnome-classic-session gnome-terminal nautilus-open-terminal control-center liberation-mono-fonts

install gnome classic session

install gnome classic session

### 设置默认启动图形界面# unlink /etc/systemd/system/default.target# ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target

configuring graphics

configuring graphics

# reboot

在服务器重启之后,我们就有了一个工作着的 CentOS 7 桌面环境了。

现在,我们要在服务器上安装 VNC 服务器了。

2. 安装 VNC 服务器

现在要在我们的 CentOS 7 上安装 VNC 服务器了。我们需要执行下面的命令。

# yum install tigervnc-server -y

vnc server

vnc server

3. 配置 VNC

然后,我们需要在 /etc/systemd/system/ 目录里创建一个配置文件。我们可以将 /lib/systemd/sytem/vncserver@.service拷贝一份配置文件范例过来。

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

copying vnc server configuration

copying vnc server configuration

接着我们用自己最喜欢的编辑器(这儿我们用的 nano )打开 /etc/systemd/system/vncserver@:1.service ,找到下面这几行,用自己的用户名替换掉 。举例来说,我的用户名是 linoxide 所以我用 linoxide 来替换掉 :

ExecStart=/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i"PIDFile=/home/<USER>/.vnc/%H%i.pid

替换成

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

如果是 root 用户则

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

configuring user

configuring user

好了,下面重启 systemd 。

# systemctl daemon-reload

最后还要设置一下用户的 VNC 密码。要设置某个用户的密码,必须要有能通过 sudo 切换到用户的权限,这里我用 linoxide 的权限,执行“su linoxide”就可以了。

# su linoxide$ sudo vncpasswd

setting vnc password

setting vnc password

确保你输入的密码多于6个字符

4. 开启服务

用下面的命令(永久地)开启服务:

$ sudo systemctl enable vncserver@:1.service

启动服务。

$ sudo systemctl start vncserver@:1.service启动时报错的话,将type=forking改为type=simple

5. 防火墙设置

我们需要配置防火墙来让 VNC 服务正常工作。

$ sudo firewall-cmd --permanent --add-service vnc-server$ sudo systemctl restart firewalld.service

allowing firewalld

allowing firewalld

现在就可以用 IP 和端口号(LCTT 译注:例如 192.168.1.1:1 ,这里的端口不是服务器的端口,而是视 VNC 连接数的多少从1开始排序)来连接 VNC 服务器了。

6. 用 VNC 客户端连接服务器

好了,现在已经完成了 VNC 服务器的安装了。要使用 VNC 连接服务器,我们还需要一个在本地计算机上安装的仅供连接远程计算机使用的 VNC 客户端。

remote access vncserver from vncviewer

remote access vncserver from vncviewer

你可以用像 Tightvnc viewer 和 Realvnc viewer 的客户端来连接到服务器。

要用更多的用户连接,需要创建配置文件和端口,请回到第3步,添加一个新的用户和端口。你需要创建vncserver@:2.service 并替换配置文件里的用户名和之后步骤里相应的文件名、端口号。请确保你登录 VNC 服务器用的是你之前配置 VNC 密码的时候使用的那个用户名

VNC 服务本身使用的是5900端口。鉴于有不同的用户使用 VNC ,每个人的连接都会获得不同的端口。配置文件名里面的数字告诉 VNC 服务器把服务运行在5900的子端口上。在我们这个例子里,第一个 VNC 服务会运行在5901(5900 + 1)端口上,之后的依次增加,运行在5900 + x 号端口上。其中 x 是指之后用户的配置文件名 vncserver@:x.service 里面的 x 。

在建立连接之前,我们需要知道服务器的 IP 地址和端口。IP 地址是一台计算机在网络中的独特的识别号码。我的服务器的 IP 地址是96.126.120.92,VNC 用户端口是1。

执行下面的命令可以获得服务器的公网 IP 地址(LCTT 译注:如果你的服务器放在内网或使用动态地址的话,可以这样获得其公网 IP 地址)。

# curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'

总结

好了,现在我们已经在运行 CentOS 7 / RHEL 7 的服务器上安装配置好了 VNC 服务器。VNC 是自由开源软件中最简单的一种能实现远程控制服务器的工具,也是一款优秀的 Teamviewer Remote Access 替代品。VNC 允许一个安装了 VNC 客户端的用户远程控制一台安装了 VNC 服务的服务器。下面还有一些经常使用的相关命令。好好玩!

其他命令:
  • 关闭 VNC 服务。


  1. # systemctl stop vncserver@:1.service

禁止 VNC 服务开机启动。

  1. # systemctl disable vncserver@:1.service

关闭防火墙。

  1. # systemctl stop firewalld.service


1. 安装 GNOME(GNOME版本)

Centso7安装vnc实现图形化访问

安装桌面

yum groupinstall "GNOME Desktop"------可能会报错,详见安装X11安装图形化教程,安装docker 和 libdevmapper*  2个包就可以了

安装vnc server

yum install tigervnc-server

拷贝配置文件

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

编辑修改配置文件

vi /etc/systemd/system/vncserver@:1.service 

#把配置文件中的,改成root和root的家目录路径。

[Service]

Type=simple

# Clean any existing files in /tmp/.X11-unix environment

ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i> /dev/null 2>&1 || :'

#ExecStart=/sbin/runuser -l -c "/usr/bin/vncserver %i"

#PIDFile=/home//.vnc/%H%i.pid

ExecStart=/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 || :'

 

启动vnc,设置密码vncpasswd。

vncserver

#然后按照提示输入两次密码

设置开机自动启动:

systemctl daemon-reload

systemctl enable vncserver@:1.service

客户端连接vnc,在vnc客户端输入ip:5901 进行连接,输入设置的vnc密码登录。

其他:

本文是以root用户为例,如果其他用户也需要登录,那么,还需要将文件复制为 
cp /lib/systemd/system/vncserver@.service /lib/systemd/system/vncserver@:2.service 
同时,将文件内的%i改为:2,并建立对应用户的vncpasswd。 

 5.  启用配置文件 systemctl enable vncserver@:1.service

 6. 设置登陆密码:vncpasswd

 7. 启动vncserver:systemctl start vncserver@:1.service

 8 . 启动状态查看:systemctl status vncserver@:1.service

 9.查看端口状态:netstat -lnt | grep 590*

 10.查看报错信息:grep vnc /var/log/messages

11报错:

Warning: localhost.localdomain:1 is taken because of /tmp/.X11-unix/X1

Remove this file if there is no X server localhost.localdomain:1

执行:rm -rf /tmp/.X11-unix/*