在树莓派中将eth0设置为DHCP

时间:2019-04-01 13:53:04

标签: linux networking raspberry-pi3 dhcp

我想将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

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

3 个答案:

答案 0 :(得分:1)

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

  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-dhcpd-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 可能会允许通信。

答案 1 :(得分:0)

您是否尝试过此操作: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)

NetWork Manager(nmcli)可能正在设备上运行,并且已配置为使用静态IP。

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

nmcli d

nmcli con show

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

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

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

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