[环境搭建] Windows环境下使用 VNC 连接CentOS

1. 准备CentOS环境

2. 为CentOS配置yum源

2.1 使用iso镜像文件作为yum源

有些时候,操作系统因为各种原因个无法连外网,这个时候就需要挂载包含全软件包的ISO镜像作为yum源,这样比较快捷。

2.1.1 备份现有的yum配置文件

创建备份目录:mkdir /etc/yum.repos.d/bak

将现有的所有配置移入备份目录:mv /etc/yum.repos.d/* /etc/yum.repos.d/bak/

创建本地yum配置:touch /etc/yum.repos.d/local.repo

2.1.2 下载ios文件,并把iso文件放到/mnt目录下

推荐华为的仓库:http://mirrors.huaweicloud.com/centos/7.9.2009/isos/x86_64/CentOS-7-x86_64-Everything-2009.iso

注:推荐使用everything版本,防止yum的时候缺少依赖包,浪费点下载事件和存储空间换取便利性。

通过ftp、http或mobaxterm的上传文件功能把文件传到系统的 /mnt 目录下。

这里推荐用 mobaxterm 上传文件,比较简单。

现在我们的iso文件全路径为:

/mnt/CentOS-7-x86_64-Everything-2009.iso

2.1.3 挂载iso文件

mount -t iso9660 /mnt/CentOS-7-x86_64-Everything-2009.iso /opt/centos

至此,我们便可以访问/opt/centos目录了,这里面的内容就是iso里面的内容。

2.1.4 编写本地仓库配置

把如下内容写入 2.1.1创建的配置文件/etc/yum.repos.d/local.repo中:

[local]

name=local

baseurl=file:///opt/centos

enabled=1

gpgcheck=0

2.1.5 创建yum缓存

yum clean

yum makecache

2.1.6 设置开机自动挂载

在/etc/fstab中,添加最后的一行内容如下:

/mnt/CentOS-7-x86_64-Everything-2009.iso /opt/centos iso9660 loop 0 0

PS:如果不想挂载了,可以取消挂载:

umount /opt/centos 

2.2 使用网络yum源

参考:[其他] 使用yum管理linux软件包_ykun089的博客-CSDN博客

3. 安装和配置tigervnc-server

目前市面上vnc server有两个常用版本,一个是realvnc,一个是tigervnc。realvnc需要向社区订阅并申请license,比较麻烦,而tigervnc是CentOS发行版的标配,因此考虑使用tigervnc server。

3.1 安装tigervnc-server

yum -y install tigervnc-server

3.2 查看安装包信息

[root@localhost etc]# rpm -qi tigervnc-server
Name        : tigervnc-server
Version     : 1.8.0
Release     : 21.el7
Architecture: x86_64
Install Date: Sat 21 Nov 2020 01:06:07 AM CST
Group       : User Interface/X
Size        : 518102
License     : GPLv2+
Signature   : RSA/SHA256, Thu 15 Oct 2020 03:02:28 AM CST, Key ID 24c6a8a7f4a80eb5
Source RPM  : tigervnc-1.8.0-21.el7.src.rpm
Build Date  : Wed 14 Oct 2020 12:13:38 AM CST
Build Host  : x86-01.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://www.tigervnc.com
Summary     : A TigerVNC server
Description :
The VNC system allows you to access the same desktop from a wide
variety of platforms.  This package includes set of utilities
which make usage of TigerVNC server more user friendly. It also
contains x0vncserver program which can export your active
X session.

3.3 查看安装包对应的文件

[root@localhost etc]# rpm -ql tigervnc-server
/etc/sysconfig/vncservers
/usr/bin/vncserver
/usr/bin/vncserver_wrapper
/usr/bin/x0vncserver
/usr/lib/systemd/system/vncserver@.service
/usr/lib/systemd/system/xvnc.socket
/usr/lib/systemd/system/xvnc@.service
/usr/share/man/man1/vncserver.1.gz
/usr/share/man/man1/x0vncserver.1.gz

3.4 查看tigervnc对应的配置文件

[root@localhost etc]# rpm -qc tigervnc-server
/etc/sysconfig/vncservers

3.5 设置vnc密码

vnc密码是我们用vnc viewer链接vnc server的时候需要输入的,相当于vnc server自己管理了一个账户系统。

[root@localhost etc]# vncserver

You will require a password to access your desktops.

Password:
Verify:
Would you like to enter a view-only password (y/n)? n
A view-only password is not used

New 'kvmserver:1 (root)' desktop is kvmserver:1

Creating default startup script /root/.vnc/xstartup
Creating default config /root/.vnc/config
Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/kvmserver:1.log

3.6 查看vnc进程信息和服务端口

[root@localhost etc]# vncserver -list

TigerVNC server sessions:

X DISPLAY #     PROCESS ID
:1              29993
:2              30628


[root@localhost etc]# ps aux | grep 29993

[root@localhost etc]# ps aux | grep 30628


[root@localhost etc]# netstat -anp | grep vnc
tcp        0      0 0.0.0.0:5901            0.0.0.0:*               LISTEN      29993/Xvnc
tcp        0      0 0.0.0.0:5902            0.0.0.0:*               LISTEN      30628/Xvnc
tcp        0      0 0.0.0.0:6001            0.0.0.0:*               LISTEN      29993/Xvnc
tcp        0      0 0.0.0.0:6002            0.0.0.0:*               LISTEN      30628/Xvnc
tcp        0      0 192.168.13.148:5901     192.168.13.78:11966     ESTABLISHED 29993/Xvnc
tcp6       0      0 :::5901                 :::*                    LISTEN      29993/Xvnc
tcp6       0      0 :::5902                 :::*                    LISTEN      30628/Xvnc
tcp6       0      0 :::6001                 :::*                    LISTEN      29993/Xvnc
tcp6       0      0 :::6002                 :::*                    LISTEN      30628/Xvnc
unix  2      [ ACC ]     STREAM     LISTENING     594815   29993/Xvnc           @/tmp/.X11-unix/X1
unix  2      [ ACC ]     STREAM     LISTENING     603126   30628/Xvnc           

其中的 5901~6001 端口就是服务监听端口可用端口,我们在vnc viewer中连接的时候就连此端口。

注意:如果两个会话连接同一个端口,那么这两个会话的画面将是一样的。

3.7 设置分辨率

[root@localhost etc]# vncserver -geometry 1920x1080

New 'kvmserver:2 (root)' desktop is kvmserver:2

Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/kvmserver:2.log

3.8 配置防火墙

[root@localhost etc]# firewall-cmd --get-services
RH-Satellite-6 amanda-client amanda-k5-client amqp amqps apcupsd audit bacula bacula-client bgp bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client distcc dns docker-registry docker-swarm dropbox-lansync elasticsearch etcd-client etcd-server finger freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master git gre high-availability http https imap imaps ipp ipp-client ipsec irc ircs iscsi-target isns jenkins kadmin kerberos kibana klogin kpasswd kprop kshell ldap ldaps libvirt libvirt-tls lightning-network llmnr managesieve matrix mdns minidlna mongodb mosh mountd mqtt mqtt-tls ms-wbt mssql murmur mysql nfs nfs3 nmea-0183 nrpe ntp nut openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole plex pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius redis rpc-bind rsh rsyncd rtsp salt-master samba samba-client samba-dc sane sip sips slp smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh steam-streaming svdrp svn syncthing syncthing-gui synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client upnp-client vdsm vnc-server wbem-http wbem-https wsman wsmans xdmcp xmpp-bosh xmpp-client xmpp-local xmpp-server zabbix-agent zabbix-server

[root@localhost etc]# firewall-cmd --info-service=vnc-server
vnc-server
  ports: 5900-5903/tcp
  protocols:
  source-ports:
  modules:
  destination:

[root@localhost etc]# firewall-cmd --add-service=vnc-server --permanent
success

[root@localhost etc]# firewall-cmd --reload
success

[root@localhost etc]# firewall-cmd --list-services
dhcpv6-client ssh vnc-server

3.9 在windows上使用vnc viewer连接

PS:在链接之前,要先去server端看一下那些端口可用

[root@localhost etc]# netstat -anp | grep vnc
tcp        0      0 0.0.0.0:5901            0.0.0.0:*               LISTEN      29993/Xvnc
tcp        0      0 0.0.0.0:5902            0.0.0.0:*               LISTEN      30628/Xvnc
tcp        0      0 0.0.0.0:6001            0.0.0.0:*               LISTEN      29993/Xvnc
tcp        0      0 0.0.0.0:6002            0.0.0.0:*               LISTEN      30628/Xvnc
tcp        0      0 192.168.13.148:5901     192.168.13.78:11966     ESTABLISHED 29993/Xvnc
tcp6       0      0 :::5901                 :::*                    LISTEN      29993/Xvnc
tcp6       0      0 :::5902                 :::*                    LISTEN      30628/Xvnc
tcp6       0      0 :::6001                 :::*                    LISTEN      29993/Xvnc
tcp6       0      0 :::6002                 :::*                    LISTEN      30628/Xvnc
unix  2      [ ACC ]     STREAM     LISTENING     594815   29993/Xvnc           @/tmp/.X11-unix/X1
unix  2      [ ACC ]     STREAM     LISTENING     603126   30628/Xvnc       

上面可见5901,5902是可用的,因为6001 和 6002不再防火墙配置范围内,所以不可用。

3.10 最终效果

4.一些问题

4.1.vnc Viewer无法连接

查看vncserver相应界面进程的tcp地址绑定情况:

上图中127.0.0.1的都是仅供本地vnc Viewer登录使用的,这这个问题会导致别的机器上的vnc Viewer连不上。此时需要在启动vncserver 的窗口实例时指定 -localhost no 参数。

即 vncserver -loacalhost no 。此时创建的窗口实例绑定的socket信息如下:

这样便可以从其他机器上访问了。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值