Ubuntu Server 18.04 设置为静态 IP 并获取 DHCP 地址

Ubuntu Server 18.04 设置为静态 IP 并获取 DHCP 地址

我遇到了问题建立债券,我现在决定仅在没有粘合的接口上使用。

但现在我有4我的两个接口上有 IP,2x 静态和 2x DHCP。但它配置为只有静态地址。为什么还有 DHCP 地址?所有地址都是可以访问的。

编辑:如果我输入以下命令,一切都很好,但再次启动后 4 个 IP。
- ip 地址刷新 enp2s0
- ip 地址刷新 enp3s0
- netplan 应用

当前配置:

  root@gemini:/etc/netplan# cat 01-netcfg.yaml
        # This file describes the network interfaces available on your system
        # For more information, see netplan(5).
        network:
          version: 2
          renderer: networkd
          ethernets:
            enp3s0:
              dhcp4: false
              dhcp6: false
              addresses: [192.168.88.8/24]
              gateway4: 192.168.88.1
              nameservers:
                search: [lan]
                addresses: [127.0.0.1, 192.168.88.1]

        enp2s0:
          addresses: [192.168.89.8/24]
          dhcp4: false
          dhcp6: false

输出:

root@gemini:/etc/netplan# ip addr
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: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:13:3b:21:bd:35 brd ff:ff:ff:ff:ff:ff
    inet 192.168.89.8/24 brd 192.168.89.255 scope global enp2s0
       valid_lft forever preferred_lft forever
    inet 192.168.88.131/24 brd 192.168.88.255 scope global enp2s0
       valid_lft forever preferred_lft forever
    inet6 2001:16b8:49d:d000:4a0f:31a2:4b9f:34e5/64 scope global dynamic mngtmpaddr noprefixroute
       valid_lft 6775sec preferred_lft 3175sec
    inet6 2001:16b8:49d:d000:213:3bff:fe21:bd35/64 scope global dynamic mngtmpaddr noprefixroute
       valid_lft 6775sec preferred_lft 3175sec
    inet6 fe80::213:3bff:fe21:bd35/64 scope link
       valid_lft forever preferred_lft forever
3: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether d0:50:99:1d:04:46 brd ff:ff:ff:ff:ff:ff
    inet 192.168.88.8/24 brd 192.168.88.255 scope global enp3s0
       valid_lft forever preferred_lft forever
    inet 192.168.88.143/24 brd 192.168.88.255 scope global secondary enp3s0
       valid_lft forever preferred_lft forever
    inet6 2001:16b8:49d:d000:65d1:f7ff:7ccf:111/64 scope global dynamic mngtmpaddr noprefixroute
       valid_lft 6775sec preferred_lft 3175sec
    inet6 2001:16b8:49d:d000:d250:99ff:fe1d:446/64 scope global dynamic mngtmpaddr noprefixroute
       valid_lft 6775sec preferred_lft 3175sec
    inet6 fe80::d250:99ff:fe1d:446/64 scope link
       valid_lft forever preferred_lft forever

在对 enp2s0-lines 添加注释后:

root@gemini:~# ip addr
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: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:13:3b:21:bd:35 brd ff:ff:ff:ff:ff:ff
    inet 192.168.88.131/24 brd 192.168.88.255 scope global enp2s0
       valid_lft forever preferred_lft forever
    inet6 2001:16b8:432:7a00:2aeb:3ee5:3206:4eb4/64 scope global dynamic mngtmpaddr noprefixroute
       valid_lft 7049sec preferred_lft 3449sec
    inet6 fe80::16bb:68ed:8dd0:1ce/64 scope link
       valid_lft forever preferred_lft forever
3: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether d0:50:99:1d:04:46 brd ff:ff:ff:ff:ff:ff
    inet 192.168.88.8/24 brd 192.168.88.255 scope global enp3s0
       valid_lft forever preferred_lft forever
    inet 192.168.88.143/24 brd 192.168.88.255 scope global secondary enp3s0
       valid_lft forever preferred_lft forever
    inet6 2001:16b8:432:7a00:b020:677:85b1:37c8/64 scope global dynamic mngtmpaddr noprefixroute
       valid_lft 7049sec preferred_lft 3449sec
    inet6 2001:16b8:432:7a00:d250:99ff:fe1d:446/64 scope global dynamic mngtmpaddr noprefixroute
       valid_lft 7049sec preferred_lft 3449sec
    inet6 fe80::d250:99ff:fe1d:446/64 scope link
       valid_lft forever preferred_lft forever

..

    root@gemini:~# lshw -C network
      *-network
           description: Ethernet interface
           product: RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller
           vendor: Realtek Semiconductor Co., Ltd.
           physical id: 0
           bus info: pci@0000:02:00.0
           logical name: enp2s0
           version: 06
           serial: 00:13:3b:21:bd:35
           size: 1Gbit/s
           capacity: 1Gbit/s
           width: 64 bits
           clock: 33MHz
           capabilities: pm msi pciexpress msix vpd bus_master cap_list ethernet physical tp mii 10bt 10bt-fd 100bt 100bt-fd 1000bt 1000bt-fd autonegotiation
           configuration: autonegotiation=on broadcast=yes driver=r8169 driverversion=2.3LK-NAPI duplex=full firmware=rtl8168e-3_0.0.4 03/27/12 ip=192.168.88.131 latency=0 link=yes multicast=yes port=MII speed=1Gbit/s
           resources: irq:17 ioport:e000(size=256) memory:d0804000-d0804fff memory:d0800000-d0803fff
      *-network
           description: Ethernet interface
           product: RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller
           vendor: Realtek Semiconductor Co., Ltd.
           physical id: 0
           bus info: pci@0000:03:00.0
           logical name: enp3s0
           version: 11
           serial: d0:50:99:1d:04:46
           size: 1Gbit/s
           capacity: 1Gbit/s
           width: 64 bits
           clock: 33MHz
           capabilities: pm msi pciexpress msix vpd bus_master cap_list ethernet physical tp mii 10bt 10bt-fd 100bt 100bt-fd 1000bt 1000bt-fd autonegotiation
           configuration: autonegotiation=on broadcast=yes driver=r8169 driverversion=2.3LK-NAPI duplex=full firmware=rtl8168g-2_0.0.1 02/06/13 ip=192.168.88.8 latency=0 link=yes multicast=yes port=MII speed=1Gbit/s
           resources: irq:18 ioport:d000(size=256) memory:d0704000-d0704fff memory:d0700000-d0703fff
    root@gemini:~#

..
root@gemini:~# ls -al /etc/netplan
insgesamt 20
drwxr-xr-x   2 root root  4096 Jul 16 21:58 .
drwxr-xr-x 118 root root 12288 Jul 15 22:23 ..
-rw-r--r--   1 root root   457 Jul 16 21:58 01-netcfg.yaml
root@gemini:~#

...

root@gemini:~# cat /etc/network/interfaces
# 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
root@gemini:~#

root@gemini:~# cat /etc/netplan/*
network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
      addresses: [192.168.88.8/24]
      gateway4: 192.168.88.1
      nameservers:
        search: [lan]
        addresses: [192.168.88.1]
root@gemini:~# ip addr
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: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:13:3b:21:bd:35 brd ff:ff:ff:ff:ff:ff
    inet 192.168.88.131/24 brd 192.168.88.255 scope global enp2s0
       valid_lft forever preferred_lft forever
    inet6 2001:16b8:43f:b900:f085:3f0f:356d:c796/64 scope global dynamic mngtmpaddr noprefixroute
       valid_lft 7126sec preferred_lft 3526sec
    inet6 fe80::16bb:68ed:8dd0:1ce/64 scope link
       valid_lft forever preferred_lft forever
3: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether d0:50:99:1d:04:46 brd ff:ff:ff:ff:ff:ff
    inet 192.168.88.8/24 brd 192.168.88.255 scope global enp3s0
       valid_lft forever preferred_lft forever
    inet 192.168.88.143/24 brd 192.168.88.255 scope global secondary enp3s0
       valid_lft forever preferred_lft forever
    inet6 2001:16b8:43f:b900:46ad:aeda:6b62:258a/64 scope global dynamic mngtmpaddr noprefixroute
       valid_lft 7126sec preferred_lft 3526sec
    inet6 2001:16b8:43f:b900:d250:99ff:fe1d:446/64 scope global dynamic mngtmpaddr noprefixroute
       valid_lft 7126sec preferred_lft 3526sec
    inet6 fe80::d250:99ff:fe1d:446/64 scope link
       valid_lft forever preferred_lft forever

答案1

如果您在每个接口上都设置了 dhcp4: false,但仍然通过 DHCP 分配 IP,那么这是来自该文件之外的某个地方。

要么您在 /etc/netplan 中拥有另一个 yaml 文件来覆盖您的 dhcp4:false 设置;要么 netplan 外部的某些东西正在为您调用 dhcp。

检查是否有任何 dhclient 进程正在运行 ( ps awxf| grep dhclient),如果有,找出是什么启动了系统中的这些进程。如果有,这肯定是 networkd 外部的,它不使用 dhclient 来处理其 dhcp 请求。

答案2

暂时设置/etc/netplan/01-netcfg.yaml为这样...

network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
      addresses: [192.168.88.8/24]
      gateway4: 192.168.88.1
      nameservers:
        search: [lan]
        addresses: [192.168.88.1]

sudo netplan --debug generate# 生成配置文件

sudo netplan apply# 应用配置

reboot# 重启计算机

ip addr# 注意,如果 enp3s0 只有 1 个 IPv4 地址(而不是两个)... 并且 enp2s0 根本没有出现...

更新#1:

设置/etc/netplan/01-netcfg.yaml为看起来像这样...(检查地址

network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
      addresses: [192.168.88.8/24]
      gateway4: 192.168.88.1
      nameservers:
        search: [lan]
        addresses: 192.168.88.1
    enp2s0:
      addresses: [192.168.89.8/24]
      gateway4: 192.168.89.1
      nameservers:
        addresses: 192.168.89.1

sudo netplan --debug generate# 生成配置文件

sudo netplan apply# 应用配置

reboot# 重启计算机

相关内容