Setting Up a VNC Server on Linux: A Step-by-Step Guide
Updated On: Oct. 31, 2024 Author: Kevin

Setting Up a VNC Server on Linux: A Step-by-Step Guide

In today's remote work environment, having reliable access to your desktop is essential. A Virtual Network Computing (VNC) server allows you to connect to your Linux machine from anywhere, providing a graphical interface that mimics sitting right in front of your computer. This tutorial will walk you through the process of installing and configuring a VNC server on your Linux system, enabling you to access your desktop remotely with ease. Whether you’re managing a home server or working with cloud instances, this guide will equip you with the necessary steps to set up a VNC server effectively.

 

Prerequisites

Ensure you have sudo privileges on your Linux machine. If you’re working with a headless server, make sure to install a lightweight desktop environment (e.g., XFCE or LXDE) first. This step is crucial as it allows you to have a graphical interface when accessing your machine remotely via VNC.

 

Installation Steps

 

1. Update your package list:

sudo apt update

This command updates the local package index to ensure you have the latest information about available packages. It’s an essential first step to avoid installation issues later.

 

2. Install the TigerVNC standalone server:

sudo apt install tigervnc-standalone-server

TigerVNC is a popular implementation of VNC that provides a high-performance server. Installing it will enable you to host a remote desktop session on your Linux machine.

 

3. Install a lightweight desktop environment (if needed; skip if you want to use your existing desktop environment like Plasma or GNOME):

sudo apt install xfce4 xfce4-goodies

This command installs XFCE along with additional plugins and tools that enhance the desktop experience. If you already have a desktop environment installed and prefer to use it, you can skip this step.

 

4. Set a password for your VNC session:

vncpasswd

Running this command prompts you to set a password for accessing your VNC server. This step is vital for securing your remote sessions.

 

5. Start the VNC server without localhost restriction:

vncserver -localhost no

This command starts the VNC server and allows connections from external clients. The `-localhost no` option is important for enabling remote access.

 

6. List your VNC sessions:

vncserver -list

Use this command to view all active VNC sessions on your machine. It helps you keep track of which sessions are running and their associated display numbers.

 

7. Kill the VNC server session (if needed):

vncserver -kill :1

If you need to stop a running VNC session, this command allows you to kill it by specifying the display number. Replace `:1` with the appropriate number for your session.

 

8. Create a systemd service for VNC:


sudo nano /etc/systemd/system/[email protected]
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[Service]
Type=forking
User=YOUR_USERNAME_HERE  
Group=YOUR_USERNAME_HERE  
WorkingDirectory=/home/YOUR_USERNAME_HERE  
PIDFile=/home/YOUR_USERNAME_HERE/.vnc/%H:%i.pid
ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver :%i -localhost no -geometry 1280x800
ExecStop=/usr/bin/vncserver -kill :%i
[Install]
WantedBy=multi-user.target

This configuration creates a systemd service for managing the VNC server. Be sure to replace YOUR_USERNAME_HERE with your actual username to ensure proper permissions and access to your home directory. This step specifies the user, working directory, and other parameters needed to start the server automatically at boot.

This configuration creates a systemd service for managing the VNC server. It specifies the user, working directory, and other parameters needed to start the server automatically at boot.

 

9. Reload systemd to apply changes:

sudo systemctl daemon-reload

After creating or modifying systemd service files, it's necessary to reload the systemd manager configuration. This step ensures that your new VNC service is recognized by the system.

 

10. Enable the VNC service to start at boot:

sudo systemctl enable [email protected]

Enabling the service allows it to start automatically when your machine boots up. This way, you won’t have to manually start the VNC server each time.

 

11. Start the VNC service:

sudo systemctl start [email protected]

This command starts the VNC service immediately, allowing you to connect to your server right away.

 

12. Check the status of the VNC service:

sudo systemctl status [email protected]

Use this command to verify that the VNC server is running properly. It provides details about the service's status and any error messages if it failed to start.

 

Accessing the VNC Server Remotely

To connect to your VNC server remotely, follow these steps:

1. Install a VNC viewer client on your local machine.

Popular options include RealVNC Viewer, TightVNC Viewer, or Remmina. Choose one that fits your operating system and preferences.

2. Open the VNC viewer client and enter the IP address of your Linux machine followed by the display number (e.g., 192.168.1.100:1).

Make sure to replace `192.168.1.100` with the actual IP address of your server. The display number corresponds to the session you want to connect to.

3. Enter the password you set earlier when prompted.

This step secures your connection, ensuring that only authorized users can access the VNC session.

4. You should now be connected to your VNC session and can interact with your Linux desktop remotely.

Enjoy the convenience of accessing your Linux machine as if you were sitting in front of it, enabling you to perform tasks efficiently from anywhere.