Configuring a VNC server

Article published on November 10, 2005 under Linux tutorials

Prerequisites

  • Having VNC installed on the server machine

Configuring the service

First step in order to configure VNC as a service is to edit the configuration file and add informations about yourself:

[fabrizio@wilburn]$ sudo vi /etc/sysconfig/vncservers

# The VNCSERVERS variable is a list of display:user pairs.
#
# Uncomment the line below to start a VNC server on display :1
# as my 'myusername' (adjust this to your own).  You will also
# need to set a VNC password; run 'man vncpasswd' to see how
# to do that.
#
# DO NOT RUN THIS SERVICE if your local area network is
# untrusted!  For a secure way of using VNC, see
# URL:http://www.uk.research.att.com/vnc/sshvnc.html

# VNCSERVERS="1:myusername"
VNCSERVERS="1:fabrizio"

Once configuration file has modified and saved we have to generate a VNC password using command vncpasswd:

[fabrizio@wilburn]$ vncpasswd
Password:
Verify:

The command just executed creates a hidden directory .vnc under your home and store in file passwd an encrypted version of the password you entered.
Now let's start VNC service by typing the following command:

[fabrizio@wilburn]$ sudo /sbin/service vncserver start
Starting VNC server: 1:fabrizio                    [  OK  ]

The execution of VNC will create under .vnc folder some configuration files. Now stop the service and edit file xstartup uncommenting the emphasized lines:

[fabrizio@wilburn]$ sudo /sbin/service vncserver stop
Stopping VNC server: 1:fabrizio                    [  OK  ]

[fabrizio@wilburn]$ vi ~/.vnc/xstartup

#!/bin/sh

# Uncomment the following two lines for normal desktop:

unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc


[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
twm &	

Save file, restart service and try to connect to it using vncviewer and providing the password that you entered in the beginning of this tutorial:

[fabrizio@wilburn]$ sudo /sbin/service vncserver start
Starting VNC server: 1:fabrizio                    [  OK  ]

[fabrizio@wilburn]$ vncviewer localhost:1

Configuring multiples VNC services

If you plan to run more than one VNC server you had to modify your configuration file as follows:

[fabrizio@wilburn]$ sudo vi /etc/sysconfig/vncservers

# The VNCSERVERS variable is a list of display:user pairs.
#
# Uncomment the line below to start a VNC server on display :1
# as my 'myusername' (adjust this to your own).  You will also
# need to set a VNC password; run 'man vncpasswd' to see how
# to do that.
#
# DO NOT RUN THIS SERVICE if your local area network is
# untrusted!  For a secure way of using VNC, see
# URL:http://www.uk.research.att.com/vnc/sshvnc.html

# VNCSERVERS="1:myusername"
VNCSERVERS="1:fabrizio 2:angela"

Consider now that all the above steps need to be done by every user in order to make service function correctly. If you want to connect from external source to one of these virtual servers, you need to use port 5901 for first server, port 5902 for second and so on:

[fabrizio@heimdall]$ vncviewer wilburn:5902

In the previous example user fabrizio will connect to server angela on wilburn machine.


Learn more