【问题标题】:Set eth0 as DHCP in raspberry pi在树莓派中将 eth0 设置为 DHCP
【发布时间】:2023-08-08 20:49:01
【问题描述】:

我想将 RPI (Raspbian Stretch) 的 eth0 设置为 DHCP,我的目标是当我连接任何使用 TCP/IP 协议进行通信的设备时,该设备将收到一个 IP 地址。

我发现许多指南都导致将 eth0 ip 地址设为静态,这不是我的意图。

当前有一个设备通过 eth0 连接,ifconfig 说它有一些 IP 但 ping 设备的主机名没有得到回复。 wlan0 通过 wifi 连接。

这里有一些信息:

pi@raspberrypi:~ $ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 169.254.31.197  netmask 255.255.0.0  broadcast 169.254.255.255
        inet6 fe80::ad5a:8219:4c27:b59b  prefixlen 64  scopeid 0x20<link>
        ether b8:27:eb:f3:f2:87  txqueuelen 1000  (Ethernet)
        RX packets 81  bytes 26568 (25.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 46  bytes 10544 (10.2 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 31  bytes 3472 (3.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 31  bytes 3472 (3.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.71  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fd01::60ff:8818:5965:dc58  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::473d:110e:5474:8000  prefixlen 64  scopeid 0x20<link>
        ether b8:27:eb:a6:a7:d2  txqueuelen 1000  (Ethernet)
        RX packets 955  bytes 74427 (72.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 184  bytes 22096 (21.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

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

任何关于如何实现我的目标的建议都会很高兴收到。

【问题讨论】:

    标签: linux networking raspberry-pi3 dhcp


    【解决方案1】:

    你试过这个吗:https://wiki.debian.org/NetworkConfiguration#Using_DHCP_to_automatically_configure_the_interface 即只需将其添加到 /etc/network/interfaces:

    auto eth0
    allow-hotplug eth0
    iface eth0 inet dhcp
    

    如果你真的很迂腐,你可以创建一个像 /etc/network/interfaces.d/eth0-dhcp 这样的文件并将这些行粘贴到其中 - 最终结果将是相同的。

    【讨论】:

    • 嗨,我试过了 - 它所做的只是完全删除所有接口
    【解决方案2】:

    如果您只是想连接两个设备(没有路由/转发/等),即对于独立的测试网络,这应该可以:

    1. 弄清楚您想要使用的 IP 范围以及您想要使用的 IP 要拥有的 DHCP 服务器(我们称之为 IP_DHCP

    2. 安装isc-dhcp 服务器

    3. 为 eth0 设置静态 IP 地址(例如,将其设置为您的 IP_DHCP 之前选择的)

    4. 为所需的网络范围配置 /etc/dhcp/dhcpd.conf (man dhcpd 有一个不错的参考),使其具有权威性(除非有 是其他 DHCP 服务器)。

    5. 运行service isc-dhcpd-server start

    配置示例:

    sudo apt-get install isc-dhcp-server
    sudo nano /etc/conf/dhcpd.conf
    

    取消注释 #authoritative 使其具有权威性

    按照以下几行添加(根据您在步骤 1 中的决定进行自定义),其中 routersIP_DHCP

    subnet 10.0.0.0 netmask 255.255.255.0 {
      range 10.0.0.1 10.0.0.100;
      option subnet-mask 255.255.255.0;
      option broadcast-address 10.0.0.255;
      option routers 10.0.0.1;
    }
    

    保存缓冲区并关闭

    sudo service isc-dhcpd-server start
    

    祝你好运!

    附:如果您遇到 connect:network not found 问题,那么您的路由表 (route) 设置可能存在问题。

    类似的东西

    sudo route add default gw 10.0.0.1

    IP 地址是您的 eth0 可能会允许通信。

    【讨论】:

      【解决方案3】:

      网络管理器 (nmcli) 可能正在设备上运行并已配置为使用静态 IP。

      以下命令可以帮助您检查您的设备是否由 nmcli 管理:

      nmcli d

      nmcli con 展示

      运行以下命令并将“有线连接 1”替换为您的连接名称。

      nmcli con show '有线连接 1' | grep 'ipv4.method'

      如果设置为 DHCP,ipv4.method 将显示“自动”,如果设置为静态,则显示“手动”。

      http://www.intellamech.com/RaspberryPi-projects/rpi_nmcli.html

      【讨论】:

      • 您好,它不是由 nmcli 管理的。 pi@raspberrypi:~ $ nmcli d bash: nmcli: command not found
      最近更新 更多