1. 查看网卡设备号
ip address
2: enp8s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 00:e0:4c:b6:03:c5 brd ff:ff:ff:ff:ff:ff
3: enp9s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:e0:4c:b6:03:c6 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.174/24 brd 192.168.1.255 scope global enp9s0
valid_lft forever preferred_lft forever
inet6 fe80::2e0:4cff:feb6:3c6/64 scope link
valid_lft forever preferred_lft forever
2. 修改 YAML 文件
sudo vim /etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
ethernets:
eth0: # 配置网卡的名称
dhcp4: no # 禁用dhcp,如果需要启动则写yes
optional: true
addresses: # 配置静态IP地址和掩码
- 192.168.1.173/24
gateway4: 192.168.1.1 # 设置网关地址
nameservers:
addresses: [202.102.152.3, 114.114.114.114] # 设置主、备DNS,多个DNS服务地址需要用英文逗号隔开
version: 2
3. 应用配置
sudo netplan apply
4. 确认IP地址已经更改
ip addr show
必须修改 /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
增加network: {config: disabled}
否则重启后/etc/netplan/50-cloud-init.yaml配置不生效