Setting up a Raspberry Pi without mouse/keyboard

In this article we will configure a Raspberry Pi Zero W without a mouse and a keyboard, but rather directly from macOS. The idea is to create a bootable Raspbian image with the configuration needed to automatically connect to WiFi at the first boot, allowing you to connect to it by ssh. At the end we will see an example on how to install GIT and Node.js.

Steps

  • Create a bootable micro SD card with Raspbian
  • Set up SSH access
  • Set up WiFi
  • Boot the Raspberry
  • Access from SSH
  • Install screen
  • Install GIT
  • Install Node.js

Bootable SD card

As first step you’ll need a micro SD card that will contain Raspbian operating system. For Raspbian Lite image installations the creators recommend a minimum of 4GB.

Format the SD card

Once you connected the SD card to your Mac, identify its name by running:

# List partitions of each disk
diskutil list

This command will list the partitions of each connected disk. Find your SD card name, that should be something like /dev/diskN, where N is an integer number. Double check the name, otherwise with next commands you will be destroying your primary partition!

Format the SD card with the correct filesystem, in our case ExFAT.

# Format the diskN in ExFAT
diskutil eraseDisk ExFAT raspbian diskN

where diskN is the name of the SD card. In case you are wondering, raspbian is used as name for the card. Remember to unmount the disk before make it bootable!

# Unmoun the diskN
diskutil unmountDisk /dev/diskN

Create a bootable Raspbian image

Download the Raspbian operating system from raspberrypi.org/downloads/raspbian. Since I will use it on my Raspberry Pi Zero W, I went for the lite version, without desktop.

Once downloaded, unzip it and keep track of where the .img has been extracted. We now create a bootable SD card by exploiting the macOS built dd command (data duplicator):

# Copy Raspbian inside the SD card
# Be careful to select correct destination or dd will erase everything without asking!
sudo dd bs=1m if=path_of_your_image.img of=/dev/diskN conv=sync
You can easily insert the path the the *.img* file just by drag and dropping it over the terminal once you have written: sudo dd bs=1m if=

The parameter bs is the block size, while conv=sync means that dd will pad every input block to the input buffer size (these values should guarantee decent performance when running the copy).

Depending on the image you chose and the class of your SD card, this process can take more than 15 minutes. Check the progress by pressing Ctrl+T.

SSH & WiFi configuration

When the copy of Raspbian on the card has been completed, navigate to Volumes folder:

# Navigate to /Volumes folder
cd /Volumes

# List dir content
ls

ls’s output should contain a partition called boot, the one we just created. Go inside it.

# Navigate to /Volumes/boot folder
cd boot

To enable SSH access we just need to create an empty file called ssh:

# Create a new file called ssh
touch ssh

To allow the Raspberry to connect to the WiFi, we need to create another file that contains the network information:

# Create a new file called wpa_supplicant.conf
touch wpa_supplicant.conf 

Open it with your preferred editor, in my case Visual Studio Code (code wpa_supplicant.conf), and insert into it the following structure, taking care of replacing the necessary fields:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=IT

network={
    ssid="WiFi name"
    psk="WiFi password"
    key_mgmt=WPA-PSK
}

If you need to customize this settings you can refer to the official page.

Before ejecting the disk, close the editor you used to update the wpa_supplicant.conf, otherwise you will receive an error message.

# Eject the SD card
diskutil eject diskN

Boot the Raspberry

Insert the SD card into your Raspberry and wait a few moments. If you have a Raspberry Pi Zero W the green LED should start blinking and then stop once booted. The Raspberry should now be connected to WiFi.

Try to connect using the default account [email protected] If you receive a message saying “Cannot resolve raspberry.local”, try with the IP address.

# Connect with SSH
ssh [email protected]

If you cannot connect to it using its name, try this approach. We need its IP address in order to access it from SSH. If you have access to the router you can find the connected devices from the management page (eg. 192.168.1.1), otherwise you can use a network scanner.

Once that you found the IP, just type:

# Connect with SSH
ssh [email protected]_address
Default parameters:
user: pi
password: raspberry

The default password for the pi user is raspberry. You are now inside your Raspberry!

Keep your SSH session running

Screen is an utility that will allow you to keep multiple terminal sessions running and switch between them. It can be used in SSH to keep your session running.

Let’s say for example that you created a Node.js app that you want to keep alive inside the Raspberry even when you terminate the SSH session: screen fits perfectly this scenario. Install it with:

# Install screen command
sudo apt-get install screen

Now you can start a new screen session by just typing screen at the command line. To disconnect but leaving the session running:

# Disconnect from screen but keep the session running
Ctrl+A and then Ctrl+D

You will see the message “[detached]”, indicating that the screen session is still running. If you terminate your SSH session, the screen session remain active. The next time you connect in SSH and you want to reconnect to the previous screen session, just type:

# Reconnects to an existing session
screen -r

For a more complete screen tutorial I suggest this article.

Installing GIT

Nothing as simple as running:

# Installs git
sudo apt-get install git

Just remember to fix your identity:

git config --global user.name "John Doe"
git config --global user.email [email protected]

Installing Node.js

To install Node.js you need to know which ARM processor your Raspberry is using:

# Detects the ARM model
uname -m

For example for Raspberry Pi Zero W you will need ARMv6.

Go the download page of Node.js and find the desired version. Copy the download link and download it inside your Raspberry with:

# Download Node.js 
wget https://nodejs.org/dist/xxx-linux-armvxx.tar.gz

For my Raspberry Pi Zero W I chose https://nodejs.org/dist/v8.9.0/node-v8.9.0-linux-armv6l.tar.gz.

Uncompress the contents:

# Uncompress
tar -xzf xxx-linux-armvxx.tar.gz

Navigate inside the folder that contain the uncompressed version of Node.js you chose:

# Navigate into the uncompressed folder
cd node-vxx-linux-armvxx

# Copy folder's contents to /usr/local/
sudo cp -R * /usr/local/

Finally check that node is installed properly:

# Check Node version
node -v