Saturday, December 25, 2010

How to install VNC server on an Ubuntu PC and activate during boot

Vnc4server on Ubuntu 10.04

sudo apt-get install vnc4server

execute “vncserver” as my local user.  This prompts me to set a password and ~/.vnc directory for the first time.

modify ~/.vnc/xstartup to start gnome, and not startup x-window-manager.  My xstartup file now looks like this:

#!/bin/sh
# Uncomment the following two lines for normal desktop:
 unset SESSION_MANAGER
sh /etc/X11/xinit/xinitrc
#[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
#[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
#xsetroot -solid grey
#vncconfig -iconic &
#x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#x-window-manager &
kill currently running vncserver by vncserver -kill :1


configure the VNC server to run at bootup on the Ubuntu PC.

In practice, you will not want to logon to the SERVER just to initiate the VNC server.  Moreover, you may not even have a monitor for your Ubuntu Server, because you may be using the monitor as a dual screen for your primary desktop

Open up a terminal and login as root

sudo -i
Run all the commands outlined in step 3 (for the Ubuntu PC) as root.
Create a new file  in /etc/init.d called  vncserver and ensure it has the following contents:

#!/bin/sh -e
### BEGIN INIT INFO
# Provides:          vncserver
# Required-Start:    networking
# Default-Start:     S
# Default-Stop:      0 6
### END INIT INFO
PATH="$PATH:/usr/X11R6/bin/"
# The Username:Group that will run VNC
#export USER="YOUR_USERNAME"
#${RUNAS}
# The display that VNC will use
DISPLAY="1"
# Color depth (between 8 and 32)
DEPTH="16"
# The Desktop geometry to use.
#GEOMETRY="<WIDTH>x<HEIGHT>"
#GEOMETRY="800x600"
#GEOMETRY="1024x768"
GEOMETRY="1280x1024"
# The name that the VNC Desktop will have.
NAME="your-vnc-server"
OPTIONS="-name ${NAME} -depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY}"
. /lib/lsb/init-functions
case "$1" in
start)
su ${USER} -c "/usr/bin/vncserver ${OPTIONS}"
;;
stop)
su ${USER} -c "/usr/bin/vncserver -kill :${DISPLAY}"
;;
restart)
$0 stop
$0 start
;;
esac
exit 0

Make the above script executable:
sudo chmod +x /etc/init.d/vncserver

Make this script into a system service by adding this script to the runtime scripts invoked at bootup time.

sudo update-rc.d vncserver defaults

To start the service without rebooting the SERVER:

sudo /etc/init.d/vncserver start

If you made a mistake/typo in the script, you can undo the creation of the system-wide service (step 5) by running the command:

sudo update-rc.d -f vncserver remove

You may then correct your mistake and then repeat steps 5 and 6.
Well done!  You may now connect to your Ubuntu PC SERVER via remote desktop by simply powering on the SERVER and giving
it enough time to bootup and run all of its system-wide services.