Install_vnc_server

  • 阿里云官方脚本, 安装vnc server
#!/bin/bash
#########################################
#Function:    install vnc server
#Usage:       bash install_vnc_server.sh
#Author:      Customer service department
#Company:     Alibaba Cloud Computing
#Version:     3.0
#########################################

check_os_release()
{
  while true
  do
    os_release=$(grep "Red Hat Enterprise Linux Server release" /etc/issue 2>/dev/null)
    os_release_2=$(grep "Red Hat Enterprise Linux Server release" /etc/redhat-release 2>/dev/null)
    if [ "$os_release" ] && [ "$os_release_2" ]
    then
      if echo "$os_release"|grep "release 5" >/dev/null 2>&1
      then
        os_release=redhat5
        echo "$os_release"
      elif echo "$os_release"|grep "release 6" >/dev/null 2>&1
      then
        os_release=redhat6
        echo "$os_release"
      else
        os_release=""
        echo "$os_release"
      fi
      break
    fi
    os_release=$(grep "Aliyun Linux release" /etc/issue 2>/dev/null)
    os_release_2=$(grep "Aliyun Linux release" /etc/aliyun-release 2>/dev/null)
    if [ "$os_release" ] && [ "$os_release_2" ]
    then
      if echo "$os_release"|grep "release 5" >/dev/null 2>&1
      then
        os_release=aliyun5
        echo "$os_release"
      elif echo "$os_release"|grep "release 6" >/dev/null 2>&1
      then
        os_release=aliyun6
        echo "$os_release"
      elif echo "$os_release"|grep "release 7" >/dev/null 2>&1
      then
        os_release=aliyun7
        echo "$os_release"
      else
        os_release=""
        echo "$os_release"
      fi
      break
    fi
    os_release_2=$(grep "CentOS" /etc/*release 2>/dev/null)
    if [ "$os_release_2" ]
    then
      if echo "$os_release_2"|grep "release 5" >/dev/null 2>&1
      then
        os_release=centos5
        echo "$os_release"
      elif echo "$os_release_2"|grep "release 6" >/dev/null 2>&1
      then
        os_release=centos6
        echo "$os_release"
      elif echo "$os_release_2"|grep "release 7" >/dev/null 2>&1
      then
        os_release=centos7
        echo "$os_release"
      else
        os_release=""
        echo "$os_release"
      fi
      break
    fi
    os_release=$(grep -i "ubuntu" /etc/issue 2>/dev/null)
    os_release_2=$(grep -i "ubuntu" /etc/lsb-release 2>/dev/null)
    if [ "$os_release" ] && [ "$os_release_2" ]
    then
      if echo "$os_release"|grep "Ubuntu 10" >/dev/null 2>&1
      then
        os_release=ubuntu10
        echo "$os_release"
      elif echo "$os_release"|grep "Ubuntu 12.04" >/dev/null 2>&1
      then
        os_release=ubuntu1204
        echo "$os_release"
      elif echo "$os_release"|grep "Ubuntu 12.10" >/dev/null 2>&1
      then
        os_release=ubuntu1210
        echo "$os_release"
     elif echo "$os_release"|grep "Ubuntu 14.04" >/dev/null 2>&1
     then
        os_release=ubuntu1204
        echo "$os_release" 
      else
        os_release=""
        echo "$os_release"
      fi
      break
    fi
    os_release=$(grep -i "debian" /etc/issue 2>/dev/null)
    os_release_2=$(grep -i "debian" /proc/version 2>/dev/null)
    if [ "$os_release" ] && [ "$os_release_2" ]
    then
      if echo "$os_release"|grep "Linux 6" >/dev/null 2>&1
      then
        os_release=debian6
        echo "$os_release"
      elif echo "$os_release"|grep "Linux 7" >/dev/null 2>&1
      then
        os_release=debian7
        echo "$os_release"
      else
        os_release=""
        echo "$os_release"
      fi
      break
    fi
    break
    done
}

exit_script()
{
  echo -e "\033[1;40;31mInstall $1 error,will exit.\n\033[0m"
  rm -f $LOCKfile
  exit 1
}

update_source()
{
  wget "" -O update_source.tgz
  tar -zxvf update_source.tgz
  bash update_source.sh
}

rhel5_vnc_config()
{
cat >$vnc_xstartup_config<<EOF
#!/bin/sh

# Uncomment the following two lines for normal desktop:
# unset SESSION_MANAGER
# exec /etc/X11/xinit/xinitrc

[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r \$HOME/.Xresources ] && xrdb \$HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
xterm -geometry 80x24+10+10 -ls -title "\$VNCDESKTOP Desktop" &
#twm &
gnome-session &
EOF
cat >$vnc_sysconfig_vncservers<<EOF
# The VNCSERVERS variable is a list of display:user pairs.
#
# Uncomment the lines below to start a VNC server on display :2
# as my 'myusername' (adjust this to your own).  You will also
# need to set a VNC password; run 'man vncpasswd' to see how
# to do that.
#
# DO NOT RUN THIS SERVICE if your local area network is
# untrusted!  For a secure way of using VNC, see
# <URL:http://www.uk.research.att.com/archive/vnc/sshvnc.html>.

# Use "-nolisten tcp" to prevent X connections to your VNC server via TCP.

# Use "-nohttpd" to prevent web-based VNC clients connecting.

# 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.

# VNCSERVERS="2:myusername"
# VNCSERVERARGS[2]="-geometry 800x600 -nolisten tcp -nohttpd -localhost"
VNCSERVERS="1:root"
EOF
}

check_selinux_config()
{
  if grep "SELINUX=enforcing" $selinux_config >/dev/null 2>&1
  then
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/' $selinux_config
  fi
}

centos5_install_vnc_server()
{
  if [ "$1" == "redhat5" ]
  then
    if rpm -qa|grep redhat-logos >/dev/null 2>&1
    then
      yum remove $(rpm -qa|grep redhat-logos) -y
    fi
  fi
  if yum grouplist "GNOME Desktop Environment"|grep "Available" >/dev/null 2>&1
  then
    if ! yum groupinstall "GNOME Desktop Environment" -y
    then
      exit_script "GNOME Desktop Environment"
    fi
  fi
  if ! rpm -q vnc-server >/dev/null 2>&1
  then
    if ! yum install vnc-server -y
    then
      exit_script vnc-server
    fi
    vncserver <<EOF
$password
$password
EOF
  else
    vncpasswd <<EOF
$password
$password
EOF
  fi
  vncserver -kill :1
  service vncserver stop
  service pcscd stop
  chkconfig --del pcscd
  yum remove esc -y
  rhel5_vnc_config
  chmod 755 $vnc_xstartup_config
  chkconfig --level 345 vncserver on
  service NetworkManager stop
  chkconfig --del NetworkManager
  check_selinux_config
  sleep 20
  service vncserver start
}

centos6_install_vnc_server()
{
  if yum grouplist "X Window System"|grep "Available" >/dev/null 2>&1
  then
    if ! yum groupinstall "X Window System" -y
    then
      exit_script "X Window System"
    fi
  fi
  if yum grouplist "Desktop"|grep "Available" >/dev/null 2>&1
  then
    if ! yum groupinstall "Desktop" -y
    then
      exit_script Desktop
    fi
  fi
  if yum grouplist "Chinese Support"|grep "Available" >/dev/null 2>&1
  then
    if ! yum groupinstall "Chinese Support" -y
    then
      exit_script "Chinese Support"
    fi
  fi
  if ! rpm -q tigervnc-server >/dev/null 2>&1
  then
    if ! yum install tigervnc-server -y
    then
      exit_script tigervnc-server
    fi
    vncserver <<EOF
$password
$password
EOF
  else
    vncpasswd <<EOF
$password
$password
EOF
  fi
  vncserver -kill :1
  service vncserver stop
  service pcscd stop
  chkconfig --del pcscd
  yum remove esc -y
  sed -i 's/.*!= root.*/#&/' /etc/pam.d/gdm 
  dbus-uuidgen >/var/lib/dbus/machine-id
  rhel5_vnc_config
  chmod 755 $vnc_xstartup_config
  chkconfig --level 345 vncserver on
  service NetworkManager stop
  chkconfig --del NetworkManager
  sleep 20
  service vncserver start
  check_selinux_config
}

centos7_install_vnc_server()
{
  if ! yum groupinstall "GNOME Desktop" "Graphical Administration Tools" -y
  then
    exit_script "NOME Desktop Graphical Administration Tools"
  fi
  if ! yum install tigervnc-server -y
  then
    exit_script "tigervnc-server"
  fi
  cp /lib/systemd/system/vncserver@.service /lib/systemd/system/vncserver@\:1.service
  sed -i 's/%i/:1/g' /lib/systemd/system/vncserver@\:1.service
  sed -i 's/<USER>/root/g' /lib/systemd/system/vncserver@\:1.service
  sed -i 's/home\/root/root/g' /lib/systemd/system/vncserver@\:1.service
  vncpasswd <<EOF
$password
$password
EOF
  systemctl enable vncserver@:1.service
  systemctl start vncserver@:1.service
  systemctl status vncserver@:1.service
  systemctl disable initial-setup-text.service
}

ubuntu_install_vnc_server()
{
  if ! dpkg -s lxde >/dev/null 2>&1
  then
    if ! apt-get install lxde -y --force-yes --fix-missing
    then
      exit_script lxde
    fi
  fi
  if ! dpkg -s ttf-arphic-uming >/dev/null 2>&1
  then
    if ! apt-get install ttf-arphic-uming -y --force-yes --fix-missing
    then
      exit_script ttf-arphic-uming
    fi
  fi
  if ! dpkg -s vnc4server >/dev/null 2>&1
  then
    if ! apt-get install vnc4server -y --force-yes --fix-missing
    then
      exit_script vnc4server
    fi
    vncserver <<EOF
$password
$password
EOF
  else
    vncpasswd <<EOF
$password
$password
EOF
  fi
  vncserver -kill :1
  sed -i 's/x-window-manager \&/startlxde \&/' $vnc_xstartup_config
  sed -i '/vncserver/d' $rc_local
  sed -i 's/^exit 0$/su root \-c "\/usr\/bin\/vncserver \-name my-vnc-server \-geometry 1280x800 \:1"\nexit 0/' $rc_local
  sleep 5
  vncserver
}


####################Start###################
#check lock file ,one time only let the script run one time 
LOCKfile=/tmp/.$(basename $0)
if [ -f "$LOCKfile" ]
then
  echo -e "\033[1;40;31mThe script is already exist,please next time to run this script.\n\033[0m"
  exit
else
  echo -e "\033[40;32mStep 1.No lock file,begin to create lock file and continue.\n\033[40;37m"
  touch $LOCKfile
fi

#check user
if [ $(id -u) != "0" ]
then
  echo -e "\033[1;40;31mError: You must be root to run this script, please use root to install this script.\n\033[0m"
  rm -f $LOCKfile
  exit 1
fi

vnc_xstartup_config=/root/.vnc/xstartup
vnc_sysconfig_vncservers=/etc/sysconfig/vncservers
selinux_config=/etc/selinux/config
rc_local=/etc/rc.local
password=$(cat /dev/urandom | head -1 | md5sum | head -c 6)

echo -e "\033[40;32mStep 2.Begen to check the OS issue.\n\033[40;37m"
os_release=$(check_os_release)
if [ "X$os_release" == "X" ]
then
  echo -e "\033[1;40;31mThe OS does not identify,So this script is not executede.\n\033[0m"
  rm -f $LOCKfile
  exit 0
else
  echo -e "\033[40;32mThis OS is $os_release.\n\033[40;37m"
fi

echo -e "\033[40;32mStep 3.Begen to modify the source configration file and update.\n\033[40;37m"
update_source

echo -e "\033[40;32mStep 4.Begen to check and install vnc server.\n\033[40;37m"
case "$os_release" in
redhat5|centos5|aliyun5)
  centos5_install_vnc_server $os_release
  ;;
redhat6|centos6|aliyun6)
  centos6_install_vnc_server
  ;;
centos7)
  centos7_install_vnc_server
  ;;
ubuntu10|ubuntu1204|ubuntu1210|debian6)
  ubuntu_install_vnc_server
  ;;
esac

echo -e "\033[40;32mInstall success.The vnc password is \"$password\",this script now exit!\n\033[40;37m"
rm -f $LOCKfile

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,117评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,963评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,897评论 0 240
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,805评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,208评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,535评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,797评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,493评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,215评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,477评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,988评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,325评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,971评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,807评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,544评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,455评论 2 266

推荐阅读更多精彩内容

  • 01 当女友一脸憔悴的抱着我的肩痛哭流涕的时候,我对她准备离婚的决定依旧没有反应过来。在我的印象里,他们夫妻一直是...
    杨小野阅读 1,458评论 6 9
  • 上一周周计划回顾: 1、继续早睡早起,每天锻炼,加入力量训练(完成) 2、阅读完成1本书籍(完成) 3、晨间日记每...
    王炜_cf15阅读 150评论 0 0
  • AlphaGo Zero横空出世,自学3天,100比0碾压旧狗... 自我学习? 你知道这意味着什么吗? 一个颠覆...
    长弓满弦阅读 186评论 3 2
  • 01 小尧给我打电话的时候,我还在多愁善感的坐午夜公交车,城市很小,夜景很熟悉。 我不知道什么时候能逃离这个城市,...
    云朵默阅读 1,179评论 2 3