Centos7.5 minimal系统安装VNC


在一般服务器安装系统时默认安装的minimal版本(即不带图形界面的),所以一般直接通过ssh远程连接后用指令的形式操作。对于开发环境,或者需要看图形的(比如我是做图像处理的,开发过程中我需要经常查看图像,又比如我想使用IDE开发这也肯定需要图形化界面了),拥有一个图形化界面能够方便我们快速开发以及验证结果。正式环境的话不建议安装图形化界面。 比较常用的方法就是在服务器上安装图形化界面,然后用VNC远程连接(注意,这里主要是针对需要远程连接的服务器,如果是自己使用的实体机,直接安装带图形界面的完整版就行了)。

安装图像化界面

环境配置操作都是在root用户下进行的:

  • 更新系统
yum upgrade -y
  • 更新安装依赖环境,否则后面安装图形界面时会报错check error: file /boot/efi/EFI/centos from install of fwupdate-efi-12-5.e17.centos.x86_64 conflicts with file from package grub2-common-1:2.02-0.64.el7.centos.noarch
yum update grub2-common
yum install fwupdate-efi
  • 安装图形化界面
yum -y groupinstall "GNOME Desktop" "Graphical Administration Tools"
  • 重启
reboot

安装VNC

  • 安装tigervnc-server
yum install -y tigervnc-server

我安装的版本号为:tigervnc-server.x86_64 0:1.8.0-22.el7

为root用户配置VNC

当前用户为root用户

  • 复制并重命名vnc启动文件至/etc/systemd/system/
[root@centos ~]# cp /lib/systemd/system/[email protected] /etc/systemd/system/vncsercer@:1.service
  • 设置root的VNC登录密码
[root@centos ~]# vncpasswd
Password:
Verify:
Would you like to enter a view-only password (y/n)? n
A view-only password is not used
  • 打开刚刚复制的文件
vi /etc/systemd/system/vncserver@:1.service

打开后是内容如下(未修改过):

# The vncserver service unit file
#
# Quick HowTo:
# 1. Copy this file to /etc/systemd/system/[email protected]
# 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 <USER> %i
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'

[Install]
WantedBy=multi-user.target

根据提示Replace <USER> with the actual user name只需要修改<USER>root即可:

ExecStart=/usr/bin/vncserver_wrapper root %i
  • 接着关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
  • 启动vnc服务并查看状态
systemctl daemon-reload
systemctl start vncserver@:1.service
systemctl status vncserver@:1.service

开启成功:

[root@centos ~]# systemctl status vncserver@:1.service
● vncserver@:1.service - Remote desktop service (VNC)
   Loaded: loaded (/etc/systemd/system/vncserver@:1.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2021-01-18 17:59:52 CST; 6s ago
  Process: 4691 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS)
 Main PID: 4696 (vncserver_wrapp)
   CGroup: /system.slice/system-vncserver.slice/vncserver@:1.service
           ├─4696 /bin/sh /usr/bin/vncserver_wrapper root :1
           └─4919 sleep 0.1
  • 设置开机自启动
systemctl enable vncserver@:1.service
  • 如果需要修改分辨率的话,编辑config文件:
vi ~/.vnc/config

geometry设置成你想要使用的分辨率:

## Supported server options to pass to vncserver upon invocation can be listed
## in this file. See the following manpages for more: vncserver(1) Xvnc(1).
## Several common ones are shown below. Uncomment and modify to your liking.
##
# securitytypes=vncauth,tlsvnc
# desktop=sandbox
geometry=1920x1080
# localhost
# alwaysshared
  • 重启vnc服务:
systemctl restart vncserver@:1.service

为自定义用户配置VNC

  • 进入自定义用户账号(这里以用户centos为例)
su centos

注意现在已经是在非root用户下了

  • 设置centos用户的VNC登录密码
[centos@centos ~]$ vncpasswd
Password:
Verify:
Would you like to enter a view-only password (y/n)? n
A view-only password is not used
  • 再登录root账号,后面的配置需要权限
su
  • 复制并重命名vnc启动文件至/etc/systemd/system/(注意刚刚root用的是:1这里是:2
[root@centos ~]# cp /lib/systemd/system/[email protected] /etc/systemd/system/vncsercer@:2.service
  • 打开刚刚复制的文件
vi /etc/systemd/system/vncserver@:2.service

同样将<USER>修改为当前用户名centos即可:

ExecStart=/usr/bin/vncserver_wrapper centos %i
  • 启动vnc服务并查看状态
systemctl daemon-reload
systemctl start vncserver@:2.service
systemctl status vncserver@:2.service

开启成功:

[root@centos ~]# systemctl status vncserver@:2.service
● vncserver@:2.service - Remote desktop service (VNC)
   Loaded: loaded (/etc/systemd/system/vncserver@:2.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2021-01-19 09:34:52 CST; 6s ago
  Process: 14877 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS)
 Main PID: 14881 (vncserver_wrapp)
   CGroup: /system.slice/system-vncserver.slice/vncserver@:2.service
           ├─14881 /bin/sh /usr/bin/vncserver_wrapper centos :2
           └─15221 sleep 0.1
  • 设置开机自启动
systemctl enable vncserver@:2.service
  • 如果需要修改分辨率的话,编辑config文件(注意这是是要修改对应用户目录下的config):
vi /home/centos/.vnc/config

geometry设置成你想要使用的分辨率:

## Supported server options to pass to vncserver upon invocation can be listed
## in this file. See the following manpages for more: vncserver(1) Xvnc(1).
## Several common ones are shown below. Uncomment and modify to your liking.
##
# securitytypes=vncauth,tlsvnc
# desktop=sandbox
geometry=1920x1080
# localhost
# alwaysshared
  • 重启vnc服务:
systemctl restart vncserver@:2.service

远程连接VNC图形化界面

远程连接VNC的话可以使用VNC Viewer工具,直接在官方网址上下载,基本支持所有主流操作系统。下面是以windows客户端为例,安装完成后,点击左上角的File->New connection创建连接。在VNC Server中输入需要远程的主机ip+:+连接的用户标识。比如刚刚root用户的标识是1那么就输入ip:1,如果要连接自定义用户centos,那么就输入ip:2。点击OK就会提示输入密码,就是上面创建的对应用户密码,然后就能连接登录使用了。
vncviewer登录后展示:
登录VNC Server


VNC异常中断后无法启动

今天连接使用VNC时突然异常中断了,重启服务也没有用(重启服务后通过systemctl status指令查看发现成功启动后立即退出了)。然后通过ps -ef | grep vnc指令发现了一堆僵尸进程,猜测时异常中断导致的,然后全部kill掉,再重启服务就能够正常使用了。

猜你喜欢

转载自blog.csdn.net/qq_37541097/article/details/112782495
今日推荐