天天看点

Centos7.5 minimal系统安装VNC安装图像化界面安装VNC为root用户配置VNC为自定义用户配置VNC远程连接VNC图形化界面VNC异常中断后无法启动

文章目录

  • 安装图像化界面
  • 安装VNC
  • 为root用户配置VNC
  • 为自定义用户配置VNC
  • 远程连接VNC图形化界面
  • 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/

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

    root

    的VNC登录密码
[[email protected] ~]# 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/[email protected]: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 [email protected]:<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)
#
# [[email protected] ~]$ 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 [email protected]:1.service
systemctl status [email protected]:1.service
           

开启成功:

[[email protected] ~]# systemctl status [email protected]:1.service
● [email protected]:1.service - Remote desktop service (VNC)
   Loaded: loaded (/etc/systemd/system/[email protected]: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/[email protected]:1.service
           ├─4696 /bin/sh /usr/bin/vncserver_wrapper root :1
           └─4919 sleep 0.1
           
  • 设置开机自启动
systemctl enable [email protected]: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 [email protected]:1.service
           

为自定义用户配置VNC

  • 进入自定义用户账号(这里以用户

    centos

    为例)
su centos
           

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

  • 设置

    centos

    用户的VNC登录密码
[[email protected] ~]$ 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

[[email protected] ~]# cp /lib/systemd/system/[email protected] /etc/systemd/system/[email protected]:2.service
           
  • 打开刚刚复制的文件
vi /etc/systemd/system/[email protected]:2.service
           

同样将

<USER>

修改为当前用户名

centos

即可:

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

开启成功:

[[email protected] ~]# systemctl status [email protected]:2.service
● [email protected]:2.service - Remote desktop service (VNC)
   Loaded: loaded (/etc/systemd/system/[email protected]: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/[email protected]:2.service
           ├─14881 /bin/sh /usr/bin/vncserver_wrapper centos :2
           └─15221 sleep 0.1
           
  • 设置开机自启动
systemctl enable [email protected]: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 [email protected]:2.service
           

远程连接VNC图形化界面

远程连接VNC的话可以使用

VNC Viewer

工具,直接在官方网址上下载,基本支持所有主流操作系统。下面是以windows客户端为例,安装完成后,点击左上角的

File

->

New connection

创建连接。在

VNC Server

中输入需要远程的主机

ip

+

:

+连接的用户标识。比如刚刚

root

用户的标识是

1

那么就输入

ip:1

,如果要连接自定义用户

centos

,那么就输入

ip:2

。点击

OK

就会提示输入密码,就是上面创建的对应用户密码,然后就能连接登录使用了。

Centos7.5 minimal系统安装VNC安装图像化界面安装VNC为root用户配置VNC为自定义用户配置VNC远程连接VNC图形化界面VNC异常中断后无法启动

登录后展示:

Centos7.5 minimal系统安装VNC安装图像化界面安装VNC为root用户配置VNC为自定义用户配置VNC远程连接VNC图形化界面VNC异常中断后无法启动

VNC异常中断后无法启动

今天连接使用VNC时突然异常中断了,重启服务也没有用(重启服务后通过

systemctl status

指令查看发现成功启动后立即退出了)。然后通过

ps -ef | grep vnc

指令发现了一堆僵尸进程,猜测时异常中断导致的,然后全部kill掉,再重启服务就能够正常使用了。

继续阅读