79
77

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Raspberry Pi を無線 LAN 経由で SSH 接続できるようにする

Posted at

前回 Raspberry Pi とシリアル通信で接続したけど、ケーブルだと何かと面倒なので、無線 LAN 経由で SSH 接続できるようにする

やったこと

  • Raspberry Pi に USB 無線 LAN 子機を接続
  • 起動時に、自動的にアクセスポイントに接続できるようにする
  • Avahiを使って、hostsファイルなしに、ホスト名を指定してアクセスできるようにする

使ったもの

Raspberry Pi に USB 無線 LAN 子機を接続

認識されていることを確認

$ dmsg 

[    3.199637] usb 1-1.3: new high-speed USB device number 4 using dwc_otg
[    3.326389] usb 1-1.3: New USB device found, idVendor=0411, idProduct=01a2
[    3.342376] usb 1-1.3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[    3.363297] usb 1-1.3: Product: 802.11 n WLAN
[    3.377053] usb 1-1.3: Manufacturer: Ralink
[    3.386278] usb 1-1.3: SerialNumber: 1.0

$ ifconfig

...

wlan0     Link encap:Ethernet  HWaddr xx:xx:xx:xx:xx:xx
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

...

インターフェース wlan0 として認識された

起動時に、自動的にアクセスポイントに接続できるようにする

$ cd /etc/network/
$ vi interfaces

/etc/network/interfaces は、Debian、Ubuntuで使われるネットワーク設定ファイル

auto wlan0              # 起動時に自動的に wlan0 を立ち上げる
iface wlan0 inet dhcp   # wlan0 は、DHCP を使う
wpa-ssid "********"     # ssidを指定
wpa-psk "********"      # パスフレーズを指定

ネットワークの再起動

$ sudo /etc/init.d/networking restart

Running /etc/init.d/networking restart is deprecated because it may not re-enable some interfaces ... (warning).
Reconfiguring network interfaces...Internet Systems Consortium DHCP Client 4.2.2
Copyright 2004-2011 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/wlan0/**:**:**:**:**:**
Sending on   LPF/wlan0/**:**:**:**:**:**
Sending on   Socket/fallback
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 6
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 11
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 13
DHCPREQUEST on wlan0 to 255.255.255.255 port 67
DHCPOFFER from 192.168.100.254
DHCPACK from 192.168.100.254
bound to 192.168.100.101 -- renewal in 37700 seconds.
done.

状態の確認

$ iwconfig wlan0
wlan0     IEEE 802.11bgn  ESSID:"URoad-355926"
          Mode:Managed  Frequency:2.417 GHz  Access Point: **:**:**:**:**:**
          Bit Rate=65 Mb/s   Tx-Power=20 dBm
          Retry  long limit:7   RTS thr:off   Fragment thr:off
          Power Management:on
          Link Quality=70/70  Signal level=-35 dBm
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:5   Missed beacon:0

$ ifconfig wlan0
wlan0     Link encap:Ethernet  HWaddr **:**:**:**:**:**
          inet addr:192.168.100.101  Bcast:192.168.100.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:23 errors:0 dropped:0 overruns:0 frame:0
          TX packets:26 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:3273 (3.1 KiB)  TX bytes:5449 (5.3 KiB)

外部にアクセスしてみる

$ curl "http://www.google.co.jp"

Macからアクセスしてみる

$ ssh pi@192.168.100.101
pi@192.168.100.101's password:
Linux raspberrypi 3.10.24+ #614 PREEMPT Thu Dec 19 20:38:42 GMT 2013 armv6l

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: Fri Dec 20 22:59:56 2013

NOTICE: the software on this Raspberry Pi has not been fully configured. Please run 'sudo raspi-config'

つながった!!

Avahi を使って、hostsファイルなしに、ホスト名を指定してアクセスできるようにする

Avahiって?

各サーバが自前でDNSサーバのように、自身のIPアドレスと紐付いたホスト名を通知してくれる仕組みらしい

  • macだと Bonjour と言われてる仕組み

何が得なの?

  • DHCPの場合、IPアドレスが毎回変わるので、それを調べる手間が省ける
  • 固定IPでも、利用する各マシンで hosts ファイルを書く必要がなくなる

インストール

$ sudo apt-get install avahi-daemon 

ホスト名(/etc/hostname)が、raspberrypi なら、raspberrypi.local で接続できるようになったとメッセージが出る

Macからアクセスしてみる


$ ssh pi@raspberrypi.local
pi@raspberrypi.local's password:
Linux raspberrypi 3.10.24+ #614 PREEMPT Thu Dec 19 20:38:42 GMT 2013 armv6l

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: Fri Dec 20 23:12:36 2013 from 192.168.100.100

NOTICE: the software on this Raspberry Pi has not been fully configured. Please run 'sudo raspi-config'

繋がった!!

TODO

セキュリティ的にいい加減なところ色々

  • rootでの接続の不許可
  • 公開鍵でSSH接続
  • 使ってないポートを閉じる

参考

79
77
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
79
77

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?