Wednesday, March 18, 2015

Instaling and Configuring VNC Server on CentOS 7 and Fedora 20

Introduction:

VNC Server gives you a good way and reliable way to run desktop environment on your server so you can have GUI control over your server just as you would use your local computer.
This tutorial is split into two parts, first installing VNC server, the second in configuring VNC server.

Why VNC:
  • More reliable than ssh graphics and RDP.
  • Using GUI tools to control your server.
  • Allows you to share clipboard between your local computer and the remote server.
  • You can use and host that have VNC client to control your server.

Note:
This tutorial was tested on both CentOS 7 and Fedora 20. If you encountered any problem, please leave a comment and I will help you get your problem solved.

Installing VNC Server:
You need to install the following in order to get VNC server up and running on you server:
  • Desktop Environment (XFCE, KDE, Gnome)
  • VNC Server.
  • Open ports in iptables to allow remote connections.
Now let us dive into the instructions

Installing your desktop environment:

  • XFCE
      yum groupinstall xfce
  • Gnome
      yum groupinstall "GNOME Desktop"

Installing VNC Server:
yum install tigervnc-server

Configuring VNC Server:
In this step we will configure VNC server in order to get it up and running and can pass iptables.

First we need to copy the template of the VNC server service file and put it on its real folder.
cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service
We got the service file in its location, but it still not ready for actual usage.

Second we need to edit that file to properly configure it
vi /etc/systemd/system/vncserver@:1.service
 Go to this section
[Service]
Type=forking
User=
ExecStart=/usr/bin/vncserver <:port_number> -geometry -depth 24
ExecStop=/usr/bin/vncserver -kill <:port_number>

and change it according to the following:

  • : Is the username that you like to login to when using VNC. For example engmah
  • <:port_number>: The port of the VNC service on which you will connect to. For example :2
  • : The default screen resolution that will be user, but in VNC client you can resize the screen dynamically without altering this value. For example: 1280x1024.

Here is an example:
[Service]
Type=forking
User=engmah
ExecStart=/usr/bin/vncserver :1 -geometry 1280x1024 -depth 24
ExecStop=/usr/bin/vncserver -kill :1
Thirdly we need to set up password for VNC.

Here the password is different from the password of the user that wil be logged in when using VNC. It is the password of the VNC session not the password of Linux user. So you can make it different than the password of the user.
To set VNC session password login to the user and execute:
vncpasswd
Then enter VNC session password. You will use this password when connecting to VNC server on the same port that you specified for the user in VNC service configuration file. For example we specified in the file the user engmah and the port is 2 so when connecting on port 2 we will be asked for the password for VNC session that is set via engmah user.

Now we need to run VNC server from the same user (e.g engmah)
vncserver
Finally we need to run the service:

Now login to root and execute the following commands
systemctl daemon-reload
systemctl enable vncserver@:1.service 
systemctl start vncserver@:1.service
Here is an explanation for these three commands
systemctl daemon-reload 
Is used to reload systemd so it recognizes your new service file
systemctl enable vncserver@:1.service
Enable your service to run at startup. Ti disable it from starting at startup use the following command
systemctl disable vncserver@:1.service
systemctl start vncserver@:1.service
Starts your service. If you want at anytime to stop your service use the following command
systemctl stop vncserver@:1.service
Note:
In the name of the service (i.e vncserver@:1.service) you noticed that we used @:1. The number we specify here is the number of the service port we specified in the configuration file of the service. This is useful because we can create many VNC services for many users, just create a new service file with a different port number and different username.

Extra things to do:

On some Linux systems, you will notice that after doing all of this, you still cannot connect to you VNC servcer. This is because one of two common problems or both of them. Let us make sure that these problems are solved before testing our VNC server.

First Problem:

You firewall blocks servers from listening on ports for incmming connection, so you need to enable VNC server in your firewall.

If you are using firewalld, execute the following commands:
firewall-cmd --permanent --zone=public --add-service vnc-server
firewall-cmd --reload
If you are using iptables, open the ports that are used by VNC server. To open the port, execute the following commands:
iptables -I INPUT 5 -m state --state NEW -m tcp -p tcp -m multiport --dports 5901:5990,6001:6090 -j ACCEPT
service iptables save
service iptables restart
Second Problem:

The xstartup file in ~/.vnc folder is not executable. This is a common problem on CentOS 6.x, but let us make sure that xstartup file is executable in order to get rid of any expected error. Login to the user that you set in VNC service file and execute the following command:
chmod +x ~/.vnc/xstartup
Clipboard Sharing:
In order to allow cliboard sharing between your server and VNC client, you need to keep vnc config running on your server. To run vnc config use the following command on your server:
vncconfig &
The & is used so you can close the terminal and still have vnc config running. Just minimize that dialog and you will have clipboard sharing between server and client.

Adding more users to access VNC server:
To add another user, follow the same steps you did in this tutorial, but change the port number. Here are examples.
cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:2.service
[Service]
Type=forking
User=engmah
ExecStart=/usr/bin/vncserver :2 -geometry 1280x1024 -depth 24
ExecStop=/usr/bin/vncserver -kill :2
systemctl enable vncserver@:2.service 
systemctl start vncserver@:2.service
Connecting to VNC server:
Using any VNC client connect to the IP of your server and the port number of the service. The port number that you use to connect to the VNC server is the port number of the service plus 5900. For example in previous steps we used port 1 so we connect to port (5900 + 1 = 5901).
192.168.1.80:5901

No comments:

Post a Comment