Linux

Ubuntu Linux 18.04 設定靜態網路 IP 位址教學

本篇介紹如何在 Ubuntu Linux 18.04 伺服器中設定靜態網路 IP 位址。

netplan 是 Ubuntu Linux 17.10 開始所提供的一個新網路組態指令工具,可以讓管理者更容易管理 Ubuntu Linux 系統的網路設定,其設定檔採用 YAML 語法,底層可以結合 NetworkManager 或 systemd-networkd 來運作(可以在設定檔的 renderers 指定要用哪一個),算是一種高階的網路操作介面。


netplan 會讀取 /etc/netplan 目錄下的 *.yaml 設定檔進行網路的設定,所有的網路設定都會放在這裡。

設定之前先查看一下系統上所有的網路介面:

# 列出所有網路介面
ifconfig -a
eno1: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether e4:43:4b:20:bd:d8  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

eno2: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether e4:43:4b:20:bd:d9  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

eno3: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether e4:43:4b:20:bd:da  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
        device memory 0x9e180000-9e1fffff

eno4: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether e4:43:4b:20:bd:db  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
        device memory 0x9e100000-9e17ffff

確認好網路介面之後,即可開始編輯設定檔。

一般來說在安裝系統時如果有使用到網路,在 /etc/netplan 目錄下就應該會有基本的設定檔,若完全沒有任何設定檔,可以使用以下指令自動產生預設的設定檔:

# 產生網路介面設定檔
sudo netplan generate

打開 /etc/netplan/01-netcfg.yaml 這個網路介面設定檔(或是其他的設定檔亦可),將 IP 位址、網路遮罩、預設閘道、DNS 伺服器等資訊填入其中。

# 網路介面設定檔
network:
  version: 2
  renderer: networkd # 選擇使用 networkd 網路 daemon
  ethernets:
    eno4:                              # 指定網路卡
      addresses: [ 192.168.12.34/24 ]  # IP 位址與網路遮罩
      gateway4: 192.168.12.254         # 預設閘道
      nameservers:
          search: [ your.domain.tw ]      # 搜尋網域
          addresses: [ 8.8.8.8, 8.8.4.4 ] # DNS 伺服器

設定好之後,測試一下新的網路設定是否有問題:

# 測試並套用網路介面設定檔
sudo netplan try

執行這行指令之後,若設定檔語法沒問題,就會套用新的設定,並讓管理者進行確認,如果在 120 秒內沒有進行確認,就會自動恢復成原來的網路設定,這個功能可以避免在遠端更改設定時,不小心把自己檔在外面。

如果在本機直接操作,也可以直接套用新的設定檔:

# 套用網路介面設定檔
sudo netplan apply

查看網路設定:

# 查看網路設定
ifconfig eno4
eno4: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.12.34  netmask 255.255.255.0  broadcast 192.168.12.255
        inet6 2001:e10:2000:17:e643:4bff:fe20:bddb  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::e643:4bff:fe20:bddb  prefixlen 64  scopeid 0x20<link>
        ether e4:43:4b:20:bd:db  txqueuelen 1000  (Ethernet)
        RX packets 14028  bytes 945915 (945.9 KB)
        RX errors 0  dropped 686  overruns 0  frame 0
        TX packets 617  bytes 123948 (123.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device memory 0x9e100000-9e17ffff

關於更詳細的設定檔語法說明,請參考 netplan 的線上手冊:

man netplan

參考資料:TecmintLINUX 技術手札

G. T. Wang

個人使用 Linux 經驗長達十餘年,樂於分享各種自由軟體技術與實作文章。

Recent Posts

光陽 KYMCO GP 125 機車接電發動、更換電瓶記錄

本篇記錄我的光陽 KYMCO ...

1 年 ago

[開箱] YubiKey 5C NFC 實體金鑰

本篇是 YubiKey 5C ...

2 年 ago

[DIY] 自製竹火把

本篇記錄我拿竹子加上過期的苦茶...

2 年 ago