Wednesday, November 23, 2011

VNC Configuration

There are several ways to configure the vnc server. This HOWTO shows you how to configure VNC using the 'vncserver' service as supplied by CentOS.

1. Installing the required packages

The server package is called 'vnc-server'. Run the command rpm -q vnc-server.
The result will be either package vnc-server is not installed or something like vnc-server-4.0-11.el4.
If the server is not installed, install it with the command: yum install vnc-server.
The client program is 'vnc'. You can use the command yum install vnc to install the client if rpm -q vnc shows that it is not already installed.
Make sure to install a window manager in order to get a normal GUI desktop. You can use the command yum groupinstall "GNOME Desktop Environment" to install the Gnome Desktop and requirements, for example. Other popular desktop environments are "KDE" and "XFCE-4.4". XFCE is more light-weight than Gnome or KDE and available from the "extras" repository.
<!> If you are running CentOS 5, yum groupinstall "GNOME Desktop Environment" may complain about a missing libgaim.so.0. This is a known bug. Please see CentOS-5 FAQ for details.
<!> If you are running CentOS 6, the server is tigervnc-server not vnc-server.

2. Configuring un-encrypted VNC

We will be setting up VNC for 3 users. These will be 'larry', 'moe', and 'curly'.
You will perform the following steps to configure your VNC server:
  1. Create your VNC users.
  2. Set your users' VNC passwords.
  3. Edit the server configuration.
  4. Create and customize xstartup scripts.
  5. Start the VNC service.
  6. Test each VNC user.
  7. Setup the VNC service to start on reboot.
  8. Additional optional enhancements

2.1. Create your VNC users

As root:
$ su -
# useradd larry
# useradd moe
# useradd curly
# passwd larry
# passwd moe
# passwd curly

2.2. Set your users' VNC passwords

Login to each user, and run vncpasswd. This will create a .vnc directory.
[~]$ cd .vnc
[.vnc]$ ls
passwd

2.3. Edit the server configuration

Edit /etc/sysconfig/vncservers, and add the following to the end of the file.
VNCSERVERS="1:larry 2:moe 3:curly"
VNCSERVERARGS[1]="-geometry 640x480"
VNCSERVERARGS[2]="-geometry 640x480"
VNCSERVERARGS[3]="-geometry 800x600"
Larry will have a 640 by 480 screen, as will Moe. Curly will have an 800 by 600 screen.

2.4. Create xstartup scripts ( Skip this step for CentOS 6 )

We will create the xstartup scripts by starting and stopping the vncserver as root.
# /sbin/service vncserver start
# /sbin/service vncserver stop
Login to each user and edit the xstartup script. To use Larry as an example, first login as larry
[~]$ cd .vnc
[.vnc] ls
mymachine.localnet:1.log  passwd  xstartup
Edit xstartup. The original should look like:
#!/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 &
Add the line indicated below to assure that an xterm is always present, and uncomment the two lines as directed if you wish to run the user's normal desktop window manager in the VNC. Note that in the likely reduced resolution and color depth of a VNC window the full desktop will be rather cramped and a look bit odd. If you do not uncomment the two lines you will get a gray speckled background to the VNC window.
#!/bin/sh
# Add the following line to ensure you always have an xterm available.
( while true ; do xterm ; done ) &
# 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 &

2.5. Start the VNC server

Start the vncserver as root.
# /sbin/service vncserver start

2.6. Test each VNC user

2.6.1. Testing with a java enabled browser

Let us assume that mymachine has an IP address of 192.168.0.10. The URL to connect to each of the users will be:
Larry is http://192.168.0.10:5801
Moe   is http://192.168.0.10:5802
Curly is http://192.168.0.10:5803
Connect to http://192.168.0.10:5801. A java applet window will pop-up showing a connection to your machine at port 1. Click the [ok] button. Enter larry's VNC password, and a 640x480 window should open using the default window manager selected for larry . The above ports  5801, 5802 and 5803  must be open in the firewall {iptables) for the source IP addresses or subnets of a given client.

2.6.2. Testing with a vnc client

For Larry: vncviewer 192.168.0.10:1
For   Moe: vncviewer 192.168.0.10:2
For Curly: vncviewer 192.168.0.10:3
To test larry using vncviewer, vncviewer 192.168.0.10:1. Enter Larry's VNC password, and a 640x480 window should open using Larry's default window manager. The vncviewer client will connect to port 590X where X is an offset of 1,2,3 for Larry, Moe, and Curly respectively, so these ports must be open in the firewall for the IP addresses or subnets of the clients.

2.6.3. Starting vncserver at boot

To start vncserver at boot, enter the command /sbin/chkconfig vncserver on.
For basic VNC configuration the procedure is now complete. The following sections are optional refinements to enhance security and functionality.

3. VNC encrypted through an ssh tunnel

You will be connecting through an ssh tunnel. You will need to be able to ssh to a user on the machine. For this example, the user on the vncserver machine is Larry.
  1. Edit /etc/sysconfig/vncservers, and add the option -localhost.
    VNCSERVERS="1:larry 2:moe 3:curly"
    VNCSERVERARGS[1]="-geometry 640x480 -localhost"
    VNCSERVERARGS[2]="-geometry 640x480 -localhost"
    VNCSERVERARGS[1]="-geometry 800x600 -localhost"
    
  2. /sbin/service vncserver restart
  3. Go to another machine with vncserver and test the VNC.
    1. vncviewer -via larry@192.168.0.10 localhost:1
    2. vncviewer -via moe@192.168.0.10 localhost:2
    3. vncviewer -via curly@192.168.0.10 localhost:3
By default, many vncviewers will disable compression options for what it thinks is a "local" connection. Make sure to check with the vncviewer man page to enable/force compression. If not, performance may be very poor!

4. Recovery from a logout ( Not implemented for CentOS 6 )

If you logout of your desktop manager, it is gone!
  • We added a line to xstartup to give us an xterm where we can restart our window manager.
    • For gnome, enter gnome-session.
    • For kde, enter startkde.

5. Remote login with vnc-ltsp-config

To allow remote login access via a vnc-client to the Centos system, the RPM packages named vnc-ltsp-config and xinetd can be installed. When a vnc-client connects to one of the configured ports, the user will be given a login screen. The sessions will *not* be persistent. When a user logs out, the session is gone.
The rpm package vnc-ltsp-config is easily installed via the EPEL repository noted in Available Repositories
Note: There are no major dependencies for the package so the vnc-ltsp-config*.rpm could easily be downloaded and installed without the need for enabling the EPEL repository.
Install, as root via:
# yum install xinetd vnc-ltsp-config
# /sbin/chkconfig xinetd on
# /sbin/chkconfig vncts on
# /sbin/service xinetd restart
Next, as root edit the file "/etc/gdm/custom.conf".
  • To the next blank line below the "[security]" section add "DisallowTCP=false"
  • To the next blank line below the "[xdmcp]" section add "Enable=true"
  • Make sure you are in a position to either run "gdm-restart" for default Gnome installs or just reboot the CentOS box.
This will add the ability to get the following default vnc-client based session connections:
resolution
color-depth
port
1024x768
16
5900/tcp
800x600
16
5901/tcp
640x480
16
5902/tcp
1024x768
8
5903/tcp
800x600
8
5904/tcp
640x480
8
5905/tcp