Headless Raspberry Pi Setup

What we want to do

I would like to set­up a Rasp­ber­ry Pi (RPi) from scratch run­ning rasp­bian, but doing it with­out an attached key­board or dis­play.

Preparing the SD Card

To get you up and run­ning a SD with rasp­bian a Debian vari­ant built for the RPi is need­ed. How it is pre­pared is well doc­u­ment­ed on the rasp­ber­ry pi web­site:

Instruc­tions how to pre­pare an SD card for the RRi

After the flash­ing of the SD card is com­plete, a new par­ti­tion called boot should show up. The RPi will be lat­er accessed via SSH, there­fore the SSH serv­er has to be start­ed in the RPi’s boot process. To con­fig­ure it to do so, an emp­ty file called ssh has to be placed in the boot par­ti­tion. It has to have no exten­sion.

Head­less SSH serv­er set­up

If you plan to con­nect the RPi to the net­work via wire­less net­work­ing it has to know the net­work name (SSID) and the cre­den­tials to con­nect. To also pro­vide this infor­ma­tion upfront, a sec­ond con­fig­u­ra­tion file on the boot par­ti­tion is need­ed. In it the net­works to con­nect to are defined.

Head­less wire­less net­work­ing set­up

Now every­thing is ready for the first boot of the fresh­ly pre­pared image.

First Booting

Before boot­ing, the RPi is con­nect­ed via an eth­er­net cable direct­ly to a host com­put­er. Please make sure, that IPv6 is enabled on this com­put­er. Then pow­er up the RPi by attach­ing an appro­bi­ate pow­er sup­ply. The red LED should light up and the green one should flash indi­cat­ing SD card access. Fur­ther­more, the LEDs on the Eth­er­net Port should light up to con­firm a link to the host com­put­er.

How does one know the IP address to connect to the RPi’s SSH server?

There are sev­er­al option: the cheat­ing solu­tion would be to attach a mon­i­tor or TV via HDMI, but this is not tru­ely head­less any­more. Anoth­er pos­si­bil­i­ty is the mDNS pro­to­col. If run­ning macOS or a lin­ux or win­dows with mDNS enable, try to ping the default host­name raspberrypi.local. This is the eas­i­est way to con­nect as no IP address has to be known as the RPi announces itself on the net­work via mDNS.

If this fails, there is also an alter­na­tive route using a fea­ture defined in the IPv6 pro­to­col. This will work with any IPv6-enabled host com­put­er. The RPi has the IPv6 pro­to­col enabled by default.

We will use a spe­cial link-local mul­ti­cast address which is defined as the all nodes address that means that every con­nect­ed device to the same net­work will answer. Link-local address­es are valid only for one defined net­work inter­face, so one eth­er­net port or a WLAN. In our case the net­work con­nect­ed to the eth­er­net port just con­sists of two devices: the host com­put­er and the RPi. The idea is now to send a ping to all attached hosts using the spe­cial IPv6 address ff02::1%<name of interface>.

To exe­cute the ping, start up a ter­mi­nal and use the ifconfig or ip a com­mand to find out how the inter­face is called the RPi is con­nect­ed to. In the case of my mac, the eth­er­net inter­face used to con­nect to the RPi is called en4. There­fore the com­mand was as fol­lows:

ping6 ff02::1%en4
PING6(56=40+8+8 bytes) fe80::aaaa:bbbb:cccc:dddd%en4 --> ff02::1%en4 16 bytes from fe80::aaaa:bbbb:cccc:dddd%en4, icmp_seq=0 hlim=64 time=0.256 ms 16 bytes from fe80::ca1b:6999:e36:4d7c%en4, icmp_seq=0 hlim=64 time=0.658 ms

This result shows that when the spe­cial all nodes address is pinged, two hosts answer, the own link-local IPv6 address (fe80::aaaa:bbbb:cccc:dddd%en4) and the address of the RPi (fe80::ca1b:6999:e36:4d7c%en4). Please note that the inter­face iden­ti­fi­er is part of this link-local address.

Connecting to the RPi via SSH

With the know address, con­nect­ing to the RPi is easy. Use the ssh-client on the ter­mi­nal with the deter­mined address (in this case an IPv6 link-local address):

ssh pi@fe80::ca1b:6999:e36:4d7c%en4

or

ssh pi@raspberrypi.local(if mDNS is enabled)

Use the default pass­word raspberry to log in. Then type in passwd and change the default pass­wort imme­di­ate­ly.

You can now use the ifconfig com­mand to find out the address of the WLAN inter­face for exam­ple.

Finalizing Network Setup

If the RPi is going to be con­nect­ed via cable its link-local address (fe80::ca1b:6999:e36:4d7c) stays the same. Care has to be tak­en that the cor­rect out­put inter­face on the host com­put­er is cho­sen, e.g. if the RPi is con­nect­ed via cable but the host com­put­er is con­nect­ed to the net­work via a WLAN inter­face called en0, the cor­rect link-local IPv6 address would then be fe80::ca1b:6999:e36:4d7c%en0.

Thomas

Chemist, Programmer, Mac and iPhone enthusiast. Likes coding in Python, Objective-C and other languages.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.