VNC

How To Set Up VNC For CentOS 9: A Step-by-Step Guide

Setting up a Virtual Network Computing (VNC) server is crucial for remote desktop access, especially when managing CentOS servers without a graphical user interface (GUI). VNC allows you to access the desktop environment of your server from another machine, providing a simple way to manage applications and perform tasks remotely. In this guide, we will walk you through the steps to set up VNC on CentOS 9. Let’s get started!

Prerequisites

Before we dive into the setup, here are a few requirements you need to have:

  1. A CentOS 9 server with root privileges or a user with sudo rights.
  2. A desktop environment (like GNOME) installed on your server.
  3. VNC Viewer installed on your local machine to connect to the CentOS server.

To install a desktop environment, you can run the following command if it’s not already installed:

sudo dnf groupinstall "Server with GUI"

After installation, set the graphical target as default:

sudo systemctl set-default graphical.target

Reboot your server to apply changes:

sudo reboot

Step 1: Install the VNC Server

CentOS 9 uses the TigerVNC server as the default VNC server. To install it, execute the command below:

sudo dnf install tigervnc-server

Step 2: Configure the VNC Server

Next, you’ll need to create a configuration file for the VNC server. For this example, we’ll configure it for a specific user.

  1. Switch to the user account for which you want to set up the VNC. For instance, if the username is user, switch to that user account:
su - user
  1. Create a VNC password for the user:
vncpasswd

Enter the password when prompted. This password will be used to connect to the VNC server from your viewer.

  1. After setting the password, exit out of the user account to return to the root account:
exit
  1. Now, create a VNC configuration file in /etc/systemd/system/. The configuration file will be named vncuser@:1.service, where user is the username and :1 is the display number.
sudo nano /etc/systemd/system/vncuser@:1.service

Insert the following configuration into the file, replacing user with the appropriate username:

[Unit]
Description=Start TigerVNC server at startup
After=display-manager.service

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

[Install]
WantedBy=multi-user.target
  1. Save and exit the editor (in nano, do this by pressing CTRL + X, then Y, and then Enter).

Step 3: Start and Enable the VNC Service

Now, you need to enable and start the VNC service using systemd:

  1. Reload the systemd to recognize the new service:
sudo systemctl daemon-reload
  1. Start the VNC service:
sudo systemctl start vncuser@:1.service
  1. To enable the VNC server to start on boot, use:
sudo systemctl enable vncuser@:1.service

Step 4: Configure Firewall

If your server has a firewall enabled, you need to allow the VNC service through it. You can do this by allowing port 5901 (for display :1):

sudo firewall-cmd --permanent --add-port=5901/tcp
sudo firewall-cmd --reload

Step 5: Connect to the VNC Server

At this point, your VNC server is up and running. Now it’s time to connect:

  1. On your local machine, launch the VNC Viewer (e.g., TightVNC, RealVNC, or TigerVNC).
  2. Enter the server’s IP address and port. For example, if your server’s IP is 123.456.789.0, you would enter:
123.456.789.0:1
  1. Enter the password you created earlier when prompted.

Conclusion

Congratulations! You’ve successfully set up a VNC server on CentOS 9. This enables you to access your server’s graphical desktop environment remotely, making it easier to manage applications and perform various tasks.

Remember that while VNC is convenient, it’s also essential to implement security measures such as using SSH tunneling or VPNs when connecting to your server for enhanced security.