最後更新: 2020-06-17
目錄
- ifupdown
- NIC Set IP By DHCP
- 人手設定 IP
- DNS 設定
- ifup 及 ifdown 會 control 的 interface
- Restart Network
- Sub-Interface (Dual IP Address)
- Bringing up an interface without an IP address
- Disable IPv6
- Dummy Bridge
- netplan
ifupdown
安了 ifupdown package 才有 networking.service,
之後才可以透過 /etc/network/interfaces 設定 NIC, IP
它依賴: iproute2
DNS
如果要用 config 檔 ("interfaces") 設定 NIC 的 DNS,
那就要安 resolvconf # systemd-resolved
Notes:
/etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
NIC Set IP By DHCP
設定檔: /etc/network/interfaces
auto eth0
iface eth0 inet dhcp
Remark
auto
-
identify the physical interfaces to be brought up when ifup is run with the -a option
(This option is also used by the system boot scripts)
套用修改過的 settings
ifdown eth0; ifup eth0
人手設定 IP
/etc/network/interfaces
auto eth0 iface eth0 inet static address ?.?.?.? netmask ?.?.?.? network ?.?.?.? broadcast ?.?.?.? gateway ?.?.?.? dns-nameservers ?.?.?.? ?.?.?.?
DNS 設定
/etc/resolv.conf
nameserver n.n.n.n nameserver m.m.m.m
它會受 /etc/network/interfaces 的 "dns-nameservers" 影響
ifup 及 ifdown 會 control 的 interface
# 1 - "auto"
auto lo eth0
相當於
auto lo auto eth0
ifup -a 要會對它們操作
# 2 - "allow-hotplug"
brought up when udev detects them.
auto eth0 allow-hotplug eth1
`ifup --allow=hotplug eth0 eth1` 時, 只有 eth1 會 up,
因為注明了 eth1 是 "allow-hotplug" Group
Restart Network
"/etc/init.d/networking restart" or "invoke-rc.d networking restart" would run:
ifdown -a
then
ifup -a
anything with allow-hotplug won't be bought back up until it detects a hotplug event
P.S.
# 此 CMD 有相同效果
nohup sh -c "ifdown eth0 && ifup eth0"
Sub-Interface (Dual IP Address)
CLI
ifconfig eth0:1 192.168.99.14
Permanent config
/etc/network/interfaces
auto eth0:1 iface eth0:1 inet static address 192.168.99.14 netmask 255.255.255.252 broadcast 192.168.99.15 network 192.168.99.12
ifup eth0:1
Bringing up an interface without an IP address
/etc/network/interfaces
iface eth0 inet manual
pre-up ifconfig $IFACE up
post-down ifconfig $IFACE down
up route add default gw 192.168.18.1
down route delete default gw 191.168.18.1
The manual Method
to define interfaces for which no configuration is done by default.
Such interfaces can be configured manually by means of
- up and down commands
- /etc/network/if-*.d scripts
* 要自行行 ifup 時才會 set route
Bridge without NIC
# Bridge without IP
/etc/network/interfaces
auto vmbr1 iface vmbr1 inet manual bridge_ports none bridge_maxwait 0 bridge_stp off bridge_waitport 0 bridge_fd 0
# Bridge with IP
/etc/network/interfaces
# bridge for lxc
auto lxcbr0
iface lxcbr0 inet static
address 192.168.200.254
netmask 255.255.255.0
bridge_ports none
bridge_stp off
bridge_waitport 0
bridge_fd 0
bridge_maxwait 0
# pre-up, up, down, post-down ...
/etc/network/interfaces
auto vmbr1 iface vmbr1 inet manual pre-up brctl addbr $IFACE up ifconfig $IFACE up; down ifconfig $IFACE down; post-down brctl delbr $IFACE
由於沒有用 bridge_ports, 所以要用 pre-up, up ...
"$IFACE" 相當 NIC vmbr1
# Bridge without IP & Add NIC
/etc/network/interfaces
auto vmbr1
iface vmbr1 inet manual
pre-up brctl addbr $IFACE
up ifconfig $IFACE up; brctl addif $IFACE eth1
down ifconfig $IFACE down; brctl delif $IFACE eth1
post-down brctl delbr $IFACE
netplan
Ubuntu 18.04 LTS 後改用了 netplan, 沒有再用 /etc/network/interface 了
netplan 係一個 yaml 設定檔
舊 Config 方式
如果想用舊方式, 那就要:
apt install ifupdown
netplan config file
/etc/netplan/00-config.yaml
單一 IP
network: ethernets: eth0: addresses: - 192.168.88.35/24 gateway4: 192.168.88.1 nameservers: addresses: [8.8.8.8, 8.8.4.4] dhcp4: no version: 2
Remark: addresses 用單行寫
network: ethernets: eth0: addresses: [192.168.88.35/24] gateway4: 192.168.88.1 nameservers: addresses: [8.8.8.8, 8.8.4.4] dhcp4: no version: 2
多 IP 格式
network:
ethernets:
eth0:
addresses: [192.168.8.11/24, 192.168.88.11/24]
gateway4: 192.168.88.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
dhcp4: no
version: 2
多 NIC
network: ethernets: eth0: addresses: [192.168.8.11/24] gateway4: 192.168.88.1 nameservers: addresses: [8.8.8.8,8.8.4.4] eth1: addresses: [192.168.88.11/24] version: 2
Apply Settings
檢查設定檔格式, 如果正確的話就會套用. 並且要在 120 秒內 Confirm, 否則會自動還原設定!
netplan try
... Do you want to keep these settings? Press ENTER before the timeout to accept the new configuration Changes will revert in 118 seconds Configuration accepted.
直接套用, 不測試設定有沒有問題 !
netplan apply
DHCP
network: version: 2 ethernets: eth0: dhcp4: true dhcp-identifier: mac
有 NIC 沒有 connect 時
network:
ethernets:
...
enp2s0:
optional: true
dhcp4: no
version: 2
An optional device is not required for booting.
Normally, networkd will wait some time for device to become configured
before proceeding with booting.