title: linux一键安装vncserver脚本

date: 2016-04-11 14:32:04

tags:

linux多数情况下是作为服务器使用的,管理员一般也喜欢使用命令行来管理服务器,所以服务器往往都没有配备显示器及输入输出设备,但有时候还是会用到Linux桌面,所以发一下这个脚本,希望可以帮助到有用的同学。

  1. #!/bin/bash
  2. #########################################
  3. #Function: install vnc server
  4. #Usage: bash install_vnc_server.sh
  5. #Author: Customer service department
  6. #Company: Alibaba Cloud Computing
  7. #Version: 3.0
  8. #########################################
  9. check_os_release()
  10. {
  11. while true
  12. do
  13. os_release=$(grep "Red Hat Enterprise Linux Server release" /etc/issue 2>/dev/null)
  14. os_release_2=$(grep "Red Hat Enterprise Linux Server release" /etc/redhat-release 2>/dev/null)
  15. if [ "$os_release" ] && [ "$os_release_2" ]
  16. then
  17. if echo "$os_release"|grep "release 5" >/dev/null 2>&1
  18. then
  19. os_release=redhat5
  20. echo "$os_release"
  21. elif echo "$os_release"|grep "release 6" >/dev/null 2>&1
  22. then
  23. os_release=redhat6
  24. echo "$os_release"
  25. else
  26. os_release=""
  27. echo "$os_release"
  28. fi
  29. break
  30. fi
  31. os_release=$(grep "Aliyun Linux release" /etc/issue 2>/dev/null)
  32. os_release_2=$(grep "Aliyun Linux release" /etc/aliyun-release 2>/dev/null)
  33. if [ "$os_release" ] && [ "$os_release_2" ]
  34. then
  35. if echo "$os_release"|grep "release 5" >/dev/null 2>&1
  36. then
  37. os_release=aliyun5
  38. echo "$os_release"
  39. elif echo "$os_release"|grep "release 6" >/dev/null 2>&1
  40. then
  41. os_release=aliyun6
  42. echo "$os_release"
  43. elif echo "$os_release"|grep "release 7" >/dev/null 2>&1
  44. then
  45. os_release=aliyun7
  46. echo "$os_release"
  47. else
  48. os_release=""
  49. echo "$os_release"
  50. fi
  51. break
  52. fi
  53. os_release_2=$(grep "CentOS" /etc/*release 2>/dev/null)
  54. if [ "$os_release_2" ]
  55. then
  56. if echo "$os_release_2"|grep "release 5" >/dev/null 2>&1
  57. then
  58. os_release=centos5
  59. echo "$os_release"
  60. elif echo "$os_release_2"|grep "release 6" >/dev/null 2>&1
  61. then
  62. os_release=centos6
  63. echo "$os_release"
  64. elif echo "$os_release_2"|grep "release 7" >/dev/null 2>&1
  65. then
  66. os_release=centos7
  67. echo "$os_release"
  68. else
  69. os_release=""
  70. echo "$os_release"
  71. fi
  72. break
  73. fi
  74. os_release=$(grep -i "ubuntu" /etc/issue 2>/dev/null)
  75. os_release_2=$(grep -i "ubuntu" /etc/lsb-release 2>/dev/null)
  76. if [ "$os_release" ] && [ "$os_release_2" ]
  77. then
  78. if echo "$os_release"|grep "Ubuntu 10" >/dev/null 2>&1
  79. then
  80. os_release=ubuntu10
  81. echo "$os_release"
  82. elif echo "$os_release"|grep "Ubuntu 12.04" >/dev/null 2>&1
  83. then
  84. os_release=ubuntu1204
  85. echo "$os_release"
  86. elif echo "$os_release"|grep "Ubuntu 12.10" >/dev/null 2>&1
  87. then
  88. os_release=ubuntu1210
  89. echo "$os_release"
  90. elif echo "$os_release"|grep "Ubuntu 14.04" >/dev/null 2>&1
  91. then
  92. os_release=ubuntu1204
  93. echo "$os_release"
  94. else
  95. os_release=""
  96. echo "$os_release"
  97. fi
  98. break
  99. fi
  100. os_release=$(grep -i "debian" /etc/issue 2>/dev/null)
  101. os_release_2=$(grep -i "debian" /proc/version 2>/dev/null)
  102. if [ "$os_release" ] && [ "$os_release_2" ]
  103. then
  104. if echo "$os_release"|grep "Linux 6" >/dev/null 2>&1
  105. then
  106. os_release=debian6
  107. echo "$os_release"
  108. elif echo "$os_release"|grep "Linux 7" >/dev/null 2>&1
  109. then
  110. os_release=debian7
  111. echo "$os_release"
  112. else
  113. os_release=""
  114. echo "$os_release"
  115. fi
  116. break
  117. fi
  118. break
  119. done
  120. }
  121. exit_script()
  122. {
  123. echo -e "\033[1;40;31mInstall $1 error,will exit.\n\033[0m"
  124. rm -f $LOCKfile
  125. exit 1
  126. }
  127. update_source()
  128. {
  129. wget "http://oss.aliyuncs.com/aliyunecs/update_source.tgz?spm=5176.7150518.1996836753.5.67nzlg&file=update_source.tgz" -O update_source.tgz
  130. tar -zxvf update_source.tgz
  131. bash update_source.sh
  132. }
  133. rhel5_vnc_config()
  134. {
  135. cat >$vnc_xstartup_config<<EOF
  136. #!/bin/sh
  137. # Uncomment the following two lines for normal desktop:
  138. # unset SESSION_MANAGER
  139. # exec /etc/X11/xinit/xinitrc
  140. [ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
  141. [ -r \$HOME/.Xresources ] && xrdb \$HOME/.Xresources
  142. xsetroot -solid grey
  143. vncconfig -iconic &
  144. xterm -geometry 80x24+10+10 -ls -title "\$VNCDESKTOP Desktop" &
  145. #twm &
  146. gnome-session &
  147. EOF
  148. cat >$vnc_sysconfig_vncservers<<EOF
  149. # The VNCSERVERS variable is a list of display:user pairs.
  150. #
  151. # Uncomment the lines below to start a VNC server on display :2
  152. # as my 'myusername' (adjust this to your own). You will also
  153. # need to set a VNC password; run 'man vncpasswd' to see how
  154. # to do that.
  155. #
  156. # DO NOT RUN THIS SERVICE if your local area network is
  157. # untrusted! For a secure way of using VNC, see
  158. # <URL:http://www.uk.research.att.com/archive/vnc/sshvnc.html>.
  159. # Use "-nolisten tcp" to prevent X connections to your VNC server via TCP.
  160. # Use "-nohttpd" to prevent web-based VNC clients connecting.
  161. # Use "-localhost" to prevent remote VNC clients connecting except when
  162. # doing so through a secure tunnel. See the "-via" option in the
  163. # 'man vncviewer' manual page.
  164. # VNCSERVERS="2:myusername"
  165. # VNCSERVERARGS[2]="-geometry 800x600 -nolisten tcp -nohttpd -localhost"
  166. VNCSERVERS="1:root"
  167. EOF
  168. }
  169. check_selinux_config()
  170. {
  171. if grep "SELINUX=enforcing" $selinux_config >/dev/null 2>&1
  172. then
  173. sed -i 's/SELINUX=enforcing/SELINUX=disabled/' $selinux_config
  174. fi
  175. }
  176. centos5_install_vnc_server()
  177. {
  178. if [ "$1" == "redhat5" ]
  179. then
  180. if rpm -qa|grep redhat-logos >/dev/null 2>&1
  181. then
  182. yum remove $(rpm -qa|grep redhat-logos) -y
  183. fi
  184. fi
  185. if yum grouplist "GNOME Desktop Environment"|grep "Available" >/dev/null 2>&1
  186. then
  187. if ! yum groupinstall "GNOME Desktop Environment" -y
  188. then
  189. exit_script "GNOME Desktop Environment"
  190. fi
  191. fi
  192. if ! rpm -q vnc-server >/dev/null 2>&1
  193. then
  194. if ! yum install vnc-server -y
  195. then
  196. exit_script vnc-server
  197. fi
  198. vncserver <<EOF
  199. $password
  200. $password
  201. EOF
  202. else
  203. vncpasswd <<EOF
  204. $password
  205. $password
  206. EOF
  207. fi
  208. vncserver -kill :1
  209. service vncserver stop
  210. service pcscd stop
  211. chkconfig --del pcscd
  212. yum remove esc -y
  213. rhel5_vnc_config
  214. chmod 755 $vnc_xstartup_config
  215. chkconfig --level 345 vncserver on
  216. service NetworkManager stop
  217. chkconfig --del NetworkManager
  218. check_selinux_config
  219. sleep 20
  220. service vncserver start
  221. }
  222. centos6_install_vnc_server()
  223. {
  224. if yum grouplist "X Window System"|grep "Available" >/dev/null 2>&1
  225. then
  226. if ! yum groupinstall "X Window System" -y
  227. then
  228. exit_script "X Window System"
  229. fi
  230. fi
  231. if yum grouplist "Desktop"|grep "Available" >/dev/null 2>&1
  232. then
  233. if ! yum groupinstall "Desktop" -y
  234. then
  235. exit_script Desktop
  236. fi
  237. fi
  238. if yum grouplist "Chinese Support"|grep "Available" >/dev/null 2>&1
  239. then
  240. if ! yum groupinstall "Chinese Support" -y
  241. then
  242. exit_script "Chinese Support"
  243. fi
  244. fi
  245. if ! rpm -q tigervnc-server >/dev/null 2>&1
  246. then
  247. if ! yum install tigervnc-server -y
  248. then
  249. exit_script tigervnc-server
  250. fi
  251. vncserver <<EOF
  252. $password
  253. $password
  254. EOF
  255. else
  256. vncpasswd <<EOF
  257. $password
  258. $password
  259. EOF
  260. fi
  261. vncserver -kill :1
  262. service vncserver stop
  263. service pcscd stop
  264. chkconfig --del pcscd
  265. yum remove esc -y
  266. sed -i 's/.*!= root.*/#&/' /etc/pam.d/gdm
  267. dbus-uuidgen >/var/lib/dbus/machine-id
  268. rhel5_vnc_config
  269. chmod 755 $vnc_xstartup_config
  270. chkconfig --level 345 vncserver on
  271. service NetworkManager stop
  272. chkconfig --del NetworkManager
  273. sleep 20
  274. service vncserver start
  275. check_selinux_config
  276. }
  277. centos7_install_vnc_server()
  278. {
  279. if ! yum groupinstall "GNOME Desktop" "Graphical Administration Tools" -y
  280. then
  281. exit_script "NOME Desktop Graphical Administration Tools"
  282. fi
  283. if ! yum install tigervnc-server -y
  284. then
  285. exit_script "tigervnc-server"
  286. fi
  287. cp /lib/systemd/system/vncserver@.service /lib/systemd/system/vncserver@\:1.service
  288. sed -i 's/%i/:1/g' /lib/systemd/system/vncserver@\:1.service
  289. sed -i 's/<USER>/root/g' /lib/systemd/system/vncserver@\:1.service
  290. sed -i 's/home\/root/root/g' /lib/systemd/system/vncserver@\:1.service
  291. vncpasswd <<EOF
  292. $password
  293. $password
  294. EOF
  295. systemctl enable vncserver@:1.service
  296. systemctl start vncserver@:1.service
  297. systemctl status vncserver@:1.service
  298. systemctl disable initial-setup-text.service
  299. }
  300. ubuntu_install_vnc_server()
  301. {
  302. if ! dpkg -s lxde >/dev/null 2>&1
  303. then
  304. if ! apt-get install lxde -y --force-yes --fix-missing
  305. then
  306. exit_script lxde
  307. fi
  308. fi
  309. if ! dpkg -s ttf-arphic-uming >/dev/null 2>&1
  310. then
  311. if ! apt-get install ttf-arphic-uming -y --force-yes --fix-missing
  312. then
  313. exit_script ttf-arphic-uming
  314. fi
  315. fi
  316. if ! dpkg -s vnc4server >/dev/null 2>&1
  317. then
  318. if ! apt-get install vnc4server -y --force-yes --fix-missing
  319. then
  320. exit_script vnc4server
  321. fi
  322. vncserver <<EOF
  323. $password
  324. $password
  325. EOF
  326. else
  327. vncpasswd <<EOF
  328. $password
  329. $password
  330. EOF
  331. fi
  332. vncserver -kill :1
  333. sed -i 's/x-window-manager \&/startlxde \&/' $vnc_xstartup_config
  334. sed -i '/vncserver/d' $rc_local
  335. sed -i 's/^exit 0$/su root \-c "\/usr\/bin\/vncserver \-name my-vnc-server \-geometry 1280x800 \:1"\nexit 0/' $rc_local
  336. sleep 5
  337. vncserver
  338. }
  339. ####################Start###################
  340. #check lock file ,one time only let the script run one time
  341. LOCKfile=/tmp/.$(basename $0)
  342. if [ -f "$LOCKfile" ]
  343. then
  344. echo -e "\033[1;40;31mThe script is already exist,please next time to run this script.\n\033[0m"
  345. exit
  346. else
  347. echo -e "\033[40;32mStep 1.No lock file,begin to create lock file and continue.\n\033[40;37m"
  348. touch $LOCKfile
  349. fi
  350. #check user
  351. if [ $(id -u) != "0" ]
  352. then
  353. echo -e "\033[1;40;31mError: You must be root to run this script, please use root to install this script.\n\033[0m"
  354. rm -f $LOCKfile
  355. exit 1
  356. fi
  357. vnc_xstartup_config=/root/.vnc/xstartup
  358. vnc_sysconfig_vncservers=/etc/sysconfig/vncservers
  359. selinux_config=/etc/selinux/config
  360. rc_local=/etc/rc.local
  361. password=$(cat /dev/urandom | head -1 | md5sum | head -c 6)
  362. echo -e "\033[40;32mStep 2.Begen to check the OS issue.\n\033[40;37m"
  363. os_release=$(check_os_release)
  364. if [ "X$os_release" == "X" ]
  365. then
  366. echo -e "\033[1;40;31mThe OS does not identify,So this script is not executede.\n\033[0m"
  367. rm -f $LOCKfile
  368. exit 0
  369. else
  370. echo -e "\033[40;32mThis OS is $os_release.\n\033[40;37m"
  371. fi
  372. echo -e "\033[40;32mStep 3.Begen to modify the source configration file and update.\n\033[40;37m"
  373. update_source
  374. echo -e "\033[40;32mStep 4.Begen to check and install vnc server.\n\033[40;37m"
  375. case "$os_release" in
  376. redhat5|centos5|aliyun5)
  377. centos5_install_vnc_server $os_release
  378. ;;
  379. redhat6|centos6|aliyun6)
  380. centos6_install_vnc_server
  381. ;;
  382. centos7)
  383. centos7_install_vnc_server
  384. ;;
  385. ubuntu10|ubuntu1204|ubuntu1210|debian6)
  386. ubuntu_install_vnc_server
  387. ;;
  388. esac
  389. echo -e "\033[40;32mInstall success.The vnc password is \"$password\",this script now exit!\n\033[40;37m"
  390. rm -f $LOCKfile

linux一键安装vncserver脚本的更多相关文章

  1. linux一键安装nginx脚本

    #!/bin/sh echo "----------------------------------start install nginx ------------------------- ...

  2. linux一键安装php脚本

    #!/bin/sh echo "----------------------------------start install php --------------------------- ...

  3. linux一键安装mysql脚本

    #!/bin/sh if [ -s /etc/my.cnf ];then rm -rf /etc/my.cnf fi echo "------------------------------ ...

  4. 禅道Linux一键安装版

    禅道Linux一键安装版 Linux一键安装包内置了apache, php, mysql这些应用程序,只需要下载解压缩即可运行禅道.从7.3版本开始,linux一键安装包分为32位和64位两个包,大家 ...

  5. Rehat一键安装mysql脚本和备份数据库脚本

    Rehat一键安装mysql脚本 ##说明:适用,Rehat 5 6 7 1.运行状态,运行成功输出mysql临时密码 2.代码如下 #!/bin/bash #获取系统信息 sudo cat /etc ...

  6. Linux一键安装宝塔控制面板

    Linux一键安装宝塔的命令行 yum install -y wget && wget -O install.sh http://download.bt.cn/install/inst ...

  7. (转)CentOS一键安装Nginx脚本

    原文:https://www.xiaoz.me/archives/10301 https://blog.slogra.com/post-676.html-----centos7一键安装nginx脚本

  8. Linux一键安装web环境全攻略phpstudy版

    此教程主要是应对阿里云Linux云服务器ecs的web环境安装,理论上不限于阿里云服务器,此教程对所有Linux云服务器都具有参考价值. 写这篇文章的目的:网上有很多关于Linux一键安装web环境全 ...

  9. Linux一键安装LNMP环境

    Linux一键安装LNMP环境 官方地址:https://lnmp.org/. 参考安装步骤:https://lnmp.org/install.html. 一键安装可以选择mysql版本.php版本, ...

随机推荐

  1. Linux shell 脚本中”2>&1″的含义解释

    脚本是:nohup /mnt/Nand3/H2000G  >/dev/null  2>&1 &   对于& 1 更准确的说应该是文件描述符 1,而1 一般代表的就是 ...

  2. CUICatalog: Invalid asset name supplied: (null)

    出现这个问题的根本原因是你调用了[UIImage imageNamed:name]这个方法 但是name = nil;所以报出该错误.   解决方法,在项目中搜索[UIImage imageNamed ...

  3. SQL IN BETWEEN操作符

    IN 操作符 IN 操作符允许我们在 WHERE 子句中规定多个值. SQL IN 语法 SELECT column_name(s) FROM table_name WHERE column_name ...

  4. JavaWeb 之 重复提交表单和验证码相关的问题!

    下面我们首先来说一下表单的重复提交问题,我们知道在真实的网络环境中可能受网速带宽的原因会造成页面中表单在提交的过程中出现网络的延迟等问题,从而造成多次提交的问题!下面我们就具体来分析一下造成表单提交的 ...

  5. Centos 中 vim 的配置

    工欲善其事,必先利其器,我们要用好 vim 就先来把它配置的顺手一点,这样可以大大提高我们的工作学习效率 1.跳转指令 Ctags1 从下面地址下载ctags,将其中的ctags.exe复制到vim目 ...

  6. HDU 3721 Building Roads (2010 Asia Tianjin Regional Contest) - from lanshui_Yang

    感慨一下,区域赛的题目果然很费脑啊!!不过确实是一道不可多得的好题目!! 题目大意:给你一棵有n个节点的树,让你移动树中一条边的位置,即将这条边连接到任意两个顶点(边的大小不变),要求使得到的新树的直 ...

  7. IBM X3650 服务器更换内存的过程记录

    前几天,由于天气气温太高,导致中心机房UPS自动保护性关闭,最终导致所有服务器都掉电.其中一台IBM X3650服务器报警,显示内存故障.原来这台服务器共有4G内存,现在打开服务器进入系统后,显示只有 ...

  8. js十进制等互相转换

    //十进制转其他 var x=110; alert(x); alert(x.toString(8)); alert(x.toString(32)); alert(x.toString(16));  / ...

  9. acdream暴力专场中的优美暴力

    F - 小晴天老师系列——苹果大丰收 Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Other ...

  10. CWnd类

    CWnd类的成员 .数据成员 m_hWnd 指明与这个CWnd对象相关联的HWND句柄 .构造和析构 CWnd 构造一个CWnd对象 DestroyWindow 销毁相关联的Windows窗口 .初始 ...