DEV Community

Okinea Dev
Okinea Dev

Posted on • Originally published at okineadev.Medium on

How to set up VNC on a headless Raspberry Pi with Ubuntu Server 24.04


Photo by Jainath Ponnala on Unsplash

If you have Ubuntu Server installed on your Raspberry Pi and you want to connect to its interface without a monitor or mouse?

Then you have come to the right place!

After all, here are instructions on how to install the Gnome graphical interface and how to connect to it via VNC

First of all. we need to update the packages using below command:



sudo apt update && sudo apt upgrade


Enter fullscreen mode Exit fullscreen mode

After that, we will already install the minimal Gnome shell (you need to download ~350 MB):



sudo apt install gnome-core


Enter fullscreen mode Exit fullscreen mode

Next, install the TigerVNC server and other add-ons for its operation:



sudo apt install tigervnc-standalone-server xserver-xorg-core xserver-xorg-video-fbdev


Enter fullscreen mode Exit fullscreen mode

Now that we have installed the graphical shell and all the other packages, we can now start configuring the VNC server

You need to create a password to connect to the VNC server, to do this, run this command:



vncpasswd


Enter fullscreen mode Exit fullscreen mode

Come up with a strong password, minimum length —  6 characters.

Next, we need to configure the ~/.vnc/xstartup file in order to enter the GUI when connecting via VNC

Run this command to open the ~/.vnc/xstartup file:



nano ~/.vnc/xstartup


Enter fullscreen mode Exit fullscreen mode

Then paste this into it:



#!/bin/bash
# Start Gnome 3 Desktop

unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS

# Set the X session type and GDK backend to X11.
export XDG_SESSION_TYPE=x11
export GDK_BACKEND=x11

[-x /etc/vnc/xstartup] && exec /etc/vnc/xstartup
[-r $HOME/.Xresources] && xrdb $HOME/.Xresources

# Enable access control for X server
xhost +

# Start the Gnome session.
exec gnome-session


Enter fullscreen mode Exit fullscreen mode

After that, be sure to give this file executable rights:



chmod +x ~/.vnc/xstartup


Enter fullscreen mode Exit fullscreen mode

Now we are almost done!

All we have to do is start the VNC server and connect to it.

We will connect via ssh with port-forwarding using this command:



# Replace `okineadev@rpi.local` with the 
# username and address of your Raspberry Pi

ssh -L 5901:localhost:5901 okineadev@rpi.local


Enter fullscreen mode Exit fullscreen mode

This command will connect you to the Raspberry Pi as usual, but with port 5901 forwarded to your computer, this is necessary because TigerVNC itself is not secure and the connection can be intercepted by attackers, so we will connect via port-forwarding because ssh itself provides encryption and connection security

Now we can start the VNC server:



vncserver :1


Enter fullscreen mode Exit fullscreen mode

Now we can connect using TigerVNC:

And voila — now we were able to connect to the Raspberry PI using VNC! 😄

If you notice any mistakes in my instructions — report it in the comments

If you liked this guide, don’t forget to give it a few 👏

👇

Top comments (0)