# 樹莓的初始安裝

## 準備microSD卡

從樹莓派(Raspberry Pi)網站下載 Raspbian Jessie Lite版的鏡像檔，再複製到MicroSD記憶卡。 此過程與作業系統相關，樹莓派的網站已說明。

## 啟用SSH 存取

基於安全原因，預設中SSH 存取並沒有啟用。為了啟用SSH，在MicroSD記憶卡的啟動磁區根目錄內，新增一個名為ssh空白的檔案。下面是用簡單命令完成的方法示範。

```
$ cd  <啟動磁區根目錄>
$ > ssh
```

## 啟動Raspberry並連接

取出microSD卡，並把它插入到樹莓派，再將樹莓派的電源插上。

***驗證***

```
紅色LED –亮時代表有電源。綠色LED，代表microSD卡的存取，不規則閃爍1至2秒。
```

接下來，所有的操作都是用SSH命令列來完成 (Windows中用Putty，OS X或Linux上用終端機)。用乙太網路的線路將樹莓派連接上有DHCP伺服器的網路後，即可使用網址 raspberrypi.local 來存取樹莓派。

***驗證***

```
在您的電腦鍵入 ping -c3 raspberrypi.local。回應應如下所示。
$ Ping -c3 raspberrypi.local
  PING raspberrypi.local (192.168.1.212): 56 data bytes
  64 bytes from 192.168.1.212: icmp_seq = 0 ttl = 64 time = 0.452 ms
  64 bytes from 192.168.1.212: icmp_seq = 1 ttl = 64 time = 0.251 ms
  64 bytes from 192.168.1.212: icmp_seq = 2 ttl = 64 time = 0.222 ms
  --- raspberrypi.local ping statistics ---
  3 packets transmitted, 3 packets received, 0.0% packet loss
  Round-trip min / avg / max / stddev = 0.222 / 0.308 / 0.452 / 0.102 ms
```

連接到樹莓。使用者帳號是 pi 而密碼為 raspberry (密碼稍後可更改，就像使用者帳號)。

***驗證***

```
如果一切正確完成，您連上MoodleBox了，控制台會顯示:
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.

pi @ raspberrypi: ~ $
```

## 樹莓初始設置

更新樹莓的作業系統，然後啟動raspi-config程序。

```
$ sudo apt-get update -y
$ sudo apt-get dist-upgrade -y
$ sudo raspi-config
```

使用 raspi-config 程序，執行下列操作：

* 將microSD卡的磁區加大至最大尺寸;
* 變更使用者帳號pi的密碼(使用強度高的密碼)。在此為了散佈方便，我們選擇 Moodlebox4$ 當密碼。
* 調整地區編碼成zh-TW\.UTF-8;
* 設定Wi-Fi技術的時區和國家;
* 變更樹莓的主機名稱為moodlebox;&#x20;

現在，不論是用SSH或SFTP，要連到樹莓都使用 moodlebox.local 網址，使用者帳號為pi，密碼為Moodlebox4$。

## 更改預設的使用者帳號

使用樹莓派的預設帳號pi不會產生任何問題。然而，由於安全性，不建議使用這個眾所周知的使用者帳號。因此我們這裡更改預設帳號為 moodlebox。

我們從建立一個帳號及密碼皆為tempuser的臨時使用者帳號開始。

```
$ sudo useradd -m tempuser
$ sudo passwd tempuser
```

這個使用者帳號稍後會刪除，所以密碼的品質不重要。為了方便，我們以下皆使用Moodlebox4$。為了讓所有操作順利，將tempuser帳號加到sudo群組後，我們就可以離線了。

```
$ sudo usermod -a -G sudo tempuser
$ exit
```

使用 tempuser 帳號及 Moodlebox4$ 連線至 Moodlebox

```
$ ssh tempuser@moodlebox.local
$ tempuser@moodlebox.local's password:
```

連上Moodlebox後，先將要修改的檔案備份起來，以防有問題產生。

```
$ cd /etc
$ sudo tar -czf /home/pi/authfiles.tgz passwd group shadow gshadow sudoers sudoers.d/010_pi-nopasswd systemd/system/autologin@.service
```

接下來變更檔案中的moodlebox使用者帳號及其資料夾，我們將舊的資料夾加一個"-"符號，以避免混淆。

```
$ sudo sed -i 's/\bpi\b/moodlebox/g' passwd group shadow gshadow sudoers sudoers.d/010_pi-nopasswd systemd/system/autologin@.service
$ sudo mv /etc/sudoers.d/010_pi-nopasswd /etc/sudoers.d/010_moodlebox-nopasswd
$ sudo mv /home/pi /home/moodlebox
$ sudo ln -s /home/moodlebox /home/pi
$ exit
```

我們用新的帳號重新連上 MoodleBox，但密碼仍是 MoodleBox4$。

```
$ ssh moodlebox@moodlebox.local
$ moodlebox@moodlebox.local's password:
```

***驗證***

```
如果一切正確，您可連上MoodleBox且螢幕上顯示:
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.
moodlebox@moodlebox:~ $
```

最後，刪除臨時使用者帳號tempuser和備份檔案即完成：

```
$ sudo userdel tempuser
$ sudo rm -rf /home/tempuser/
$ sudo rm authfiles.tgz
```

現在可用 moodlebox 帳號和 moodlebox4$ 的密碼連接至 MoodleBox 了。

## 修正Debian Jessie的錯誤

我們修正一個會影響 MoodleBox 的錯誤：

```
$ sudo sed -i '/# The named pipe \/dev\/xconsole/,$d' /etc/rsyslog.conf
$ sudo systemctl restart rsyslog
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gxiangco.gitbook.io/moodlebox-on-raspberry-pi-3/chapter3.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
