6
5

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 1 year has passed since last update.

【Raspberry pi 4】固定IPアドレスの設定方法

Last updated at Posted at 2022-04-21

環境

  • Raspberry Pi 4 Model B Rev 1.4
  • Debian GNU/Linux 11 (bullseye) *RaspbianOS

CLI接続方法

参考:Tera Termを利用してCLI接続する(追記予定)
参考:コマンドプロンプトでSSH接続する(追記予定)

現在の設定確認

IPアドレス/サブネットマスク

ifconfig
admin@TKY-RP001:~ $ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.11  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::3620:aa2c:3ee4:54d7  prefixlen 64  scopeid 0x20<link>
        ether dc:a6:32:bf:85:38  txqueuelen 1000  (Ethernet)
        RX packets 3599  bytes 1513580 (1.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2055  bytes 219031 (213.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 24  bytes 2590 (2.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 24  bytes 2590 (2.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

admin@TKY-RP001:~ $

今回は有線接続しているので[eth0]-[inet/netmask]を確認する。
無線接続されていれば[wlan0]が表示される。

  • IPアドレス:192.168.1.11
  • サブネットマスク:255.255.255.0(24ビット)

デフォルトゲートウェイ

route
admin@TKY-RP001:~ $ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.254   0.0.0.0         UG    202    0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     202    0        0 eth0
admin@TKY-RP001:~ $

[Destination]の[default]を確認する。

  • デフォルトゲートウェイ:192.168.1.254

DNSサーバ設定

resolv.conf
admin@TKY-RP001:~ $ cat /etc/resolv.conf
# Generated by resolvconf
nameserver 192.168.1.254
admin@TKY-RP001:~ $

[nameserver]がDNSサーバ設定です。

  • DNSサーバ:192.168.1.254

設定変更実施

Configファイルの編集

admin@TKY-RP001:~ $ sudo nano /etc/dhcpcd.conf

[# Example static IP configuration:]という項目があるので、その後ろにある項目を参考に編集する。
*コメントアウトを外し忘れないように。

dhcpd.conf
# Example static IP configuration:
interface eth0
static ip_address=192.168.1.248/24
static routers=192.168.1.254
static domain_name_servers=192.168.1.25

今回は固定するアドレスを変更しました。

  • IPアドレス:192.168.1.11 → 192.168.1.248
  • サブネットマスク:255.255.255.0(24ビット)
  • デフォルトゲートウェイ:192.168.1.254
  • DNSサーバ:192.168.1.254

編集前後を確認する場合は下記折り畳みを開いてください。

Default File
dhcpcd.conf
admin@TKY-RP001:~ $ 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
# Respect the network MTU. This is applied to DHCP routes.
option interface_mtu

# Most distributions have NTP support.
#option ntp_servers

# A ServerID is required by RFC2131.
require dhcp_server_identifier

# Generate SLAAC address using the Hardware Address of the interface
#slaac hwaddr
# OR generate Stable Private IPv6 Addresses based from the DUID
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
編集後ファイル
dhcpd.conf
admin@TKY-RP001:~ $ 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
# Respect the network MTU. This is applied to DHCP routes.
option interface_mtu

# Most distributions have NTP support.
#option ntp_servers

# A ServerID is required by RFC2131.
require dhcp_server_identifier

# Generate SLAAC address using the Hardware Address of the interface
#slaac hwaddr
# OR generate Stable Private IPv6 Addresses based from the DUID
slaac private

# Example static IP configuration:
interface eth0
static ip_address=192.168.1.248/24
static routers=192.168.1.254
static domain_name_servers=192.168.1.254

# 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

設定反映

Interfaceの再起動

いくつか方法がありますが、一番影響が少ないのはSystemctlを利用する方法でしょうか。
それでも他Interfaceでサービス提供している場合、瞬断する可能性があるので影響確認は十分にしましょう。

Interface再起動
admin@TKY-RP001:~ $ sudo systemctl restart networking

(2022/04/24追記:systemctlの再起動ではIPアドレスが変わらないので、使えませんでした。)

その他の方法

接続出来なくなる可能性があります。
SSH/Telnetなどのリモートアクセスで該当Interfaceに接続している場合はDownと同時に切断されます。
再接続しようと思っても、InterfaceがDownしているためアクセス不可になる可能性があります。
複数のアクセス経路がないリモートアクセスの場合はRebootを使用してください。

ifconfigでの再起動
admin@TKY-RP001:~ $ sudo ifconfig eth0 down
admin@TKY-RP001:~ $ sudo ifconfig eth0 up

デバイスごとの再起動を行います。他サービスに影響が出る可能性があります。
当然ですが影響が許容できることを確認してから実施してください。

デバイス再起動
admin@TKY-RP001:~ $ sudo reboot

反映確認

設定値を確認します。

  • IPアドレス:192.168.1.248
  • サブネットマスク:255.255.255.0(24ビット)
  • デフォルトゲートウェイ:192.168.1.254
  • DNSサーバ:192.168.1.254
ifconfig
sit-admin@TKY-RP001:~ $ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.248  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::3620:aa2c:3ee4:54d7  prefixlen 64  scopeid 0x20<link>
        ether dc:a6:32:bf:85:38  txqueuelen 1000  (Ethernet)
        RX packets 5275  bytes 956732 (934.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 872  bytes 95606 (93.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 23  bytes 2537 (2.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 23  bytes 2537 (2.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

admin@TKY-RP001:~ $
route
admin@TKY-RP001:~ $ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.254   0.0.0.0         UG    202    0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     202    0        0 eth0
admin@TKY-RP001:~ $
resolv.conf
admin@TKY-RP001:~ $ cat /etc/resolv.conf
# Generated by resolvconf
nameserver 192.168.1.254

admin@TKY-RP001:~ $
6
5
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
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?