Virtual Network Computing (VNC) works by capturing the display’s frame buffer and making it available across the network. This recipe shows you how to install the basic VNC server and configure it to provide remote users access to their graphical desktop environment as if they were physically in front of the system.
Installing the basic VNC service
To install the prerequisite, run the appropriate command as root:
For CentOS/RHEL 6:
# yum install tigervnc-server tigervnc
For CentOS/RHEL 5:
# yum install vnc vnc-server
For VNC to be useful, it also requires that the X Windows System is installed along with a window manager. If a graphical desktop environment is not already installed, refer below post for the steps.
Configuring the VNC service
1. Edit the /etc/sysconfig/vncservers file to include the users it will start VNC servers for.
– Add a line to that file as follows:
# vi /etc/sysconfig/vncservers VNCSERVERS="N:user"
Where,
N is the number of the display VNC server will be running and user is the username by which the server will be run as. (Usually starting at 1)
– Multiple displays and users can be specified by placing a space between them, as follows:
# vi /etc/sysconfig/vncservers VNCSERVERS="N:user1 Y:user2"
2. Then the configuration of VNC should be customized for each individual user:
For CentOS/RHEL 6, add a line(s) to the file as follows:
# vi /etc/sysconfig/vncservers VNCSERVERARGS[1]="-geometry 1024x768 -nolisten tcp" VNCSERVERARGS[2]="-geometry 800x600 -nolisten tcp"
For CentOS/RHEL 5, add a line(s) to the file as follows:
# vi /etc/sysconfig/vncservers VNCSERVERARGS[1]="-geometry 1024x768 -nohttpd" VNCSERVERARGS[2]="-geometry 800x600 -nohttpd"
In the above example, the users configured to use displays N and Y (1 and 2 for our example) are now allowed to access VNC remotely if the ports are open on the machine’s firewall.
3. The firewall ports that will need to be opened for the example users, whom are using displays N and Y:
For user1: Open port 5901 (5900 + N), where we choose N as display 1.
For user2: Open port 5902 (5900 + Y), where we choose Y as display 2.
Configuring each user for VNC access
For each user specified, a VNC password needs to be set. VNC passwords are completely separate from the normal system password for that account. A user can set their VNC password by executing the vncpasswd command as shown below:
$ vncpasswd Password: Verify:
By default, VNC starts up only a simple window manager and a terminal window. To enable a full Red Hat desktop environment over VNC, please refer to below post.
Starting the VNC service
1. In order to start the vncserver service immediately, run the following command:
# service vncserver start Starting VNC server: 1:user1
2. In order for the vncserver service to start persistently after reboot, run the following command:
# chkconfig vncserver on
3. The success of the command above can be verified by running:
# chkconfig --list vncserver vncserver 0:off 1:off 2:off 3:on 4:on 5:on 6:off
Connecting with a VNC client
Now that VNC is running, vncviewer command can be used to connect from a remote Linux machine to the proper VNC-based X session. If the remote system is running Windows, a Windows-based VNC viewer program can be freely downloaded from the website http://www.tigervnc.org/.
# vncviewer vncserver-ipaddress:N
To connect to the user display using the vncviewer client, replace N with the number of the VNC-based X session for the desired user, as per the configuration in /etc/sysconfig/vncservers.
If connecting using a port number instead of display number, most vnc clients will require the syntax of vncserver-ipaddress::portnumber (ex. 127.0.0.1::5901).
Troubleshooting
These VNC-based X sessions must be left running when users are finished with them. They can do this by simply closing the vncviewer program instead of logging out. This maintains the state of the desktop so that when they reconnect, all their programs will be in the same state in which they were left.
If a user accidentally logs out from within a VNC-based X session they should follow these steps to get X working again:
- Log in to the server as the user for whom VNC needs to be restarted.
- Issue the command vncserver -kill :N
- Run vncserver :N
If a connection to display issue is present, ensure that the resulting file has the execute bit set:
# chmod 755 ~username/.vnc/xstartup