How to Configure VNC (Virtual Network Computing) on CentOS
Introduction
VNC (Virtual Network Computing) allows administrators to remotely access a graphical desktop environment on a Linux server. It is useful for managing applications that require a GUI and for users who prefer graphical administration over the command line.
This guide explains how to install, configure, and access a VNC server on a CentOS system.
Prerequisites
Before proceeding, ensure that:
- You have root or sudo access to the server.
- The server is running CentOS.
- Internet access is available to download required packages.
- Required firewall ports (5901, 5902, etc.) can be opened.
- A desktop environment is available or can be installed on the server.
Implementation
Step 1: Install VNC Server
Check whether the VNC package is already installed:
rpm -q tigervnc-server
If it is not installed, install it using yum:
yum install tigervnc-server -y yum install vnc -y
If additional dependencies are required, install them:
yum install mesa-dri-drivers -y yum install xkeyboard-config -y yum install xorg-x11-fonts-misc -y yum install libXfont -y
Step 2: Install Desktop Environment
If a graphical desktop environment is not already available, install it:
yum groupinstall "Desktop" -y
Step 3: Create a User for VNC Access
Create a new user account:
useradd myuser passwd myuser
Step 4: Configure VNC Password
Switch to the newly created user:
su - myuser
Set the VNC password:
vncpasswd
This creates the .vnc directory and stores the encrypted VNC password.
Exit back to the root user:
exit
Step 5: Configure VNC Server
Edit the VNC configuration file:
vi /etc/sysconfig/vncservers
Add the following configuration:
VNCSERVERS="1:myuser" VNCSERVERARGS[1]="-geometry 1024x768"
To enable VNC access for both a normal user and root:
VNCSERVERS="1:myuser 2:root" VNCSERVERARGS[1]="-geometry 1024x768" VNCSERVERARGS[2]="-geometry 1024x768"
Save and exit the file.
Step 6: Start the VNC Service
Start the VNC server:
service vncserver start
Enable it to start automatically after reboot:
chkconfig vncserver on
Step 7: Verify VNC is Running
Check the VNC processes:
ps -ef | grep Xvnc
You should see entries similar to:
/usr/bin/Xvnc :1 /usr/bin/Xvnc :2
The display number determines the listening port:
| Display | Port |
|---|---|
| :1 | 5901 |
| :2 | 5902 |
| :3 | 5903 |
Step 8: Open Firewall Ports
Allow the required VNC ports through the firewall.
Example for port 5901:
iptables -I INPUT -p tcp --dport 5901 -j ACCEPT service iptables save service iptables restart
If a cloud firewall or hardware firewall is in use, ensure the same ports are allowed there as well.
Step 9: Connect to the VNC Server
From a VNC client, connect using:
SERVER_IP:5901
or
SERVER_IP:1
Example:
192.168.1.100:5901
Enter the VNC password configured earlier when prompted.
Verification
Confirm the following:
- VNC service is running.
- Required firewall ports are open.
- VNC client successfully connects to the server.
- Desktop environment loads correctly after authentication.
You can verify the listening ports using:
netstat -tulpn | grep 59
or
ss -tulpn | grep 59
Conclusion
VNC provides a convenient graphical interface for managing CentOS servers remotely. By installing the VNC server, configuring user access, and opening the required ports, administrators can securely connect to and manage their systems through a desktop environment from anywhere.
