Raspberry Pi 3 購入やWindowsからRapsberry Pi 3へのSSH接続でWindowsからラズパイをリモート操作できるようにしました。
ただ、自宅のネットワーク環境はDHCPなので、ラズパイのIPアドレスが変わってしまう可能性があります。そうするとWindowsからアクセスするときラズパイのIPが分からなくて困ります。(ルーターの管理画面でアドレスの払い出し状況確認すれば分かるのですが、面倒です。)
そこで、ラズパイのアドレスを静的IPにします。
目次
自宅のLAN環境
[NTTから来てる光ファイバ]-[ルータ]-[無線アクセスポイント]-[PCやスマホ等々]
- ルータ: NTTフレッツ光ネクストの契約でレンタルしているもの。今はIPをDHCPで割り当てている。
- 無線アクセスポイント: 昔からうちにあった無線LANルータを、ルータとしてではなく単なる無線アクセスポイントとして設定して使用している。
- PCやスマホ等々: DHCPでIPアドレスを取得している。
ルータの設定変更
現在のルータのDHCP設定を見ると、ルータが192.168.1.0/24のアドレスが軒並みDHCPでの払い出し用アドレスになっていました(192.168.1.2~192.168.1.254の253個)。

ラズパイに割り当てる静的IPを捻出するために、DHCPによるアドレス払い出しの範囲を狭めます。今回は192.168.1.2~192.168.1.99の97個にします。これで192.168.1.100以降を静的IP用に使えます。
ラズパイのIPアドレスの設定変更
自分のラズパイの設定ファイルを見ていきます。
/etc/network/interfaces
pi@raspberrypi:~ $ 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
最後の行以外全部コメントです。/etc/network/interfaces.d の中身は空。
/etc/dhcpcd.conf
pi@raspberrypi:~ $ cat /etc/dhcpcd.conf # A sample configuration for dhcpcd. # See dhcpcd.conf(5) for details. # Allow users of this group to interact with dhcpcd via the control socket. #controlgroup wheel # Inform the DHCP server of our hostname for DDNS. hostname # Use the hardware address of the interface for the Client ID. clientid # or # Use the same DUID + IAID as set in DHCPv6 for DHCPv4 ClientID as per RFC4361. # Some non-RFC compliant DHCP servers do not reply with this set. # In this case, comment out duid and enable clientid above. #duid # Persist interface configuration when dhcpcd exits. persistent # Rapid commit support. # Safe to enable by default because it requires the equivalent option set # on the server to actually work. option rapid_commit # A list of options to request from the DHCP server. option domain_name_servers, domain_name, domain_search, host_name option classless_static_routes # Most distributions have NTP support. option ntp_servers # Respect the network MTU. This is applied to DHCP routes. option interface_mtu # A ServerID is required by RFC2131. require dhcp_server_identifier # Generate Stable Private IPv6 Addresses instead of hardware based ones slaac private # Example static IP configuration: #interface eth0 #static ip_address=192.168.0.10/24 #static ip6_address=fd51:42f8:caae:d92e::ff/64 #static routers=192.168.0.1 #static domain_name_servers=192.168.0.1 8.8.8.8 fd51:42f8:caae:d92e::1 # It is possible to fall back to a static IP if DHCP fails: # define static profile #profile static_eth0 #static ip_address=192.168.1.23/24 #static routers=192.168.1.1 #static domain_name_servers=192.168.1.1 # fallback to static profile on eth0 #interface eth0 #fallback static_eth0
「# Example static IP configuration:」というセクションがあるので、ここに設定すればいいようです。ifconfigを見るとラズパイのWifiインタフェース名は「wlan0」なので、これに今回は192.168.1.101を設定します。以下の記述を追記します。
interface wlan0 static ip_address=192.168.1.101/24 static routers=192.168.1.1 static domain_name_servers=192.168.1.1 8.8.8.8
pi@raspberrypi:~ $ cd /etc pi@raspberrypi:/etc $ ls -l dhcp* -rw-rw-r-- 1 root netdev 1701 11月 22 14:46 dhcpcd.conf -rw-r--r-- 1 root root 42 11月 29 02:56 dhcpcd.duid -r-------- 1 root root 192 11月 29 02:56 dhcpcd.secret dhcp: 合計 16 -rw-r--r-- 1 root root 1426 11月 26 2016 debug drwxr-xr-x 2 root root 4096 11月 29 01:39 dhclient-enter-hooks.d drwxr-xr-x 2 root root 4096 11月 29 01:16 dhclient-exit-hooks.d -rw-r--r-- 1 root root 1735 11月 26 2016 dhclient.conf pi@raspberrypi:/etc $ su パスワード: root@raspberrypi:/etc# cp -p dhcpcd.conf dhcpcd.conf.20180224.bak root@raspberrypi:/etc# vi dhcpcd.conf root@raspberrypi:/etc# diff dhcpcd.conf.20180224.bak dhcpcd.conf 45a46,49 > interface wlan0 > static ip_address=192.168.1.101/24 > static routers=192.168.1.1 > static domain_name_servers=192.168.1.1 8.8.8.8 root@raspberrypi:/etc# reboot
再起動後に192.168.1.101へ向けてSSH接続もVNC接続も成功しました。