How to Install and Configure VNC Server on VPS for Remote Desktop Access

A Virtual Network Computing (VNC) server enables remote desktop access to your VPS, allowing you to control your server as if you were sitting in front of it. This is particularly useful if you need a graphical user interface (GUI) for managing your VPS or running applications that require a desktop environment. In this guide, we will walk you through the process of installing and configuring a VNC server on a VPS for remote desktop access.


Prerequisites

Before proceeding with the installation, ensure you have the following:

  • A VPS running a supported operating system (Ubuntu 20.04 or later, CentOS, etc.).
  • Root or sudo privileges to install software and configure system settings.
  • A secure connection to your VPS via SSH (use an SSH client like PuTTY or terminal).
  • A domain name or public IP address to access the VNC server remotely.

Step 1: Update Your VPS

First, update your VPS to make sure that all system packages are up to date:

 
sudo apt update && sudo apt upgrade -y

This ensures that your VPS has the latest security patches and software updates.


Step 2: Install a Desktop Environment

VNC works by providing a graphical interface to your VPS, so you need to install a desktop environment. For this guide, we'll install Xfce, a lightweight and fast desktop environment that works well on a VPS.

Install Xfce Desktop Environment:

 
sudo apt install xfce4 xfce4-goodies -y

This command installs Xfce and some additional useful tools and features.


Step 3: Install VNC Server

Next, install a VNC server on your VPS. We'll use TigerVNC, one of the most popular and reliable VNC servers.

Install TigerVNC Server:

 
sudo apt install tigervnc-standalone-server tigervnc-common -y

This installs the TigerVNC server and the required dependencies.


Step 4: Configure VNC Server

Once the VNC server is installed, you need to configure it. The VNC server will allow you to connect to the desktop environment you installed earlier.

Start the VNC server to set up a password:

The first time you run TigerVNC, you need to set a password for your VNC sessions.

 
vncserver

You will be prompted to set a password for your VNC session. Enter a secure password (it should be between 6-8 characters). You will also be asked if you want to create a view-only password (you can type 'n' for no).

Stop the VNC server:

After setting the password, stop the VNC server to configure the session properly.

 
vncserver -kill :1

Here, :1 refers to the VNC session number. If you are using a different session, replace 1 with the correct number.


Step 5: Configure VNC Server to Use Xfce

Now, you need to configure the VNC server to use the Xfce desktop environment.

  1. Edit the VNC configuration file:

    Create or edit the xstartup file in the ~/.vnc directory:

     
    nano ~/.vnc/xstartup
  2. Modify the xstartup file to start Xfce:

    Replace the contents of the file with the following:

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

    This tells the VNC server to start the Xfce desktop environment when you connect.

  3. Make the xstartup file executable:

     
    chmod +x ~/.vnc/xstartup

Step 6: Start the VNC Server

Now that the VNC server is configured to use Xfce, you can start it again.

 
vncserver :1

Here, :1 indicates the VNC session number. You can create multiple sessions (e.g., :2, :3, etc.) if needed.

You should see output indicating that the VNC server has started successfully, and it will display the address you need to connect to, typically IP_Address:1.


Step 7: Configure Firewall for VNC Access

If you have a firewall enabled on your VPS, you need to allow traffic on the VNC port (default is 5901 for :1, 5902 for :2, etc.).

Allow VNC traffic in UFW (Uncomplicated Firewall):

 
sudo ufw allow 5901/tcp

For other sessions, replace 5901 with the appropriate port (e.g., 5902, 5903, etc.).

Allow VNC traffic in other firewalls:

If you're using iptables or another firewall, make sure to open the appropriate port for VNC.


Step 8: Connect to VNC Server from Your Local Machine

To connect to your VNC server from your local machine, you need a VNC client. Some popular VNC clients include:

  • TightVNC (for Windows)
  • RealVNC (for Windows, macOS, and Linux)
  • Remmina (for Linux)
  • VNC Viewer (for macOS and Windows)

Steps to connect:

  1. Open your VNC client on your local machine.
  2. Enter the IP address of your VPS and the VNC session number (e.g., your_vps_ip:1).
  3. When prompted, enter the password you set up earlier.

Once you successfully connect, you should be able to see the Xfce desktop environment running on your VPS.


Step 9: Set VNC Server to Start Automatically (Optional)

To ensure the VNC server starts automatically on boot, you can create a systemd service.

  1. Create a systemd service file:

     
    sudo nano /etc/systemd/system/[email protected]
  2. Add the following content:

    ini
     
    [Unit] Description=Start TigerVNC server at startup After=multi-user.target [Service] Type=forking User=<your_username> PAMName=login PIDFile=/home/<your_username>/.vnc/%H%i.pid ExecStart=/usr/bin/vncserver %i ExecStop=/usr/bin/vncserver -kill %i [Install] WantedBy=multi-user.target

    Replace <your_username> with your actual username.

  3. Enable the service to start at boot:

     
    sudo systemctl daemon-reload sudo systemctl enable [email protected] sudo systemctl start [email protected]

Step 10: Secure Your VNC Connection (Optional but Recommended)

VNC does not use encryption by default, so it’s highly recommended to tunnel your VNC connection over SSH to ensure that the connection is secure.

Set up SSH tunneling:

  1. On your local machine, create an SSH tunnel by running the following command:

     
    ssh -L 5901:localhost:5901 your_username@your_vps_ip

    This command creates a secure SSH tunnel from your local machine's port 5901 to the VPS's port 5901.

  2. Connect to VNC locally:

    In your VNC client, connect to localhost:5901 instead of using the remote VPS address. The SSH tunnel will securely route the traffic to your VPS.


Step 11: Stop the VNC Server

When you’re done with your VNC session, you can stop the server by running:

 
vncserver -kill :1

This will stop the VNC session, and the desktop environment will be closed.


Conclusion

You have successfully installed and configured a VNC server on your VPS for remote desktop access. This setup allows you to control your VPS as if you were using a physical computer, providing a GUI for managing applications and performing tasks.

Remember to secure your VNC connection by using SSH tunneling, especially if you're connecting over a public network.

Was this answer helpful? 0 Users Found This Useful (0 Votes)

Powered by WHMCompleteSolution