Raspberry Pi によるイーサネットコンバータ (1) 準備

イーサネットコンバータが必要になったので、Raspberry Piで作ってみることにした。

パソコン 有線ネットワークポート (DHCP Client)
↓
Raspberry Pi 有線ポート (DHCP Server)
↓
NAPT
↓
Raspberry Pi 無線ポート
↓
無線アクセスポイント(モバイルWi-Fiルータ)
↓
インターネット

という流れです。 普通にイーサネットコンバータを買った方が安いし速度が出そうですが、 Raspberry Piを一度使ってみたかったので…。

準備

購入したもの

家にあったもの

インストー

OSインストール(Micro SDカード作成)

GUIは不要だし、速く起動させたいので Raspbian Lite をインストールすることにした。 手順は Raspberry PiへのOSインストールの手順 ddコマンド編 そのままで OK だった。(MacOS Sierra上で実行)

OSイメージをダウンロードしてチェックし、圧縮ファイルを展開

$ curl -O http://ftp.jaist.ac.jp/pub/raspberrypi/raspbian_lite/images/raspbian_lite-2017-07-05/2017-07-05-raspbian-jessie-lite.zip
$ curl -O http://ftp.jaist.ac.jp/pub/raspberrypi/raspbian_lite/images/raspbian_lite-2017-07-05/2017-07-05-raspbian-jessie-lite.zip.sha256
$ shasum -a 256 2017-07-05-raspbian-jessie-lite.zip > checked.txt
$ diff checked.txt 2017-07-05-raspbian-jessie-lite.zip.sha256
$ unzip 2017-07-05-raspbian-jessie-lite.zip

MicroSDカードをMacにセットし、デバイス名を確認。

$ df -h
Filesystem      Size   Used  Avail Capacity iused      ifree %iused  Mounted on
/dev/disk0s2   223Gi  144Gi   78Gi    65% 1250450 4293716829    0%   /
devfs          191Ki  191Ki    0Bi   100%     660          0  100%   /dev
map -hosts       0Bi    0Bi    0Bi   100%       0          0  100%   /net
map auto_home    0Bi    0Bi    0Bi   100%       0          0  100%   /home
/dev/disk1s2   931Gi  825Gi  106Gi    89% 1395350 4293571929    0%   /Volumes/Macintosh HD
/dev/disk2s2   119Gi   95Gi   24Gi    81%     273 4294967006    0%   /Volumes/SSD120
/dev/disk3s1   249Mi  113Mi  136Mi    46%     512          0  100%   /Volumes/878
/dev/disk4s1   7.6Gi  2.3Gi  5.3Gi    31%       0          0  100%   /Volumes/NO NAME

→ /dev/disk4s1 だった。

MicroSDカードのパーティションをアンマウント。

$ sudo diskutil umount /dev/disk4s1
Password:
Volume NO NAME on disk4s1 unmounted

dd コマンドでイメージを書き込み。

$ sudo dd bs=1m if=2017-07-05-raspbian-jessie-lite.img of=/dev/rdisk4
dd: /dev/rdisk4: Invalid argument
1645+1 records in
1645+0 records out
1724907520 bytes transferred in 158.689131 secs (10869727 bytes/sec)

→ Invalid argument というエラーが表示されている。よくわからないが、 /dev/rdisk4 ではなく /dev/disk4 を指定して書き込んでみる。

$ sudo dd bs=1m if=2017-07-05-raspbian-jessie-lite.img of=/dev/disk4
1645+1 records in
1645+1 records out
1725629563 bytes transferred in 1028.788584 secs (1677341 bytes/sec)

書き込みがかなり遅いが、今度はエラーは出なかった。

OS起動

MicroSDカードをRaspberry Piにセットし、ネットワークケーブル(ストレート) でMacRaspberry Piを直結して起動。起動させたらMacからpingを打ってみる。 Raspbian Liteはデフォルトでavahi-daemonがインストールされているので、 raspberrypi.localで名前解決できる。

ping raspberrypi.local
PING raspberrypi.local (192.168.1.46): 56 data bytes
64 bytes from 192.168.1.46: icmp_seq=0 ttl=64 time=50.380 ms
64 bytes from 192.168.1.46: icmp_seq=1 ttl=64 time=1.706 ms
64 bytes from 192.168.1.46: icmp_seq=2 ttl=64 time=1.334 ms
^C

通信できているようだ。sshで接続してみる。

$ ssh pi@raspberrypi.local
ssh: connect to host raspberrypi.local port 22: Connection refused

接続拒否された。調べたところ、最近のRaspbianではbootパーティションsshというファイルが無いと接続拒否する設定になっているようだ。 MicroSDカードを取り出してMacに挿して

$ cd /Volumes/boot
$ touch ssh

で ‘ssh’ ファイルを作成した。再度Raspberry PiMicroSDカードをセットして 起動させ、Macからデフォルトで存在するpiユーザー(パスワード: raspberry)で 接続。

$ ssh pi@raspberrypi.local
The authenticity of host 'raspberrypi.local (2001:a452:8188:dc00:5b8a:2cff:a2c1:ce49)' can't be established.
RSA key fingerprint is SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'raspberrypi.local,2001:a452:8188:dc00:5b8a:2cff:a2c1:ce49' (RSA) to the list of known hosts.
pi@raspberrypi.local's password:

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

今度は接続できた。メッセージに従い、piユーザーのパスワードを変更しておく。

pi@raspberrypi:~ $ passwd
Changing password for pi.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

また、普段ログインするユーザーも作っておく。

pi@raspberrypi:~ $ sudo adduser user
Adding user `user' ...
Adding new group `user' (1001) ...
Adding new user `user' (1001) with group `user' ...
Creating home directory `/home/user' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for user
Enter the new value, or press ENTER for the default
    Full Name []: 
    Room Number []:
    Work Phone []:
    Home Phone []:
    Other []:
Is the information correct? [Y/n] Y
pi@raspberrypi:~ $ sudo adduser user sudo
Adding user `user' to group `sudo' ...
Adding user user to group sudo
Done.

無線設定

USB無線子機を接続し、確認。

$ lsusb
Bus 001 Device 004: ID 2019:ab2a PLANEX GW-USNano2 802.11n Wireless Adapter [Realtek RTL8188CUS]
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

→ 自動的に “PLANEX GW-USNano2 802.11n Wireless Adapter” として認識された。

Wi-Fi接続情報を作成。下記の “SSID” には接続したい無線アクセスポイントの SSIDを、"passphrase" にはパスフレーズを入力。

$ wpa_passphrase SSID passphrase

画面に表示された内容を /etc/wpa_supplicant/wpa_supplicant.conf の末尾に追加。 wlan0インターフェースを再起動。

$ sudo ifdown wlan0
$ sudo ifup wlan0
$ ifconfig
(...中略...)
wlan0     Link encap:Ethernet  HWaddr 00:22:cf:fd:8b:56
          inet addr:192.168.1.47  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: 2001:a452:8188:dc00:e417:8a7d:40ec:d4ad/64 Scope:Global
          inet6 addr: fe80::6caf:649c:143d:17b4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:9 errors:0 dropped:7 overruns:0 frame:0
          TX packets:25 errors:0 dropped:11 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:4099 (4.0 KiB)  TX bytes:6925 (6.7 KiB)

IPアドレスが取得できている。OK。基本的な設定ができたので、今日はここまで。