How to Install and Configure VNC Server on CentOS 7

VNC allows us to open a remote GUI session to a server and thus providing us with a fully graphical interface accessible from any remote location.

VNC server is a free and open source software which is designed for allowing remote access to the Desktop Environment of the server via a VNC Client (VNC viewer).

In this tutorial, we'll learn what is VNC and how to install or configure VNC Server on CentOS 7.

Some Benefits of VNC server are listed below:

  • Remote GUI administration makes work easy & convenient.
  • Clipboard sharing between host CentOS server & VNC-client machine.
  • GUI tools can be installed on the host CentOS server to make the administration more powerful
  • Host CentOS server can be administered through any OS having the VNC-client installed.
  • More reliable over ssh graphics and RDP connections.

First of all we'll need a working Desktop Environment (X-Windows), if we don't have a working GUI Desktop Environment (X Windows) running, we'll need to install it first.

Note: The commands below must be running under root or user with sudo privilege.

1. Installing X-Windows

First of all to install X-Windows we'll need to execute the below commands in a shell or terminal. It will take few minutes to install its packages.

$sudo yum check-update
$sudo yum groupinstall "X Window System"

installing x windows

$sudo yum install gnome-classic-session gnome-terminal nautilus-open-terminal control-center liberation-mono-fonts
$sudo unlink /etc/systemd/system/default.target
$sudo ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target

configuring graphics

$sudo reboot

After our machine restarts, we'll get a working CentOS 7 Desktop.

2. Installing VNC Server Package on CentOS

Now, we'll install VNC Server package in our CentOS 7 machine. To install VNC Server, we'll need to execute the following command to install tigervnc server.

$sudo yum install tigervnc-server -y

vnc server

3. Configuring VNC

Then, we'll need to create a configuration file under '/etc/systemd/system/' directory. We can copy the 'vncserver@:1.service' file from example file from '/lib/systemd/system/vncserver@.service'.

$sudo cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service

copying vnc server configuration

Now we'll open '/etc/systemd/system/vncserver@:1.service' in a text editor. Then find the below lines of text in that file and replace '<USER>' with your username.

Here, in my case its linoxide so I am replacing '<USER>' with 'linoxide' and looks like below:

ExecStart=/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i"
PIDFile=/home/<USER>/.vnc/%H%i.pid

to

ExecStart=/sbin/runuser -l linoxide -c "/usr/bin/vncserver %i"
PIDFile=/home/linoxide/.vnc/%H%i.pid

If you are creating for root user then:

ExecStart=/sbin/runuser -l root -c "/usr/bin/vncserver %i"
PIDFile=/root/.vnc/%H%i.pid

configuring user

Now, we'll need to reload our systemd.

$ sudo systemctl daemon-reload

Finally, we'll create VNC password for the user . To do so, first you'll need to be sure that you have sudo access to the user, here I will login to user "linoxide" then, execute the following. To login to we'll run 'su linoxide' without quotes .

# su linoxide
$ sudo vncpasswd

setting vnc password

Make sure that you enter passwords more than 6 characters.

4. Enabling and Starting the service

To enable service at startup ( Permanent ) execute the commands shown below.

$ sudo systemctl enable vncserver@:1.service

Then, start the service.

$ sudo systemctl start vncserver@:1.service

5. Allowing Firewalls

We'll need to allow VNC services in firewall now.

$ sudo firewall-cmd --permanent --add-service vnc-server
$ sudo systemctl restart firewalld.service

allowing firewalld

Now you can able to connect VNC server using IP and Port ( Eg : ip-address:1 ).

6. Connecting the machine with VNC Client

Finally, we are done installing VNC Server. No, we'll wanna connect the server machine and remotely access it. For that we'll need a VNC Client installed in our computer which will only enable us to remote access the server machine.

remote access vncserver from vncviewer

You can use VNC client like Tightvnc viewer and Realvnc viewer to connect Server.

To connect with additional users create files with different ports, please go to step 3 to configure and add a new user and port, You'll need to create 'vncserver@:2.service' and replace the username in config file and continue the steps by replacing service name for different ports. Please make sure you logged in as that particular user for creating vnc password.

VNC by itself runs on port 5900. Since each user will run their own VNC server, each user will have to connect via a separate port. The addition of a number in the file name tells VNC to run that service as a sub-port of 5900. So in our case, 'aruns' VNC service will run on port 5901 (5900 + 1) and further will run on 5900 + x. Where, x denotes the port specified when creating config file 'vncserver@:x.service' for the further users.

We'll need to know the IP Address and Port of the server to connect with the client. IP addresses are the unique identity number of the machine. Here, my IP address is 96.126.120.92 and port for this user is 1. We can get the public IP address by executing the below command in a shell or terminal of the machine where VNC Server is installed.

$sudo curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'

How to uninstall VNC Server

You can easily uninstall vnc server using yum command.

Run the following command to remove tigervnc from the centos server:

$sudo yum remove tigervnc-server

Conclusion

Finally, we installed and configured VNC Server in the machine running CentOS 7/RHEL 7 (Red Hat Enterprises Linux). VNC is the easiest FOSS tool for remote access and also a good alternative to TeamViewer remote access tool. VNC allows a user with VNC client installed to control the machine with VNC Server installed.

Here are some more commands that might be useful while working with vnc.

$sudo systemctl stop vncserver@:1.service

To disable VNC service from startup:

$sudo systemctl disable vncserver@:1.service
Arun Pyasi 6:00 am

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.

1 Comment

  1. Hi,

    To change the run level instead of:
    # unlink /etc/systemd/system/default.target
    # ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target
    Type:
    # systemctl set-default graphical.target

    Regards.

    CertDepot