Networking

netplan - 配置了 DHCP 的網路,但沒有網際網路

  • November 28, 2021

我正在嘗試將我的 Ubuntu Server 20.04 LTS 配置為在其網路配置中使用 DHCP。

網上很多教程都推薦我用下面的設置創建下面的文件…

root@sinj:/home/brlight# cat /etc/netplan/99_config.yaml
network:
 version: 2
 renderer: networkd
 ethernets:
   ens3:
     dhcp4: true

…並執行以下命令…

root@sinj:/home/brlight# netplan --debug generate
root@sinj:/home/brlight# netplan apply

**問題:**伺服器正在通過 DHCP 獲取其設置,但無法訪問 Internet。


加:

與同一網路上的其他伺服器不同,該伺服器似乎無法獲取網關設置。

伺服器沒問題…

[root@ssh_brl ~]# ip route show
default via 10.0.0.1 dev eth0 proto dhcp metric 100 
10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.6 metric 100 
10.2.0.0/24 via 10.0.0.7 dev eth0 proto dhcp metric 100

伺服器有問題…

root@sinj:/home/brlight# ip route show
10.0.0.0/24 dev ens3 proto kernel scope link src 10.0.0.17 
10.2.0.0/24 via 10.0.0.7 dev ens3 proto dhcp src 10.0.0.17 metric 100 

其他資訊:

一世

root@sinj:/home/brlight# ip route show
10.0.0.0/24 dev ens3 proto kernel scope link src 10.0.0.17 
10.2.0.0/24 via 10.0.0.7 dev ens3 proto dhcp src 10.0.0.17 metric 100 

root@sinj:/home/brlight# lshw -C network
 *-network                 
      description: Ethernet controller
      product: Virtio network device
      vendor: Red Hat, Inc.
      physical id: 3
      bus info: pci@0000:00:03.0
      version: 00
      width: 32 bits
      clock: 33MHz
      capabilities: msix bus_master cap_list rom
      configuration: driver=virtio-pci latency=0
      resources: irq:11 ioport:c060(size=32) memory:fc056000-fc056fff memory:fc000000-fc03ffff
    *-virtio0
         description: Ethernet interface
         physical id: 0
         bus info: virtio@0
         logical name: ens3
         serial: 52:54:00:6c:ec:1f
         capabilities: ethernet physical
         configuration: autonegotiation=off broadcast=yes driver=virtio_net driverversion=1.0.0 ip=10.0.0.17 link=yes multicast=yes

root@sinj:/home/brlight# networkctl status ens3
● 2: ens3                                                              
            Link File: /usr/lib/systemd/network/99-default.link       
         Network File: /run/systemd/network/10-netplan-ens3.network   
                 Type: ether                                          
                State: routable (configured)
                 Path: pci-0000:00:03.0                               
               Driver: virtio_net                                     
               Vendor: Red Hat, Inc.                                  
                Model: Virtio network device                          
           HW Address: 52:54:00:6c:ec:1f                              
                  MTU: 1500 (min: 68, max: 65535)                     
 Queue Length (Tx/Rx): 1/1                                            
     Auto negotiation: no                                             
                Speed: n/a                                            
              Address: 10.0.0.17 (DHCP4)                              
                       fe80::5054:ff:fe6c:ec1f                        
                  DNS: 10.0.0.1                                       

Mar 06 01:47:12 sinj systemd-networkd[641]: ens3: IPv6 successfully enabled
Mar 06 01:47:12 sinj systemd-networkd[641]: ens3: Link UP
Mar 06 01:47:12 sinj systemd-networkd[641]: ens3: Gained carrier
Mar 06 01:47:12 sinj systemd-networkd[641]: ens3: DHCPv4 address 10.0.0.17/24 via 10.0.0.1
Mar 06 01:47:13 sinj systemd-networkd[641]: ens3: Gained IPv6LL

root@sinj:/home/brlight# dig www.google.com

; <<>> DiG 9.16.1-Ubuntu <<>> www.google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 10647
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;www.google.com.                        IN      A

;; ANSWER SECTION:
www.google.com.         41      IN      A       172.217.30.36

;; Query time: 48 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sat Mar 06 04:37:04 UTC 2021
;; MSG SIZE  rcvd: 59

root@sinj:/home/brlight# dhclient -v ens3
Internet Systems Consortium DHCP Client 4.4.1
Copyright 2004-2018 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/ens3/52:54:00:6c:ec:1f
Sending on   LPF/ens3/52:54:00:6c:ec:1f
Sending on   Socket/fallback
DHCPREQUEST for 10.0.0.17 on ens3 to 255.255.255.255 port 67 (xid=0x601d9dc1)
DHCPACK of 10.0.0.17 from 10.0.0.5 (xid=0xc19d1d60)
RTNETLINK answers: File exists
bound to 10.0.0.17 -- renewal in 933 seconds.

我們

root@sinj:/home/brlight# cat /var/lib/dhcp/dhclient.leases
lease {
 interface "ens3";
 fixed-address 10.0.0.17;
 option subnet-mask 255.255.255.0;
 option dhcp-lease-time 4000;
 option routers 10.0.0.1;
 option dhcp-message-type 5;
 option dhcp-server-identifier 10.0.0.5;
 option domain-name-servers 10.0.0.1;
 option dhcp-renewal-time 1000;
 option rfc3442-classless-static-routes 24,10,2,0,10,0,0,7;
 option dhcp-rebinding-time 2000;
 option host-name "sinj";
 renew 6 2021/03/06 05:20:33;
 rebind 6 2021/03/06 05:38:40;
 expire 6 2021/03/06 06:12:00;
}
lease {
 interface "ens3";
 fixed-address 10.0.0.17;
 option subnet-mask 255.255.255.0;
 option dhcp-lease-time 4000;
 option routers 10.0.0.1;
 option dhcp-message-type 5;
 option dhcp-server-identifier 10.0.0.5;
 option domain-name-servers 10.0.0.1;
 option dhcp-renewal-time 1000;
 option rfc3442-classless-static-routes 24,10,2,0,10,0,0,7;
 option dhcp-rebinding-time 2000;
 option host-name "sinj";
 renew 6 2021/03/06 18:50:24;
 rebind 6 2021/03/06 19:10:57;
 expire 6 2021/03/06 19:44:17;
}

root@sinj:/home/brlight# ip route
10.0.0.0/24 dev ens3 proto kernel scope link src 10.0.0.17 
10.2.0.0/24 via 10.0.0.7 dev ens3 
10.2.0.0/24 via 10.0.0.7 dev ens3 proto dhcp src 10.0.0.17 metric 100

[參考:https://ubuntu.com/server/docs/network-configuration,http://manpages.ubuntu.com/manpages/cosmic/man5/netplan.5.html,https://www.krizna。 com/ubuntu/setup-network-ubuntu-18-04/,https://websiteforstudents.com/configure-static-ip-addresses-on-ubuntu-18-04-beta/,https://www.linuxtechi。 com/assign-static-ip-address-ubuntu-20-04-lts/]

抱歉,這比你發布的晚了 8 個月——但我遇到了同樣的問題,正在Google上搜尋,看看其他人是否已經解決了。

幾個月前,我正在嘗試使用wireguard,並在兩所房子之間建立了一個私人VPN——一切都很好,所以我修改了我的DHCP伺服器配置,通過isc-dhcp-server將靜態路由(到另一所房子)推送出去

這工作正常,除了Ubuntu 18.04 和 20.04 虛擬機現在都不會設置預設路由。

如果您從 DHCP 伺服器 conf 中註釋掉任何選項 rfc3442-classless-static-routes 選項 ms-classless-static-routes 並重新啟動,然後 netplan 應用 - 您應該會發現預設路由回來了。

與他們重複 - 預設路線消失。

netplan ip 租賃 ens18 - 確實表明它知道預設路由 - 它只是從不配置它。

RFC3442 說:

許多客戶端可能沒有實現無類靜態路由選項。因此,DHCP 伺服器管理員應將其 DHCP

伺服器配置為發送路由器選項和無類靜態路由

選項,並應在路由器選項和無類靜態路由選項中指定預設路由器

因為之前它說:

如果 DHCP 伺服器返回無類靜態路由選項和路由器選項,則 DHCP 客戶端必須忽略路由器選項。

所以 DHCP 客戶端正在做正確的事情,而不是設置預設路由 - 因為您使用的是選項 rfc3442-classless-static-routes

解決方案是在預設路由前面加上(在你的情況下)0、10、0、0、1,

在我的網路上是:

option rfc3442-classless-static-routes 0, 172, 16, 0, 1, 22, 172, 16, 4, 172, 16, 0, 250;

這意味著預設路由是 172.16.0.1 並且可以通過 172.16.0.250 訪問 172.16.4/22 網路

在應用網路計劃後的客戶端上:

# ip route show
default via 172.16.0.1 dev ens18 proto dhcp src 172.16.0.220 metric 100
172.16.0.0/22 dev ens18 proto kernel scope link src 172.16.0.220
172.16.4.0/22 via 172.16.0.250 dev ens18 proto dhcp src 172.16.0.220 metric 100

希望這對一直在搜尋並可能遇到此答案的其他人有所幫助。

引用自:https://serverfault.com/questions/1056147