Automatically start VNC server on startup

I installed the Ubuntu desktop on a Ubuntu 9.10 VPS server and am able to connect to the server using TightVNC. However, the VNC server on this VPS can only be started by logging in through SSH and typing the following command:

vncserver :1 -geometry 800x600 -depth 16 -pixelformat rgb565

If I run this command on startup or as a schedule task, it won't start. What are my options?


Solution 1:

I found these instructions by searching Google for "ubuntu launch vnc server on startup".

  1. Install the VNC server.
  2. Launch vncserver for the first time to set up a password.
  3. Add the following file as /etc/init.d/vncserver (be sure to modify the USER, GEOMETRY, NAME, etc. to your liking).
  4. sudo chmod +x /etc/init.d/vncserver
  5. sudo update-rc.d vncserver defaults

/etc/init.d/vncserver

#!/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="mythtv"
#${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="my-vnc-server"

OPTIONS="-name ${NAME} -depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY}"

. /lib/lsb/init-functions

case "$1" in
start)
log_action_begin_msg "Starting vncserver for user '${USER}' on localhost:${DISPLAY}"
su ${USER} -c "/usr/bin/vncserver ${OPTIONS}"
;;

stop)
log_action_begin_msg "Stoping vncserver for user '${USER}' on localhost:${DISPLAY}"
su ${USER} -c "/usr/bin/vncserver -kill :${DISPLAY}"
;;

restart)
$0 stop
$0 start
;;
esac

exit 0

Solution 2:

If you want a more dynamic configuration and the ability to connect for multiple users then there is a better way to do this. As root create the file (and directory if it doesn't exist) /etc/sysconfig/vncservers i.e. do:

mkdir -p /etc/vncserver
touch /etc/vncserver/vncservers.conf

Add servers as needed for each user by adding something like the following to the vncservers.conf file you just created:

VNCSERVERS="1:justin 2:bob"
VNCSERVERARGS[1]="-geometry 1920x1080 -depth 24"
VNCSERVERARGS[2]="-geometry 800x600 -depth 8"

next create an empty init script and make it executable:

touch /etc/init.d/vncserver
chmod +x /etc/init.d/vncserver

add the following to /etc/init.d/vncserver:

#!/bin/bash

unset VNCSERVERARGS
VNCSERVERS=""
[ -f /etc/vncserver/vncservers.conf ] && . /etc/vncserver/vncservers.conf
prog=$"VNC server"

start() {
        . /lib/lsb/init-functions
        REQ_USER=$2
        echo -n $"Starting $prog: "
        ulimit -S -c 0 >/dev/null 2>&1
        RETVAL=0
        for display in ${VNCSERVERS}
        do
                export USER="${display##*:}"
                if test -z "${REQ_USER}" -o "${REQ_USER}" == ${USER} ; then
                        echo -n "${display} "
                        unset BASH_ENV ENV
                        DISP="${display%%:*}"
                        export VNCUSERARGS="${VNCSERVERARGS[${DISP}]}"
                        su ${USER} -c "cd ~${USER} && [ -f .vnc/passwd ] && vncserver :${DISP} ${VNCUSERARGS}"
                fi
        done
}

stop() {
        . /lib/lsb/init-functions
        REQ_USER=$2
        echo -n $"Shutting down VNCServer: "
        for display in ${VNCSERVERS}
        do
                export USER="${display##*:}"
                if test -z "${REQ_USER}" -o "${REQ_USER}" == ${USER} ; then
                        echo -n "${display} "
                        unset BASH_ENV ENV
                        export USER="${display##*:}"
                        su ${USER} -c "vncserver -kill :${display%%:*}" >/dev/null 2>&1
                fi
        done
        echo -e "\n"
        echo "VNCServer Stopped"
}

case "$1" in
start)
start $@
;;
stop)
stop $@
;;
restart|reload)
stop $@
sleep 3
start $@
;;
condrestart)
if [ -f /var/lock/subsys/vncserver ]; then
stop $@
sleep 3
start $@
fi
;;
status)
status Xvnc
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
exit 1
esac

As Stephen mentioned in his answer you'll need to run vncserver AT LEAST ONCE AS EACH USER you want to login as. I put that in caps because if you skip that step none of it will work. So as root you could do:

su justin -c vncserver
su bob -c vncserver

This will create a .vnc directory in each users home dir with the appropriate startup scripts.

Finally, do the following:

update-rc.d vncserver defaults 99

now you can either reboot or start the service manually by typing:

service vncserver start

Solution 3:

In Ubuntu 12.1 I was able to go into System Settings->Users and select a user and set "Automatic Login->ON"

Then I was able to use tightVNC to get in without logging in to the box itself.

Worked well for headless ubuntu linux box

Solution 4:

I access the Ubuntu of friends I help, to install or configure or to teach them something.
As I need access from the Internet through the modem, I use vino.
All sharing and Security options are turned on during access.
I don't want the vino-server to be active all the time: it's just fine it doesn't autostart.
I had no System>Remote Desktop menu.
I edited /usr/share/applications/vino-preferences.desktop as follows:

# OnlyShowIn=Unity;
Exec=bash -c 'vino-preferences;/usr/lib/vino/vino-server&zenity --info --text="Accès par Internet: `curl http://ipecho.net/plain`:5900"'

Before work, I ask my friends to run Remote Desktop Preferences and to tick Sharing Allow... on.
On exiting Preferences, vino-server starts and they tell me the IP address to use.
When work is finished, they run Preference again to tick Sharing Allow... off.
On exiting Preferences, vino-server stops and would stop even if started in Sharing off state.
I find this procedure very convenient as well as the safest for the user.

PS: developers prefer their programs to run locally (within a user session) because a bug cannot affect the global system that way.