脳汁portal

アメリカ在住(だった)新米エンジニアがその日学んだIT知識を書き綴るブログ

raspberryPi3のIPを固定する方法(static ip)

raspberryPiは通常自動でIPをアサインしてくれますが、それでは困る場合などに固定IPアドレスにする方法です。
raspbianのversion upに伴い、raspberryPi2の時とは方法が変わっているようです。

ちなみにversionは8.0です

$ cat /etc/debian_version
8.0

/etc/network/interfacesの確認

$ cat /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)

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

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

iface eth0 inet manual

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

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

上のほうのコメントを見ると、staticIPにするにはdhcpcd.confを編集すればよいことがわかります。
とりあえず書かれている通りにmanを見ます

$ man dhcpcd.conf
     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

             Here is an example for PPP which gives the destination a default route.  It uses the special desti-
             nation keyword to insert the destination address into the value.
                   interface ppp0
                   static ip_address=
                   destination routers

staticで検索すると上記のような記入例が出てくるので、自分のIPに変更して実際に書き込みます

/etc/dhcpcd.confの編集

$ vi /etc/dhcpcd.conf
### ファイル末尾に追加
interface eth0
static ip_address=192.168.0.10/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1

### wifi(wlan0)の場合は
interface wlan0
.
.
.

これで完了です。
後はraspberryPiを再起動して設定したIPでアクセスできることが確認できればOKです。