Ubuntu 从 17.10 开始,已放弃在 /etc/network/interfaces 里固定 IP 的配置,interfaces 文件不复存在,即使配置也不会生效,而是改成 netplan 方式 ,配置写在 /etc/netplan/01-netcfg.yaml ,
50-cloud-init.yaml
或者类似名称的 yaml 文件里。
1. 查看网卡设备号
ip address
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,如果需要启动则写yesoptional: trueaddresses: # 配置静态IP地址和掩码- 192.168.31.173/24 gateway4: 192.168.16.1 # 设置网关地址 nameservers:addresses: [202.102.152.3, 114.114.114.114] # 设置主、备DNS,多个DNS服务地址需要用英文逗号隔开version: 2renderer: networkd #指定后端采用systemd-networkd或者NetworkManager,可不填写则默认使用systemd-workd
根据自己的需要配置好之后保存文件
3. 应用配置
sudo netplan apply
4. 确认IP地址已经更改
ip addr show
连接主机...
连接主机成功
Last login: Wed Jul 24 12:42:56 2024 from 192.168.31.172
root@ubuntu:~# cd /etc/netplan/
root@ubuntu:/etc/netplan# sudo netplan apply
root@ubuntu:/etc/netplan# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000link/ether dc:a6:32:c5:41:01 brd ff:ff:ff:ff:ff:ffinet 192.168.31.173/24 brd 192.168.31.255 scope global eth0valid_lft forever preferred_lft foreverinet6 fe80::dea6:32ff:fec5:4101/64 scope link valid_lft forever preferred_lft forever
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether dc:a6:32:c5:41:02 brd ff:ff:ff:ff:ff:ffinet 192.168.137.208/24 brd 192.168.137.255 scope global dynamic noprefixroute wlan0valid_lft 604416sec preferred_lft 604416secinet6 fe80::21a0:fa53:db59:10cf/64 scope link noprefixroute valid_lft forever preferred_lft forever
root@ubuntu:/etc/netplan#
确保你的Ubuntu系统没有通过DHCP获取IP地址,并且网络接口配置正确。如果你有多个网络接口,请确保只为你想要配置静态IP的接口设置静态地址。