Ubuntu20.4配置静态IP


前期工作

先看一下IP地址

zhang@admin:~$ ifconfig
enp0s3: flags=4163  mtu 1500
        inet 192.168.1.9  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::79c1:25e8:5f05:4ebf  prefixlen 64  scopeid 0x20
        ether 08:00:27:4a:f2:9c  txqueuelen 1000  (以太网)
        RX packets 103  bytes 24704 (24.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 156  bytes 16663 (16.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (本地环回)
        RX packets 145  bytes 11983 (11.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 145  bytes 11983 (11.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

正文开始

sudo vim /etc/netplan/00-installer-config.yaml创建配置文件,输入以下内容

enp0s3网卡名称要认准

network:
  ethernets:
    enp0s3:     #配置的网卡的名称
      addresses: [192.168.1.138/24]    #配置的静态ip地址和掩码
      dhcp4: no    #关闭DHCP,如果需要打开DHCP则写yes
      optional: true
      gateway4: 192.168.0.1    #网关地址
      nameservers:
         addresses: [192.168.1.1,114.114.114.114]    #DNS服务器地址,多个DNS服务器地址需要用英文逗号分隔开
  version: 2
  renderer: networkd    #指定后端采用systemd-networkd或者Network Manager,可不填写则默认使用systemd-workd

sudo netplan apply 使网络配置生效