VNC stands for VIrtual Network computing. It is the technology of remotely accessing other computers desktop environment.. Using VNC you can control that computer’s desktop with your mouse and keyboard as your are actually sitting in front of that desktop.
In this post I will show you how to configure VNC access to a server running Red hat or CentOS linux operating system.
Our setup :
CentOS 7.2 server with IP address 192.168.8.106
Installing the necessary software:
Before heading up to configure VNC for remote desktop access, we have to make sure that the server has a graphical environment installed. Otherwise it does not make sense to offer GUI environment because is not present in the server to begin with. Lets assume the server does have a GUI installed, we will install it now.
$ yum -y groupinstall "server with GUI"
Then install the software for vnc.
$ yum -y install tigervnc tigervnc-server
Firstly we need to create a user, who can use vnc service. Lets add a user called “red” with a password “redpass”
$ useradd red $ passwd red
Now, we need to modify the service file for this user. Go to the “/usr/lib/systemd/system” directory. Here you will see a file called “vncserver@.service” . Each user that is allowed to access vnc server have to have their own copy of this file. So to create a copy of this file for our first vnc user (“red” in this case”) we enter following command
$ cp vncserver@.service vncserver@\:1.service
Here we copying it to vncserver@\:1.service file where 1 denotes the first user. Now we will edit this file to associate it with user “red”. In the file, go to the section called “service” and replace <user> with the username we have created previously.
[Service] Type=forking # Clean any existing files in /tmp/.X11-unix environment ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :' ExecStart=/usr/sbin/runuser -l red -c "/usr/bin/vncserver %i" PIDFile=/home/red/.vnc/%H%i.pid ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
Restart the system daemon to make the change effective.
$ systemctl daemon-reload
Now login as user red and enter the command “vncpasswd” . This step is to set up a password for vnc session of this user. Remember this is not user’s system password , this is only for VNC.
Now log back into the system as root. Start the vnc service for user red and enable it to start automatically every time server starts/
$ systemctl start vncserver@:1.service $ systemctl enable vncserver@:1.service
Firewall Configuration:
We need to the open the firewall, so it allows the vnc traffic to pass through.
$ firewall-cmd --permanent --add-service=vnc-server $ firewall-cmd --reload
Accessing the VNC server:
There are several Vnc client available on the internet. We are going to use Realvnc viewer
Now to access the server we need the IP address of the server , we want to connect to and the port number. Port number for vnc start from 5900 and corresponds to the vnc service file of for that user in the server. So the user for first service file will use 5901, next one will use 5902 and so on.
Start Realvnc viewer . Go to file > New connection. Put 192.168.8.106:5901 .
Now the connection will show on the opening window. Click on it, it will ask for username and password. Put the password we have set for our vnc user.