VNC是个比较不错的远程工具,无论基于Windows平台或Linux平台,由于最近在学习Centos的相关知识,所以准备在Centos 7上搭建一个VNC server便于我从客户端机器通过VNC viewer来远程连接操作。

如果你没有安装桌面环境(X Windows),可以使用以下命令来安装,大概花费几分钟时间来安装此packages.

[[email protected] ~]# yum check-update
[[email protected] ~]# yum groupinstall "X Windows System"
[[email protected] ~]# yum install gnome-classic-session gnome-terminal nautilus-open-terminal control-center liberation-mono-fonts
[[email protected] ~]# unlink /etc/systemd/system/default.target
[[email protected] ~]# ln
-sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target
[[email protected] ~]# reboot

重启后就可以进入到Centos 7的桌面环境了。

Step 1 输入以下命令安装VNC 包

[[email protected] ~]# yum install tigervnc-server -y

Step 2 在/etc/systemd/system/目录创建一个文件[email protected]:1.service,并复制实例配置文件/lib/systemd/system/[email protected]至其中

[[email protected] ~]# cp /lib/systemd/system/[email protected] /etc/systemd/system/[email protected]:1.service

Step 3 打开并编辑/etc/systemd/system/[email protected]:1.service文件并用你自己的用户名取代其中的user项

[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=/sbin/runuser -l root -c "/usr/bin/vncserver %i"
PIDFile=/root/.vnc/%H%i.pid

注意如上root,我所使用的是root账号登陆,所以将<user>更改为root

Step 4 重启systemd

[[email protected] system]# systemctl daemon-reload

Step 5 为用户创建VNC密码

[[email protected] system]# vncpasswd

此处我创建密码为123456

Step 6 启用并启动服务

[[email protected] system]# systemctl enable [email protected]:1.service
ln -s '/etc/systemd/system/[email protected]:1.service' '/etc/systemd/system/multi-user.target.wants/[email protected]:1.service'
[[email protected] system]# systemctl start [email protected]:1.service

Step 7 允许VNC服务通过防火墙

[[email protected] ~]# firewall-cmd --permanent --add-service vnc-server
[[email protected] ~]# systemctl restart firewalld.service

现在我可以通过使用运行在Win10 上的VNC viewer客户端来远程查看Centos 7的图形界面

在Centos 7上安装VNC server

在Centos 7上安装VNC server可以看到Centos 7的桌面环境了

若需停止VNC服务,输入如下指令。

[[email protected] ~]# systemctl stop [email protected]:1.service

禁用VNC服务从系统启动(永久)

[[email protected] ~]# systemctl disable [email protected]:1.service

停止防火墙服务

[[email protected] ~]# systemctl stop firewall.service

Have a nice day!