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
# 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

# The Username:Group that will run VNC
export USER="root"

# The display that VNC will use

# Color depth (between 8 and 32)

# The Desktop geometry to use.

# The name that the VNC Desktop will have.

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

. /lib/lsb/init-functions

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

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

$0 stop
$0 start

exit 0


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

  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".

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

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