My Raspberry Pi is located away from my desktop setup. Normally this is OK since I can just SSH into the terminal. I ran across a scenario where I needed to access the GUI. In the past I would physically move it over and connect it to my monitor, keyboard, and mouse. Turns out there’s a much easier way to do this.

VNC to the Rescue#

VNC is built into the Raspberry Pi operating system and allows for easy remote desktop connections. Here’s how:

Enable VNC#

  1. SSH to the Pi.
  2. Run the command:
sudo raspi-config
  1. Use the arrow keys to navigate to Interface Options.
  2. Select VNC and press Enter.
  3. Choose Yes to enable the server.
  4. Select Finish to exit.

Start the VNC Virtual Session#

vncserver-virtual

This will give you the IP address you need to use when remote connecting to the virtual session.

Connect via VNC Viewer#

  1. Download VNC Viewer or other VNC client.
  2. Open the Viewer, type in the IP address from the previous step.
  3. Log in using the username and password you would normally use to log into Raspberry Pi.

Clean Up#

After you’re done, close the VNC client application and go back to terminal window where you are SSH’ed into the Pi. Run this to kill the VNC session.

vncserver-virtual -kill :1

Replace the :1 with the session number (the :# part of the IP address you used) if it is different.

Firewall Issues#

If you have UFW or Firewalld (firewall packages) installed and active you will need to allow traffic into the port that VNC uses. The port number depends on the display number of the IP address. When you ran the command, it should have printed a line like this:

New desktop is raspberrypi:1 (or :2, :3, etc.)

  • If it is :1, use port 5901.
  • If it is :2, use port 5902.

For UFW this would work:

sudo ufw allow 5901/tcp

For Firewalld use this:

sudo firewall-cmd --add-port=5901/tcp

To make it permanent in Firewalld (so it stays open after a reboot):

sudo firewall-cmd --runtime-to-permanent