2020年3月17日 星期二

Ubuntu 18.04 網路設定


Ubuntu 18.04 網路設定檔異動
在 Ubuntu 14.04 時代,網路介面的設定檔在 /etc/network/interfaces,但在 18.04 以後改用了 netplan 來做網路介面的管理,設定檔也跟著改變。

打開 /etc/network/interfaces 後,你會看到以下的說明:
# ifupdown has been replaced by netplan(5) on this system.  See
# /etc/netplan for current configuration.
# To re-enable ifupdown on this system, you can run:
#    sudo apt install ifupdown

若要修改網路設定檔,大至上要二個步驟:
確認網路介面的 ID (Ubuntu 18.04 的網路介面代號與 14.04 不同)
修改網路設定檔:/etc/netplan/50-cloud-init.yaml


確認網路介面的 ID

舊版的網路卡 ID 會是「eth0」這樣的表示,不過在 Ubuntu 18.04 代號有變動,記得先使用 ifconfig 確認:
# ifconfig -a    or    # networkctl -a

    只用 ifconfig 只會看到現在既有啟用的設定,對於新安裝的網卡因為沒設定過所以沒資訊,需要用ifconfig -a or networkctl -a 才看的到
    # ifconfig
    enp0s31f6: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 10.10.8.33  netmask 255.255.255.0  broadcast 10.10.8.255
            inet6 fe80::428d:5cff:feb4:af78  prefixlen 64  scopeid 0x20<link>
            ether 40:8d:5c:b4:af:78  txqueuelen 1000  (Ethernet)
            RX packets 10194  bytes 1009214 (1.0 MB)
            RX errors 0  dropped 1186  overruns 0  frame 0
            TX packets 2796  bytes 904975 (904.9 KB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
            device interrupt 16  memory 0xed400000-ed420000
 
    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  (Local Loopback)
            RX packets 94  bytes 7028 (7.0 KB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 94  bytes 7028 (7.0 KB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 
    # ifconfig -a
    enp0s31f6: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 10.10.8.33  netmask 255.255.255.0  broadcast 10.10.8.255
            inet6 fe80::428d:5cff:feb4:af78  prefixlen 64  scopeid 0x20<link>
            ether 40:8d:5c:b4:af:78  txqueuelen 1000  (Ethernet)
            RX packets 503  bytes 61885 (61.8 KB)
            RX errors 0  dropped 40  overruns 0  frame 0
            TX packets 239  bytes 47864 (47.8 KB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
            device interrupt 16  memory 0xed400000-ed420000
 
    enp2s0f0: flags=4098<BROADCAST,MULTICAST>  mtu 1500
            ether 3c:fd:fe:69:29:c8  txqueuelen 1000  (Ethernet)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 
    enp2s0f1: flags=4098<BROADCAST,MULTICAST>  mtu 1500
            ether 3c:fd:fe:69:29:c9  txqueuelen 1000  (Ethernet)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 
    enp2s0f2: flags=4098<BROADCAST,MULTICAST>  mtu 1500
            ether 3c:fd:fe:69:29:ca  txqueuelen 1000  (Ethernet)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 
    enp2s0f3: flags=4098<BROADCAST,MULTICAST>  mtu 1500
            ether 3c:fd:fe:69:29:cb  txqueuelen 1000  (Ethernet)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            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  (Local Loopback)
            RX packets 88  bytes 6632 (6.6 KB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 88  bytes 6632 (6.6 KB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

     
    # networkctl
    IDX LINK             TYPE               OPERATIONAL SETUP   
      1 lo               loopback           carrier     unmanaged
      2 enp2s0f0         ether              off         unmanaged
      3 enp2s0f1         ether              off         unmanaged
      4 enp2s0f2         ether              off         unmanaged
      5 enp2s0f3         ether              off         unmanaged
      6 enp0s31f6       ether              routable    configured



** 修改網路設定檔

Ubuntu 18.04 使用的 netplan 設定檔位於 /etc/netplan/*.yaml,如果是新安裝好的機器,應該可以看到「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:
        enp0s31f6:
            dhcp4: true
    version: 2

更改每個都啟用
# 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:
        enp0s31f6:
            dhcp4: true
        enp2s0f0:
            dhcp4: true
        enp2s0f1:
            dhcp4: true
        enp2s0f2:
            dhcp4: true
        enp2s0f3:
            dhcp4: true
    version: 2

YAML 的檔案格式可以參考 netplan.io 的說明。

假設要改成 static IP,則設定檔的大概會改成以下這個樣子:
network:
    ethernets:
        enp0s31f6:
            addresses: [192.168.0.10/24]
            gateway4: 192.168.0.1
            nameservers:
                addresses: [8.8.8.8, 1.1.1.1]
            dhcp4: false
    version: 2

    ** dhcp4: 關閉 DHCP 自動取得 IP,原本是 true 所以改成 no or false
    ** addresses: 靜態 IP 與 Mask
    ** nameservers: DNS 服務器,可以設定多筆
    ** gateway4: IPv4 所使用的 Gateway

設定檔修改以後不會立即生效,需要呼叫 netplan 讀取並套用新的設定:
# sudo netplan apply

沒有留言:

張貼留言