ICTについて使える情報を発信していきます. 遠回りしたり, 自分のペースで...

このブログを検索

Raspbian install on Raspberry Pi 3

色々設定していたRasberry Pi3のシステムがぶっ壊れたので, OSの再インストールをする羽目に…
どうせなら最初からSDカード使ってインストールするまでを備考録として残していきます.
OSのダウンロードからSSH設定まで書きます.

ちなみに, ちまたにたくさん備考録が転がっているので, 自分にあったものを選んで下さい. これ大事.
さらにちなみに, 公式のドキュメントは「Raspberry Pi Software Guide」です.

環境 & 必要な物

絶対必要なもの

  • macOS (PC)
  • TOSHIBA EXCERIA 32GB (SDカード32GB以下を推奨. 1 )
  • Raspberry Pi3 Model B
  • やる気

あったら良いもの

  • SDカードリーダー(SDカード読み取れないパソコンの場合, 必要)
  • キーボード
  • マウス
  • 有線LAN

今回は, SSH設定をOSインストール前にするので, キーボードとかマウスとかは準備しません. もちろん, あった方が良いではありますが…保険になるし.
文字だけの画面(ターミナル)が怖くない方はできます.

Raspbian インストール (CUI使用)

CUI(Command User Interface)は, Terminalというアプリ使いますって話.

OSのダウンロード

OSのダウンロードを行います.
Raspbian 公式 ダウンロードページ」からRaspbianのダウンロードが行なえます.
下に行くほどぞろぞろありますが, 基本は, ページの上の方にある, NOOBSかRASPBIANのどちらかを選びます.
どちらも機能的に変わりはないのですが, 今回はRASPBIANを使います. (ggったらNOOBSのやり方は「Raspberry PiのOSをインストールしてみた(NOOBS篇)」にあります.)

出典: Raspbian 公式 ダウンロードページ

次に, 「RASPBIAN STRETCH WITH DESKTOP」か「RASPBIAN STRETCH LITE」か選べます. 「RASPBIAN STRETCH LITE」はアプリが色々入っていないので, 今回は「RASPBIAN STRETCH WITH DESKTOP」を選びます.
「RASPBIAN STRETCH WITH DESKTOP」の横の「Download ZIP」を選んで下さい. (というかTorrentあったのかよ懐いな)

出典: Raspbian 公式 Raspbian ダウンロードページ

1.7Gあるので少し時間がかかります.
お茶でもして待ちま…この空き時間にSDカードの初期化をしましょう.

SDカードの初期化

次に, SDカードの初期化をやります.
SDカードを挿して, Terminalを開きましょう. TerminalはFinderを開いて,
アプリケーション(Application) -> ユーティリティ -> ターミナル.app(terminal.app)
をダブルクリックすれば開けます.

アプリの画面が出ればOKです.
次のようにコマンドを打ちます. $の後ろがコマンドです. 下からコピペする時は, $のところの後ろをコピペして下さい.

$ diskutil list
/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *251.0 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:                  Apple_HFS Macintosh HD            250.1 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3

/dev/disk1 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *31.0 GB    disk1
   1:                        EFI EFI                     209.7 MB   disk1s1
   2:       Microsoft Basic Data RASBIAN                 30.8 GB    disk1s2

$ # 今回は/dev/disk1のSizeが32GBなので, これじゃろと当たりをつけてやる. ちなみにこれ失敗したらひどいことになる場合もあるので慎重に.
$ diskutil unMountDisk /dev/disk1 
Unmount of all volumes on disk1 was successful

$ # RASBIANのところは任意の「英大文字」にして下さい. 英小文字はダメです. 
$ diskutil eraseDisk FAT32 RASBIAN /dev/disk1
Started erase on disk1
Unmounting disk
Creating the partition map
Waiting for partitions to activate
Formatting disk1s2 as MS-DOS (FAT32) with name RASBIAN
512 bytes per physical sector
/dev/rdisk1s2: 60194048 sectors in 1881064 FAT32 clusters (16384 bytes/cluster)
bps=512 spc=32 res=32 nft=2 mid=0xf8 spt=32 hds=255 hid=411648 drv=0x80 bsec=60223488 bspf=14696 rdcl=2 infs=1 bkbs=6
Mounting disk
Finished erase on disk1

怖い方はGUIを使っても良いかと.
やり方は「Mac で「SD カード」をフォーマット(初期化)する方法」の「SDカードを初期化する.」の章に書いてあります.

これでSDカードの初期化は終わりです.
自分はここで苦戦しました. 2

SDカードにimgをコピーする.

SDカードを初期化している間に, ダウンロードが済んだと思います. ...済んだよね?
「XXXX-YY-DD-raspbian-stretch.zip」
みたいなのがダウンロードされたと思うので, それを解凍します.

解凍したら, またTerminalでコマンドを打っていきます.

$ diskutil list
/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *251.0 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:                  Apple_HFS Macintosh HD            250.1 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3

/dev/disk1 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *31.0 GB    disk1
   1:                        EFI EFI                     209.7 MB   disk1s1
   2:       Microsoft Basic Data RASBIAN                 30.8 GB    disk1s2

$ # 念のため, もう一度diskutil listで調べてから, /dev/diskを指定する.
$ diskutil unMountDisk /dev/disk1

$ # 「if=」まで書いたら, finderからドラッグ・アンド・ドロップしてくる.
$ # もちろん, パスについて知っているならパスを書く.
$ sudo dd bs=1m of=/dev/disk1 if=<imgのpath>
Password:

~~~しばらく待つ~~~

4724+0 records in
4724+0 records out
4953473024 bytes transferred in 2701.965241 secs (1833285 bytes/sec)

Passwordは, パソコンのパスワードを入れます. 見た目は何も変わらないが, ちゃんと入力されているので, パスワードを入力したらEnterを押して下さい.

Passwordがしっかり入力されると, しばらく止まっているように見えますが, SDカードに書き込んでいます. なのでSDカードに何もしない, ターミナルはそのままでゆっくり待ちましょう.

~ transferred ~とか出てきたら終了です.
Finderのデバイス欄にbootというのができていると思います.

これでimgができました.

SSHの準備

SSHとは, アバウトに言うとパソコンを遠隔操作できるようなものです.

SSHの準備をする方法は簡単で, このできたbootの中に「ssh」という何も書いていないファイルを追加するだけです.

実際にTerminalで次のコマンドを打てば終わりです.

$ touch /Volumes/boot/ssh

Wifiの準備.

sshで遠隔操作すると言っても, パソコンがどうにかしてRaspberry Piに「インターネット的に」接続していなきゃダメです. なので

  1. PCとRaspberry Piを有線LANでつなぐ.
  2. Raspberry Piとルーターを有線LANで繋いで, PCからルーター経由でアクセスする.
  3. Raspberry Piとルーターを無線LAN(Wifi)で繋いで, PCからルーター経由でアクセスする.

のいずれかの方法を取る必要があります.

今回は3の「Raspberry Piとルーターを無線LAN(Wifi)で繋いで, PCからルーター経由でアクセスする.」をやります.

ルーターにパスワードがある場合を想定しています. パスワードがある場合, Wifiに繋ぐ時にパスワードを要求されるはずです. 無い場合は, 「Wifiの準備.」をスキップして構いません.

また, WifiはRaspberry Pi3 Model Bの場合, 5GHz帯域では認識しないので注意して下さい.3

次のようなものを先程のbootの中に作ります.

  • 「wpa_supplicant.conf」というファイル.
  • 「wpa_supplicant.conf」の中身はこんな感じ.
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=JP
network={
 ssid=<SSID>
 psk=<Password>
 key_mgmt=WPA-PSK
}        

6行目の<SSID>にはwifiのネットワーク名(SSID)を, 7行目の<Password>は, Wifiにつなげる時に打つパスワードを入れて下さい.
ネットワーク名は上のWifiのマークで調べられます.

例えば, SSIDがhogeで, Passwordがhugaなら,

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=JP
network={
 ssid="hoge"
 psk="fuga"
 key_mgmt=WPA-PSK
}    

になります.
後はこれを, 次のコマンドを使ってファイルを作るだけです.

$ echo 'ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=JP
network={
 ssid="hoge"
 psk="fuga"
 key_mgmt=WPA-PSK
}' > /Volumes/boot/wpa_supplicant.conf

もしどこか間違えても, 修正してもう一度コマンドを打てば良いです.
ファイルが次のように作成されているのを確認して下さい.

これで, Raspbianをインストールする準備は整いました.

Raspbian インストール

それではようやく, Raspbianのインストールです.
SDカードを「安全に」取り外して, RasberryPi3にSDカードを挿入します. そして電源を入れると終わりです!

ここで, hdmiを持っている人はテレビとRasberryPiをつなげると, インストールされてる画面が出ます. ワクワクしますよね. 面白い.

しばらく待つと, インストールが完了します. 大体1分~5分くらいです.
これでRaspbianのインストールは終了です.

SSH設定

SSHが繋がるかの確認

インストールが完了したらSSHをしてみましょう. 2つの方法があります.

  1. IPアドレスからsshする
  2. ホスト名からsshする

1はちょっと知識がないとめんどくさいので, 2を推奨します. 1はあまり詳しく解説しません.

1. IPアドレスからsshする

LAN内で使われているIPアドレスを調べる6つの方法(Linux/Windows)」で紹介されている, arp-scanというコマンドを使います.

ターミナルで, 次のようにコマンドを打って下さい.

$ brew install arp-scan
$ sudo arp-scan -I en0 -l #en0は無線LANのこと. 
Password:
Interface: en0, datalink type: EN10MB (Ethernet)
Starting arp-scan 1.9 with 256 hosts (http://www.nta-monitor.com/tools/arp-scan/)
xxx.xxx.xxx.xxx xx:xx:xx:xx:xx:xx (Unknown)
192.168.0.x     xx:xx:xx:xx:xx:xx Raspberry Pi Foundation
192.168.0.x     xx:xx:xx:xx:xx:xx Raspberry Pi Foundation (DUP: 2)
xxx.xxx.xxx.xxx xx:xx:xx:xx:xx:xx Hon Hai Precision Ind. Co.,Ltd.
xxx.xxx.xxx.xxx xx:xx:xx:xx:xx:xx (Unknown)

521 packets received by filter, 0 packets dropped by kernel
Ending arp-scan 1.9: 256 hosts scanned in 1.862 seconds (137.49 hosts/sec). 5 responded

右側に「Raspberry Pi Foundation」と書かれてあるものの左の数字「192.168.0.x」に注目します. 今回は7行目です.

もし, 「Raspberry Pi Foundation」が見つからない場合は, 準備の段階で失敗しているので, やり直すか他のサイトを参考にして下さい. 4

「ssh pi@<先程注目した数字>」みたいなコマンドを打てば良いです.
今回なら「ssh pi@192.168.0.x」ですね.
途中で(yes/no)問われるので, yesと打ちます.
次にpasswordを要求されますが, 「raspberry」と打って下さい.

$ ssh pi@192.168.0.x
The authenticity of host 'raspberrypi.local (xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx)' can't be established.
ECDSA key fingerprint is SHA256:xxxxxxx...
Are you sure you want to continue connecting (yes/no)? yes <- yesと打つ.
Warning: Permanently added 'raspberrypi.local,xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx' (ECDSA) to the list of known hosts.
pi@raspberrypi.local's password: #raspberryと打つ 

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.
Last login: Wed Apr 18 01:24:38 2018

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:~ $ 

「pi@raspberrypi:~ $ 」が出ればsshができてます. つまりRaspberryPiを遠隔操作できます. 確認は以上で終わりです.

sshしたのが確認できたら, exitしましょう.

pi@raspberrypi:~ $ exit

2. ホスト名からsshする

「ssh pi@raspberrypi.local」というコマンドを打ちます.

すると, (yes/no)と言われるのでyesと打ちます.
「pi@raspberrypi.local’s password」は, 「raspberry」と入力して下さい.

$ ssh pi@raspberrypi.local
The authenticity of host 'raspberrypi.local (xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx)' can't be established.
ECDSA key fingerprint is SHA256:xxxxxxx...
Are you sure you want to continue connecting (yes/no)? yes <- yesと打つ.
Warning: Permanently added 'raspberrypi.local,xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx' (ECDSA) to the list of known hosts.
pi@raspberrypi.local's password: <raspberryと入力>

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.
Last login: Wed Apr 18 01:24:38 2018

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:~ $ 

「pi@raspberrypi:~ $ 」が出ればsshができてます. つまりRaspberryPiを遠隔操作できます. 確認は以上で終わりです.

sshしたのが確認できたら, exitしましょう.

pi@raspberrypi:~ $ exit

SSH公開鍵認証設定

SSHで毎回パスワードを打つのは少しめんどくさいと思います. しかし, sshでの公開鍵認証設定をすると, 毎回パスワードを打たずに済みます. ということで設定していきましょう.

HomeBrewさえインストールされていたら, 「ssh-copy-id」というコマンドが使えるようになるので, それを使っていきます.
ターミナルで, 次のコマンドを打って下さい.
password はRaspberryPi3のパスワードを打ちます. パスワードを変えていないなら, 「raspberry」です.

$ brew install ssh-copy-id
$ ssh-copy-id pi@raspberrypi.local
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/${UserName}/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
pi@raspberrypi.local's password: パスワードを入力.

Number of key(s) added:        1

Now try logging into the machine, with:   "ssh 'pi@raspberrypi.local'"
and check to make sure that only the key(s) you wanted were added.

これでおkです. では, sshしてみましょう.

$ ssh pi@raspberrypi.local
Linux raspberrypi 4.14.34-v7+ #1110 SMP Mon Apr 16 15:18:51 BST 2018 armv7l

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.
Last login: Mon May 21 00:32:37 2018 from 192.168.0.5

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:~ $ 

パスワード無しで入れましたね.
これで, OSのダウンロードからSSH設定まで終了です. お疲れ様でした.

まとめ

今回はOSのダウンロードからSSH設定までしました. よくここまで頑張りました. 偉いw

後半, 画像撮影やHomeBrewの説明など, とっても省略したので, 疑問が多く残ると思います. わからないことがあれば質問下さい.

次は細かいRasberry piの設定を行います.

参考文献


  1. 32GB以上だと, インストールはややこしいです. やっている人は結構いるみたいなので, 「raspbian 64gb sd」で検索してみて下さい. ↩︎

  2. 自分はeraseDiskで書き込み禁止状態になっていて苦労しました. ずっと「error: -69877: couldn’t open device」と出ました. 調べてみるとカードリーダの調子が悪かった. こちらのatottoさんの「ちょっと甘めにさして認識させてから奥までさせばちゃんと書き込みできる」に助けられました.
    他の解決方法も色々あるので, 「sd 書き込み禁止 mac」で調べると良いかも. ↩︎

  3. Macから周囲のWi-Fi電波状況をカンタンに確認する方法:Mac Tips」から帯域を調べる方法が確認できるので, こちらからWifiの帯域を確認することができます. ↩︎

  4. Raspberry Pi 3を初回起動してからSSH接続まで」が良い感じ. ↩︎

0 コメント:

コメントを投稿

Popular Posts