CentOS 8.0.1905
服务器上初始安装没有安装图形化桌面环境,现在需要对该服务器安装图形桌面化环境和安装TigerVNC
,并在本地(windows平台)使用VNC
客户端进行远程桌面连接
环境说明
- Microsoft Windows 10 Pro 21H2
- VMware® Workstation 15.0.0
- OS: CentOS Linux 8
- IMG: CentOS-8-x86_64-1905-dvd1.iso
- Xvnc TigerVNC 1.11.0 - built Jul 21 2021 11:25:16
使用华为云CentOS-Vault
现在 CentOS 8(非 Stream 版本)已被官方移除出该仓库。需要使用centos-vault
镜像
先浏览官网链接,查看使用说明
官网链接: https://mirrors.huaweicloud.com/home
备份/etc/yum.repos.d 目录下的文件
1 | [root@centos-106 yum.repos.d]# cp -a /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak |
或
1 | 创建备份目录 |
配置华为云CentOS-Vault yum源
1 | [root@centos-106 yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-8-reg.repo |
执行yum clean all清除原有yum缓存。
执行yum makecache(刷新缓存)或者yum repolist all(查看所有配置可以使用的文件,会自动刷新缓存)
1 | [root@centos-106 yum.repos.d]# yum clean all |
安装配置图形桌面环境
关闭SELINUX,后面配置完成再启用
1 | [root@centos-106 selinux]# nano /etc/selinux/config |
查看防火墙状态
1 | [root@centos-106 ~]# sestatus |
安装图形化桌面环境
1 | [root@centos-106 yum.repos.d]# dnf groupinstall "Server with GUI" -y |
设置图形界面为默认运行级别
1 | systemctl set-default graphical.target |
启动图形界面
1 | systemctl start graphical.target |
重启
1 | reboot |
向导设置
重启后会看到图形化界面的一个向导,前面的基本点next
(定位服务可关),最后几步有个新建用户的步骤,建立一个用户给下一步骤中的 VNC Server服务 使用(不一定非使用vncuser这个名称)
安装 VNC Server 服务
安装 TigerVNC
以 root
用户身份运行以下命令,基本都使用root
用户权限操作(提示权限不足要在命令前加 sudo 很烦),需要切换vncuser
用户会说明
1 | [root@centos-106 ~]# dnf install tigervnc-server -y |
创建 vnc 用户并设置密码(前面已创建可跳过)
如不想使用在上一步骤建立的用户,可在这一步骤中新建用户
1 | [root@centos-106 ~]# useradd vnc_user |
拷贝 vncserver@.service 文件
创建/etc/systemd/system/vncserver@.service
配置文件。要创建这个文件,从/usr/lib/systemd/system/vncserver@.service
复制
1 | [root@centos-106 ~]# ~]# cp /usr/lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@.service |
不需要在文件名中包含显示号,因为 systemd 会自动根据需要
在内存中创建适当的命名实例,将服务文件中的 '%i'
替换为显示号。对于单个用户,无需重命名该文件。
VNC 桌面的默认大小为 1024x768。
可以使用
~/.vnc/config
文件进一步配置用户的 VNC 会话。例如,要更改 VNC 窗口大小,请添加以下行:
geometry=
x
在 /etc/tigervnc/vncserver.users 中为端口1配置用户 vncuser(即:1=vncuser)
1 | [root@centos-106 system]# nano /etc/tigervnc/vncserver.users |
从 root 切换到 vncuser,配置文件中定义的用户的连接密码
这一步骤中需要设置一个连接密码(最长八个字符,多出的字符会被切掉);在view-only password (y/n)?
选 n
;选y
只能查看不能输入
1 | [root@centos-106 ~]# su - vncuser |
查看 5901 端口 VNC server 的监听状态
lsof -i :5901 或 netstat -tulpn | grep 5901
1 | [vncuser@centos-106 ~]$ lsof -i :5901 |
配置防火墙,开放 5901 端口:
1 | [root@centos-106 ~]# firewall-cmd --permanent --zone=public --add-port=5901/tcp |
启动服务
1 | 重新加载systemd守护程序配置 |
本地使用 VNC 客户端进行远程桌面连接
VNC
客户端的选择有很多,例如以下用到的UltraVNC Viewer
;还有VNC Viewer
、MobaXterm
,任君选择
打开软件,输入 服务器地址:端口号 ,点击 Connect
输入连接密码,点击 Log On
登陆界面
踩坑
运行 yum update 之后,repo 文件的变化
在配置华为云CentOS-Vault yum源之后,运行yum update
,好家伙把/etc/yum.repos.d/
目录下*.repo
文件给干掉了,又得重复一次配置华为云CentOS-Vault yum源
的步骤
1 | [root@centos-106 yum.repos.d]# ll |
运行 vncserver 等旧格式命令的提示
在运行vncserver
开头的命令后,会有一行提示如下
1 | WARNING: vncserver has been replaced by a systemd unit and is about to be removed in future releases. |
在处理这个步骤耗费了本人非常多的时间,例如启动报错,启动不报错能连接但是黑屏各种问题。最后在CentOS论坛看到一篇帖子,看到提示去找 Red Hat 官网手册才处理好这个问题;顺便说一句搜索引擎各种关于安装VNC Server 的中文资料没一个能用(没标明环境和版本,索骥失败),最终还是去看官网手册;看来少吃别人嚼过的东西
最后
- 参考资料:
1 | - vncserver failed by ShinShin on 2020/11/17 : |
- 发现即使指定要求只下载
Server with GUI
,但安装时查看到软件包列表还是发现很多包一并安装了,这可能对已有运行服务产生影响,慎重使用,起码懂得回退的操作 - 现在服务器操作系统一般都上虚拟化了,操作这些安装升级的时候要记得做个快照,备份之类的
- 如有需要玩这个的话,强烈建议先在虚拟机多轮测试再去实际操作,多建立快照
- 配置完成后别忘了开防火墙策略SELINUX=enforcing,重开SELINUX真耗时间
- 业余选手,勿喷
- 欢迎“
来电”来函探讨