Most Linux boxes are terminal based. The idea is that running a GUI (or an "X" Window system to be specific) takes up a lot of resources that would be better used to improve the efficiency of the server instead. We can do everything via the command line - and often faster. However, there are some use cases where you might want to use a GUI instead. And not just on client machines, but on servers. Ease of use is one big factor, where you want to present a familiar interface to the user.
If you want to run a GUI on a Linux server, you need a way to transmit the desktop to a remote client. Without a GUI, we can use programs like PuTTY to issue commands. But doing the same thing when the server is running a GUI is a whole different ball game. For that, we need a specialized program capable of performing this complex task. And the standard for that is VNC. It stands for "Virtual Network Computing" and it uses the client server model. In this tutorial, I'm going to show you how to set up a VNC server on a CentOS/RHEL system and connect to it from a remote client.
Step 1: Install the Tiger VNC Package
To install Tiger VNC on CentOS, simply type:
yum install tigervnc-server
This will install the server along with a whole bunch of other dependencies.
Wait for the installation to be complete and you're done!
Step 2: Create the Service File in /etc/systemd/system/
Unlike other packages, tigervnc-server doesn't automatically set the program to run on startup. This is because it doesn't know how we want to run it. In this example, I'm just going to configure it for a single user - root. The installation has created a file called:
The first step is to copy this file to the /etc/systemd/system/ location and give it an affix like this:
cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service
Note that I've changed the name from "vncserver@.service" to "vncserver@:1.service. If I wanted to allow login from another user, I'd create another copy with "2", and so on and so forth.
Step 3: Modify the Service File with the User Name
The files that we've copied into /etc/systemd/system are incomplete. Using a text editor, we need to replace <USER> with the name of the user who we want to allow access via Tiger VNC. This is done in two places. First open up the service file for editing:
Then using the search function, replace <USER> with the user name in these two locations:
Important: Make sure that the directory for PIDFile exists! If not, modify the path. In the original file for example, the path was:
This directory didn't exist in my installation and due to this, I got an error that said:
"Job for vncserver@:1.service failed because a configured resource limit was exceeded."
This error is extremely misleading. In reality, it was because of the incorrect directory name in the original service file that I later modified to:
After making this change, the resource error vanished.
Step 4: Enable the VNC Service and Start it Up
Once you've made and saved the changes to the config file, you can enable it using the command:
systemctl enable vncserver@:1.service
Repeat this for each user's service file that you created and changed. This command will place symbolic links into the appropriate "wants" directories in /etc/systemd/system. Now reload the services configuration with:
And then start/restart the VNC services:
systemctl enable vncserver@:1.service
Now that the services are running, we can set the passwords and the connection port numbers.
Step 5: Run vncserver and set the Password and Get the Port Number
Next, log in with the user who needs to connect and run the command "vncserver". Since I'm already logged in as root, I just ran the command:
This will prompt you to enter a password for connecting to the VNC server with the client. It's separate from your login password. It also tells us that my port number is "1" via this text:
This means that the port number is 5900+1 = 5901 for this particular user. You can verify this by looking at the log file created at /root/.vnc/Testing-out-NVC:1.log
So now we have the port numbers and the password. We can finally connect!
Step 6: Connecting to the Server via a Client
Head over to https://www.realvnc.com/download/viewer/ and download the client for your operating system. Mine is Windows, so I just download it using the link:
Start up the program and go through the welcome screen. Now in the top box, type in the IP address or name of the server followed by the port number of the user you want to connect as and separated by a colon (:)
This will give you a warning about an unencrypted connection. Since we're just learning how to connect, it's ok for now.
And finally, it'll ask you for the password that you set when executing the "vncserver" command:
This will allow you to finally connect to the remote VNC server. If you haven't installed an X window system like Gnome, you'll just get a blank screen:
In the next tutorial, I'll show you how to install Gnome and what it looks like when connected with VNC.