Sunday, December 30, 2012

Installing VNC Server on CentOS


Virtual Network Computing Logo

What is VNC?

VNC (Virtual Network Computing) is a graphical desktop sharing system that is used to remotely control another computer. It transmits the keyboard and mouse events from one computer to another, relaying the graphical screen updates back in the other direction, over a network.

VNC is platform-independent. That is a windows user may connect to a linux server or vice versa. Multiple users using different operating systems can connect to the same VNC server at the same exact time.
Using VNC you can remotely control your home computer from work or vice versa.
You may also remotely control your webserver as long as you have root access to it (usually VPS and Dedicated servers).

Requirements for this Tutorial:

  • Linux CentOS Server
  • Windows Client.
  • In this tutorial I will connect from my windows operating system to my linux server using VNC.
  • To be able to successfully install and run VNC you need to have root access to your server.
  • In this tutorial I'm assuming you are using CentOS operating system. Although installing it on a different Linux operating should be very similar.

Installing VNC Server on CentOS

First we need to install X Window System (X11) which is a software system and network protocol that provides a basis for graphical user interfaces.
yum groupinstall "X Window System"

Then we install the GNOME Desktop Environment, which is a desktop environment and graphical user interface that runs on top of a computer operating system and was developed mainly by Red Hat employees.

For CentOS 5, run the following command:
yum groupinstall "GNOME Desktop Environment"
For CentOS 6, run the following command:
yum groupinstall desktop

Now let's install the VNC Server:
For CentOS 5
yum install vnc-server
For CentOS 6
yum install tigervnc-server

And let's install some fonts (You may skip this step, but later on if you remotely login and you're unable to view the remote desktop's icons and text, you will have to install some fonts)
yum install xorg-x11-fonts-Type1
or
yum groupinstall fonts

Configuring VNC Server for root access:


To login to the remote server as root:

Edit the /etc/sysconfig/vncservers file:
vi /etc/sysconfig/vncservers

Uncomment the following two lines and remove -nolisten tcp -localhost:
# VNCSERVERS="2:myusername"
# VNCSERVERARGS[2]="-geometry 800x600 -nolisten tcp -localhost"

Where 2 is the port number and the myusername is the linux user.

To login as the root with a 800x600 resolution, we change the username to root and the arguments of the port as follow:
VNCSERVERS="2:root"
VNCSERVERARGS[2]="-geometry 800x600"
Save (:w) and quit editing the file (:q)

Create a password for VNC root user using the following command, you can use the same command to change your password later:
vncpasswd

Start the VNC Server:
service vncserver start

After starting the server, a new file will be created /root/.vnc/xstartup


If you are using CentOS 5, edit /root/.vnc/xstartup (You can skip this in CentOS 6):
vi /root/.vnc/xstartup
And comment twm & and append gnome-session & as follow:
#twm &
gnome-session &
Save (:w) and quit editing the file (:q)


Restart the VNC Server:
service vncserver restart

Congratulations, your VNC server should be up and running, check the "Installing VNC Client" below to login and test your connection as root. Or if you want to login as a different user other than root, check the section below:


Configuring VNC Server for root access and another user:


Make sure you are logged in to the server as root:
su -

Create the new user(s) and set their password (if they don't exist yet). I will create a new user for this tutorial called userone:
useradd userone
passwd userone

Edit the /etc/sysconfig/vncservers file:
vi /etc/sysconfig/vncservers

Uncomment the following two lines and remove -nolisten tcp -localhost:
# VNCSERVERS="2:myusername"
# VNCSERVERARGS[2]="-geometry 800x600 -nolisten tcp -localhost"
Where 2 is the port number and the myusername is the linux user.

Please note that only the last digit of the port number is entered and the first three are implicit. For example, 2 defines port 5902. 9 indicates use of port 5909

To login as the root with a 640x480 resolution, we change the username to root and the arguments of the port as follow:
VNCSERVERS="1:root"
VNCSERVERARGS[1]="-geometry 640x480"

To login as userone with a 800x600 resolution, we add the port number and the username to VNCSERVERS and we add a new argument for the port. I will use port number 2 for userone.
VNCSERVERS="1:root 2:userone"
VNCSERVERARGS[1]="-geometry 640x480"
VNCSERVERARGS[2]="-geometry 800x600"
 Save (:w) and quit editing the file (:q)


Switch users and create a password for your VNC users using the following command, you can use the same command to change your password later:
su -
vncpasswd

su userone
vncpasswd

Login back as root and start (or restart) the VNC Server:
su -
service vncserver start

After starting the server, a new file for each user will be created:
root will have the following file created: /root/.vnc/xstartup
userone will have the following file created: /home/userone/.vnc/xstartup

If you are using CentOS 5, edit /root/.vnc/xstartup (You can skip this in CentOS 6):
vi /root/.vnc/xstartup
And comment twm & and append gnome-session & as follow:
#twm &
gnome-session &
Save (:w) and quit editing the file (:q)

Do the same for userone: (You can skip this in CentOS 6):
su userone
cd
vi .vnc/xstartup
And comment twm & and append gnome-session & as follow:
#twm &
gnome-session &
Save (:w) and quit editing the file (:q)


Login back to root and Restart the VNC Server:
su -
service vncserver restart

Congratulations, your VNC server should be up and running, check the "Installing VNC Client" below to login and test your connection.


Installing VNC Client


To connect to your server remotely, you need to download install VNC Viewer.

Open VNC Viewer and enter your server's ip or hostname and the port number, for example:

VNC Viewer

Enter the password that you have created with vncpasswd, and connect.

You will see your server's desktop, such as:

VNC Viewer Desktop


Notes:

You will most probably need to disable Network Manager after you install the GUI. Network manager usually overwrite the settings of /etc/resolv.conf after every reboot.
service NetworkManager stop
chkconfig NetworkManager off

If you want to use a web browser to browse the internet, you can install firefox using the following command:
yum install firefox