Press "Enter" to skip to content

Ubuntu 14.04安装配置VNC桌面

Ubuntu 14.04安装配置VNC桌面的过程,记录一下。由于Ubuntu 14.04采用的新版Gnome对远程桌面支持的非常不友好,因此,网上的解决方案普遍是安装旧版gnome,或者安装xfce桌面,然后再开启VNC桌面。本文介绍的是前一种解决方案。如果想通过安装xfce桌面再开启VNC,可以参考这篇文章

$ sudo apt-get install gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal vnc4server

$ vim ~/.vnc/xstartup    #新建文件输入以下内容
#!/bin/sh
   
export XKL_XMODMAP_DISABLE=1
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
 
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
 
gnome-session &
gnome-panel &
gnome-settings-daemon &
metacity &
nautilus &
gnome-terminal &
vncconfig -nowin &

#给予可执行权限

$ sudo chmod +x ~/.vnc/xstartup

然后开启桌面就行了

#开启桌面
$ vnc4server -geometry 1366x768 :1    #也可以把后面的:1省略(冒号也要省略),系统会自动分配

#杀死一个桌面
$ vnc4server -kill :1    #必须自着桌面ID号,这里是:1

如果服务器上有多个用户,可以写个脚本,来纠正大家的问题

sudo mkdir /opt/1404
sudo vim /opt/1404/create_vnc_env.sh  #输入以下内容
#!/bin/bash

#check system version
ver=`lsb_release -r | awk '{print $2}'`
if [ "$ver" != "14.04" ]; then
    echo "Error: This script can only work on Ubuntu 14.04"
    exit 0
fi

rm -rf ~/.vnc || echo "Error! Do not have permission to delete .vnc directory!"
rm -rf ~/.config/dconf
mkdir ~/.vnc
mkdir -p ~/.config/dconf

cat > ~/.vnc/xstartup <<eof
#!/bin/sh
   
export XKL_XMODMAP_DISABLE=1
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
 
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
 
gnome-session &
gnome-panel &
gnome-settings-daemon &
metacity &
nautilus &
gnome-terminal &
vncconfig -nowin &
eof
chmod +x ~/.vnc/xstartup

echo "Those VNC Desktop are already running:"
ps -ef | grep Xvnc | grep -v 'color=auto' | grep -v 'grep Xvnc' | awk '{printf "Desktop " $9 " belongs to " $12 "\n" }'
echo ""
echo ""

if [ -x ~/.vnc/xstartup ]; then
    echo "Done!"
    echo "start a VNC Desltop: vnc4server -geometry 1366x768 :1"
    echo "Stop a VNC Desktop:  vnc4server -kill :1"
else
    echo "Something error!"
fi
sudo chmod +x /opt/1404/create_vnc_env.sh

然后让大家在开启VNC桌面前,先运行一次此脚本(首次开启VNC桌面的时候运行一次就可以了),然后再开启VNC桌面就行了。

参考文档:
http://glody.net/?p=27
http://qiita.com/ryunosinfx@github/items/b28e23f65c74a0f59d03

================================================
================================================

2016.03.09补充

发现了新的解决方案:安装xrdp及mate桌面以后,可以直接在Windows系统里使用远程桌面来连接;

sudo apt-get install xrdp
sudo apt-add-repository ppa:ubuntu-mate-dev/ppa
sudo apt-add-repository ppa:ubuntu-mate-dev/trusty-mate
sudo apt-get update 
sudo apt-get upgrade
sudo apt-get install ubuntu-mate-core ubuntu-mate-desktop

echo mate-session >~/.xsession
sudo service xrdp restart

但是这种方案仍然有一些缺点,例如:
依然使用VNC协议(注定没办法复制粘贴中文);
如果不小心把远程桌面关掉,重新再连接时,不会进入旧的VNC终端,而是会再启动一个新的VNC终端,这样如果使用人数多了以后,系统里会有几百上千个VNC进程……

参考资料
XRDP – Reconnect to existing sessions made easier – New xrdp package on Ubuntu repository
ubuntu 14.04使用xrdp实现windows 远程桌面连接

Leave a Reply

Your email address will not be published. Required fields are marked *