Install VNC Server and Desktop on CentOS 6

By default your server would probably be setup as a headless (server) version, without a desktop GUI to use. You can install a desktop environment separately onto the operating system and connect remotely using a VNC client so you can manage your server through a GUI, alternative to an SSH session. This guide is done assuming you're using the root user.

Before we get started lets go ahead and update all of the currently installed system packages. Also we will install nano which is an easy to use linux text editor, if you're not familiar with the vi text editor, this will be useful for you.

yum update -y
yum install nano -y

Once you've ran yum update we can install the required packages for the desktop environment. Run the following groupinstall to do this easily and wait, as it might take a couple of minutes.
sudo yum groupinstall Desktop -y

Once the install is completed lets go ahead and install our VNC server and an additional required package.
sudo yum install tigervnc-server xorg-x11-fonts-Type1 -y

Lets set it so the VNC server starts after boot.
chkconfig vncserver on

Now set a VNC password to use when connecting. Run the following command.
passwd
It will ask you to set a password, enter the password that you want to use and press enter, then re-enter to verify.

Lets edit the VNC server configuration now.
nano /etc/sysconfig/vncservers

After you've opened up the vncserver configuration lets navigate to the bottom of the file and add the following lines:
VNCSERVERS="1:root"
VNCSERVERARGS[1]="-geometry 1024x600"
Save the file after making these changes.

Restart the VNC service now so the changes take affect.
service vncserver restart

Now lets kill the active VNC session so we can edit the xstartup file for the user that it's running under.
vncserver -kill :1

Edit the xstartup file now by running:
nano .vnc/xstartup

At the very bottom of the file you'll see the following line:
twm &
Lets comment this line out by adding a # in front:
# twm &

Lets also add the 'exec gnome-session &' line after the line you just commented out.
#twm & 
exec gnome-session &
This is how the bottom of the file should look now (above ^), now lets save this file.

Restart the VNC server now.
service vncserver restart

Now our server VNC and Desktop installation is complete.

Now we'll need something to connect to our servers new desktop environment. We prefer using RealVNC Viewer, download and install this or another VNC viewer of your choice, to your computer.

Once you've installed RealVNC Viewer open the program and enter your server IP and the VNC session port (default is 1 for this guide) and click Connect. We'll use "127.0.0.1" as our server IP as an example. Just change 127.0.0.1 with your servers IP address.



Once you click Connect a prompt will appear next asking for your password. Enter the VNC password that we set earlier in this guide and press OK.



Now it should connect you and the desktop should appear. If so, you're all set!




Done!


Tip: If you're unable to connect to the server through your VNC client, your server firewall might be blocking the connection. Try adding the following rules to the firewall to fix this.
iptables -I INPUT 5 -m state --state NEW -m tcp -p tcp -m multiport --dports 5901:5903,6001:6003 -j ACCEPT
service iptables save
service iptables restart
In most cases this isn't needed however.