How to Install and Connect to a VNC Server on Ubuntu and CentOS VPS: A Simplified Guide

How to Install and Connect to a VNC Server on Ubuntu and CentOS VPS: A Simplified Guide

If you're looking to manage your VPS remotely with a graphical user interface (GUI), installing a VNC (Virtual Network Computing) server is a great option. With VNC, you can access your VPS just like you're sitting in front of it, using your mouse and keyboard to interact with the desktop environment.

In this article, we’ll show you how to install a VNC server on both Ubuntu and CentOS, and how to connect to it remotely.

What Is a VNC Server?

VNC is a protocol that allows you to control a computer remotely by transmitting the graphical desktop to another device. It’s ideal for those who want to manage their server visually (as opposed to using a command-line interface) or need access to desktop-based applications on their VPS.

Why Use VNC?

  • Graphical Interface: Makes it easier to interact with your VPS.
  • Remote Access: Connect from anywhere to control your VPS.
  • Convenience: Perfect for GUI-based tasks or configurations.

Prerequisites

  • A VPS running Ubuntu or CentOS.
  • Root or sudo access to your VPS.
  • Basic familiarity with the command line.

Step 1: Install the Required Packages

For Ubuntu:

  1. Update your system:
bash
sudo apt update && sudo apt upgrade -y
  1. Install the VNC server (we'll use TightVNC as an example):
bash
sudo apt install tightvncserver -y
  1. Install a desktop environment (e.g., XFCE, a lightweight option):
bash
sudo apt install xfce4 xfce4-goodies -y

For CentOS:

  1. Update your system:
bash
sudo yum update -y
  1. Install the VNC server:
bash
sudo yum install tigervnc-server -y
  1. Install a desktop environment (e.g., GNOME or XFCE):
bash
sudo yum groupinstall "GNOME Desktop" -y

Step 2: Configure the VNC Server

For Ubuntu and CentOS:

  1. Set a password for your VNC session:
bash
vncserver

You’ll be prompted to set a password (this will be required when you connect remotely).

  1. Stop the VNC server to configure it:
bash
vncserver -kill :1
  1. Configure the VNC to use the correct desktop environment.
  • For Ubuntu: Edit the xstartup file to start XFCE:
bash
nano ~/.vnc/xstartup

Replace the file’s content with:

bash
#!/bin/sh xrdb $HOME/.Xresources startxfce4 &
  • For CentOS: You might need to use a different startup file or make adjustments based on the desktop environment you installed. For GNOME, you can leave it as is for now.
  1. Make the xstartup file executable:
bash
chmod +x ~/.vnc/xstartup

Step 3: Start the VNC Server

Start the VNC server again:

bash
vncserver :1

The :1 specifies the display number. If you want multiple sessions, you can use :2, :3, etc.

Step 4: Open the VNC Port in the Firewall

You’ll need to open the VNC port in your firewall (default is 5901 for display :1).

For Ubuntu (using UFW):

bash
sudo ufw allow 5901/tcp

For CentOS:

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

Step 5: Connect to Your VPS Using a VNC Client

  1. Install a VNC client on your local machine (e.g., TightVNC Viewer, RealVNC, or TigerVNC Viewer).

  2. Connect to your VPS by entering the VPS IP address and the VNC port. For example:

php
<Your VPS IP>:5901
  1. Enter the password you set earlier to log in.

Step 6: (Optional) Secure the Connection with SSH

By default, VNC is not secure. To protect your data, it’s a good idea to tunnel the VNC connection over SSH:

  1. On your local machine, open a terminal and run:
bash
ssh -L 5901:localhost:5901 your_username@your_vps_ip
  1. Now, connect to localhost:5901 in your VNC client instead of the direct IP.

Conclusion

You’ve successfully installed and configured a VNC server on your VPS running Ubuntu or CentOS, and learned how to connect to it remotely. This gives you a convenient graphical interface to manage your server, especially when handling tasks that are easier through a GUI.

For added security, always use SSH tunneling when connecting via VNC. If you need any help or have questions about setting up VNC on your VPS, feel free to reach out!

Related Articles

STAY TUNED
Image