Raspberry Pi

How to Change VNC Port for Remote Access Raspberry Pi

Raspberry Pi is a portable mini-computer and an ideal desktop replacement since it has a good processing power. Because of its portability, the Raspberry Pi device can be accessed from a remote location either through SSH or VNC Server. SSH is a secure remote access service that provides several configuration options to protect your device from unauthorized access. VNC Server on the other hand allows you to access the Raspberry Pi desktop directly using the default port number 5900. This will put your device at risk since anyone who will hack your IP and crack the password will eventually access your Raspberry Pi device from a remote location using the default port. Thus, it is better to change the default port number for VNC on Raspberry Pi to secure your device from unauthorized remote access.

In this guide, you will find:

How to Change VNC Ports for Remote Access Raspberry Pi

Conclusion

How to Change VNC Ports for Remote Access Raspberry Pi

You can change VNC ports for remote access Raspberry Pi using:

  • Command
  • VNC Server File
  • SSH
  • PuTTY

How to Change VNC Ports for Remote Access Raspberry Pi Using Command

One of the simplest ways to change the VNC ports for remote access Raspberry Pi is adding the port number with the VNC server you want to create. You have to use the keyword rfbport and assign it the port number according to your choice.

The following command will assign port number 5906 to the VNC server created on Raspberry Pi:

vncserver-virtual rfbport=5906

The above command will work on Raspberry Pi Bookworm OS, however, if you are using other Raspberry Pi OSs, you can use the following command to change VNC port:

vncserver rfbport=5906

The vncserver command will create a desktop session with port 5906, which you can confirm from below-mentioned output of the above command:

In the above output, the 6 relates to port 5906, because it uses 5900 as default port for VNC.

How to Change VNC Ports for Remote Access Raspberry Pi Using VNC Server

You can also add the keyword RfbPort with the port number and add this inside the VNC Server file, which you can open from the following command:

sudo nano /etc/vnc/config.d/vncserver-x11

Inside this file, just add the following line:

RfbPort = port_no

Now save the file using CTRL+X, add Y and press Enter to exit back to the terminal. Then reboot the device to apply the changes.

How to Change VNC Ports for Remote Access Raspberry Pi Using SSH Command

Remotely accessing Raspberry Pi through VNC over SSH is a secure way compared to directly accessing it through VNC. The reason is the hacker can easily crack your Raspberry Pi password and login to your Raspberry Pi using the default username and VNC port number. Thus, if you use SSH for VNC access, you can later configure your SSH and make it more secure. This will help prevent unauthorized access to your Raspberry Pi device.

Changing VNC ports for remote access Raspberry Pi is straightforward if you are using a Mac or Linux system. The only thing you have to do is to open the Mac or Linux terminal and execute the following command:

ssh -L new_port_no:localhost:default_port_no -N -f -l username Host_IP_Address

Note: If you are having trouble accessing the device remotely on Mac or Linux, you have to enable screen sharing and remote login option from the System settings.

How to Change VNC Ports for Remote Access Raspberry Pi Using PuTTY

PuTTY is an open-source SSH client for Windows that allows you to change the port number for VNC to remotely access the Raspberry Pi. You can use the following steps to change VNC port on Windows and access the Raspberry Pi device on the same system with the desired port number:

Step 1: Install and Open PuTTY on Windows

Install PuTTY on Windows from here and open it from the Start Menu, then enter the username and host IP address of your Raspberry Pi using the following syntax:

username@Host_IP_Address

Note: The Raspberry Pi’s IP address can be found via terminal through hostname -I command.

Step 2: Save the Current PuTTY Session

Enter the session name in the Saved Sessions box then save the session using the Save button:

Step 3: Forward Port in PuTTY on Windows

Go to the Connection settings, expand it then navigate to the SSH settings and choose the Tunnels option:

Step 4: Enter Source Port and Destination

Enter the new port number in the Source port box, and write the IP address with the default VNC port number in the Destination box. Then click on the Add button:

Step 5: Open the PuTTY Session

Move to Session option again and click on the Open button to open the PuTTY session:

This will change the VNC port number and you can use this port number to access the Raspberry Pi device.

How to Access Raspberry Pi Through VNC with Port Number

You must have to download and install VNC Viewer on your other computer to access the Raspberry Pi device. Then open VNC Viewer and use the Raspberry Pi IP address with the desired port number to access it by entering username and password.

Note: If you have used the SSH command to change VNC port for Raspberry Pi, you can use localhost with the desired port number instead of providing the Raspberry Pi IP address to access the device.

How to Kill Current VNC Session on Raspberry Pi

If you have created multiple VNC servers with different port numbers and want to kill those servers to reduced load on Raspberry Pi, you can use the following command:

vncserver-virtual -kill :port_no

Note: Use vncserver only instead of vncserver-virtual if you are using Raspberry Pi OS other than Raspberry Pi Bookworm.

How to Fix “You’ll have to kill the Xvnc process manually” Error on Raspberry Pi

While executing the kill command to kill the VNC Server with a desired port number, you may encounter error “You’ll have to kill the Xvnc process manually”, as shown below:

This error occurs because you have set the port number in the VNC Server file, and you have to remove the line “RfbPort = port_no” from the file and then reboot the device. This will not only fix the above issue but it will fix the connection refused issue that will be encountered while accessing the Raspberry Pi from the VNC Viewer.

Conclusion

Changing the default VNC port for Raspberry Pi is crucial for securing your Raspberry Pi device from unauthorized access. You can change VNC ports for remote access Raspberry Pi by using command or inside the VNC Server file. You can also use the SSH command to change VNC ports for Raspberry Pi, however, it will work only for Mac or Linux systems. For Windows, you have to use PuTTY to change the default VNC ports for Raspberry Pi using the step-by-step guide presented in the above section of this guide.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.