Setup the Raspberry Pi 4

This post is used as a note for setup a Raspberry Pi 4 without attaching HDMI, keyboard and mouse.

1. Download the Raspbian image

The Raspbian system image can be downloaded from https://www.raspberrypi.org/downloads/raspbian/.

Well, as a matter of fact, there are many other OSes that could run on a Raspberry Pi. But I didn't try them, so the steps for them may vary from this one.

2. Check the path to your microSD card

Before proceeding the following (possibly dangerous) operation, you should check whether you have the correct path to the MicroSD card which is intended for installing the Raspbian system.

It's rather simple to confirm the correct path, just type the command below to terminal before inserting the MicroSD card to your computer

ls /dev/disk*

Then insert the MicroSD card and type the abovementioned command again. You shall the differences between outputs in these two terminals.

For example, the figure below shows that /dev/disk2 appears after attaching my MicroSD card to my Mac.

3. Unzip the archived image and flash it to the MicroSD card.

Unzipping is boring. To flash the system image to an external microSD card, we can use dd utility which ships with macOS and most Linux distributes.

The command below requires substitutions based on your settings. /PATH/TO/THE/UNZIPPED/SYSTEM.IMG should be replaced with the path to the .img file you just unzipped. And DISKNUM should be substituted with the aforementioned path in step 2.

sudo dd if=/PATH/TO/THE/UNZIPPED/SYSTEM.IMG of=/dev/DISKNUM bs=32m

4. Setup Wi-Fi connection

To use Raspberry Pi without HDMI, keyboard and mouse, we need SSH. However, to SSH into the device, it should at least connect to Wi-Fi in the first place.

Therefore, we should create and edit a file named wpa_supplicant.conf under the boot partition on your MicroSD card. On macOS, the boot partition will be mounted at /Volumes/boot. If you are using a Linux distro, the path may vary.

cd /Volumes/boot
touch wpa_supplicant.conf

The template for wpa_supplicant.conf is quite straightforward. For instance, if you have 2 Wi-Fi APs, one at home, the other is located in your office, then just write

network={
   ssid="NETWORK_NAME_1"
   psk="NETWORK_PASSWORD_1"
   key_mgmt=WPA-PSK
 }
 

 network={
   ssid="NETWORK_NAME_2"
   psk="NETWORK_PASSWORD_2"
   key_mgmt=WPA-PSK
 }

Of course, the ssid and psk should be changed correspondingly.

5. Enable SSH

To enable remote login to your Raspberry Pi is as simple as ABC. Just create an empty file named ssh under the boot partition on the MicroSD card.

6. Connect to the Raspberry Pi

Plug the power to Raspberry Pi and then wait for about 30 seconds. Then you can connect to your Raspberry Pi with

ssh [email protected]

Remember to type yes if a prompt appears.

7. (Optional) Using a mirror repo if you are ... in China

As is known to all, you don't really have the Internet in China. Therefore I opt for tuna repo which is hosted by Tsinghua University.

sudo sed -i 's#http://raspbian.raspberrypi.org/raspbian/#http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/#g' /etc/apt/sources.list
sudo sed -i 's#http://archive.raspberrypi.org/debian/#http://mirrors.tuna.tsinghua.edu.cn/raspberrypi/#g' /etc/apt/sources.list.d/raspi.list
声明: 本文为0xBBC原创, 转载注明出处喵~

One thought on “Setup the Raspberry Pi 4”

Leave a Reply

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