Ubuntu 22.04 網路設定 (靜態IP)

Ubuntu 22.04 Static IP 的網路設定方法與 18.04 和 20.04 相同,一樣要編輯 /etc/netplan/00-installer-config.yaml

$ sudo nano /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
  version: 2
  ethernets:
    ens33: # 要設定的網卡名稱
      dhcp4: false
      addresses: [192.168.0.10/24] # 輸入要設定的 IP
      gateway4: 192.168.0.1 # 輸入 Gateway (大多數都是 .1)
      nameservers:
        addresses: [1.1.1.1,8.8.8.8]

使用 sudo netplan apply 命令讓網路進行生效

$ sudo netplan apply

這時會跳出以下警告訊息。

** (generate:1319): WARNING **: 09:05:28.876: `gateway4` has been deprecated, use default routes instead.
See the 'Default routes' section of the documentation for more details.

** (process:1317): WARNING **: 09:05:29.584: `gateway4` has been deprecated, use default routes instead.
See the 'Default routes' section of the documentation for more details.

查看訊息後才知道在 Ubuntu 22.04 中原本的 gateway4 的將要被替換掉,雖然該警告訊息不會影響網路的使用,但是在設定的時候出現一串訊息還是不免會讓人害怕,為了要避免警告訊息的出現,我們需要修改 Gateway4,因為 22.04 已經替換為 route。

修改完成如下:

# This is the network config written by 'subiquity'
network:
  version: 2
  ethernets:
    ens33:
      dhcp4: false
      addresses: [192.168.0.50/24]
      routes:
      - to: default
        via: 192.168.0.1
      nameservers:
        addresses: [1.1.1.1,8.8.8.8]
       

再次使用命令讓新設定的 IP 生效。

$ sudo netplan apply

執行後就完全不會跳出警告訊息了