VNC Server Autostart on RHEL5 / CentOS5

The following steps will autostart a VNC server on a Red Hat Enterprise Linux 5.x or CentOS 5.x operating system after a reboot. The Linux user account that needs VNC server to automatically start up after system reboot, must have a VNC password. To create a new (or reset a forgotten) VNC password, just login as root or su (switch user) with that Linux user account and execute this simple command:

vncpasswd

Enter a password when prompted, which is used for VNC authentication. A hidden directory named .vnc is created in the user home directory by the vncpasswd command (if it’s not current exists).

To check if the file xstartup exists, execute:

ls -la $HOME/.vnc

If it does not exist, type:
 vncserver :1

If you get a similar message “A VNC server is already running as :1”, then another instance of VNC server running with the same display number. To resolve this, just try to replace the :1 with :2, :3, etc. Alternatively, you may execute this netstat command with root user privilege:

# netstat -tulpan | grep vnc
tcp 0 0 0.0.0.0:5801 0.0.0.0:* LISTEN 3402/Xvnc
tcp 0 0 0.0.0.0:5802 0.0.0.0:* LISTEN 8447/Xvnc
tcp 0 0 0.0.0.0:5901 0.0.0.0:* LISTEN 3402/Xvnc
tcp 0 0 0.0.0.0:5902 0.0.0.0:* LISTEN 8447/Xvnc
tcp 0 0 0.0.0.0:6001 0.0.0.0:* LISTEN 3402/Xvnc
tcp 0 0 0.0.0.0:6002 0.0.0.0:* LISTEN 8447/Xvnc

The netstat output shows that there are two VNC servers running with display number 1 and 2. So, for the 3rd VNC server to start, the command should be vncserver :3.

Edit $HOME/.vnc/xstartup file to un-comment two lines in order to get the “normal” Linux Desktop view:

unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc

Switch user to root account (i.e. su – root), edit /etc/sysconfig/vncservers and append the display number and Linux user account information to the VNCSERVERS (an array variable). This configuration file defines who can start up VNC server with what display number via the VNCSERVERS array (that’s read by Linux start up scripts /etc/init.d/vncserver). For example,

VNCSERVERS="1:root 2:tester 3:anotheruser"

That means there are three Linux user accounts (root, tester, and anotheruser) will start up VNC server with display number 1, 2, and 3 respecitively, as Linux boots up.

Note: Don’t simply add more than one VNCSERVERS array in /etc/sysconfig/vncserver configuration file. Otherwise, only the last VNCSERVERS array will be used.

Ensure the VNC server (the daemon or server process) is set to autorun on system boots to your runlevel. For example,

# chkconfig ––list | grep vnc
vncserver 0:off 1:off 2:off 3:off 4:off 5:on 6:off

The ––list option of chkconfig shows VNC server is set to auto run in Linux runlevel 5 (the default multi-user runlevel with Linux Desktop console). To configure VNC server to auto run when Linux boots into runlevel 5, use the ––level with on option switch:

chkconfig --level 5 vncserver on

To check if this configuration works, you can reboot the computer

shutdown -r now

Tested and worked: April 20, 2010 on Red Hat Enterprise Linux 5.3 and CentOS 5.4.

Source: http://www.walkernews.net/2008/06/20/configure-vnc-server-to-auto-start-up-in-red-hat-linux/