RaspberryPi3 B+入門 Step1‐ 初期設定

# はじめに

- RaspberryPi3 B+の初期設定についての備忘録

## 公式サイト

- [Raspberry Pi](https://www.raspberrypi.org/)
- [Raspberry Pi Documents](https://www.raspberrypi.org/documentation/)

##### The Raspberry Pi 3 Model B+ is the latest product in the Raspberry Pi 3 range.
>・Broadcom BCM2837B0, Cortex-A53 (ARMv8) 64-bit SoC @ 1.4GHz
>・1GB LPDDR2 SDRAM
>・2.4GHz and 5GHz IEEE 802.11.b/g/n/ac wireless LAN, Bluetooth 4.2, BLE
>・Gigabit Ethernet over USB 2.0 (maximum throughput 300 Mbps)
>・Extended 40-pin GPIO header
>・Full-size HDMI
>・4 USB 2.0 ports
>・CSI camera port for connecting a Raspberry Pi camera
>・DSI display port for connecting a Raspberry Pi touchscreen display
>・4-pole stereo output and composite video port
>・Micro SD port for loading your operating system and storing data
>・5V/2.5A DC power input
>・Power-over-Ethernet (PoE) support (requires separate PoE HAT)

## SDカード初期化
- [SDカードメモリフォーマッター](https://www.sdcard.org/jp/downloads/formatter_4/)

## OS
- [Raspbian](https://www.raspberrypi.org/downloads/raspbian/)
- [NOOBS](https://www.raspberrypi.org/downloads/noobs/)
- [Win10 IoT Core](https://go.microsoft.com/fwlink/?LinkId=846058)

## Image Writer

- [Etcher](https://www.balena.io/etcher/)

## 初期設定

### raspi-config

```txt:raspi-config
$ sudo raspi-config
```

#### ロケール，言語設定
![init-1.png](https://qiita-image-store.s3.amazonaws.com/0/89366/7bdd3f16-24ae-bc27-3d46-23be21654d6b.png)
![init-2.png](https://qiita-image-store.s3.amazonaws.com/0/89366/5c58febe-0fb8-db2b-9bac-bd8227a528ad.png)
![init-3.png](https://qiita-image-store.s3.amazonaws.com/0/89366/5507f62c-0687-33f5-36f3-84de2bd02ebc.png)
※ [ ]en_GB.UTF-8 UTF-8 を解除し、[*]ja_JP.UTF-8 UTF-8 を選択
![init-4.png](https://qiita-image-store.s3.amazonaws.com/0/89366/1b04b972-2da9-f94d-6aae-a8dbdcb5c523.png)

#### タイムゾーン設定 (Asia/Tokyo)
![init-timezone-1.png](https://qiita-image-store.s3.amazonaws.com/0/89366/358224ce-5b78-300f-142d-1b8509b6583f.png)
![init-timezone-2.png](https://qiita-image-store.s3.amazonaws.com/0/89366/8e05fb26-87b1-bc6c-94b0-c4b87b3a4e71.png)
![init-timezone-3.png](https://qiita-image-store.s3.amazonaws.com/0/89366/61ab2169-6722-7a58-5f38-5bc43eb34a6e.png)

#### キーボード設定
![init-kb-1.png](https://qiita-image-store.s3.amazonaws.com/0/89366/a4341429-9d6f-3d38-66b6-cafde62103ec.png)
![init-kb-2.png](https://qiita-image-store.s3.amazonaws.com/0/89366/71cb7768-df21-6761-1909-8eaf60391826.png)
![init-kb-3.png](https://qiita-image-store.s3.amazonaws.com/0/89366/ce4ba641-4915-46b5-0002-57b5693bc1f1.png)
![init-kb-4.png](https://qiita-image-store.s3.amazonaws.com/0/89366/eeb6c5c4-7fdb-1569-c651-9802bc8c55e9.png)
![init-kb-5.png](https://qiita-image-store.s3.amazonaws.com/0/89366/19eb40a4-6675-f77d-1e10-943cc4193f76.png)

#### ホスト名設定
![init-nw-1.png](https://qiita-image-store.s3.amazonaws.com/0/89366/3f1c2478-6c34-d906-2974-c7e85dcb7df1.png)
![init-nw-2.png](https://qiita-image-store.s3.amazonaws.com/0/89366/14b55d69-3343-c73a-6ec0-b9cfec95839d.png)
![init-nw-3.png](https://qiita-image-store.s3.amazonaws.com/0/89366/529f737a-b620-7a73-7642-a78ddb837975.png)
![init-nw-4.png](https://qiita-image-store.s3.amazonaws.com/0/89366/5f44c3ec-968c-5f73-58d8-3958884a3f78.png)

#### Wi-Fi国設定 (JP)
![init-wifi-1.png](https://qiita-image-store.s3.amazonaws.com/0/89366/f84bedb0-d789-3ae0-8c87-adbe6bbf549a.png)
![init-wifi-2.png](https://qiita-image-store.s3.amazonaws.com/0/89366/91329f16-b8d3-df5c-4d98-20526e71f83d.png)
![init-wifi-3.png](https://qiita-image-store.s3.amazonaws.com/0/89366/bee1e28f-49ec-ab67-4cc7-34b7ea59e350.png)

#### パスワード変更
![init-pw-1.png](https://qiita-image-store.s3.amazonaws.com/0/89366/172a52d2-30fb-8f75-4dae-b4398ee36220.png)
![init-pw-2.png](https://qiita-image-store.s3.amazonaws.com/0/89366/77fed506-8714-ef82-6995-90b39d979f9e.png)
![init-pw-3.png](https://qiita-image-store.s3.amazonaws.com/0/89366/3663a1c5-0822-b6b2-0acf-ac1cf917be3f.png)

#### SSH有効化
![init-if-1.png](https://qiita-image-store.s3.amazonaws.com/0/89366/57a76d9b-b194-e573-42ec-a90ea7425e11.png)
![init-if-2.png](https://qiita-image-store.s3.amazonaws.com/0/89366/868bbdce-8f5b-d46d-7e0e-bb3de48d6d80.png)
![init-if-3.png](https://qiita-image-store.s3.amazonaws.com/0/89366/085371d8-2870-cd02-7972-25bc9da3dce3.png)
![init-if-4.png](https://qiita-image-store.s3.amazonaws.com/0/89366/2d3a6069-656e-d12b-5e8d-3f2a1e1f2f79.png)

#### raspi-configの終了 (reboot)
![init-fin-1.png](https://qiita-image-store.s3.amazonaws.com/0/89366/495a17e5-df6a-022c-045a-fa0359803bcf.png)


## Wi-Fi設定

- [Debian WiFi](https://wiki.debian.org/WiFi)

### 無線LANルータの設定状況
- SSIDステルスを設定している
- 通信方法
 - 規格：WPA2
 - 暗号化：AES
 - 認証：事前共有鍵(PSK)

### まずは状態確認

```sh:iwconfig
$ iwconfig
```

```sh:iwlist
$ iwlist wlan0 scan
```

```sh:ifconfig
$ ifconfig
```

```sh:パスワードを暗号化する
$ sudo wpa_passphrase 'ssid' 'MyPassphrase' >> /etc/wpa_supplicant/wpa_supplicant.conf
```

```sh:wpa_supplicant.confファイルを編集する
$ sudo vi /etc/wpa_supplicant/wpa_supplicant.conf
```

```txt:/etc/wpa_supplicant/wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=JP
network={
        ssid="ネットワーク識別子"
        scan_ssid=1
        psk="パスフレーズ"
}
```
※ 詳細は、[FreeBSD manual page](https://www.freebsd.org/cgi/man.cgi?query=wpa_supplicant.conf&sektion=5&apropos=0&manpath=NetBSD+6.1.5)

### /etc/network/interfaces.d/配下に個別に設定ファイルを作成する

##### DHCPの場合
```txt:/etc/network/interfaces.d/wlan0
auto wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
```

##### IP固定の場合
```txt:/etc/network/interfaces.d/wlan0
auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
  address 192.168.0.200
  netmask 255.255.255.0
  gateway 192.168.0.1
  wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
```
##### dhcpdサービスを停止する
```sh:IP固定にした場合dhcpdを停止してもOK
$ sudo systemctl stop dhcpd.service
$ sudo systemctl disable dhcpcd.service
$ systemctl status dhcpcd.service
● dhcpcd.service - dhcpcd on all interfaces
   Loaded: loaded (/lib/systemd/system/dhcpcd.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
```

### ファイルへのアクセス制御

```sh:
$ sudo chmod 0600 /etc/wpa_supplicant/wpa_supplicant.conf
$ sudo chmod 0600 /etc/network/interfaces.d/wlan0
```

### ネットワークに繋がったら…

```sh:update
$ sudo apt update
```

```sh:upgrade
$ sudo apt upgrade
```

```sh:dist-upgrade
$ sudo apt dist-upgrade
```

## スクリーンセーバーの設定

【現象】SSH接続して10分くらい放置してると、SSHが切れてしまう。

- 対策としては3つほどあった。

##### 1. XScreenSaverをインストールする

```py:
$ sudo apt install screensaver
```

##### 2. xsetコマンドで一時しのぎ

```py:
$ sudo xset s off
$ sudo xset -dpms
$ sudo xset s noblank
```

##### 3. xserver-commandで恒久的に

```py:
$ sudo vi /etc/lightdm/lightdm.conf
```

```sh:/etc/lightdm/lightdm.conf
#xserver-command=X
xserver-command=X -s 0 -dpms
```

# おわりに

- Wi-Fi設定の記述方法が色々あって調べるのに結構面倒だった。
- 最終的には、この設定で個人的にはベストかなと。