Run Raspberry pi in a truely headless mode

Squirrel
Coinmonks
Published in
7 min readJul 3, 2018

--

Say, I just bought a raspberry pi and wish to checkout how it works. But all I have is my Laptop, the Pi, a micro SD card and my wifi network. How do I connect and control the Pi?

Here is the story of how Nick became the headless nick

Update: Now you can remote control Pi graphically through the internet easily, added steps to do that.

1 — Download Raspbian

Your Pi needs an OS. Download Raspbian from Raspberrypi.org ‘s download section: https://www.raspberrypi.org/downloads/raspbian/

2 — Flash it onto an SD card

You need to flash this downloaded image to the micro SD card. Assuming your laptop has a SD card slot, you need a flashing software like etcher. Go ahead and download from: https://etcher.io/

3—Configure WiFi

Its easier to make two devices talk to each other if they are in the same network. An ethernet cable can easily make your laptop’s network available to the Pi. But we don’t have one. So we are going to add a file to the SD card so that the Pi boots with a wifi pre-configured.

The SD card mounts as two volumes boot and rootfs . Open the boot volume and create a file named wpa_supplicant.conf On booting the RPi, this file will be copied to /etc/wpa_supplicant directory in /rootfs partition. The copied file tells the Pi the WiFi setup information. This would overwrite any existing wifi configuration, so if you had already configured wifi on the pi, then that will be overwritten.

A typical wpa_supplicant.conf file is as follows

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=«your_ISO-3166-1_two-letter_country_code»

network={
ssid="«your_SSID»"
psk="«your_PSK»"
key_mgmt=WPA-PSK
}

Your SSID is your wifi’s name. To find out SSID on ubuntu, you can use theiwgetid command. The psk is the wifi password and your country code can be found here: https://www.wikiwand.com/en/ISO_3166-1_alpha-2#/Officially_assigned_code_elements

so replace all the «item» fields in the above text

4 — Enable SSH

We will later access the Pi using a secured shell (SSH), SSH is disabled by default in raspbian. To enable SSH, create a file named ssh in the boot partition. If you are on linux, use the touch command to do that.

5 — Find Pi’s ipaddress

Before switching on your raspberry pi, we need to findout the existing devices connected to the network. Make sure your laptop is connected to the same wifi network as the one you configured on pi above.

Run the command hostname -I to find out your laptops’ ipaddress. Say it is 192.168.1.8 then when connected your pi’s ip would be 192.168.1.x

Run the command nmap -sn 192.168.1.0/24 to findout existing devices in the network in range 0 to 24 for the last portion of ipaddress. I get

Starting Nmap 7.01 ( https://nmap.org ) at 2018-07-03 18:39 IST
Nmap scan report for 192.168.1.1
Host is up (0.0020s latency).
Nmap scan report for 192.168.1.8
Host is up (0.000097s latency).
Nmap done: 256 IP addresses (5 hosts up) scanned in 2.58 seconds

Remove the micro SD card from your laptop and insert it into Pi. Power it up using a power source (5v regular android charger) and try nmap -sn 192.168.1.0/24 again, to see which ipaddress newly appears

Starting Nmap 7.01 ( https://nmap.org ) at 2018-07-03 18:39 IST
Nmap scan report for 192.168.1.1
Host is up (0.0020s latency).
Nmap scan report for 192.168.1.2
Host is up (0.040s latency).
Nmap scan report for 192.168.1.8
Host is up (0.000097s latency).
Nmap done: 256 IP addresses (5 hosts up) scanned in 2.58 seconds

so 192.168.1.2 should be the ip address of the pi.

6— SSH into the Pi

To create a secured shell connection, in linux we can use the ssh command. If you are on windows, try downloading PuttY from https://www.putty.org/

To connect to Pi you need the default username and password of the device. On first boot, the username and password would be as follows.

username : pi
password: raspberry

now you can do

ssh pi@192.168.1.2

Type in y when asked if you sure to continue connecting with the device. Then when asked for password, type in the pass.

You should now be inside Pi’s SSH. Smile :) we should savour such small victories

7— Change default password

Its a good practice to change the password to something else. You can use the passwd command to do that.

pi@raspberrypi:~ $ passwd
Changing password for pi.
(current) UNIX password: raspberry
Enter new UNIX password: iwonttellyou
Retype new UNIX password: iwonttellyou
passwd: password updated successfully

8 — See the screen

Sometimes it doesn’t feel right if we can’t use the mouse. For that we need to look into the Raspbian desktop.

We need to setup VNC (Virtual Network Connection) to see and control Pi graphically. Let’s do that.

sudo apt-get update
sudo apt-get install -y realvnc-vnc-server realvnc-vnc-viewer

These commands would update Pi’s softwares and install realvnc which will be used to setup remote sessions.

9— Access Pi remotely

On the raspberry pi’s ssh prompt, type in vncserver to start the service. This will print an ipaddress where you can access the desktop remotely, note that down. Mine says

New desktop is raspberrypi:1 (192.168.1.2:1)

To access the remote desktop, you need a vncviewer (client) for your laptop. Fortunately RealVNC is available for a lot of OSes, pick one for your OS from https://www.realvnc.com/en/connect/download/viewer/

If you are on debian/ubuntu, you might have to do some additional step after downloading the executable.

cd ~/Downloads
chmod +x VNC-Viewer-6.17.1113-Linux-x64
mv VNC-Viewer-6.17.1113-Linux-x64 ~
cd ~
./VNC-Viewer-6.17.1113-Linux-x64
RealVNC viewer

Here we are changing the file mode of the downloaded executable to make it installable. We are also moving the executable from the downloads folder to the home folder.

Once RealVNC viewer is installed, add the raspberry pi device to the connections (File > New connection). Enter the pi’s desktop identifier (192.168.1.2:1 ), give it a friendly name, click Ok. Enter pi’s username and password when prompted. That’s it, you should now see the desktop. Give me a five!

Raspberry Pi’s Remote desktop

10 — Remote control from the internet

No more port forwarding and fiddling with router settings, Team Viewer has released a version for Arm based devices and we are gonna use that. Yaay!

Start by SSHing into your headless device and from the terminal do

wget https://download.teamviewer.com/download/linux/teamviewer-host_armhf.deb

Once downloaded you can install it using:

sudo apt install teamviewer-host_armhf.deb

You can rm the teamviewer deb file after installation.

Now now, how do I setup a remote session with this?

After installing we should have a teamviewer cli tool installed on the pi. Lets check if the teamviewer daemon is running.

teamviewer daemon status

Should return a lot of text in which you will find “active (running)” in it.

In you don’t find that, try

sudo teamviewer daemon start

Once that’s done, let’s do the setup. You need a teamview account. If you don’t have one, go to https://login.teamviewer.com/LogOn#register and create one. After that type in

sudo teamviewer setup

accept any license terms and input the emailid and password you gave in the signup process. Since this is the first time you are trying to remote control this device from this account, you should get an email to Add device to trusted list

Click on the link in the email to do that. If required, try the setup command again and in the end you should get this nice text

*** You have successfully added this computer to your Computers & Contacts. You can now access it with a simple double click in your Computers & Contacts list. ***

Open (or install) team viewer in your laptop. Sign In. You should see the Raspberry Pi device listed under “your computers”. Simply double click and start controlling it over the internet.

Don’t tell anyone, It even works after you restart you pi 😉

💥 💥 💥

References:

preparing the wpa_supplicant.conf file

Finding the wifi ssid

Finding ipaddress of pi

Installing teamviewer for Arm devices

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

Also, Read

--

--

Squirrel
Coinmonks

Codes in python. Rants about ML and stuff. Trains to be a coding superhero.