How to Install and Configure VNC Server in CentOS 7 / RHEL 7

The following simple guide helps you to setup the VNC server on RHEL 7/CentOS 7 machines, VNC help to share the desktop with the other machines which have a client installed.

VNC server and client are not installed, by default. You need to setup the vnc server manually.

Step 1: Prerequisites

Before installing the VNC server, you would need install Gnome desktop using YUM command.

READ: Install Gnome Desktop on CentOS 7 / RHEL 7

Step 2: Install VNC Server in CentOS 7

VNC server package is available on base operating system repository. So, you can install the tigervnc server and X11 fonts packages using the YUM command.

yum install -y tigervnc-server xorg-x11-fonts-Type1

Step 3: Configure VNC Server in CentOS 7

Copy the VNC server configuration file to /etc/systemd/system/ for configuring the system service. While copying, you can mention which port it should listen.

By default VNC server listens on 5900, but if you setup port-offset for VNC; we can run a service on a sub-port of the default port 5900. For example, if we mention port 5, the desktop will be accessible on 5905.  You can connect the VNC server by specifying ipaddress:sub-port (192.168.2.109:5 or 192.168.2.109:5905)

cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:5.service

Edit the copied file.

vi /etc/systemd/system/vncserver@:5.service

At the end of the file, you will find the lines like below.

[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[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=/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i"
PIDFile=/home/<USER>/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
[Install]
WantedBy=multi-user.target

Replace <USER> with your real user, in my case, i replaced with a user called “raj” .

[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[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=/sbin/runuser -l raj -c "/usr/bin/vncserver %i"
PIDFile=/home/raj/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
[Install]
WantedBy=multi-user.target

Step 5: Firewall

Add firewall rules to allow the VNC connection. The following rule is set as per the port-offset mentioned in the previous step.

firewall-cmd --permanent --add-port=5905/tcp
firewall-cmd --reload

Step 6: Start VNC Server in CentOS 7

Start VNC server after switching to raj.

[raj@server ~]$ vncserver

Set the password.

You will require a password to access your desktops.

Password:
Verify:
xauth:  file /home/raj/.Xauthority does not exist

New 'localhost.localdomain:1 (raj)' desktop is server.itzgeek.com:1

Creating default startup script /home/raj/.vnc/xstartup
Starting applications specified in /home/raj/.vnc/xstartup
Log file is /home/raj/.vnc/server.itzgeek.com:1.log

Reload the systemctl daemon as root.

systemctl daemon-reload

Start the VNC service as root.

systemctl start vncserver@:5.service

Enable it on system startup as root.

systemctl enable vncserver@:5.service

Step 7: Access VNC Desktop

Open up a VNC viewer from Windows machine and enter a remote desktop address.

ipaddress:5905

OR

ipaddress:5

Configure VNC Server in CentOS 7 - VNC Connection
Configure VNC Server in CentOS 7 – VNC Connection
You will be asked to enter the password, enter the password that you have created earlier.
Configure VNC Server in CentOS 7 - VNC Authendication
Configure VNC Server in CentOS 7 – VNC Authentication
Now you will get the vnc desktop session.
Configure VNC Server in CentOS 7 - VNC Desktop
Configure VNC Server in CentOS 7 – VNC Desktop
 That’s all. You have successfully configured VNC server on CentOS 7 / RHEL 7.

Further Reading

You might also like