使用yum安装VNC-Server
等待系统安装完毕即可,我这里之前安装过了,所以tigervnc-server存在。
[root@localhost ~]# yum install vnc-server -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.dgut.edu.cn
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/3): epel/x86_64/primary_db | 7.0 MB 00:00:00
(2/3): epel/x86_64/updateinfo | 1.0 MB 00:00:00
(3/3): updates/7/x86_64/primary_db | 13 MB 00:00:02
Package tigervnc-server-1.8.0-22.el7.x86_64 already installed and latest version
Nothing to do
[root@localhost ~]#
配置VNC-Server服务文件
安装vnc-server完成,将vncserver文件从/lib/systemd/system里面拷贝到/etc/systemd/system目录下。设置vncserver的密码,view-only password我这里选择 'y' 可以直接输入 'n'。
[root@localhost ~]# cp /lib/systemd/system/vncserver@.service /etc/systemd/system/
[root@localhost system]# cp vncserver@.service vncserver@\:1.service
[root@localhost system]# ls
basic.target.wants dbus-org.freedesktop.nm-dispatcher.service getty.target.wants pki-tomcatd-nuxwdog.target.wants system-update.target.wants
bluetooth.target.wants default.target graphical.target.wants pki-tomcatd.target.wants timers.target.wants
dbus-org.bluez.service default.target.wants httpd.d printer.target.wants vmtoolsd.service.requires
dbus-org.fedoraproject.FirewallD1.service dev-virtio\x2dports-org.qemu.guest_agent.0.device.wants local-fs.target.wants remote-fs.target.wants vncserver@:1.service
dbus-org.freedesktop.Avahi.service dirsrv.target.wants multi-user.target.wants sockets.target.wants vncserver@.service
dbus-org.freedesktop.ModemManager1.service display-manager.service network-online.target.wants sysinit.target.wants
[root@localhost system]#
#设置vnc密码,view-only password 可以选择 n
[root@localhost system]# vncpasswd
Password:
Verify:
Would you like to enter a view-only password (y/n)? y
Password:
Verify:
[root@localhost system]#
修改vncserver文件,将ExecStart=/usr/bin/vncserver_wrapper的[User]改为你想远程的用户名,打印vncserver@:1.service文件内容如下。
[root@localhost system]# vim vncserver@\:1.service
[root@localhost system]# cat vncserver@\:1.service
# 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 in the wrapper script located in /usr/bin/vncserver_wrapper
# 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=simple
# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
ExecStart=/usr/bin/vncserver_wrapper root %i
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
[Install]
WantedBy=multi-user.target
[root@localhost system]#
启动vnc服务,restart服务时系统出现Warning,可按提示输入systemctl daemon-relod再重启一下服务。将vnc服务设置开机自启动,查看端口是否正常启用(通常端口是以5900开始,这里显示正常,表示配置成功)。
[root@localhost system]# systemctl restart vncserver@\:1.service
Warning: vncserver@:1.service changed on disk. Run 'systemctl daemon-reload' to reload units.
[root@localhost system]# systemctl daemon-reload
[root@localhost system]# systemctl restart vncserver@\:1.service
[root@localhost system]# systemctl enable vncserver@\:1.service
Created symlink from /etc/systemd/system/multi-user.target.wants/vncserver@:1.service to /etc/systemd/system/vncserver@:1.service.
[root@localhost system]#
[root@localhost system]# netstat -lnp | grep vnc
tcp 0 0 0.0.0.0:5901 0.0.0.0:* LISTEN 15752/Xvnc
tcp 0 0 0.0.0.0:6001 0.0.0.0:* LISTEN 15752/Xvnc
tcp6 0 0 :::5901 :::* LISTEN 15752/Xvnc
tcp6 0 0 :::6001 :::* LISTEN 15752/Xvnc
unix 2 [ ACC ] STREAM LISTENING 148136 15752/Xvnc @/tmp/.X11-unix/X1
unix 2 [ ACC ] STREAM LISTENING 148137 15752/Xvnc /tmp/.X11-unix/X1
[root@localhost system]#
在端口正常开启后,需配置防火墙,使其开放vnc端口。若命令不能成功使用,则需开启防火墙,也可直接关闭防火墙。还需关闭SELinux,找到SELINUX将后面设置成disabled。
[root@localhost system]# firewall-cmd --zone=public --add-port=5901/tcp --permanent
success
[root@localhost system]# systemctl start firewalld
[root@localhost system]#
[root@localhost system]# vim /etc/sysconfig/selinux
[root@localhost system]# cat /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
使用MobaXterm连接Centos
打开MobaXterm,点击Session选择vnc连接,输入需要远程的系统IP,选择正确的端口号,点击确定等待连接。
以上配置正确,即可连接Centos系统,并登录进系统(在登录进系统的时候,需之前设置vnc密码登录)。
~文章已经结束了~
声明:本人所写的只是本人在使用中遇到的问题做个记录总结,很多内容也是百度,或者查阅官方文档,可能与他人有重复;若需按照本文配置,请认真确认是否适合你的情况,造成一切损失;本人概不负责。