TORIPIYO DIARY

recent events, IoT, programming, security topics

introduce Raspbian OS into Raspberry Pi Zero WH

f:id:ha107chan:20180408193229j:plain

These days I purchased raspberrypi zero WH in switch science online shop for 1800 yen. (actually this device is more powerful than my first PC that I purchased when I enter university... It was over 150000 yen in those days.)

I would like to introduce how to use Raspbian OS on Raspberry Pi Zero WH. This method doesn't require external display or keyboard. Initial Wifi configuration for Raspberry Pi Zero is set up through USB connection.

1. Get OS image from official site

At first we need to install Raspbian OS image into micro SD card. Please access to this page and download "RASPBIAN STRETCH LITE" zip file. (not Desktop version)
www.raspberrypi.org

2. Write OS image into micro SD with etcher software

f:id:ha107chan:20180409203557j:plain
Use etcher software (which can be installed by "brew cask install etcher" command).
Select downloaded image and micro SD device, then Click 'Flash'. Installing OS image into micro SD will start.
f:id:ha107chan:20180416111646p:plain

3. Enable SSH connection

When micro SD is connected to Mac, boot drive will be mounted. Create ssh file to enable SSH service.

touch /Volumes/boot/ssh
4. Enable Internet connectivity through USB

Add "modules-load=dwc2,g_ether" configuration between "rootwait" and "quiet".

vi /Volumes/boot/cmdline.txt

Add "dtoverlay=dwc2" at the end of config.txt file as well.

echo "dtoverlay=dwc2" >> /Volumes/boot/config.txt
5. Connect to Raspberry Pi Zero through USB

Change Mac's Sharing configuration ("System Preference" => "Sharing").
f:id:ha107chan:20180416112728p:plain

Username is "pi", password is "raspberry".
f:id:ha107chan:20180415234758j:plain
(micro USB is inserted into adaptor of Raspberry Pi Zero WH)

~: ssh pi@raspberrypi.local
pi@raspberrypi.local's password:
Linux raspberrypi 4.9.80+ #1098 Fri Mar 9 18:51:28 GMT 2018 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.

SSH is enabled and the default password for the 'pi' user has not been changed.
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.
6. Wifi configuration

Take a note of Wifi "ESSID" and "password". Recognized ESSID by Raspberry Pi Zero can be found by "sudo iwlist wlan0 scan | grep -i essid" command.

vim pass.txt (write wifi password into pass.txt file)
sudo cp -ipv /etc/wpa_supplicant/wpa_supplicant.conf{,backup}
sudo sh -c 'wpa_passphrase "target ESSID name" < pass.txt >> /etc/wpa_supplicant/wpa_supplicant.conf'

sudo vi /etc/wpa_supplicant/wpa_supplicant.conf # remove "#psk" line for security
rm -i pass.txt # remove pass.txt file for security

sudo wpa_cli -i wlan0 reconfigure # reconfigure wlan0 interface

In this point if you restart Raspberry Pi Zero, assigned IP address might be changed, in that case try to connect with other IP address.

7. Make key authentication
# on mac
cd ~/.ssh
ssh-keygen -t rsa -f pizero

ssh-copy-id -i pizero.pub pi@<ip address of raspberrypi zero>   # transfer public key to raspberrypi zero

ssh -i pizero.pub pi@<ip address of raspberrypi zero>

# on Raspberry Pi Zero
# disable password authentication
sudo cp -ipv /etc/ssh/sshd_config{,.backup}
sudo sh -c 'echo "PasswordAuthentication no" >> /etc/ssh/sshd_config'

sudo systemctl restart ssh
8. Disable USB connectivity of Mac

Do reverse operation on "System Preference" => "Sharing".

9. Disable USB connectivity of Raspberry Pi Zero
# remove "dtoverlay=dwc2"
vi /Volumes/boot/config.txt

# remove "modules-load=dwc2,g_ether"
vi /Volumes/boot/cmdline.txt
References