create account

Ubuntu 18.04 透過 netplan 設定網路卡 IP by samejack

View this thread on: hive.blogpeakd.comecency.com
· @samejack ·
$0.06
Ubuntu 18.04 透過 netplan 設定網路卡 IP
[![ubuntu-18.10](https://blog.toright.com/wp-content/uploads/2019/02/ubuntu-18.10-codename-.jpg)](https://blog.toright.com/?attachment_id=6294) 

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

## 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](https://websiteforstudents.com/configure-static-ip-addresses-on-ubuntu-18-04-beta/)

[原文:Ubuntu 18.04 透過 netplan 設定網路卡 IP](https://blog.toright.com/posts/6293/ubuntu-18-04-%E9%80%8F%E9%81%8E-netplan-%E8%A8%AD%E5%AE%9A%E7%B6%B2%E8%B7%AF%E5%8D%A1-ip.html)
👍  , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorsamejack
permlinkubuntu-18-04-netplan-ip
categorylinux
json_metadata{"tags":["linux","ubuntu","cn","esteem-cn","cn-curation"],"image":["https://blog.toright.com/wp-content/uploads/2019/02/ubuntu-18.10-codename-.jpg"],"links":["https://blog.toright.com/?attachment_id=6294","https://netplan.io/","https://websiteforstudents.com/configure-static-ip-addresses-on-ubuntu-18-04-beta/","https://blog.toright.com/posts/6293/ubuntu-18-04-%E9%80%8F%E9%81%8E-netplan-%E8%A8%AD%E5%AE%9A%E7%B6%B2%E8%B7%AF%E5%8D%A1-ip.html"],"app":"steemit/0.1","format":"markdown"}
created2019-02-23 05:38:30
last_update2019-02-23 05:38:30
depth0
children2
last_payout2019-03-02 05:38:30
cashout_time1969-12-31 23:59:59
total_payout_value0.046 HBD
curator_payout_value0.013 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,627
author_reputation110,019,617,866
root_title"Ubuntu 18.04 透過 netplan 設定網路卡 IP"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id80,271,359
net_rshares105,008,812,894
author_curate_reward""
vote details (21)
@cnbuddy ·
你那里天气如何?想要参加活动但是不知道从何开始?关注寻宝团@cn-activity每日整理社区活动!假如我的留言打扰到你,请回复“取消”。
properties (22)
authorcnbuddy
permlinkre-samejack-ubuntu-18-04-netplan-ip-20190223t060320118z
categorylinux
json_metadata""
created2019-02-23 06:03:21
last_update2019-02-23 06:03:21
depth1
children0
last_payout2019-03-02 06:03:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length69
author_reputation-1,449,160,991,441
root_title"Ubuntu 18.04 透過 netplan 設定網路卡 IP"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id80,272,035
net_rshares0
@steemitboard ·
Congratulations @samejack! You received a personal award!

<table><tr><td>https://steemitimages.com/70x70/http://steemitboard.com/@samejack/birthday1.png</td><td>Happy Birthday! - You are on the Steem blockchain for 1 year!</td></tr></table>

<sub>_You can view [your badges on your Steem Board](https://steemitboard.com/@samejack) and compare to others on the [Steem Ranking](https://steemitboard.com/ranking/index.php?name=samejack)_</sub>


###### [Vote for @Steemitboard as a witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1) to get one more award and increased upvotes!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-samejack-20191101t102527000z
categorylinux
json_metadata{"image":["https://steemitboard.com/img/notify.png"]}
created2019-11-01 10:25:27
last_update2019-11-01 10:25:27
depth1
children0
last_payout2019-11-08 10:25:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length619
author_reputation38,975,615,169,260
root_title"Ubuntu 18.04 透過 netplan 設定網路卡 IP"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id92,099,163
net_rshares0