BLOG POSTS
How to Install and Configure VNC on Ubuntu 24

How to Install and Configure VNC on Ubuntu 24

Setting up VNC (Virtual Network Computing) on Ubuntu 24 is one of those game-changing skills that’ll make your remote server management life infinitely easier. Whether you’re managing a headless server that suddenly needs a GUI for some task, setting up a development environment you can access from anywhere, or just want to give someone remote access to your Ubuntu desktop without the usual SSH command-line limitations, VNC is your friend. This guide will walk you through the entire process—from understanding what’s happening under the hood to getting a rock-solid VNC setup that actually works in production environments.

How VNC Actually Works (The Geeky Bits)

VNC operates on a surprisingly elegant client-server model that’s been around since the late ’90s. The VNC server captures your desktop framebuffer (basically screenshots at high frequency), compresses the image data, and transmits it over TCP/IP to VNC clients. Input events like mouse clicks and keyboard strokes travel back from client to server.

Here’s what happens when you connect:

  • Authentication: Client connects to server (usually port 5900+N where N is the display number)
  • Handshake: Both negotiate protocol version and security type
  • Screen sharing: Server sends framebuffer data, client sends input events
  • Compression: Various algorithms (RRE, CoRRE, Hextile, ZRLE) reduce bandwidth usage

The beauty of VNC is its platform independence—you can control a Linux server from Windows, macOS, or even your phone. Popular VNC implementations include TightVNC, TigerVNC, RealVNC, and x11vnc, each with different performance characteristics and feature sets.

Step-by-Step VNC Installation and Configuration

Let’s get our hands dirty. I’ll show you two approaches: the lightweight desktop route (perfect for servers) and the full desktop experience.

Method 1: Lightweight Setup with XFCE

First, update your system and install the essentials:

sudo apt update && sudo apt upgrade -y
sudo apt install xfce4 xfce4-goodies tightvncserver -y

Now set up your VNC user (don’t run VNC as root—seriously, just don’t):

# Start VNC server to create initial config
vncserver

# You'll be prompted to set a password (8 characters max)
# Choose 'n' for view-only password unless you need it

# Kill the server to modify config
vncserver -kill :1

Edit the VNC startup script to use XFCE instead of the default window manager:

nano ~/.vnc/xstartup

Replace the contents with this configuration:

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

Make it executable and restart VNC:

chmod +x ~/.vnc/xstartup
vncserver -geometry 1920x1080 -depth 24

Method 2: Full GNOME Desktop Experience

If you need the complete Ubuntu desktop experience:

sudo apt install ubuntu-desktop-minimal tigervnc-standalone-server -y

Configure VNC to use GNOME:

mkdir -p ~/.vnc
echo 'gnome-session' > ~/.vnc/xstartup
chmod +x ~/.vnc/xstartup
vncserver -localhost no -geometry 1920x1080 -depth 24

Setting Up VNC as a System Service

Running VNC manually is fine for testing, but you want it to start automatically. Create a systemd service:

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

Add this service configuration:

[Unit]
Description=Start TightVNC server at startup
After=syslog.target network.target

[Service]
Type=forking
User=yourusername
Group=yourusername
WorkingDirectory=/home/yourusername

PIDFile=/home/yourusername/.vnc/%H:%i.pid
ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1920x1080 :%i
ExecStop=/usr/bin/vncserver -kill :%i

[Install]
WantedBy=multi-user.target

Enable and start the service:

sudo systemctl daemon-reload
sudo systemctl enable vncserver@1.service
sudo systemctl start vncserver@1.service
sudo systemctl status vncserver@1.service

Security Hardening and Best Practices

Default VNC installations are about as secure as leaving your house key under the doormat. Let’s fix that.

SSH Tunneling (Highly Recommended)

Instead of exposing VNC directly, tunnel it through SSH:

# On your local machine
ssh -L 5901:localhost:5901 -N -f username@your-server-ip

# Now connect your VNC client to localhost:5901

Configure VNC to only listen on localhost:

vncserver -localhost yes -geometry 1920x1080 -depth 24

Firewall Configuration

If you must expose VNC directly (not recommended), at least restrict access:

# Allow VNC only from specific IPs
sudo ufw allow from YOUR_TRUSTED_IP to any port 5901

# Or for SSH tunneling (recommended)
sudo ufw deny 5901
sudo ufw allow ssh

Strong Authentication Setup

Create a more secure VNC password configuration:

# Generate a stronger password file
vncpasswd ~/.vnc/passwd

# Set proper permissions
chmod 600 ~/.vnc/passwd

Performance Optimization and Troubleshooting

VNC performance can range from “smooth as butter” to “watching paint dry.” Here’s how to optimize:

Performance Comparison Table

VNC Server CPU Usage Bandwidth Features Best For
TightVNC Medium Low Good compression Slow connections
TigerVNC Low Medium Modern protocols LAN environments
x11vnc High High Real desktop sharing Existing sessions
RealVNC Low Low Commercial features Enterprise use

Optimization Commands

For better performance on slower connections:

# Start with aggressive compression
vncserver -geometry 1366x768 -depth 16 -dpi 96

# Or for high-speed local networks
vncserver -geometry 1920x1080 -depth 24 -dpi 100

Common Issues and Solutions

Gray screen or no desktop:

# Check if desktop environment is installed
dpkg -l | grep xfce

# Verify xstartup file
cat ~/.vnc/xstartup

# Kill and restart VNC
vncserver -kill :1
vncserver -geometry 1920x1080 -depth 24

Can’t connect from remote machines:

# Check if VNC is listening on all interfaces
netstat -tlnp | grep :590

# Start VNC without localhost restriction
vncserver -localhost no -geometry 1920x1080 -depth 24

Font rendering issues:

# Install better fonts
sudo apt install fonts-liberation fonts-dejavu

# Add to ~/.vnc/xstartup
export XKL_XMODMAP_DISABLE=1

Advanced Use Cases and Integrations

VNC isn’t just for basic remote desktop—here are some creative applications:

Automated Testing Environments

Set up headless browsers for Selenium testing:

# Install Chrome and testing tools
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
sudo apt update
sudo apt install google-chrome-stable -y

# Start VNC for automated testing
vncserver -geometry 1920x1080 -depth 24 :99
export DISPLAY=:99

Multi-User VNC Setup

Configure VNC for multiple users with different display numbers:

# User 1 on display :1
sudo systemctl enable vncserver@1.service

# User 2 on display :2  
sudo systemctl enable vncserver@2.service

# Each user gets their own port (5901, 5902, etc.)

VNC with Docker Integration

Create containerized VNC environments:

# Dockerfile example
FROM ubuntu:24.04
RUN apt update && apt install -y xfce4 tightvncserver
RUN mkdir ~/.vnc && echo 'startxfce4' > ~/.vnc/xstartup
EXPOSE 5901
CMD ["vncserver", "-fg", "-geometry", "1920x1080", "-depth", "24"]

Monitoring and Maintenance

Keep your VNC setup healthy with monitoring scripts:

# VNC health check script
#!/bin/bash
VNC_DISPLAY=":1"
VNC_PORT="5901"

if ! pgrep -f "Xvnc $VNC_DISPLAY" > /dev/null; then
    echo "VNC server down, restarting..."
    vncserver $VNC_DISPLAY -geometry 1920x1080 -depth 24
    systemctl restart vncserver@1.service
fi

# Check connection
if ! netstat -tlnp | grep :$VNC_PORT > /dev/null; then
    echo "VNC not listening on port $VNC_PORT"
    exit 1
fi

echo "VNC server healthy"

Add this to crontab for automated monitoring:

*/5 * * * * /home/username/scripts/vnc-health-check.sh

Alternatives and When to Use Them

VNC isn’t always the right tool. Here’s when to consider alternatives:

  • XRDP: Better for Windows RDP clients, supports audio redirection
  • X2Go: Superior performance over WAN, session resumption
  • NoMachine: Commercial solution with excellent performance
  • Apache Guacamole: Clientless remote desktop via web browser

Performance comparison over 1Mbps connection:

  • VNC: 15-20 seconds for full screen refresh
  • X2Go: 3-5 seconds for full screen refresh
  • XRDP: 8-12 seconds for full screen refresh

Conclusion and Recommendations

VNC on Ubuntu 24 is incredibly versatile once you get past the initial setup hurdles. For production environments, always use SSH tunneling—I can’t stress this enough. The security implications of exposing VNC directly to the internet are just too risky.

Use VNC when you need:

  • Cross-platform remote desktop access
  • Automated GUI testing environments
  • Simple remote administration of graphical applications
  • Multiple concurrent user sessions

Skip VNC if you need:

  • High-performance gaming or video editing
  • Audio streaming (use X2Go or XRDP instead)
  • File transfer capabilities (use SSH/SFTP)
  • Session persistence across network failures

For hosting your VNC setup, consider a reliable VPS from MangoHost VPS for small to medium workloads, or step up to a dedicated server if you’re running multiple concurrent VNC sessions or resource-intensive applications.

The key to VNC success is proper security configuration, performance tuning for your specific use case, and having solid monitoring in place. Get these fundamentals right, and you’ll have a remote desktop solution that just works—which is exactly what you want when you’re managing servers at 2 AM.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked