To remote connect and access a Raspberry Pi Remote Desktop behind a NAT router and over the internet, we will be installing TightVNC Server on the Pi, as well as install the TightVNC client for Windows.
Setting up IoT or Raspberry Pi for VNC remote desktop access
For this tutorial we will assume that your Raspberry Pi doesn’t have a desktop environment installed. We will install XFCE desktop environment, to have the actual desktop accessible on the Pi.
sudo apt install -y xfce4 xfce4-goodies
Next, we will install tightvncserver to be able to access that desktop.
sudo apt install -y tightvncserver
The next thing we’ll have to do is to set up an access password for VNC clients. This is done on the first run of your VNC server. Simply run the command below:
You will be asked to provide two passwords.
One is an access password and the other is a view-only password.
The access password lets you connect to the desktop and interact with it using keyboard and mouse whereas the view-only password will only let a user observe your desktop.
The view only password is optional so you can skip setting it up when asked by pressing the enter key on your keyboard.
Now that the password is set up we will configure a startup file for VNC.
Firstly, we’ll have to shut down our currently running VNC server.
vncserver -kill :1
Then we’ll create a backup of current startup file, in case we’d like to revert back to it.
mv ~/.vnc/xstartup ~/.vnc/xstartup.bak
Finally, we’ll create a new startup file.
printf '#!/bin/bash\nxrdb $HOME/.Xresources\nstartxfce4 &\n' > ~/.vnc/xstartup sudo chmod +x ~/.vnc/xstartup
This will create the following file:
#!/bin/bash xrdb $HOME/.Xresources startxfce4 &
The first line xrdb $HOME/.Xresources tells the VNC’s GUI framework to read the server user’s Xresource file.
The second line starts the Xfce in background.
Now, re-start the VNC server using the command below.
Now we’re ready to access our Raspberry Pi desktop from our PC.
SocketXP Agent Setup
We need to setup SocketXP Agent to run in two different places:
- Raspberry Pi - in IoT Master Mode (Default Mode)
- Laptop or PC - in IoT Slave Mode
To make SocketXP agent to run in IoT Master Mode (which is the default mode of SocketXP agent) use the below command, as you would normally do.
$ socketxp connect tcp://localhost:5901 Connected to SocketXP Cloud Gateway. Access the TCP service securely using the SocketXP agent in IoT Slave Mode.where localhost port 5901 is the VNC server port, on which tightvncserver is listening for connections from a VNC viewer .
Next, to access the Raspberry Pi device from your Laptop/PC, install SocketXP Agent for Windows and run the below command:
$ ./socketxp.exe connect tcp://localhost:10111 --iot-slave --iot-device-id "2233-abcdefgh-2342abc" --iot-device-port 5901 Listening for TCP connections at: Local URL -> tcp://localhost:10111where 10111 is a local port on your PC at which you want to access the Raspberry Pi. You can choose to use any free local port instead of port 10111. You shall find the device ID of your IoT device from the SocketXP Portal page in the IoT Devices section.
Note: SocketXP IoT Slave Mode feature is available only in premium paid plans.
Connect from Windows:
Install TightVNC Viewer from the TightVNC website.
Launch TightVNC Viewer and it will bring you straight to the login window. Fill it out with the following details:
- Remote Host: localhost:10111.
When done click on “Connect”. This will bring you to the authentication window.
This is where you provide your access password that you’ve set up in the first section of this article. When you click OK, you will see the desktop of your Raspberry Pi.
SocketXP IoT Remote Access Solution is a highly scalable and secure solution to remote connect and access Raspberry Pi VNC Remote Desktop from a Windows Desktop or a Mac Book Laptop.