VNC or Virtual Network Computing is a graphical desktop sharing tool that allows you to control a computer (server) remotely from another computer (client). A VNC server transmits all keyboard and mouse events from the client computer to the server computer. If you’re not yet comfortable with the CLI things like terminal etc, you can use VNC to make it easier to manage files, software, and system settings.

In this tutorial, I will show you step-by-step on how to set up a VNC server on CentOS 7. We will install a Linux XFCE desktop on the server, then install and configure VNC server using TigerVNC.

Prerequisites

  • CentOS 7
  • Root privileges

What we will do

  1. Update CentOS System and Create a Linux User.
  2. Install XFCE Desktop and TigerVNC.
  3. Initial VNC Configuration.
  4. Configure TigerVNC.
  5. Run TigerVNC as a Service.
  6. Connect to the VNC Server Through SSH Tunnel.

Step 1 – Update CentOS and add a Linux User

Before we start installing and configuring the VNC server, please update your CentOS using the yum command.

yum update -y

For this guide, the VNC desktop will be available for a non-root user. So, we need to create a new user and add it to the ‘wheel’ group for root access.

Add a new user using the command below. I’ll choose the username ‘edward’ in this example.

useradd -m -s /bin/bash edward
passwd edward

Type in the new user password.

Now add the user to ‘wheel’ group for sudo command access.

usermod -a -G wheel edward

After all this is done, login as ‘edward’ and then run the ‘sudo su’ command.

su – edward
sudo su

Type the ‘edward’ user password and make sure you get root privileges.

Add a shell user and set a password

Step 2 – Install XFCE Desktop and TigerVNC

Linux has several desktop environments such as Gnome, Unity, KDE, LXDE, XFCE etc. For this tutorial, we will be using the XFCE desktop as our VNC desktop environment. XFCE is a lightweight deskto that is well suited for a server environment.

Before installing the XFCE desktop, install the latest EPEL repository.

yum -y install epel-release

Now install the XFCE desktop using the yum command below.

yum groupinstall “Xfce” -y

After the installation is complete, install the ‘tigervnc-server’ package.

yum -y install tigervnc-server tigervnc-server-minimal

Wait until the installation is complete.

The XFCE desktop with TigerVNC has been installed.

Step 3 – Initial VNC Configuration

In this step, we will generate the vnc configuration for the user ‘edward’.

Login as the ‘edward’ user.

su – edward

Now initiate the vnc configuration for ‘edward’ using the following command.

vncserver

You will be asked for the vnc server password – type in your password. For ‘view-only’ password, you can enable or disable it. The user who logs in to the server using a ‘view-only’ password will not be able to control the mouse and keyboard.

Setup VNC

The first time we run the ‘vncserver’ command, it will automatically create a new configuration directory ‘.vnc’ and run the first vnc session.

ls -lah ~/.vnc/
vncserver -list

And you will get the first vnc session is running, as shown below.

Start VNC and list VNC sessions

Step 4 – Configure TigerVNC

In this step, we will configure the VNC server for using the XFCE desktop. We will edit the VNC configuration file ‘xstartup’ under the ‘.vnc’ directory.

Before editing the vnc configuration, kill the first vnc session using the command below.

vncserver -kill :1

Now backup the default configuration and create a new one using vim.

mv ~/.vnc/xstartup ~/.vnc/xstartup.bekup
vim ~/.vnc/xstartup

Paste the configuration below.

#!/bin/bash
 xrdb $HOME/.Xresources
 startxfce4 &

Save and exit.

Next, copy the default ‘Xresources’ configuration in the ‘edward’ user home directory.

cp /etc/X11/Xresources ~/.Xresources

And make the ‘xstartup’ script executable by changing its access permissions. Then run the ‘vncserver’ command again.

chmod +x ~/.vnc/xstartup
vncserver

The new vnc session is running with our default desktop XFCE.

Setup XFCE as desktop environment for VNC sessions

Check it using the command below.

vncserver -list

Check VNC session

Step 5 – Running TigerVNC as a Service

In this tutorial, we will run the VNC server as a service. So we need to create new service file for it.

Goto the ‘/etc/systemd/system’ directory and create a new service file ‘vncserver@.service’.

cd /etc/systemd/system
vim vncserver@.service

Paste the following configuration there.

[Unit]
 Description=Remote desktop service (VNC)
 After=syslog.target network.target

 [Service]
 Type=forking
 User=edward
 PIDFile=/home/edward/.vnc/%H:%i.pid
 ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
 ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 :%i
 ExecStop=/usr/bin/vncserver -kill :%i

 [Install]
 WantedBy=multi-user.target

Save the file and exit vim.

Now reload systemd and start the VNC server service.

systemctl daemon-reload
systemctl start vncserver@1.service

If you don’t get any error, enable the service to launch at system boot and check the service status using systemctl.

systemctl enable vncserver@1.service
systemctl status vncserver@1.service

Following is the results in our case.

Start VNC service

Or you can check using the vncserver command as shown below.

su – edward
vncserver -list

List VNC session

The VNC Server installation and configuration has been completed.

Step 6 – Connect to the VNC Server Through SSH Tunnel

In this last step, we will connect to our VNC server through a SSH tunnel.

Open a terminal window and type in the ssh command as shown below.

ssh -L 5901:127.0.0.1:5901 -N -f -l edward 192.168.33.10

Change the IP address with your own server address, and type the ‘edward’ SSH login password.

Connect to VNC trough SSH tunnel

The command will create a tunnel between your localhost and the VNC server. The port 5901 on localhost will be forwarded to the VNC server ‘192.168.33.10’ on port 5901.

Now open your ‘vnc viewer’ application, create a new connection, and type the VNC server address with the 127.0.0.1 localhost IP and port 5901 as shown below.

Open VNC viewer

Connect to the VNC server and you will be asked for the VNC server password. Type your password and click ‘OK’.

Connect to VNC Server

Now you will get the XFCE desktop from your server.

Connection to Desktop trough VNC successful

Click the ‘use default config’ button for the first time, and you will get the XFCE desktop with default configuration.

Installation of the VNC server on CentOS 7 has been completed successfully.

Links

How to Install and Configure VNC Server on CentOS 7