章 5. 網絡設置

內容目錄

5.1. 基本網絡架構
5.1.1. 主機名解析
5.1.2. 網路介面名稱
5.1.3. 區域網網路地址範圍
5.1.4. 網路裝置支援
5.2. 現代的桌面網路調配
5.2.1. 圖形介面的網路調配工具
5.3. 沒有影象介面的現代網路配置
5.4. The modern network configuration for cloud
5.4.1. The modern network configuration for cloud with DHCP
5.4.2. The modern network configuration for cloud with static IP
5.4.3. The modern network configuration for cloud with Network Manger
5.5. 底層網路調配
5.5.1. Iproute2 指令
5.5.2. 安全的底層網路操作
5.6. 網路最佳化
5.6.1. 找出最佳 MTU
5.6.2. WAN TCP 最佳化
5.7. Netfilter 網路過濾框架
[提示] 提示

關於Debian專屬的網絡手冊,請查看Debian管理員手冊—網絡調配

[提示] 提示

systemd環境下,可以用networkd來調配網絡。請參考systemd-networkd(8)。

讓我們來回顧一下現代Debian作業系統中的基本網絡架構。

表格 5.1. 網絡調配工具一覽表

軟體包 流行度 大小 類型 說明
network-manager V:391, I:455 15414 調配::NM NetworkManager(守衛行程):自動管理網絡
network-manager-gnome V:121, I:368 5583 調配::NM NetworkManager(GNOME前端)
netplan.io V:1, I:5 249 config::NM+networkd Netplan (generator): Unified, declarative interface to NetworkManager and systemd-networkd backends
ifupdown V:591, I:980 199 調配::ifupdown 用來啓動/關閉網絡的標準工具(Debian特有)
isc-dhcp-client V:217, I:980 2866 調配::底層 DHCP客戶端
pppoeconf V:0, I:5 186 調配::輔助 調配助手,以便於使用PPPoE連接
wpasupplicant V:346, I:509 3862 調配::輔助 WPA和WPA2客戶端支援(IEEE 802.11i)
wpagui V:0, I:1 774 調配::輔助 wpa_supplicant Qt 圖形界面客戶端
wireless-tools V:175, I:241 292 調配::輔助 操控Linux無線擴展的工具
iw V:35, I:473 302 調配::輔助 配置 Linux 無線裝置的工具
iproute2 V:726, I:970 3597 調配::iproute2 iproute2, IPv6和其他高級網絡調配:ip(8),tc(8)等等
iptables V:314, I:741 2414 調配::Netfilter 封包過濾和網絡地址轉換管理工具(Netfilter
nftables V:100, I:675 182 調配::Netfilter administration tools for packet filtering and NAT (Netfilter) (successor to {ip,ip6,arp,eb}tables)
iputils-ping V:201, I:997 120 測試 測試能否連接遠程主機,通過主機名IP 地址iproute2
iputils-arping V:3, I:39 49 測試 測試能否連接遠程主機,通過ARP地址
iputils-tracepath V:2, I:31 45 測試 跟蹤存取遠程主機的路徑
ethtool V:94, I:268 739 測試 顯示或更改以太網設備的設定
mtr-tiny V:5, I:47 156 測試::底層 追蹤連接遠程主機的路徑(文本界面)
mtr V:5, I:42 209 測試::底層 追蹤連接遠程主機的路徑(文本界面和GTK界面)
gnome-nettool V:0, I:18 2492 測試::底層 獲得常見網絡資訊的工具(GNOME)
nmap V:26, I:202 4498 測試::底層 網絡映射/端口掃描(Nmap,控制檯)
tcpdump V:17, I:178 1340 測試::底層 網絡流量分析(Tcpdump,控制檯)
wireshark I:46 10514 測試::底層 網絡流量分析(Wireshark,GTK)
tshark V:2, I:25 501 測試::底層 網絡流量分析(控制檯)
tcptrace V:0, I:2 401 測試::底層 根據tcpdump的輸出生成的連接數據統計
snort V:0, I:0 2203 測試::底層 靈活的網絡入侵偵測系統(Snort
ntopng V:0, I:1 15904 測試::底層 在網頁瀏覽器中展示網絡流量
dnsutils V:18, I:293 272 測試::底層 BIND軟體包提供的網絡客戶端程序:nslookup(8),nsupdate(8),dig(8)
dlint V:0, I:3 53 測試::底層 利用域名服務器查詢來查看DNS域資訊
dnstracer V:0, I:1 59 測試::底層 跟蹤DNS查詢直至源頭

主機名解析,目前也是由 NSS (名字服務轉換 Name Service Switch) 機制來支援。這個解析的流程如下。

  1. "/etc/nsswitch.conf" 檔案裡的 "hosts: files dns" 這段規定主機名解析順序。 (代替 "/etc/host.conf" 檔案裡的"order" 這段原有的功能。)

  2. files 方式首先被呼叫。如果主機名在 "/etc/hosts" 檔案裡面發現,則回傳所有有效地址並退出。 ( "/etc/host.conf" 檔案包含 "multi on".)

  3. dns 方式被呼叫。如果主機名通過查詢 "/etc/resolv.conf" 檔案裡面寫的 網際網路域名系統 Domain Name System (DNS) 來找到,則回傳所有有效地址並退出。

A typical workstation may be installed with its host name set to, e.g., "host_name" and its optional domain name set to an empty string. Then, "/etc/hosts" looks like the following.

127.0.0.1 localhost
127.0.1.1 host_name

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

每一行由 IP 地址 開始,接下來是相關聯的主機名.

在這個例子的第二行 127.0.1.1 IP 地址也許不會在其它類 Unix 系統發現。Debian Installer 為沒有永久 IP 地址的系統建立這個條目,作為某些軟體(如 GNOME)的一個變通方法,見文件 bug #719621.

The host_name matches the hostname defined in the "/etc/hostname" (see 節 3.7.1, “主機名”).

對於有永久 IP 地址的系統,這個永久 IP 地址應當代替這裡的 127.0.1.1

對於有永久 IP 地址和有 域名系統 Domain Name System (DNS)提供完全資格域名 fully qualified domain name (FQDN) 的系統,規範名 host_name.domain_name 應當被用來代替 host_name.

如果 resolvconf 軟體包沒有安裝,"/etc/resolv.conf" 是一個靜態檔案。如果安裝了,它是一個符號連結。此外,它包含有解析策略的初始化資訊。如 DNS 是 IP="192.168.11.1",則包含如下。

nameserver 192.168.11.1

resolvconf 軟體包使這個 "/etc/resolv.conf" 檔案成為一個符號連結,並通過鉤子指令碼自動管理其內容。

For the PC workstation on the typical adhoc LAN environment, the hostname can be resolved via Multicast DNS (mDNS) in addition to the basic files and dns methods.

  • Avahi 提供 Debian 下的組播 DNS 發現框架。

  • 它和 Apple Bonjour / Apple Rendezvous 相當.

  • libnss-mdns 外掛包提供 mDNS 的主機名解析,GNU C 庫 (glibc)的 GNU 名字服務轉換 Name Service Switch (NSS) 功能支援 mDNS。

  • The "/etc/nsswitch.conf" file should have stanza like "hosts: files mdns4_minimal [NOTFOUND=return] dns" (see /usr/share/doc/libnss-mdns/README.Debian for other configurations).

  • A host name suffixed with the ".local" pseudo-top-level domain is resolved by sending a mDNS query message in a multicast UDP packet using IPv4 address "224.0.0.251" or IPv6 address "FF02::FB".

[注意] 注意

域名系統 Domain Name System 中的擴充通用頂級域名 expansion of generic Top-Level Domains (gTLD) 還在進行中。在區域網內,選擇一個域名時,請提防名字衝突 name collision

[注意] 注意

Use of packages such as libnss-resolve together with systemd-resolved, or libnss-myhostname, or libnss-mymachine, with coressponding listings on the "hosts" line in the "/etc/nsswitch.conf" file may override the traditional network configuration discussed in the above. See nss-resolve(8), systemd-resolved(8), nss-myhostname(8), and nss-mymachines(8) for more.

對於使用 systemd 的現代 Debian 桌面系統,網路介面通常由兩個服務進行初始化:lo 介面通常在“networking.service”處理,而其它介面則由“NetworkManager.service”處理。

Debian 可以透過後臺守護程序(daemon)管理軟體來管理網路連線,例如 NetworkManager (NM)(network-manager 和相關軟體包)。

  • 它們有自己的 GUI 和指令列程式來作為使用者介面。

  • 它們有自己的後臺背景程式(daemon)作為它們的系統後端。

  • 它們使你可以簡單地將系統連線到網路。

  • 它們使你可以簡單地管理有線和無線網路的調配。

  • 它們允許你調配網路而不依賴傳統的 ifupdown 軟體包。

[注意] 注意

不要在伺服器上使用這些自動網路調配工具。它們主要針對於膝上型電腦上的移動桌面使用者。

這些現代的網路調配工具需要進行適當的調配,以避免與傳統 ifupdown 軟體包發生衝突,它的組態檔案位於 “/etc/network/interfaces”。

Debian 系統 NM 的官方文件位於 “/usr/share/doc/network-manager/README.Debian” 。

本質上,如下操作即可完成桌面的網路調配。

  1. 通過下列指令使桌面使用者 foo 歸屬 “netdev” 組(另外,例如 GNOME 和 KDE 這樣的現代桌面環境會通過 D-bus 自動完成該操作)。

    $ sudo usermod -a -G foo netdev
  2. 使 “/etc/network/interfaces” 的調配保持下面那樣簡潔。

    auto lo
    iface lo inet loopback
  3. 透過下列命令重新啟動 NM。

    $ sudo systemctl restart network-manager
  4. 通過圖形介面調配網路。

[注意] 注意

只有列在 “/etc/network/interfaces” 中的介面會被 NM 管理,以避免與 ifupdown 的衝突。

[提示] 提示

如果你想擴充 NM 的網路組態功能,請尋找適當的外掛模組和補充軟體包,例如 network-manager-openconnect network-manager-openvpn-gnomenetwork-manager-pptp-gnomemobile-broadband-provider-infognome-bluetooth 等等。這同樣適用於 Wicd。

使用 systemd 的系統中,可以在 /etc/systemd/network/ 裡調配網路。參見 systemd-resolved(8)、resolved.conf(5) 和 systemd-networkd(8)。

這個允許在沒有影象介面的情況下配置現代網路。

DHCP 客戶端的配置可以透過建立 "/etc/systemd/network/dhcp.network" 檔案來進行設定。例如:

[Match]
Name=en*

[Network]
DHCP=yes

一個靜態網路配置能夠透過建立 "/etc/systemd/network/static.network" 來設定.比如:

[Match]
Name=en*

[Network]
Address=192.168.0.15/24
Gateway=192.168.0.1

The modern network configuration for cloud may use cloud-init and netplan.io packages (see 節 3.7.4, “Cloud system initialization”).

The netplan.io package supports systemd-networkd and NetworkManager as its network configuration backends, and enables the declarative network configuration using YAML data. When you change YAML:

  • Run "netplan generate" command to generate all the necessary backend configuration from YAML.

  • Run "netplan apply" command to apply the generated configuration to the backends.

See "Netplan documentation", netplan(5), netplan-generate(8), and netplan-apply(8).

See also "Cloud-init documentation" (especially around "Configuration sources" and "Netplan Passthrough") for how cloud-init can integrate netplan.io configuration with alternative data sources.

在 Linux 上的底層網路配置,使用 iproute2 程式 (ip(8), …) .

通用的網路優化超出了本文的範圍。我提及消費等級連線相關的主題。


網路管理器通常會自動設定最佳 最大傳輸單元 (MTU)

在一些場景中,在用 ping(8) 加上"-M do"選項傳送各種大小的 ICMP 報文資料包進行實驗後,你希望可以手動設定 MTU。MTU 是最大可完成沒有 IP 分片的資料包大小加上 28 位元組(IPv4)或 48 位元組(IPv6)。下面的列子,發現 IPv4 連線的 MTU 是 1460,IPv6 連線的 MTU 是 1500。

$ ping -4 -c 1 -s $((1500-28)) -M do www.debian.org
PING  (149.20.4.15) 1472(1500) bytes of data.
ping: local error: message too long, mtu=1460

---  ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms

$ ping -4 -c 1 -s $((1460-28)) -M do www.debian.org
PING  (130.89.148.77) 1432(1460) bytes of data.
1440 bytes from klecker-misc.debian.org (130.89.148.77): icmp_seq=1 ttl=50 time=325 ms

---  ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 325.318/325.318/325.318/0.000 ms
$ ping -6 -c 1 -s $((1500-48)) -M do www.debian.org
PING www.debian.org(mirror-csail.debian.org (2603:400a:ffff:bb8::801f:3e)) 1452 data bytes
1460 bytes from mirror-csail.debian.org (2603:400a:ffff:bb8::801f:3e): icmp_seq=1 ttl=47 time=191 ms

--- www.debian.org ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 191.332/191.332/191.332/0.000 ms

這個過程是 路徑 MTU (PMTU) 發現 (RFC1191) , tracepath(8) 指令能夠自動完成這個。


除了這些基本的指引方法外,你還應當知道下面的資訊。

  • 使用任何隧道方式(VPN 等.)的最佳 MTU 需要進一步減去它們上面的頭部。

  • MTU 值不應當超過通過實驗驗證的 PMTU 值。

  • 當遇到其它限制的時候,較大的 MTU 值通常比較好。

最大分片大小 (MSS) 是另外一種衡量包大小的方法。MSS 和 MTU 的關係如下.

  • 對於 IPv4, MSS = MTU - 40

  • 對於 IPv6,MSS = MTU - 60

[注意] 注意

基於 iptables(8) (參見 節 5.7, “Netfilter 網路過濾框架”) 的優化,能夠通過 MSS 來壓縮包大小,路由器會用到 MMS 。參見 iptables(8)中的"TCPMSS" .

The TCP throughput can be maximized by adjusting TCP buffer size parameters as in "TCP tuning" for the modern high-bandwidth and high-latency WAN. So far, the current Debian default settings serve well even for my LAN connected by the fast 1G bps FTTP service.

Netfilter 使用 Linux 核心 模組 (參見 節 3.8.1, “核心模組初始化”) 提供 狀態防火牆網路地址轉換 (NAT) 框架。


netfilter 主要的使用者層程式是 iptables(8).你能從 shell 手工交付式的調配 netfilter,使用 iptables-save(8) 儲存當前狀態,當系統重啟時,通過 init 指令碼呼叫 iptables-restore(8) 來恢復。

shorewall 這樣的調配幫助指令碼能夠使這個過程變得更簡單。

See documentations at Netfilter Documentation (or in "/usr/share/doc/iptables/html/").

[提示] 提示

雖然這些是為 Linux 2.4 寫的,iptables(8) 指令和 netfilter 核心功能都能夠在 Linux2.63.x 核心系列實現.