#!/bin/bash
# 日期:2020-08-13
# 功能:安装vnc-server
set -ex

# 1.安装linux默认的桌面;如果开机已安装 "gnome Desktop"的话,请忽略
yum groupinstall 'GNOME Desktop' -y

# 2.安装vnc-server
yum install tigervnc tigervnc-server -y

# 3.写入vnc配置。编号500可随意,分辨率根据实际情况给即可
echo 'VNCSERVERS="500:root"' >> /etc/sysconfig/vncservers
echo 'VNCSERVERARGS[100]="-geometry 800x600"' >> /etc/sysconfig/vncservers

# 4.设置密码

yum install -y gcc unzip expect
expect << EOF
set timeout 10
spawn vncpasswd
expect "Password:"
send "123456\n"
expect "Verify:"
send "123456\n"
expect "(y/n)?"
send "\n"
interact
EOF

# 5.开启vnc,500是用来指定vnc端口的。默认是从5900开始,500 + 5900 = 6400,所以本次vnc使用的端口是6400
vncserver :500

# 6.关闭防火墙
systemctl status firewalld |grep running && systemctl disable firewalld && systemctl stop firewalld

# 7.完成,检查端口
echo -e "\033[33m安装vnc完成,查看vnc端口 \033[0m"
yum install -y net-tools &> /dev/null && netstat -lnpt |grep 6400

# 8.设置为开机自动进入桌面
systemctl set-default graphical.target      # 设置为开机自动进入桌面

# 扩展阅读: 
# startx                                        # 使用 startx 可以进入图形化界面
# systemctl get-default                         # 获取当前启动模式
# systemctl set-default graphical.target        # 设置为开机自动进入桌面
# systemctl set-default multi-user.target       # 开机自动进入终端命令模式

# 9.设置 vncserver 为开机自动启动
chmod +x /etc/rc.local
echo `which vncserver` >> /etc/rc.local