Saturday, November 5, 2011

Automatically starting the VNC server at boot time on your Linux virtual machines

[Note: check this update before following these instructions]

These days, many people in IBM (and I am sure many other companies) are using Linux virtual machines for their normal work. The hard core geeks are happy to connect to their virtual machines via ssh and do all of their work on the command line. However, the rest of use appreciate the convenience of a graphical interface and so like to use VNC. Unfortunately, the default configuration is that the VNC server does not automatically start every time you reboot. If you reboot your server regularly, it can be a pain to continually have to log into the server and start VNC.

After a bit of digging I found this excellent blog post which describes how to auto-start the VNC server on Ubuntu.  However, most people I know tend to install Ubuntu on their laptop (which doesn't really need VNC server to be running) and use either SUSE or RedHat on their real servers. I had to make a few minor tweaks to get the script working on these Linux variants. The script below works on SUSE and RedHat variants of Linux. I have tested it on SLES 11 and RHEL5.6 - it should work on pretty much any Linux variant, but I would love to hear feedback from people if there are any issues.

As described in andrew's blog, it is necessary to start VNC manually the first time so that you can enter the security password. The first time you launch the program will create a default ~/.vnc/xstartup script which you can customize to meet your preferences. You should download the script to /etc/init.d/vncserver (making sure that the script is executable with the command "chmod +x /etc/init.d/vncserver") and then use the command "chkconfig vncserver on" to configure the server to start at boot time.

The bulk of the script is identical to Andrew's so you can read his description of how it works. I highlighted in red the places where I needed to alter it:
  1. The original script declared a dependency on the networking service, but this service is called network on other Linux variants. Changing the dependency to $network allows the script to be more portable.
  2. At the start of the script you can see some specially formatted comments which are interpreted as directives by the chkconfig command. There are several variants of this command and most systems do not have detailed documentation on what directives are used. The Debian wiki seems to have a complete list of possible directives. You don't need to worry about putting in special directives which are not understood by your variant of chkconfig because they will simply be treated as normal comments. The original script has enough directives to keep Ubuntu happy, but SUSE seems to insist on a "Required-Stop:" directive and RedHat seems to insist on the service description being included.
  3. The original script used log_action_begin_msg, but this seems to be a command only supported on Debian derivatives so I changed them to simple echo commands.
#!/bin/sh -e
### BEGIN INIT INFO
# Provides:          vncserver
# Required-Start:    $network
# Default-Start:     3 4 5
# Default-Stop:      0 6
# Required-Stop:
# Short-Description: Starts and stops VNC server
# Description: Starts and stops VNC server
### END INIT INFO
PATH="$PATH:/usr/X11R6/bin/"

# The Username:Group that will run VNC
export USER="root"
#${RUNAS}

# The display that VNC will use
DISPLAY="1"

# Color depth (between 8 and 32)
DEPTH="16"

# The Desktop geometry to use.
#GEOMETRY="x"
#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)
echo "Starting vncserver for user '${USER}' on localhost:${DISPLAY}"
su ${USER} -c "/usr/bin/vncserver ${OPTIONS}"
;;

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

restart)
$0 stop
$0 start
;;
esac

exit 0

4 comments:

  1. Great article, now I know why "chkconfig vncserver on" didn't work on my computer -- because it didn't exist!

    ReplyDelete
  2. Great article! now "chkconfig vncserver on" works on my SuSE Enterprise Server 11 SP2, I've been wondering why "chkconfig vncserver on" told me "service vncserver doesn't exist".

    ReplyDelete
  3. It works, thank you.
    Added in to autostart by: insserv vncserver
    Verification: chkconfig -list vncserver

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete