选项 1:使用 /etc/network/interfaces.d

选项 1:使用 /etc/network/interfaces.d

我尝试在运行最新 Raspbian Lite (Bullseye) 的 RPi4 上设置静态 IP,这样我就可以完全删除 DHCP。

我的/etc/network/interfaces文件如下所示:

# Include files from /etc/network/interfaces.d:
auto lo
iface lo inet loopback

我也有/etc/network/interfaces.d/wlan0,看起来像:

auto wlan0
iface wlan0 inet static
  address 192.168.0.133
  netmask 255.255.255.0
  gateway 192.168.0.1
  dns-nameservers 192.168.0.1 8.8.8.8 4.4.4.4

如果我运行ip a,我会得到以下输出:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN group default qlen 1000
    link/ether e4:5f:01:20:e9:00 brd ff:ff:ff:ff:ff:ff
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether e4:5f:01:20:e9:01 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.133/24 brd 192.168.0.255 scope global wlan0
       valid_lft forever preferred_lft forever
    inet 192.168.0.132/24 brd 192.168.0.255 scope global secondary dynamic noprefixroute wlan0
       valid_lft 6495sec preferred_lft 5595sec
    inet6 fdfc:b729:655a:4bd0:4620:dbb9:867b:fc3d/64 scope global dynamic mngtmpaddr noprefixroute
       valid_lft 1654sec preferred_lft 1654sec
    inet6 fe80::eba1:157a:7b52:9b5c/64 scope link
       valid_lft forever preferred_lft forever

这看起来合法,我可以在 ...132 和 ...133 上访问 Pi。但是,如果我禁用了 dhcpcd.service,则重新启动后我将无法再访问它(或者如果我手动停止 dhcpcd.service)

我假设这ifup没有被调用,但我可能完全关闭了。现在在设备上安装键盘和显示器有点困难,这使得调试变得复杂(因为如果它不在网络上我就无法访问它)。

任何建议都深表感谢。

编辑:

我在禁用 DHCP 的情况下启动了系统,让它停留一两分钟然后将其关闭,这样我就可以查看另一台计算机上的日志。看起来网络正在按预期运行:

Jun 10 12:28:16 rpi-display systemd[1]: Starting Raise network interfaces...
Jun 10 12:28:16 rpi-display avahi-daemon[355]: Server startup complete. Host name is rpi-display.local. Local service cookie is 166240168.
Jun 10 12:28:17 rpi-display avahi-daemon[355]: Joining mDNS multicast group on interface wlan0.IPv4 with address 192.168.0.133.
Jun 10 12:28:17 rpi-display avahi-daemon[355]: New relevant interface wlan0.IPv4 for mDNS.
Jun 10 12:28:17 rpi-display avahi-daemon[355]: Registering new address record for 192.168.0.133 on wlan0.IPv4.
Jun 10 12:28:17 rpi-display systemd[1]: Finished Raise network interfaces.

Resolv.conf 有我配置的名称服务器:

# Generated by resolvconf
nameserver 192.168.0.1
nameserver 8.8.8.8
nameserver 4.4.4.4

答案1

选项 1:使用 /etc/network/interfaces.d

您需要在/etc/network/interfaces.d/wlan0文件中提供无线参数,可以是有效的路径wpa_supplicant.conf

auto wlan0
iface wlan0 inet static
  address 192.168.0.133
  netmask 255.255.255.0
  gateway 192.168.0.1
  dns-nameservers 192.168.0.1 8.8.8.8 4.4.4.4
  wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

或者您需要在文件中提供 SSID 和密钥:

auto wlan0
iface wlan0 inet static
  address 192.168.0.133
  netmask 255.255.255.0
  gateway 192.168.0.1
  dns-nameservers 192.168.0.1 8.8.8.8 4.4.4.4
  wpa-ssid "MyNetworkName"
  wpa-psk "secretpassword"

选项 2:使用 /etc/dhcpcd.conf

删除你的/etc/network/interfaces.d/wlan0文件。

编辑你的/etc/dhcpcd.conf文件。您将看到以下示例配置:

# 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

取消注释并填写适当的值:

# Example static IP configuration:
interface wlan0
static ip_address=192.168.0.133/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1 8.8.8.8

重新启用dhcpcd.service并重新启动。


顺便说一句,USB串行电缆如在这种情况下非常有帮助,因为它允许您将 Pi 插入台式机/笔记本电脑并通过串行控制台访问它。

答案2

读完上一篇文章后我成功解决了这个问题这里并意识到dhcpcd是从一个钩子开始wpa_supplicant的。wlan0

经过一番摆弄后,我意识到我需要[email protected]手动启用。我复制了该wpa_supplicant.conf文件,因为wpa_supplicant-wlan0.conf它抱怨缺少该文件,尽管现在我读了 @larsks 答案,我可能已经能够通过interfaces.d/wlan0.

相关内容