How to Install and Configure VNC Server on CentOS

Published December 7, 2024
How to Install and Configure VNC Server on CentOS
Cheap Dedicated Server

How to Install and Configure VNC Server on CentOS


VNC (Virtual Network Computing) is a powerful tool that allows users to access and control their desktop environment remotely. This guide walks you through the installation and configuration of the VNC server on CentOS, ensuring you can manage your system from anywhere.


Prerequisites

Before proceeding, ensure you have the following:

  • A CentOS server with a non-root user and sudo privileges.
  • Basic familiarity with terminal commands.
  • An active internet connection.

Step 1: Update Your System

Start by updating your CentOS system to ensure all packages are up-to-date. Run the following command:
      Update Your System

 
sudo yum update -y


Step 2: Install the Desktop Environment

If your CentOS server does not have a desktop environment, you need to install one. For a lightweight GNOME desktop, run:

 
sudo yum groupinstall "GNOME Desktop" -y

Enable graphical mode as the default target:

 
sudo systemctl set-default graphical.target

Reboot your system to apply the changes:

 
sudo reboot


Step 3: Install the VNC Server

Install the tigervnc-server package, a popular VNC server for CentOS:
Install the VNC Server

 
sudo yum install tigervnc-server -y


Step 4: Configure VNC Server

  1. Create a VNC User: Create a user that will connect via VNC. If you already have a user, skip this step.
    Create a VNC User
     
    sudo adduser vncuser

    sudo passwd vncuser

  2. Set the VNC Password: Switch to the newly created user and set a VNC password:
    Set the VNC Password
     
    su - vncuser

    vncpasswd

    Exit the user session by typing:

     
    exit

  3. Create the VNC Configuration File: Copy the default configuration file and customize it for the user:
     
    sudo cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service

    Edit the file:

     
    sudo nano /etc/systemd/system/vncserver@:1.service

    Replace <USER> with the VNC user created earlier (e.g., vncuser):

     
    [Service]

    User=vncuser

    PIDFile=/home/vncuser/.vnc/%H:%i.pid

    ExecStart=/usr/bin/vncserver -geometry 1280x800 -depth 24 :1

    ExecStop=/usr/bin/vncserver -kill :1

  4. Reload the Systemd Daemon: After editing, reload the systemd daemon:
     
    sudo systemctl daemon-reload

  5. Enable and Start the VNC Service: Enable and start the VNC service for the user:
    Enable and Start the VNC Service
     
    sudo systemctl enable vncserver@:1.service

    sudo systemctl start vncserver@:1.service

    Check the status to ensure it’s running:

     
    sudo systemctl status vncserver@:1.service


Step 5: Configure Firewall

Allow VNC traffic through the firewall by running:
Configure Firewall

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

sudo firewall-cmd --reload


Step 6: Connect to the VNC Server

  1. Use a VNC client like TigerVNC Viewer or RealVNC Viewer on your local machine.
  2. Enter the server’s IP address and port (e.g., 192.168.1.100:1).
  3. Authenticate using the VNC password you set earlier.

Step 7: Secure the VNC Connection

For enhanced security, tunnel the VNC connection through SSH. Use the following command on your local machine:
Secure the VNC Connection

 
ssh -L 5901:127.0.0.1:5901 vncuser@<server-ip>

Connect your VNC client to 127.0.0.1:5901.


Troubleshooting

  • If you encounter issues, check the VNC log files:
     
    cat /home/vncuser/.vnc/*.log

  • Verify the firewall settings and confirm that the VNC port is open.

Conclusion

Congratulations! You have successfully installed and configured a VNC server on CentOS. With this setup, you can remotely access your CentOS desktop environment from anywhere. Always ensure your VNC server is secured with a strong password and consider using SSH tunneling for an added layer of security.

 


 

How to Install and Configure VNC Server on CentOS (F.A.Q)

How do I reset the VNC password for a user?

To reset the VNC password, switch to the user account and run the vncpasswd command:

 
su - vncuser
vncpasswd

How can I change the screen resolution for the VNC session?

Edit the ExecStart line in the VNC service file (e.g., /etc/systemd/system/vncserver@:1.service) to set the desired resolution:

 
ExecStart=/usr/bin/vncserver -geometry 1920x1080 -depth 24 :1

Then reload the systemd daemon and restart the VNC service:

 
sudo systemctl daemon-reload
sudo systemctl restart vncserver@:1.service

Why can't I connect to the VNC server?

Common causes include:

  • Firewall blocking the port: Ensure port 5901 is open.
     
    sudo firewall-cmd --permanent --add-port=5901/tcp
    sudo firewall-cmd --reload
  • Service not running: Check the status of the VNC service:
     
    sudo systemctl status vncserver@:1.service
 

How do I secure the VNC connection?

VNC is not encrypted by default. To secure it:

  • Use SSH tunneling:
     
    ssh -L 5901:127.0.0.1:5901 vncuser@<server-ip>
  • Connect your VNC client to 127.0.0.1:5901.