在Ubuntu20.04中,传统的配置/etc/network/interfaces已无效!需要打开 yaml 文件,在 /etc/netplan 文件夹下,不同机器的文件名不一样。通过编辑yaml文件进行配置(一定注意缩进格式),如下:
network:
ethernets:
ens33: #配置的网卡的名称(不同机器网卡名称不一致,ens33、eth0等等)
addresses: [192.168.16.36/24] #配置的静态ip地址和掩码,(这个地方前面是ip后面的24代表255.255.255.0)
dhcp4: false #关闭DHCP,如果需要打开DHCP则写yes
gateway4: 192.168.16.1 #网关地址
nameservers:
addresses: [202.102.128.68,114.114.114.114] #DNS服务器地址,多个DNS服务器地址需要用英文逗号分隔开
version: 2
注意: 这是修改为静态IP的方式,所以上面的 DHCP4:最好改成 FALSE
,防止自动获取IP,导致IP又变了
Ubuntu 20.04 LTS系统中修改IP地址,将DHCP自动分配的IP地址192.168.1.11
改为静态IP地址192.168.1.100
。
查看IP地址
$ sudo ip addr
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.11 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::4029:99ff:febe:719f prefixlen 64 scopeid 0x20<link>
ether 42:29:99:be:71:9f txqueuelen 1000 (Ethernet)
RX packets 2494 bytes 306105 (306.1 KB)
RX errors 0 dropped 22 overruns 0 frame 0
TX packets 384 bytes 51282 (51.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
查看netplan配置文件
$ sudo cat /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
ethernets:
ens33:
dhcp4: true
version: 2
默认配置是采用DHCP自动获取动态IP。
备份netplan配置文件
$ sudo cp /etc/netplan/00-installer-config.yaml /etc/netplan/00-installer-config_backup.yaml
修改netplan配置文件
$ sudo vim /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
ethernets:
ens33:
dhcp4: false
addresses: [192.168.1.100/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 114.114.114.114]
version: 2
更多netplan
命令参考官方网站。
检测netplan配置
$ sudo netplan try
Warning: Stopping systemd-networkd.service, but it can still be activated by:
systemd-networkd.socket
Do you want to keep these settings?
Press ENTER before the timeout to accept the new configuration
Changes will revert in 117 seconds
Configuration accepted.
- netplan try:应用配置并等待用户确认;如果网络损坏或未给出确认,将回滚。
- netplan apply:应用渲染器的所有配置,必要时重新启动它们。
# 使配置的ip生效
$ sudo netplan apply
再次查看IP地址
$ sudo ip addr
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::4029:99ff:febe:719f prefixlen 64 scopeid 0x20<link>
ether 42:29:99:be:71:9f txqueuelen 1000 (Ethernet)
RX packets 61995 bytes 4987543 (4.9 MB)
RX errors 0 dropped 22 overruns 0 frame 0
TX packets 3769 bytes 2592311 (2.5 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0