Ubuntu 18.04 透過 netplan 設定網路卡 IP

in #linux5 years ago

ubuntu-18.10

這是一篇筆記文,每一個 Linux Distribution 的網路設定都不太一樣,最近裝了 Ubuntu Server 18.04 LTS 版 (Cosmic Cuttlefish),網路設定又變得陌生了,Network Manager 改用 netplan 來管理網路設定,改好之後順便上來紀錄一下,不然下次又忘了。

Ubuntu 18.04 設定 netplan 靜態 IP

先看看原本熟悉的 /etc/network/interface 檔案變成什麼樣子:

# 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

照上面的說明看了一下 /etc/netplan 目錄,查閱一下 /etc/netplan/50-cloud-init.yaml,如下:

# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# 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:
        ens192:
            dhcp4: true
        ens224:
            dhcp4: true
    version: 2

看來可以關閉 cloud network,但是我其實也沒有要用 cloud-init,乾脆移除它,如下:

sudo apt-get remove cloud-init

然後把 /etc/netplan/50-cloud-init.yaml 改成下面這樣:

# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# 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:
        ens192:
            addresses: [192.168.32.231/24]
            gateway4: 192.168.32.1
            nameservers:
              addresses: [8.8.8.8,8.8.4.4]
            dhcp4: no
        ens224:
            dhcp4: true
    version: 2

這幾年 yaml 深得大眾的心,設定檔就是要用 yaml 格式才是潮,解說一下上述幾個設定:

  • dhcp4: 關閉 DHCP 自動取得 IP,原本是 true 所以改成 no (官方文件竟然不是 false 有點搞笑,但我實際測試 false 也是可以 work 的) * addresses: 靜態 IP 與 Mask
  • nameservers: DNS 服務器,可以設定多筆
  • gateway4: IPv4 所使用的 Gateway 修改後存檔後輸入以下命令:
sudo netplan try

這個命令執行後會檢查設定檔格式,如果正確的話就會套用,並且在 120 秒以後自動還原設定! 是不是很貼心?想必各位應該有過把遠端 Linux 網路設定改錯 restart 的痛吧,有了這個設定,至少錯了在兩分鐘以後會還原,不至於讓你的終端機斷線以後就 GG 惹~ 如果測試沒有問題,就可以執行以下命令真正進行設定:

 sudo netplan apply

就是那麼簡單,下台一鞠躬...

Ubuntu 官方參考說明

Configure Static IP Addresses On Ubuntu 18.04 LTS Server

原文:Ubuntu 18.04 透過 netplan 設定網路卡 IP

Sort:  

你那里天气如何?想要参加活动但是不知道从何开始?关注寻宝团@cn-activity每日整理社区活动!假如我的留言打扰到你,请回复“取消”。

Congratulations @samejack! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.27
TRX 0.13
JST 0.032
BTC 63096.57
ETH 2954.05
USDT 1.00
SBD 3.55