LoginSignup
187
179

More than 5 years have passed since last update.

Raspberry Pi に固定IPアドレスを割り当てる方法(Raspbian Jessie)

Last updated at Posted at 2015-12-29

今回やりたいこと

有線LAN接続した Raspberry Pi に、固定IPアドレスを割り当てる

ルーターのDHCPアドレス範囲を確認する

まずはルーターの管理画面にアクセスして、DHCPサーバーのアドレス範囲を確認します。
管理画面への初期ログイン情報は、ルーターの説明書などに記載してあります。

私の環境では、以下の設定になっていました。

開始アドレス:192.168.0.10
終了アドレス:192.168.0.254

固定IPを割り振る場合、このアドレス範囲外にする必要があるため、今回は 192.168.0.2 を設定してみます。(192.168.0.1はルーターのIPアドレス)

Raspberry Pi にログイン

前回の記事でSSH接続できるように対応したので、同じようにリモートPCからログインします。

MBP:.ssh suzy$ ssh pi@192.168.0.16
pi@192.168.0.16'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.
Last login: Wed Dec 23 23:17:20 2015
pi@raspberrypi ~ $ 

設定ファイルを編集する

固定IPを設定するには /etc/network/interfaces ファイルを変更するとの情報をWebで得たので、まずはファイルの中身を確認してみました。初期状態は以下のようになっています。

# Please note that this file is written to be used with dhcpcd.
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'.

auto lo
iface lo inet loopback

auto eth0
allow-hotplug eth0
iface eth0 inet manual

auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

auto wlan1
allow-hotplug wlan1
iface wlan1 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

何やら上のコメントが気になります...「固定IPの設定をするなら、/etc/dhcpcd.confの内容と、man dhcpcd.confコマンドの実行結果を参考にしてね!」といった感じでしょうか。

Web上の情報は、前バージョンの Raspbian Wheezy についての記載が多かったようで、Raspbian Jessie で固定IPを変更する場合は/etc/dhcpcd.confというファイルを編集します。

さっそく man dhcpcd.conf を実行。固定IPの設定方法を発見!

static value
             Configures a static value.  If you set ip_address then dhcpcd will not
             attempt to obtain a lease and just use the value for the address with an
             infinite lease time.

             Here is an example which configures a static address, routes and dns.
                   interface eth0
                   static ip_address=192.168.0.10/24
                   static routers=192.168.0.1
                   static domain_name_servers=192.168.0.1

上記を参考に /etc/dhcpcd.confを編集します。以下を追記しました。

interface eth0
static ip_address=192.168.0.2/24
static routers=192.168.0.1
static domain_name_servers=220.152.38.233

ルータやDNSサーバの設定値は、ご自身の環境に合わせて変更してください。

Raspberry Pi を再起動して、設定内容の反映を確認する

pi@raspberrypi ~ $ sudo shutdown -r now

先ほど設定したIPアドレスでSSH接続できれば、完了です。

MBP:.ssh suzy$ ssh pi@192.168.0.2
The authenticity of host '192.168.0.2 (192.168.0.2)' can't be established.
ECDSA key fingerprint is SHA256:S5qREIPX0e81RujvdUyFmrUpVG+Uqa64SuGGffWJCGc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.2' (ECDSA) to the list of known hosts.
pi@192.168.0.2'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.
Last login: Tue Dec 29 14:17:18 2015
pi@raspberrypi ~ $ 

ログインできたーー!
念のため Raspberry Pi 側でも確認、問題なさそうです。

pi@raspberrypi ~ $ ifconfig
eth0      Link encap:Ethernet  HWaddr b8:27:eb:73:f8:3b  
          inet addr:192.168.0.2  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::ba27:ebff:fe73:f83b/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:147 errors:0 dropped:0 overruns:0 frame:0
          TX packets:150 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:18196 (17.7 KiB)  TX bytes:19893 (19.4 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1104 (1.0 KiB)  TX bytes:1104 (1.0 KiB)

次にやりたいこと

本来は湿度計を作る計画なので、次は湿度センサーを使って、部屋の温度・湿度を取得するサンプルプログラムを作成してみます。

187
179
2

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
187
179