Ubuntu 22.04 指令 CLI 設定網路筆記
有圖形介面之後都被寵壞,完全不知道怎麼透過指令設定網路!在 ubuntu 內有 netplan 也有 Network Manager 管理網路,如果沒有好好設定很容易打架。
預設安裝好 ubuntu 後,應該都會有個檔案 /etc/netplan/00-installer-config.yaml
$ cat /etc/netplan/00-installer-config.yaml
network:
ethernets:
eth1:
dhcp4: true
version: 2
只要異動 netplan 組態後就要用 下列指令啟用
$ sudo netplan apply
透過下列設定可以手動指定 ip ,不過記得要把 00-installer-config.yaml 更名別的附檔名。
# mv /etc/netplan/00-installer-config.yaml /etc/netplan/00-installer-config.yaml.orig
# cat /etc/netplan/01-custom-ip-config.yaml
network:
version: 2
renderer: networkd
ethernets:
eth1:
addresses:
- 192.168.10.2/24
nameservers:
search: [liho.tw, otherdomain]
addresses: [8.8.8.8, 8.8.4.4]
routes:
- to: default
via: 192.168.10.1
如果要自訂多筆路由可這樣設定
network:
ethernets:
eth1:
#dhcp4: true
addresses:
- 172.16.0.2/16
routes:
- to: 172.17.0.0/16
via: 172.16.0.1
- to: 172.18.0.0/16
via: 172.16.0.1
這邊會注意到有 renderer 選項 是使用 networkd 理論上應該是使用 systemd-networkd 這個服務。
# systemctl status systemd-networkd
● systemd-networkd.service - Network Configuration
Loaded: loaded (/lib/systemd/system/systemd-networkd.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2022-09-28 17:30:13 CST; 15h ago
TriggeredBy: ● systemd-networkd.socket
Docs: man:systemd-networkd.service(8)
Main PID: 1199 (systemd-network)
Status: "Processing requests..."
Tasks: 1 (limit: 62941)
Memory: 2.7M
CPU: 63ms
CGroup: /system.slice/systemd-networkd.service
└─1199 /lib/systemd/systemd-networkd
Sep 28 17:32:14 lala systemd-networkd[1199]: eth1: Link UP
如果是要換成 NetworkManager 也是可以,只要把設定改成下列的描述
# mv /etc/netplan/01-custom-ip-config.yaml /etc/netplan/01-custom-ip-config.yaml.old
# cat /etc/netplan/01-network-manager-all.yaml
network:
version: 2
renderer: NetworkManager
改完記得要 netplan apply,還有確認好 NetworkManager 是否有正常啟動
# systemctl start NetworkManager
# systemctl enable NetworkManager
# systemctl status NetworkManager
● NetworkManager.service - Network Manager
Loaded: loaded (/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2022-09-28 17:32:14 CST; 15h ago
Docs: man:NetworkManager(8)
Main PID: 1668 (NetworkManager)
Tasks: 3 (limit: 62941)
Memory: 14.6M
CPU: 369ms
CGroup: /system.slice/NetworkManager.service
└─1668 /usr/sbin/NetworkManager --no-daemon
接下來就是透過 nmcli 來設定網路了,用下列方式設定 dhpc 組態
# nmcli con mod eth1 connection.autoconnect yes
# nmcli dev mod eth1 ipv4.method auto
Connection successfully reapplied to device 'eth1'.
# nmcli dev mod eth1 ipv6.method auto
Connection successfully reapplied to device 'eth1'.
檢查一下 eth1 網路狀態
# nmcli device show eth1
GENERAL.DEVICE: eth1
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 3C:EC:EF:xx:xx:xx
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: eth1
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1
WIRED-PROPERTIES.CARRIER: on
IP4.ADDRESS[1]: 192.168.10.105/24
IP4.GATEWAY: 192.168.10.1
IP4.ROUTE[1]: dst = 192.168.10.0/24, nh = 0.0.0.0, mt = 101
IP4.ROUTE[2]: dst = 0.0.0.0/0, nh = 192.168.10.1, mt = 101
IP4.DNS[1]: 8.8.8.8
IP4.DNS[2]: 8.8.4.4
IP4.DOMAIN[1]: liho.tw
IP6.ADDRESS[1]: fe80::f54b:xxxx:xxxx:xxx/64
IP6.GATEWAY: --
IP6.ROUTE[1]: dst = fe80::/64, nh = ::, mt = 1024
如果要 connection reload 可以用下列指令:
# nmcli con reload eth1
要看網路狀態可以用下列指令:
# nmcli dev status
DEVICE TYPE STATE CONNECTION
eth1 ethernet connected eth1
lo loopback unmanaged --
# nmcli con show
NAME UUID TYPE DEVICE
eth1 6a1437da-d884-37cb-b650-ab295639862e ethernet eth1
Wired connection 1 eaee1468-6f87-3efe-bdc1-5f315090bc97 ethernet --
# nmcli con show -a
NAME UUID TYPE DEVICE
eth1 6a1437da-d884-37cb-b650-ab295639862e ethernet eth1
# nmcli -t device
eth1:ethernet:connected:eth1
lo:loopback:unmanaged:
如果要變更 “Wired connection 1” 為別的名字可以用下列指令:
# nmcli connection modify "Wired connection 1" connection.id xxx
# nmcli con show
NAME UUID TYPE DEVICE
eth1 6a1437da-d884-37cb-b650-ab295639862e ethernet eth1
xxx eaee1468-6f87-3efe-bdc1-5f315090bc97 ethernet --
雖然有點不知道 “Wired connection 1” 是什麼設定 XD 只好把它刪除了
# nmcli con del xxx
Connection 'xxx' (eaee1468-6f87-3efe-bdc1-5f315090bc97) successfully deleted.
假設想要手動指定 ip 可以用下列指令:
# nmcli con mod eth1 ipv4.method manual
# nmcli con mod eth1 ipv4.addresses 192.168.10.2/24
# nmcli con mod eth1 ipv4.gateway 192.168.10.1
# nmcli con mod eth1 ipv4.ignore-auto-dns yes
# nmcli con mod eth1 ipv4.dns "8.8.8.8 8.8.4.4"
假設還有其他額外的設定可以參考下面:
# nmcli con mod eth1 ipv6.method ignore
# nmcli con mod eth1 ipv4.never-default yes
# nmcli con mod eth1 ipv6.never-default yes
# nmcli con mod eth1 802-3-ethernet.mtu 9216
# nmcli con mod eth1 ethernet.mtu 9216
# nmcli con mod eth1 ipv4.method auto
# nmcli con mod eth1 ipv6.method auto
# nmcli con down eth1
# nmcli con up eth1
如果有新的網路介面要設定,那就要用 connection add 指令
# nmcli con add type ethernet ifname ens5f0np0 con-name ens5f0np0
Connection 'ens5f0np0' (43771a64-9bc3-45af-8edd-a71288ac98c1)
successfully added.
# nmcli con add type ethernet ifname ens5f1np1 con-name ens5f1np1
Connection 'ens5f1np1' (8627abac-e7e8-45d3-9344-365dc33ec3e5)
successfully added.
最後入果不想要那麼複雜 就用 nmtui 指令 蠻直覺的 XD
# nmtui