Setup Raspberry Pi Using a Mac

Setup Raspberry Pi Using a Mac

Basic Infomation

  • OS: macOS High Sierra
  • Raspberry Pi device: Raspberry Pi model B, 1GB RAM, 802.11b/g/n, Wireless LAN, Bluetooth 4.1
  • Raspberry OS: Raspbian stretch lite 2018-04-18
  • SD card: SanDisk Ultra 32GB

Downloading Raspbian stretch lite image

  • 可至Raspbian下載最新的Raspbian stretch lite
1
2
3
4
5
6
7
8
9
10
11
12
13
MacBook-Pro:~ user$ mkdir raspbian
MacBook-Pro:~ user$ cd raspbian
MacBook-Pro:raspbian ~ user$ ls
## 此為下載好的Raspbian stretch lite image
2018-04-18-raspbian-stretch-lite.zip
MacBook-Pro:raspbian ~ user$ unzip 2018-04-18-raspbian-stretch-lite.zip
Archive: 2018-04-18-raspbian-stretch-lite.zip
inflating: 2018-04-18-raspbian-stretch-lite.img
## 解壓縮後的img檔案
MacBook-Pro:raspbian ~ user$ ll | grep img
-rw-r--r-- 1 user staff 1862270976 Apr 18 09:08 2018-04-18-raspbian-stretch-lite.img

Burning Raspbian image to SD card

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
MacBook-Pro:raspbian ~ user$ diskutil list
/dev/disk0 (internal):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme 500.3 GB disk0
1: EFI EFI 314.6 MB disk0s1
2: Apple_APFS Container disk1 500.0 GB disk0s2
/dev/disk1 (synthesized):
#: TYPE NAME SIZE IDENTIFIER
0: APFS Container Scheme - +500.0 GB disk1
Physical Store disk0s2
1: APFS Volume Macintosh HD 404.0 GB disk1s1
2: APFS Volume Preboot 23.4 MB disk1s2
3: APFS Volume Recovery 517.8 MB disk1s3
4: APFS Volume VM 7.5 GB disk1s4
/dev/disk2 (external, physical):
#: TYPE NAME SIZE IDENTIFIER
0: FDisk_partition_scheme *31.1 GB disk2
1: Windows_FAT_32 31.1 GB disk2s1
## 目標SD card為/dev/disk2
## 可以根據TYPE NAME或SIZE來判斷目標SD card的identifier
## 若挑錯identifier可能造成電腦資料被破壞,慎思!
MacBook-Pro:raspbian ~ user$ diskutil unmountDisk /dev/disk2
Unmount of all volumes on disk2 was successful
## 將Raspbian image寫入SD card
MacBook-Pro:raspbian ~ user$ sudo dd bs=1m if=2018-04-18-raspbian-stretch-lite.img of=/dev/rdisk2
Password:
1776+0 records in
1776+0 records out
1862270976 bytes transferred in 125.462224 secs (14843280 bytes/sec)

Launching Raspbian

  1. 插上SD card
  2. 接上HDMI螢幕
  3. 接上Micro USB電源線
  4. 使用預設帳號密碼登入: pi/raspberry
  5. 進入Raspbian設定頁面:

    1
    pi@raspberrypi:~ $ sudo raspi-config
  6. 變更登入密碼

  7. 開啟SSH server

  8. 開啟Wifi

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    pi@raspberrypi:~ $ sudo vi /etc/network/interfaces
    allow-hotplug wlan0
    iface wlan0 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
    pi@raspberrypi:~ $ sudo vi /etc/wpa_supplicant/wpa_supplicant.conf
    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    update_config=1
    country=TW
    pi@raspberrypi:~ $ sudo systemctl enable wpa_supplicant.service
    pi@raspberrypi:~ $ sudo reboot
    ## 須先重新登入raspberry
    pi@raspberrypi:~ $ sudo raspi-config

Launching Raspbian Without HDMI Monitor

此步驟可以達成在沒有HDMI螢幕的情況下,透過ssh經由Wifi啟動並設置Raspbian

  1. 在將Raspbian image寫入SD card後,進行下述操作

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    MacBook-Pro:raspbian ~ user$ cd /Volumes/boot
    ## 為之後要使用的ssh命令建立檔案
    MacBook-Pro:boot chung$ touch ssh
    ## 設定Wifi的配置
    ## NATWORK_NAME: Wifi device的名稱
    ## YOUR_PASSWORD: Wifi device的密碼
    MacBook-Pro:boot chung$ vim wpa_supplicant.conf
    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    update_config=1
    country=GB
    network={
    ssid="NATWORK_NAME"
    psk="YOUR_PASSWORD"
    }
  2. 將SD card插入Raspbian

  3. 接上Micro USB電源線
  4. 取得Raspbian的IP,有以下2種方式:
    • 至Wifi device的管理頁面查看新連上的裝置所被配置的IP
    • 使用網路嗅探工具,例如nmap,查看相同Wifi環境下的IP
  5. 使用ssh以及預設帳號密碼登入: pi/raspberry