1. 如果没有安装 X-Windows 桌面的话要先安装 Xwindows
- [[email protected] ~]# yum check-update
- [[email protected] ~]# yum groupinstall "X Window System"
- [[email protected] ~]# yum install gnome-classic-session gnome-terminal nautilus-open-terminal control-center liberation-mono-fonts
- [[email protected] ~]# unlink /etc/systemd/system/default.target
- [[email protected] ~]# ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target
- [[email protected] ~]# reboot
2. 查看是否已安装 vnc:
[[email protected] ~]# rpm -q tigervnc tigervnc-server
3.yum 安装安装 VNC 组件
[[email protected] ~]# yum install tigervnc-server -y
4. 修改配置信息.
在 / etc/systemd/system / 下建立文件 vncserver.service , 文件内容从 / lib/systemd/system/[email protected] 复制
[[email protected] ~]# cp /lib/systemd/system/[email protected] /etc/systemd/system/vncserver.service
然后打开这个配置文件 / etc/systemd/system/vncserver.service 替换掉默认用户名
找到这一行
- ExecStart=/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i"
- PIDFile=/home/<USER>/.vnc/%H%i.pid
这里我直接用 root 用户登录, 所以我替换成
- ExecStart=/sbin/runuser -l root -c "/usr/bin/vncserver %i"
- PIDFile=/root/.vnc/%H%i.pid
如果是其他用户的话比如 zhangsan 替换如下
- ExecStart=/sbin/runuser -l zhangsan -c "/usr/bin/vncserver %i"
- PIDFile=/home/zhangsan/.vnc/%H%i.pid
5. 重加载 systemd
[[email protected] ~]# systemctl daemon-reload
6. 为 VNC 设密码
[[email protected] ~]# vncpasswd
7. 修改防火墙设置
- iptables:
- VIM /etc/sysconfig/iptables
加入:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5900:5903 -j ACCEPT
重启防火墙服务:
service iptables restart
如果防火墙是 firewalld:
- [[email protected] ~]# firewall-cmd --permanent --add-service vnc-server
- [[email protected] ~]# firewall-cmd --reload
8. 设置 vncserver 服务
- [[email protected] ~]# systemctl enable vncserver #设置开机自启动
- [[email protected] ~]# systemctl start vncserver #启动服务
9. 这样基本上 CentOS 端就设好了, Windows 端要去下一个 VNC Viewer 的软件.
需要说明的是, 如果直接连接 ip 或者 ip:5900, 会提示 the connection was refused by the host computer. 无法连接.
正确的写法是 IP 加端口号, 但是这边的端口号不是设置的 5900, 而是在 5900 基础上加上虚拟机中的 VNC 序号, 如第一张图中序号为 1. 正确写法是 IP:5901 (如我的机子填写 192.168.58.136:5901)
转录: https://www.cnblogs.com/tibit/p/6144397.html
来源: http://www.bubuko.com/infodetail-3266060.html