1. 程式人生 > 其它 >Ubuntu20.4配置靜態IP

Ubuntu20.4配置靜態IP

前期工作

先看一下IP地址

zhang@admin:~$ ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  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<link>
        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<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        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 使網路配置生效