Debian - Networking

最後更新: 2020-06-17

目錄

  • ifupdown
  • NIC Get IP By DHCP
  • NIC Set Fix 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

 

安了它才有 networking.service,

之後才可以透過 /etc/network/interfaces 設定 Fix IP

依賴: iproute2

如果要用 config 檔設("interfaces")定 NIC, 那就要安 resolvconf # systemd-resolved

/etc/resolv.conf

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)

 


NIC Get IP By DHCP

 

/etc/network/interfaces

auto eth0
iface eth0 inet dhcp

 


NIC Set Fix IP 設定

 

/etc/network/interfaces

auto eth0
iface eth0 inet static
    address    ?.?.?.?
    netmask    ?.?.?.?
    network    ?.?.?.?
    broadcast  ?.?.?.?
    gateway    ?.?.?.?
    dns-nameservers ?.?.?.? ?.?.?.?

Remark

"auto" - physical interfaces to be brought up when "ifup -a" (system boot scripts)

# 套用 setting

ifdown eth0; ifup eth0

 

 


DNS 設定

 

/etc/resolv.conf

nameserver n.n.n.n
nameserver m.m.m.m

它會受 /etc/network/interfaces 的 "dns-nameservers" 影響

 


ifup 及 ifdown 會 control 的 interface

 

auto lo eth0   <--- ifup -a 要會對它們操作

相當於

auto lo
auto eth0

allow-hotplug eth1      <--- ifup --allow=hotplug  eth0  eth1 時, 只有 eth1 會 up

 


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

 


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.