全新安装 Ubuntu 18.04 LTS 后,我按照此处在线提供的说明将 netplan 配置为使用静态 IP 地址:
https://www.tecmint.com/ifconfig-vs-ip-command-comparing-network-configuration/
问题是网关 IP 地址似乎被忽略了。这是 yaml 文件 (/etc/netplan/50-cloud-init.yaml):
network:
version: 2
renderer: networkd
ethernets:
ens160:
dhcp4: no
dhcp6: no
addresses: [172.20.0.109/24]
gateway4: 172.20.3.254
nameservers:
addresses: [172.20.0.5,172.20.0.6]
networkctl status ens160
这是(执行后)的输出sudo netplan apply
,您可以看到网关丢失:
● 2: ens160
Link File: /lib/systemd/network/99-default.link
Network File: /run/systemd/network/10-netplan-ens160.network
Type: ether
State: routable (configured)
Path: pci-0000:03:00.0
Driver: vmxnet3
Vendor: VMware
Model: VMXNET3 Ethernet Controller
HW Address: 00:50:56:a9:bc:e5 (VMware, Inc.)
Address: 172.20.0.109
fe80::250:56ff:fea9:bce5
DNS: 172.20.0.5
172.20.0.6
这是文件网络文件(/run/systemd/network/10-netplan-ens160.network):
[Match]
Name=ens160
[Network]
Address=172.20.0.109/24
Gateway=172.20.3.254
DNS=172.20.0.5
DNS=172.20.0.6
这是输出route -n
:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
172.20.0.0 0.0.0.0 255.255.255.0 U 0 0 0 ens160
答案1
您的网络配置错误,因为网关 IP 地址超出了当前网络 (/24)。
假设网关 IP 地址正确,您可能需要配置 /22 而不是 /24 网络才能到达网关。
查看主机管理和主机管理使用 ipcalc 实用程序查看网络掩码如何影响网络可达性。
$ ipcalc 172.20.0.109/24
Address: 172.20.0.109 10101100.00010100.00000000. 01101101
Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
Wildcard: 0.0.0.255 00000000.00000000.00000000. 11111111
=>
Network: 172.20.0.0/24 10101100.00010100.00000000. 00000000
HostMin: 172.20.0.1 10101100.00010100.00000000. 00000001
HostMax: 172.20.0.254 10101100.00010100.00000000. 11111110
Broadcast: 172.20.0.255 10101100.00010100.00000000. 11111111
Hosts/Net: 254 Class B, Private Internet
$ ipcalc 172.20.0.109/22
Address: 172.20.0.109 10101100.00010100.000000 00.01101101
Netmask: 255.255.252.0 = 22 11111111.11111111.111111 00.00000000
Wildcard: 0.0.3.255 00000000.00000000.000000 11.11111111
=>
Network: 172.20.0.0/22 10101100.00010100.000000 00.00000000
HostMin: 172.20.0.1 10101100.00010100.000000 00.00000001
HostMax: 172.20.3.254 10101100.00010100.000000 11.11111110
Broadcast: 172.20.3.255 10101100.00010100.000000 11.11111111
Hosts/Net: 1022 Class B, Private Internet