[IoT-Intro] Raspberry Pi 作業系統安裝與設定 === ###### tags: `IoT-Intro` [TOC] # 燒錄 Raspbian 映像檔流程(在 Windows, MacOS 作業系統環境皆適用) * by [name=Mark Chang], [name=Shan Jung Fu] ## Step 1. Format micro SD card 1. 下載 SD Memory Card Formatter 安裝檔:https://www.sdcard.org/downloads/formatter_4/ 2. 安裝 SD Memory Card Formatter 點兩下進行安裝 3. Format micro SD card - 點選 `Format` ![](https://i.imgur.com/02Iib4k.png) - Format 完成 ![](https://i.imgur.com/Wsr0CUu.png) ## Step 2. 下載 Raspbian 映像檔 到官網提供的連結(https://downloads.raspberrypi.org/raspbian_latest)下載 ## Step 3. 下載燒錄工具 1. 到官網下載安裝檔 Etcher: https://etcher.io/ 2. 點兩下進行 Etcher 燒錄工具的安裝 3. 開啟 Etcher 應用程式,並點選左邊的 `Select images` 按鈕 ![](https://i.imgur.com/4ttAcPY.png) 4. 選擇 `2017-09-07-raspbian-stretch.zip` 檔案 ![](https://i.imgur.com/yo8muIj.png) 5. 中間選要燒錄的 SD Card ,點選右邊的 `Flash` 按鈕。 ![](http://people.cs.nctu.edu.tw/~yuming8587/etcher1.PNG) 6. 燒錄完成 <!-- ## 其他:在 Unix 或 Linux 作業系統下燒錄 Raspbian 映像檔流程 * by [name=icwang] * 利用 [fdisk(8)](https://linux.die.net/man/8/fdisk) 檢查儲存裝置及分割 * ```sudo fdisk -l``` ![image alt](https://cdn.pimylifeup.com/wp-content/uploads/2015/05/fdisk-command.png) <br> * 利用 [umount(8)](https://linux.die.net/man/8/umount) 卸載要燒錄的裝置 :::warning 在 MacOS 上沒有 ```fdisk``` 和 ```umount``` 指令, 請使用 ```diskutil``` 或是 Disk Utility ::: * [dd(1)](http://man7.org/linux/man-pages/man1/dd.1.html) * 轉換或複製一個檔案 * 拷貝 image file 到 ```/dev/mmcblk0``` ```dd bs=4M if=/path/to/.img of=/dev/mmcblk0 conv=fsync``` :::info 如果是 adapter 可能是 /dev/sdX * 其中 ```if```, ```of``` 分別為要輸入檔案來源和要寫入的檔案 <br> > 在 Unix 上,硬體的裝置驅動和特殊裝置檔案就像普通檔案一樣 > -Wikipedia <br> * 燒完之後會有兩個磁區, 一個是開機用的 boot ( FAT格式, 大部分的OS都看得到 ), 另一個是 Linux 的檔案系統 Ext4 ( 對 Linux 以外的 OS 而言是未知的檔案系統 ) --> 燒錄完後,將SD卡插上RPi3,將RPi3接上螢幕, keyboard, mouse, 電源(USB供電or變壓器)及網路線, 觀察螢幕上的開機過程, 然後接著作後續系統設定 # 2. Raspbian 系統設定 ## A. 更新 apt sources.list 的台灣 Mirror - 說明1:什麼是 Mirror ? Raspberry Pi Raspbian 套件庫的更新來源,中文稱『鏡射站』。若不設定我們的 Mirror 來源,則系統自動幫我們依回應速度決定要連線的鏡像站。若直接指定國內的 Mirror 來源,更新套件清單與更新套件、安裝套件等的網路下載速度會較快。 完整 mirror list : https://www.raspbian.org/RaspbianMirrors (官網提供,不過 10/16 清晨測試連不上) - 說明2:什麼是 sources.list ? 設定 Mirror 站台的檔案。 `/etc/apt/sources.list` 內的每列就是一個來源,包括三個以空格區隔成三個欄位。 第一個欄位:指來源類型(`deb` 表示為執行套件) 第二個欄位:Mirror 的 URL 及使用協定 第三個欄位:選定發行版本與組別 ```shell= deb http://free.nchc.org.tw/raspbian/raspbian stretch main contrib non-free rpi ( dpkg格式套件庫 ) ( 伺服器的位址及使用協定 ) ( 發行版本 ) ( 組別 ) ``` 參考連結:https://debian-handbook.info/browse/zh-TW/stable/apt.html#sect.apt-sources.list - 操作: 使用 Terminal (終端機)編輯 `/etc/apt/sources.list` 1. `sudo nano /etc/apt/sources.list` 2. 將 Mirror URL 改成 `http://free.nchc.org.tw/raspbian/raspbian` ## A.1. 設定keyboard layout RPi出廠的default設定是對應到英式鍵盤, 在某些按鍵會跟台灣使用的美式鍵盤不一樣(e.g. shilt-1跟shilt-2), 這時要使用 Terminal (終端機)編輯 `/etc/default/keyboard` 1. `sudo nano /etc/default/keyboard` 2. 將 `XKBLAYOUT="gb"` 改成 `XKBLAYOUT="us"` 3. reboot後就生效了 ## B. 網路設定 (static IP 設定) 利用 dhcpcd 來設定 fallback static IP,當 DHCP 失效時,才啟用固定 IP 。 如果自己有 Raspberry Pi ,回到有 DHCP 環境會先以 DHCP 為主。 1. 編輯 `/etc/dhcpcd.conf` 檔案 ```shell= $ sudo nano /etc/dhcpcd.conf ``` 2. 輸入以下文字 ```shell= profile static_eth0 static ip_address=<固定 IP> static routers=<閘道 IP> static domain_name_servers=<DNS 伺服器 IP> ``` 3. nano 離開並儲存 1. Ctrl-x (^X) Exit 2. 輸入 `Y` ```bash Save modified buffer? (Answering "No" will DISCARD changes.) Y Yes N No ^C Cancel ``` 3. enter ```bash File Name to Write: xxxx ^G Get Help M-D DOS Format M-A Append M-B Backup File ^C Cancel M-M Mac Format M-P Prepend ^T To Files ``` 4. release previous dhcp profile and restart dhcpcd ```shell= $ sudo dhcpcd -k && sudo dhcpcd ``` reference: 1. https://wiki.archlinux.org/index.php/dhcpcd 2. https://raspberrypi.stackexchange.com/questions/37920/how-do-i-set-up-networking-wifi-static-ip-address/37921#37921 ## C. 網路檢測工具 ### ifconfig - 說明: 全名為 interfaces config。與 Windows 作業系統環境中的 ipconfig 命令列工具檢測的內容相同。可查看網路介面配置資訊、獲得本機端的 IP address、MAC address 等資訊。 - 使用方式: ```shell= $ ifconfig $ ifconfig -a ``` ### ping - 說明:向 Destination host 傳出一個 ICMP echo 要求封包,並等待接收 echo 回應封包 - 使用方式: ```shell= $ ping 8.8.8.8 $ ping Gateway_IP $ ping www.google.com ``` ### traceroute - 說明:利用增加存活時間(TTL)值來顯示封包在 IP 網路經過的 router 的 IP 位址。每當封包經過一個 router ,其存活時間就會減1。當其存活時間是0時,主機便取消封包,並傳送一個 ICMP TTL 封包給原封包的發出者。 - 使用方式: ```shell= $ traceroute 8.8.8.8 ``` ### mtr - 說明:整合了 PING 與 Traceroute 兩種功能,可讓我們以 Traceroute 的形式列出全部節點,即時顯示每個節點的 PING 值與網路封包丟失狀況,並呈現及時更新的監控列表。 - 使用方式: ```shell= $ sudo apt install mtr-tiny $ mrt 8.8.8.8 Host Loss% Snt Last Avg Best Wrst StDev 1. vlanxxxgw.cs.nctu.edu.tw 0.0% 18 0.9 1.6 0.5 3.7 0.8 2. IP-23-142.cs.nctu.edu.tw 0.0% 18 0.4 0.4 0.4 0.4 0.0 3. 140.113.3.242 0.0% 18 2.4 1.2 0.2 8.2 2.2 4. 140.113.3.178 0.0% 18 0.8 0.9 0.7 1.6 0.0 5. 140.113.0.78 0.0% 18 0.8 0.8 0.7 0.9 0.0 6. 140.113.0.161 0.0% 17 3.2 3.2 2.8 3.8 0.0 7. 108.170.244.129 0.0% 17 3.3 3.3 3.0 3.4 0.0 8. 72.14.238.97 0.0% 17 3.4 3.4 3.1 3.7 0.0 9. google-public-dns-a.google.com 0.0% 17 2.9 3.2 2.9 3.3 0.0 ``` - 若安裝mtr-tiny時發生Unable to locate package...訊息, 則可先執行下面更新動作, 然後再重新安裝一次mtr-tiny ```= $ sudo apt-get update ``` ### nslookup - 說明:查詢 IP 與主機名稱的對應,使用 /etc/resolv.conf 這個檔案來作為 DNS 伺服器的來源選擇。 - 使用方式: ```shell= $ nslookup www.google.com Server: 127.0.0.53 Address: 127.0.0.53#53 Non-authoritative answer: Name: www.google.com Address: 216.58.200.228 $ nslookup 8.8.8.8 Server: 127.0.0.53 Address: 127.0.0.53#53 Non-authoritative answer: 8.8.8.8.in-addr.arpa name = google-public-dns-a.google.com. Authoritative answers can be found from: ``` - 若無法執行nslookup (沒這個指令), 則可以執行下面動作安裝dnsutils ```= $ sudo apt-get install dnsutils ``` ### dig - 說明:查詢 DNS 資訊 - 使用方式: ```shell= $ dig google.com ; <<>> DiG 9.10.3-P4-Ubuntu <<>> google.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 30285 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 65494 ;; QUESTION SECTION: ;google.com. IN A ;; ANSWER SECTION: google.com. 299 IN A 172.217.27.142 ;; Query time: 9 msec ;; SERVER: 127.0.0.53#53(127.0.0.53) ;; WHEN: Mon Oct 16 03:53:04 CST 2017 ;; MSG SIZE rcvd: 55 ``` ### netstat - 說明:顯示與 IP, TCP, UDP 和 ICMP 協定相關的統計資料,一般用於檢驗本機各埠的網路連接情況。 netstat 是在內核中訪問網路及相關資訊的程式,提供 TCP 連接, TCP 和 UDP 監聽的相關報告。 - 使用方式: 格式:netstat [-acCeFghilMnNoprstuvVwx][-A<網路類型>][--ip] ```shell= $ netstat -atu 列出所有 TCP 與 UDP port ``` <!-- ## A. 更新 apt sources.list 的台灣 mirror - 使用terminal編輯 /etc/apt/sources.list `sudo nano /etc/apt/sources.list` > deb http://free.nchc.org.tw/raspbian/raspbian stretch main contrib non-free rpi > ( dpkg格式套件庫 ) ( 伺服器的位址及使用協定 ) ( 發行版本 ) ( 組別 ) >> 建議更改 "伺服器的位址" 成 http://free.nchc.org.tw/raspbian/raspbian >> 完整 mirror list : https://www.raspbian.org/RaspbianMirrors --> <!-- ## B. 網路設定 (static IP 設定) 利用dhcpcd來設定fallback static IP,當DHCP失效時,才啟用固定IP。 >如果自己有raspberry pi,回到有DHCP環境會先以DHCP為主。 - 使用 editor (leafpad, vi, nano, ee) 編輯 ```/etc/dhcpcd.conf``` sudo nano /etc/dhcpcd.conf ``` profile static_eth0 static ip_address=<固定IP> static routers=<閘道IP> static domain_name_servers=<DNS伺服器IP> ... interface eth0 fallback static_eth0 #fallback接的tag要和上面profile一樣 > nano 離開方法 : Ctrl-x + Yes + Yes > vi Esc + ":wq" - 完成之後 - sudo dhcpcd -k && sudo dhcpcd >release previous dhcp profile and restart dhcpcd - ip addr >列出介面和相關資訊 reference https://wiki.archlinux.org/index.php/dhcpcd#Static_profile ---> <!-- ## C. 網路檢測工具 - ping - traceroute - dig (nslookup) - 需要安裝 `sudo apt install dnsutils` - dig [url] / nslookup [url] > notice 請先檢查是否有獲得IP!! -->