Raspberry 静态IP配置

2023-05-16

前言

        Raspberry 在使用过程中,需要手动设定静态IP地址,但是深入下去发现不同的raspberry版本不同,设置上也存在差异。

“/etc/network/interfaces”

        在之前的文章《RaspberryPi—网络设置》中详细介绍了语法说明。但是这个路径在早起的版本是起作用的,官方在Jessie版本之后,设置静态ip地址就挪地方了。具体位置是“/etc/dhcpcd.conf”。

“/etc/dhcpcd.conf”

        dhcpcd.conf 提供了比网络/接口更多的功能,我们现在可以在各种情况下更改静态 IP 地址。官方给出了一个使用的例子,例如,如果我们将这样配置的 Pi 带到学校,网络是 10.0.0.0/16,或者绕到朋友家,他的路由器使用 192.168.0.0/24,我们将无法通话除非我们连接键盘和屏幕并重新配置它。
        所以我们需要解决这种问题,看看如何来配置实现的吧:

  • 采用dhcpcd 探测网络以寻找路由器。根据它找到的路由器,它会配置一个静态 IP 地址以适应对应网络。
######################################################
# TEMPLATE: A different IP address on each network
#
#           The arping address should be the router
#           or some other machine guaranteed to be
#           available. You need to know the addresses
#           of the servers. If none of the arpings find
#           an active machine then you will get a DHCP
#           allocation.
######################################################
interface eth0
arping 192.168.2.1			# 探测网络寻找路由器
arping 192.168.0.254		# 探测网络寻找路由器

profile 192.168.2.1			# 如果探测到路由器,则系统设置为该profile的静态IP
static ip_address=192.168.2.44/24
static routers=192.168.2.1
static domain_name_servers=192.168.2.1

profile 192.168.0.254		# 如果探测到路由器,则系统设置为该profile的静态IP
static ip_address=192.168.0.44/24
static routers=192.168.0.254
static domain_name_servers=192.168.0.254

我们可以根据需要添加任意数量的“arping”行,只要每个行都有一个与之匹配的“profile”部分。这个在一个没有DHCP的网络里是非常有效的。

  • 另一个常见的情况是在没有路由器的情况下使用 Pi,因此它无法获取 dhcp 地址。如果没有任何配置,这意味着它在 169.254.0.0/16 网络中获得了“本地链接”地址。但是它得到的地址是随机的,在很多情况下是没有用的。相反,您可以告诉 dhcpcd 在 DHCP 失败时分配一个指定的地址:
######################################################
# TEMPLATE: A static IP address only when no DHCP
#
#           The profile name is arbitrary. Use "fred"
#           if you want. Not much we can put as
#           default servers, but set them up as
#           you usually would.
######################################################
interface eth0				# 启动eth0
fallback nodhcp				# 如果启动失败了按 nodhcp进行设置

profile nodhcp
static ip_address=10.0.0.1/8
static routers=10.0.0.1
static domain_name_servers=10.0.0.1

以上就是在“/etc/dhcpcd.conf”目录下的一些灵活ip设置

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Raspberry 静态IP配置 的相关文章

随机推荐