Tuesday, March 19, 2024

Accessing Graphical Desktop Of Raspberry Pi Using SSH And VNC

By Kalpesh Bhosle

Raspberry Pi is a low-cost, high-performance, credit-card-sized computer for learning, solving problems and having fun. Easy to get started with, this tiny yet powerful computer comes with additional resources including monitor screen, keyboard and mouse. Imagine the inconvenience of moving around the accessories between workplaces for accessing this tiny computer. Does it still feel tiny?

The configuration given in this article lets you set up a local network between Raspberry Pi board and a laptop using an Ethernet cable. It is a simple and convenient configuration to access Raspberry Pi remotely.

Things you need to get started

There are three things needed to configure the setup: Raspberry Pi with an SD card running Raspbian operating system (OS), laptop running Linux OS—I prefer Ubuntu 16.04—and Ethernet cable to set up a connection between Raspberry Pi and laptop (Fig. 1).

Raspberry Pi setup on laptop using Ethernet cable
Fig. 1: Raspberry Pi setup on laptop using Ethernet cable

The laptop must have an Internet connection to install a few software packages. The same connection can be shared with Raspberry Pi using an Ethernet cable to install required packages.

Installing required packages on Ubuntu

Configure Ethernet connection on Ubuntu to share its network with hosts connected to it—in this case, Raspberry Pi. Then, open connection settings for Ethernet connection from desktop.

- Advertisement -

Change IPV4 settings to Share to Other Computers and click on Save. (Fig. 2). An IP address is required to get access to Raspberry Pi over Secure Socket Shell (SSH). (SSH is a network protocol that provides the administrator with a secure way to access a remote computer. SSH service is not enabled by default in Raspberry Pi. To enable SSH, access command line using sudo raspi-config command). Since there is no monitor or keyboard connected to Raspberry Pi, it is not possible to use $ ifconfig command.

Ethernet settings in Ubuntu
Fig. 2: Ethernet settings in Ubuntu

To find out the IP address assigned to Raspberry Pi using a laptop, use nmap tool. Ubuntu needs Internet connectivity over Wi-Fi. To do that, connect it to a known Wi-Fi connection.
Open the terminal and install nmap using the following command:

$ sudo apt-get install nmap

- Advertisement -

Once the installation is complete, reboot the system. Make sure it is connected to the Internet via Wi-Fi.

Accessing Raspberry Pi using SSH

Connect Raspberry Pi to the computer with Ubuntu using an Ethernet cable. Then, start Raspberry Pi. By default, the network configuration saved in Raspberry Pi over Ethernet connection is dhcp. This allows Raspberry Pi to get the IP address from Ubuntu system.

It takes some time for Raspberry Pi to boot up and get an IP address. Once it gets an IP address, a pop up on Ubuntu system will display Connection Established.

Note down the broadcast address of the Ethernet connection on Ubuntu. Open the terminal and run $ ifconfig command as shown in Fig. 3. Make a note of the IP address of Ubuntu system (10.42.0.1 is the IP address in this example) and look for the term Bcast. This should be in the second line when ifconfig command is given.

ifconfig command
Fig. 3: ifconfig command

Search for the IP address assigned to Raspberry Pi using $ nmap -n -sP 10.42.0.255/24 command. Replace 10.42.0.255 with Bcast address noted in the previous step. nmap tool shows the details of all hosts that are up and running, along with their IP addresses. Make a note of the IP address (here, it is 10.42.0.193) assigned to Raspberry Pi.

Searching for IP address of Raspberry Pi
Fig. 4: Searching for IP address of Raspberry Pi

Login to Raspberry Pi using $ ssh -Y [email protected] command. Replace 10.42.0.193 with the IP address noted using nmap tool. Enter default password as raspberry.

This setup allows you to access Raspberry Pi anytime over SSH using the IP address assigned to it, in non-graphical command-line mode. Note that, accessing the graphical desktop requires installing additional packages such as VNC server. This is explained next.

Installing required packages on Raspberry Pi

Internet access is available on Raspberry Pi since Ubuntu connected to a Wi-Fi network has shared its connection over Ethernet. Run the following command to update the packages list from the repositories:

$ sudo apt-get update

Graphical desktop of Raspberry Pi can be shared using tightvncserver package. For that, run the following command:

$ sudo apt-get install tightvncserver

Run the following command to perform the initial set up of tightvncserver:

$ tightvncserver

Enter a suitable password for future connections. Enter n for No to view only password.

Run the following command to copy and paste from VNC server:

$ sudo apt-get install autocutsel

Add autocutsel -fork in /home/pi/.vnc/xstartup using the following command:

$ sudo nano /home/pi/.vnc/xstartup

Save it using Ctrl + x.

Contents of the modified xstartup file can be viewed using the following command:

$ cat .vnc/xstartup

Restart VNC server for autocutsel to take effect, using the following command:

$ vncserver -kill :1 then command $
vncserver :1

Viewing contents of modified xstartup file
Fig. 5: Viewing contents of modified xstartup file

SHARE YOUR THOUGHTS & COMMENTS

Electronics News

Truly Innovative Tech

MOst Popular Videos

Electronics Components

Calculators