Linux 網路卡 IP 設定 by netplan
一般來說 Linux 系統都是已經可以上網的狀態, 但是如過你想要修改網路組態, 那麼可以選用以下便利的方法來進行, 這一篇我們透過 netplan 的設定來為原本採用 DHCP 動態取得 ip 環境設定的模式修改為靜態網路(固定 ip)
# 首先我們要找到 netplan 設定檔在哪邊
cd /etc/netplan
# ls 查看目錄內容
ls -l
# 我們會看到有一個檔案是 yamnl 結尾的
50-cloud-init.yaml
# 接下來我們進行編輯
nano 50-cloud-init.yaml
# or
vi 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:
enp0s3:
dhcp4: true
version: 2
==== 原本內容 ====
# 接下來請修改內容為
==== 新內容, 覆蓋舊的內容 ====
network:
ethernets:
enp0s3:
dhcp4: no
addresses:
- 10.40.52.200/24
routes:
- to: default
via: 10.40.52.254
nameservers:
addresses:
- 8.8.8.8
- 168.95.1.1
version: 2
==== 新內容, 覆蓋舊的內容 ====
# 修改完進行存檔, 要注意在你的 ip 後面需要加上 /24 的尾巴喔!! 常有人會忘記這一點!
# 進行設定檔測試, 如果成功就會直接生效新的設定檔, 失敗的話則不會套用
netplan try
or
# 執行以下指令讀取並生效新的設定檔
netplan apply
# 完成設定後, 記得去測試看看你新設定的 ip 是否可以連得上喔!!
可以透過 http://你的ip 看看你 Linux 上的 appache/nginx 之類的 http service 是否可以連上線
或是也可以透過 ssh user@new_ip 試試看可否登入.
這個修改即使你再重新開機後也會生效喔! 所以如果這台 Linux 不在身邊, 請 double check 一下組態設定, 以免生效後發現 server 連不上就糟了!!!