服务器上不需要的 DHCP 设置

服务器上不需要的 DHCP 设置

我正在配置在私有网络上运行的新 Debian 11 服务器。除了我手动配置的静态 IP 之外,它还坚持使用 DHCP 地址。我不明白它从哪里获取 DHCP 地址以及如何关闭它。

开箱即用,它会获取一个 DHCP 地址(我猜它具有网络自动配置功能?);因为我想让它在手动分配的静态 IP 上运行,所以我修改了 /etc/network/interfaces 以配置静态 IP(除了 lo 行):

auto enp0s25
allow-hotplug enp0s25
iface enp0s25 inet static
    address 10.0.0.16/24
    gateway 10.0.0.1
    dns-nameservers 10.0.0.1

我的努力得到了回报,为该设备配置了两个 IP

user@server:~$ ip a
1: lo: ...
2: enp0s25: <BROADCAST,MULTICAST,DYNAMIC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether d0:50:99:5a:08:98 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.16/24 brd 10.0.0.255 scope global enp0s25
       valid_lft forever preferred_lft forever
    inet 10.0.0.190/24 brd 10.0.0.255 scope global secondary enp0s25
       valid_lft forever preferred_lft forever
    inet6 fe80::d250:99ff:fe5a:898/64 scope link 
       valid_lft forever preferred_lft forever

(请注意,此配置在重启后可以稳定地重现)

据我了解,配置网络应该有三种方式:

  • 网络——我正在使用它,因为这是我最习惯的
  • NetworkManager - 未安装,且服务未运行
  • systemd - systemd-networkd 声称已被禁用

那么我还找不到哪些配置?我该如何理清这一团乱麻?

网络方面还存在其他问题(ifdown/ifup 无法正常工作,重新启动网络会抛出错误),但我猜测/希望这与界面控制不明确有关。

编辑:这是与 enp0s25 相关的系统日志输出:

Dec 16 19:37:57 angelo systemd[1]: Started ifup for enp0s25.
Dec 16 19:37:57 angelo kernel: [    1.222564] e1000e 0000:00:19.0 enp0s25: renamed from eth0
Dec 16 19:37:57 angelo kernel: [    2.613911] e1000e 0000:00:19.0 enp0s25: NIC Link is Down
Dec 16 19:37:57 angelo sh[413]: ifup: failed to bring up enp0s25
Dec 16 19:37:57 angelo systemd[1]: [email protected]: Main process exited, code=exited, status=1/FAILURE
Dec 16 19:37:57 angelo systemd[1]: [email protected]: Failed with result 'exit-code'.
Dec 16 19:37:57 angelo connmand[467]: enp0s25 {create} index 2 type 1 <ETHER>
Dec 16 19:37:57 angelo connmand[467]: enp0s25 {update} flags 36866 <DOWN>
Dec 16 19:37:57 angelo connmand[467]: enp0s25 {newlink} index 2 address D0:50:99:5A:08:98 mtu 1500
Dec 16 19:37:57 angelo connmand[467]: enp0s25 {newlink} index 2 operstate 2 <DOWN>
Dec 16 19:37:57 angelo connmand[467]: Adding interface enp0s25 [ ethernet ]
Dec 16 19:37:57 angelo connmand[467]: enp0s25 {update} flags 36867 <UP>
Dec 16 19:37:57 angelo connmand[467]: enp0s25 {newlink} index 2 address D0:50:99:5A:08:98 mtu 1500
Dec 16 19:37:57 angelo connmand[467]: enp0s25 {newlink} index 2 operstate 2 <DOWN>
Dec 16 19:37:58 angelo avahi-daemon[464]: Joining mDNS multicast group on interface enp0s25.IPv4 with address 10.0.0.16.
Dec 16 19:37:58 angelo connmand[467]: enp0s25 {add} address 10.0.0.16/24 label enp0s25 family 2
Dec 16 19:37:58 angelo avahi-daemon[464]: New relevant interface enp0s25.IPv4 for mDNS.
Dec 16 19:37:58 angelo connmand[467]: enp0s25 {add} route 10.0.0.0 gw 0.0.0.0 scope 253 <LINK>
Dec 16 19:37:58 angelo avahi-daemon[464]: Registering new address record for 10.0.0.16 on enp0s25.IPv4.
Dec 16 19:37:58 angelo connmand[467]: enp0s25 {add} route 0.0.0.0 gw 10.0.0.1 scope 0 <UNIVERSE>
Dec 16 19:38:00 angelo connmand[467]: enp0s25 {add} route fe80:: gw :: scope 0 <UNIVERSE>
Dec 16 19:38:00 angelo connmand[467]: enp0s25 {update} flags 102467 <UP,RUNNING,LOWER_UP>
Dec 16 19:38:00 angelo connmand[467]: enp0s25 {newlink} index 2 address D0:50:99:5A:08:98 mtu 1500
Dec 16 19:38:00 angelo connmand[467]: enp0s25 {newlink} index 2 operstate 6 <UP>
Dec 16 19:38:00 angelo kernel: [    5.902126] e1000e 0000:00:19.0 enp0s25: NIC Link is Up 1000 Mbps Full Duplex, Flow Control: Rx/Tx
Dec 16 19:38:00 angelo kernel: [    5.902187] IPv6: ADDRCONF(NETDEV_CHANGE): enp0s25: link becomes ready
Dec 16 19:38:01 angelo avahi-daemon[464]: Joining mDNS multicast group on interface enp0s25.IPv6 with address fe80::d250:99ff:fe5a:898.
Dec 16 19:38:01 angelo avahi-daemon[464]: New relevant interface enp0s25.IPv6 for mDNS.
Dec 16 19:38:01 angelo avahi-daemon[464]: Registering new address record for fe80::d250:99ff:fe5a:898 on enp0s25.*.
Dec 16 19:38:05 angelo avahi-daemon[464]: Registering new address record for 10.0.0.190 on enp0s25.IPv4.
Dec 16 19:38:05 angelo connmand[467]: enp0s25 {add} address 10.0.0.190/24 label enp0s25 family 2
Dec 16 19:38:05 angelo connmand[467]: enp0s25 {add} route 10.0.0.1 gw 0.0.0.0 scope 253 <LINK>
Dec 16 19:38:05 angelo connmand[467]: enp0s25 {add} route 82.165.8.211 gw 10.0.0.1 scope 0 <UNIVERSE>
Dec 16 19:38:09 angelo connmand[467]: enp0s25 {del} route 82.165.8.211 gw 10.0.0.1 scope 0 <UNIVERSE>
Dec 16 19:38:42 angelo connmand[467]: enp0s25 {del} route 0.0.0.0 gw 10.0.0.1 scope 0 <UNIVERSE>

答案1

您说这是私有网络,所以我假设您可以访问路由器。更改路由器上的 DHCP 设置以赋予其静态 IP 地址不会导致不必要的 IP 关联。

  1. 输入ifconfig并查找网关 IP 地址
  2. 使用网关地址在浏览器中登录路由器
  3. 在路由器的 DHCP 设置中,您可以在那里分配静态 IP 关联。它不应该为您提供额外的 IP 地址。

答案2

我假设您正确地检查了其他所有问题,例如您提到的网络管理器未运行。但即便如此,如果您配置/etc/网络/接口正如我在下面展示的,系统应该自动使用该文件而不是网络管理器之类的东西。

我一直发现,即使在运行 dhcp 服务器的网络上,以下方法也能始终为我提供静态 ip 地址。当然,您要确保 ip 地址在 dhcp 服务器分发的块之外,否则会发生冲突。我使用了https://www.rfc-editor.org/rfc/rfc5737.html网络阻塞。

auto enp0s25
iface enp0s25 inet static
        address 192.0.2.10
        netmask 255.255.255.0
        broadcast 192.0.2.255
        network 192.0.2.0
        gateway 192.0.2.1

我宁愿不使用允许热插拔因为(人机界面):

Lines beginning with "allow-" are used to identify interfaces that should 
be brought up automatically by various subsystems. This may be done using    
a command  such as  "ifup  --allow=hotplug eth0 eth1", which will only 
bring up eth0 or eth1 if it is listed in an "allow-hotplug" line.

在您将接口更改为静态之前,接口可能已经有一个 dhcp 分配的 ip 地址。在这种情况下,它可能有 2 个 ip 地址,直到您重新启动。

顺便说一句,这可能不适用于 debian 系统,但以防万一,在 ubuntu 20.04 系统上,当我想要配置接口文件时我发现了这一点:

# ifupdown has been replaced by netplan(5) on this system.  See
# /etc/netplan for current configuration.
# To re-enable ifupdown on this system, you can run:
#    sudo apt install ifupdown

答案3

我卸载了系统connman以解决这个问题。这不是我的首选,但我找不到更好的方法,即通过修改配置文件来让它停止自动 DHCP 的无用操作。 注意:卸载 connman 会断开服务器的网络连接,因此需要重新启动

一些链接让我相信这是最好的镜头:

仍然欢迎非核化、更有针对性的解决方案。

相关内容