第 5 章 网络设置

目录

5.1. 基本网络架构
5.1.1. 主机名解析
5.1.2. 网络接口名称
5.1.3. 局域网网络地址范围
5.1.4. 网络设备支持
5.2. 现代的桌面网络配置
5.2.1. 图形界面的网络配置工具
5.3. 传统的网络连接和配置
5.4. 网络连接方式(传统)
5.4.1. 以太网 DHCP 连接
5.4.2. 以太网静态 IP 连接
5.4.3. 使用 pppconfig 的 PPP 连接
5.4.4. 使用 wvdialconf 的另一种可选的 PPP 连接
5.4.5. 使用 pppoeconf 的 PPPoE 以太网连接
5.5. 使用 ifupdown 进行基本网络配置(旧)
5.5.1. 简单的命令语法
5.5.2. "/etc/network/interfaces" 基本语法
5.5.3. 回环网络接口
5.5.4. 使用 DHCP 的网络接口
5.5.5. 使用静态 IP 地址的网络接口
5.5.6. 无线局域网接口基础
5.5.7. 使用 WPA/WPA2 的无线局域网接口
5.5.8. 使用 WEP 的无线局域网接口
5.5.9. PPP 连接
5.5.10. 另一种 PPP 连接
5.5.11. PPPoE 连接
5.5.12. ifupdown 网络配置状态
5.5.13. 网络重新配置基础
5.5.14. ifupdown-extra 包
5.6. 使用 ifupdown 的高级网络配置(旧)
5.6.1. The ifplugd package
5.6.2. The ifmetric package
5.6.3. The virtual interface
5.6.4. The advanced command syntax
5.6.5. The mapping stanza
5.6.6. 手动的可切换网络配置
5.6.7. ifupdown 系统的脚本
5.6.8. Mapping with guessnet
5.7. 底层网络配置
5.7.1. Iproute2 命令
5.7.2. 安全的底层网络操作
5.8. 网络优化
5.8.1. Finding optimal MTU
5.8.2. Setting MTU
5.8.3. WAN TCP optimization
5.9. Netfilter infrastructure
[提示] 提示

关于GNU/Linux网络的通用手册,请查看Linux 网络管理员手册

[提示] 提示

关于Debian专属的网络手册,请查看Debian管理员手册—网络配置

[警告] 警告

Instead of using the traditional interface naming scheme ("eth0", "eth1", "wlan0", …), the newer systemd uses "Predictable Network Interface Names" such as "enp0s25".

[警告] 警告

本章是基于 2013 年发布的 Debian 7.0 (Wheezy) 编写的,所以其内容正在变得过时。

[提示] 提示

尽管本手册仍用旧的 ifconfig(8) 命令和IPv4协议当作网络配置的例子,Debian在 wheezy 发行版后转向使用ip(8)命令和 IPv4+IPv6 协议。欢迎大家提供补丁,更新这个手册。

[提示] 提示

systemd环境下,可以用networkd来配置网络。请参考systemd-networkd(8)

让我们来回顾一下现代Debian操作系统中的基本网络架构。

表 5.1. 网络配置工具一览表

软件包 流行度 大小 类型 说明
ifupdown V:568, I:995 204 配置::ifupdown 用来启动/关闭网络的标准工具(Debian特有)
ifplugd V:4, I:18 209 同上 自动管理有线网络
ifupdown-extra V:0, I:1 96 同上 网络测试脚本,加强"ifupdown"软件包的功能
ifmetric V:0, I:1 21 同上 设置网络接口的路由度量
guessnet V:0, I:1 402 同上 脚本文件,利用"/etc/network/interfaces"文件来加强"ifupdown"的功能
ifscheme V:0, I:0 58 同上 映射脚本文件,增强"ifupdown"软件包的功能
ifupdown-scripts-zg2 V:0, I:0 147 同上 Zugschlus写的脚本,用于ifupdown手动管理
network-manager V:365, I:473 10959 配置::NM NetworkManager(守卫进程):自动管理网络
network-manager-gnome V:243, I:418 6132 同上 NetworkManager(GNOME前端)
wicd I:35 34 配置::wicd 有线和无线网络管理器(元软件包)
wicd-cli V:0, I:2 58 同上 有线和无线网络管理器(命令行客户端)
wicd-curses V:1, I:5 174 同上 有线和无线网络管理器(文本界面客户端)
wicd-daemon V:29, I:40 951 同上 有线和无线网络管理器(守护进程)
wicd-gtk V:24, I:37 573 同上 有线和无线网络管理器(GTK+客户端)
iptables V:243, I:996 1475 配置::Netfilter 封包过滤和网络地址转换管理工具(Netfilter
iproute V:144, I:550 23 配置::iproute2 iproute2, IPv6和其他高级网络配置:ip(8),tc(8)等等
ifrename V:1, I:2 121 同上 根据不同的静态标准来重命名网络接口:ifrename(8)
ethtool V:91, I:261 331 同上 显示或更改以太网设备的设定
iputils-ping V:268, I:997 106 测试::iproute2 测试能否连接远程主机,通过主机名IP 地址iproute2
iputils-arping V:67, I:384 57 同上 测试能否连接远程主机,通过ARP地址
iputils-tracepath V:17, I:288 90 同上 跟踪访问远程主机的路径
net-tools V:427, I:998 883 配置::net-tools NET-3网络工具箱(net-tools,IPv4网络配置):ifconfig(8)等等。
inetutils-ping V:0, I:1 337 测试::net-tools 测试能否连接远程主机,通过hostnameIP 地址(传统方式,GNU)
arping V:2, I:28 103 同上 测试能否连接远程主机,通过ARP地址(传统方法)
traceroute V:74, I:993 154 同上 跟踪连接远程主机的路径(传统方法,控制台)
isc-dhcp-client V:420, I:959 651 配置::底层 DHCP客户端
wpasupplicant V:279, I:547 2531 同上 WPA和WPA2客户端支持(IEEE 802.11i)
wpagui V:0, I:4 781 同上 wpa_supplicant Qt 图形界面客户端
wireless-tools V:81, I:276 265 同上 操控Linux无线扩展的工具
ppp V:203, I:519 927 同上 使用chat连接PPP/PPPoE
pppoeconf V:0, I:12 290 配置::辅助 配置助手,以便于使用PPPoE连接
pppconfig V:1, I:4 805 同上 配置助手,以便于使用chat连接PPP
wvdial V:0, I:8 276 同上 配置助手,以便于使用wvdialppp连接PPP
mtr-tiny V:7, I:59 105 测试::底层 追踪连接远程主机的路径(文本界面)
mtr V:6, I:41 150 同上 追踪连接远程主机的路径(文本界面和GTK+界面)
gnome-nettool V:14, I:285 2111 同上 获取常见网络信息的工具(GNOME)
nmap V:47, I:454 22207 同上 网络映射/端口扫描(Nmap,控制台)
zenmap V:3, I:12 2821 同上 网络映射/端口扫描(GTK+)
tcpdump V:22, I:198 1120 同上 网络流量分析(Tcpdump,控制台)
wireshark V:6, I:65 72 同上 网络流量分析(Wireshark,GTK+)
tshark V:3, I:37 363 同上 网络流量分析(控制台)
nagios3 I:8 9 同上 主机、服务、网络监控及管理系统(Nagios
tcptrace V:0, I:1 389 同上 根据tcpdump的输出生成的连接数据统计
snort V:1, I:1 1920 同上 灵活的网络入侵侦测系统(Snort
ntopng V:0, I:1 578 同上 在网页浏览器中展示网络流量
dnsutils V:82, I:903 462 同上 BIND软件包提供的网络客户端程序:nslookup(8),nsupdate(8),dig(8)
dlint V:1, I:23 96 同上 利用域名服务器查询来查看DNS域信息
dnstracer V:0, I:2 81 同上 跟踪DNS查询直至源头

The hostname resolution is currently supported by the NSS (Name Service Switch) mechanism too. The flow of this resolution is the following.

  1. The "/etc/nsswitch.conf" file with stanza like "hosts: files dns" dictates the hostname resolution order. (This replaces the old functionality of the "order" stanza in "/etc/host.conf".)

  2. The files method is invoked first. If the hostname is found in the "/etc/hosts" file, it returns all valid addresses for it and exits. (The "/etc/host.conf" file contains "multi on".)

  3. The dns method is invoked. If the hostname is found by the query to the Internet Domain Name System (DNS) identified by the "/etc/resolv.conf" file, it returns all valid addresses for it and exits.

For example, "/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     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

Each line starts with a IP address and it is followed by the associated hostname.

The IP address 127.0.1.1 in the second line of this example may not be found on some other Unix-like systems. The Debian Installer creates this entry for a system without a permanent IP address as a workaround for some software (e.g., GNOME) as documented in the bug #719621.

The <host_name> matches the hostname defined in the "/etc/hostname".

For a system with a permanent IP address, that permanent IP address should be used here instead of 127.0.1.1.

For a system with a permanent IP address and a fully qualified domain name (FQDN) provided by the Domain Name System (DNS), that canonical <host_name>.<domain_name> should be used instead of just <host_name>.

The "/etc/resolv.conf" is a static file if the resolvconf package is not installed. If installed, it is a symbolic link. Either way, it contains information that initialize the resolver routines. If the DNS is found at IP="192.168.11.1", it contains the following.

nameserver 192.168.11.1

The resolvconf package makes this "/etc/resolv.conf" into a symbolic link and manages its contents by the hook scripts automatically.

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

  • Avahi provides a framework for Multicast DNS Service Discovery on Debian.

  • It is equivalent of Apple Bonjour / Apple Rendezvous.

  • The libnss-mdns plugin package provides host name resolution via mDNS for the GNU Name Service Switch (NSS) functionality of the GNU C Library (glibc).

  • The "/etc/nsswitch.conf" file should have stanza like "hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4".

  • Host names ending with the ".local" pseudo-top-level domain (TLD) are resolved.

  • The mDNS IPv4 link-local multicast address "224.0.0.251" or its IPv6 equivalent "FF02::FB" are used to make DNS query for a name ending with ".local".

The hostname resolution via deprecated NETBios over TCP/IP used by the older Windows system can be provided by installing the winbind package. The "/etc/nsswitch.conf" file should have stanza like "hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4 wins" to enable this functionality. (Modern Windows system usually use the dns method for the hostname resolution.)

[注意] 注意

The expansion of generic Top-Level Domains (gTLD) in the Domain Name System is underway. Watch out for the name collision when chosing a domain name used only within LAN.

The network interface name, e.g. eth0, is assigned to each hardware in the Linux kernel through the user space configuration mechanism, udev (see 第 3.3 节 “udev 系统”), as it is found. The network interface name is referred as physical interface in ifup(8) and interfaces(5).

In order to ensure each network interface to be named persistently for each reboot using MAC address etc., there is a rules file "/etc/udev/rules.d/70-persistent-net.rules". This file is automatically generated by the "/lib/udev/write_net_rules" program, probably run by the "persistent-net-generator.rules" rules file. You can modify it to change naming rule.

[小心] 小心

When editing the "/etc/udev/rules.d/70-persistent-net.rules" rules file, you must keep each rule on a single line and the MAC address in lowercase. For example, if you find "FireWire device" and "PCI device" in this file, you probably want to name "PCI device" as eth0 and configure it as the primary network interface.

Debian squeeze 和新的发行版都可以通过后台守护进程(daemon)管理软件来管理网络连接,例如 NetworkManager (NM)(network-manager 和相关软件包)或 Wicd(wicd 和相关软件包)。

  • 它们有自己的 GUI 和命令行程序来作为用户界面。

  • 它们有自己的后台守护进程(daemon)作为它们的系统后端。

  • 它们使你可以简单地将系统连接到网络。

  • 它们使你可以简单地管理有线和无线网络的配置。

  • 它们允许你配置网络而不依赖传统的 ifupdown 软件包。

[注意] 注意

不要在服务器上使用这些自动网络配置工具。它们主要针对于笔记本电脑上的移动桌面用户。

这些现代的网络配置工具需要进行适当的配置,以避免与传统 ifupdown 软件包发生冲突,它的配置文件位于 “/etc/network/interfaces”。

[提示] 提示

使用 systemd 的系统中,可以在 /etc/systemd/network/ 里配置网络。参见 systemd-resolved(8)resolved.conf(5)systemd-networkd(8)

[注意] 注意

Some features of these automatic network configuration tools may suffer regressions. These are not as robust as the legacy ifupdown package. Check BTS of network-manager and BTS of wicd for current issues and limitations.

Debian 系统 NM 和 Wicd 的官方文档分别位于 “/usr/share/doc/network-manager/README.Debian” 和 “/usr/share/doc/wicd/README.Debian”。

本质上,如下操作即可完成桌面的网络配置。

  1. 通过下列命令使桌面用户 foo 归属 “netdev” 组(另外,例如 GNOME 和 KDE 这样的现代桌面环境会通过 D-bus 自动完成该操作)。

    $ sudo adduser foo netdev
  2. 使 “/etc/network/interfaces” 的配置保持下面那样简洁。

    auto lo
    iface lo inet loopback
  3. 通过下列命令重新启动 NM 或 Wicd。

    $ sudo /etc/init.d/network-manager restart
    $ sudo /etc/init.d/wicd restart
  4. 通过图形界面配置网络。

[注意] 注意

只有列在 “/etc/network/interfaces” 中的接口会被 NM 或 Wicd 管理,以避免与 ifupdown 的冲突。

[提示] 提示

如果你想扩展 NM 的网络配置功能,请寻找适当的插件模块和补充软件包,例如 network-manager-openconnect network-manager-openvpn-gnomenetwork-manager-pptp-gnomemobile-broadband-provider-infognome-bluetooth 等等。这同样适用于 Wicd。

[小心] 小心

这些自动网络配置工具可能无法兼容 “/etc/network/interfaces” 中传统的 ifupdown 的深奥配置,例如 第 5.5 节 “使用 ifupdown 进行基本网络配置(旧)”第 5.6 节 “使用 ifupdown 的高级网络配置(旧)” 中的那些配置。检查 network-manager 的 BTSwicd 的 BTS 来查看当前的问题和限制。

如果 第 5.2 节 “现代的桌面网络配置” 中描述的方法无法满足你的需要,那你应该使用结合了许多普通工具的传统网络连接和配置方法。

传统网络连接的每个方法都是特定的(参见 第 5.4 节 “网络连接方式(传统)”)。

用于 Linux 底层网络配置的程序有两种类型(参见 第 5.7.1 节 “Iproute2 命令”)。

  • 来自 Linux NET-3 网络系统的旧 net-tools 程序(ifconfig(8)……)。它们中的大多数都已经过时了。

  • 来自现在的 Linux 网络系统的新 Linux iproute2 程序(ip(8)……)。

尽管底层程序十分强大,但它们使用繁琐。因此创建了高层网络配置系统。

ifupdown 软件包是 Debian 中这种高层网络配置系统的实际标准。它让你可以简单地通过例如 “ifup eth0” 这样的命令来打开网络。它的配置文件位于 “/etc/network/interfaces” 中并且其典型内容如下。

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

resolvconf 软件包是为了使 ifupdown 系统支持自动重写解析器配置文件 “/etc/resolv.conf” 来使网络地址解析平滑地重新配置。现在,大多数 Debian 网络配置软件包都使用了 resolvconf 软件包(参见 “/usr/share/doc/resolvconf/README.Debian”)。

ifupdown 软件包的辅助脚本,例如 ifplugdguessnetifscheme等,是为了进行网络环境的自动动态配置,例如位于有线局域网中的移动电脑。这些相对来说比较难使用,但在当前的 ifupdown 系统下工作良好。

详细的案例参见 第 5.5 节 “使用 ifupdown 进行基本网络配置(旧)”第 5.6 节 “使用 ifupdown 的高级网络配置(旧)”

[小心] 小心

在这节描述的连接测试方式仅仅用于测试目的。不应当直接用于日常的网络连接。建议你使用 NM, Wicd, 或 ifupdown 包代替。(参见 第 5.2 节 “现代的桌面网络配置”第 5.5 节 “使用 ifupdown 进行基本网络配置(旧)”).

一台电脑典型的网络连接方式和连接路径,能够使用下面的内容概述。


每种连接方式配置脚本汇总。


网络连接缩略语意义如下。


[注意] 注意

通过电视线缆的广域网服务,通常使用 DHCP 或 PPPoE。ADSL 和 FTTP 通常使用 PPPoE。你需要咨询你的互联网服务提供商来获得广域网连接使用的精确配置。

[注意] 注意

当宽带路由器用来搭建家庭局域网环境时,局域网上的电脑需要使用宽带路由器上的 网络地址转换(NAT),来连接到广域网。在这样的情况下,局域网上的电脑网络接口需使用静态 IP 或者宽带路由器提供的 DHCP 服务。宽带路由器必须按 ISP 的指引来配置连接到广域网。

配置脚本 pppconfig 配置 PPP 连接,仅需要交互式的选择下面内容。

  • 电话号码

  • ISP 用户名

  • ISP 密码

  • 端口速率

  • modem 通信端口

  • 认证方式

表 5.6. 使用 pppconfig 的 PPP 连接配置文件列表

文件 功能
/etc/ppp/peers/<isp_name> pppconfig 生成针对 <isp_name> 的 pppd 配置文件
/etc/chatscripts/<isp_name> pppconfig 生成针对 <isp_name> 的 chat 配置文件
/etc/ppp/options pppd 常用的执行参数
/etc/ppp/pap-secret PAP 的鉴权认证数据 (有安全风险)
/etc/ppp/chap-secret CHAP 的鉴权认证数据(更安全)

[小心] 小心

"<isp_name>" 是 "互联网服务提供商" ,假设 ponpoff 命令调用时,没有参数。

你可以使用下面的底层网络配置工具测试配置。

$ sudo pon <isp_name>
...
$ sudo poff <isp_name>

参见 "/usr/share/doc/ppp/README.Debian.gz".

Debian 系统上的传统 TCP/IP 网络 搭建,ifupdown 软件包是作为一个上层工具来使用。有两个典型场景。

如果你想设置高级配置,这些传统的设置方法,是相当有用的。在下面的内容中发现细节。

ifupdown 包提供 Debian 系统中标准的高层网络配置框架。在本节中,我们通过 ifupdown 的简单介绍和许多典型列子来学习基本的网络配置。

第 5.4.1 节 “以太网 DHCP 连接” 准备系统后,在"/etc/network/interfaces" 里面,按下面的内容创建配置条目后,网络接口的 DHCP 便配置好了。

allow-hotplug eth0
iface eth0 inet dhcp

当 Linux 内核检测到物理接口 eth0, allow-hotplug 节促使 ifup 启动接口,iface 促使 ifup 使用 DHCP 来配置接口。

你需要安装 wpasupplicant 包来支持 WLAN 使用新的 WPA/WPA2.

使用 DHCP 的无线局域网连接,"/etc/network/interfaces" 文件的条目需要按下面的内容设置。

allow-hotplug ath0
iface ath0 inet dhcp
 wpa-ssid homezone
 # hexadecimal psk is encoded from a plaintext passphrase
 wpa-psk 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f

参见 "/usr/share/doc/wpasupplicant/README.modes.gz".

你需要安装 wireless-tools 包来支撑老的 WEP 无线局域网。(你的消费等级的路由器可能仍然使用不安全的架构,但这总比没有好。)

[小心] 小心

请注意:WEP 无线局域网上的网络流量,可以被其他人监听。

使用 DHCP 的无线局域网连接,"/etc/network/interfaces" 文件的条目需要按下面的内容设置。

allow-hotplug eth0
iface eth0 inet dhcp
 wireless-essid Home
 wireless-key1 0123-4567-89ab-cdef
 wireless-key2 12345678
 wireless-key3 s:password
 wireless-defaultkey 2
 wireless-keymode open

参见 "/usr/share/doc/wireless-tools/README.Debian".

你需要按之前的描述配置 PPP 连接(参见 第 5.4.3 节 “使用 pppconfig 的 PPP 连接”). 然后,按下面的方式给第一个 PPP 设备 ppp0 增加 “/etc/network/interfaces" 文件里的条目.

iface ppp0 inet ppp
 provider <isp_name>

你需要按之前的描述先配置好使用 wvdial 的另外一种 PPP 连接(参见第 5.4.4 节 “使用 wvdialconf 的另一种可选的 PPP 连接”). 然后,按下面的方式给第一个 PPP 设备 ppp0 增加 “/etc/network/interfaces" 文件里的条目.

iface ppp0 inet wvdial

使用 PPPoE 直接连接到广域网的电脑,你需要按之前的描述用 PPPoE 连接配置系统(参见 第 5.4.5 节 “使用 pppoeconf 的 PPPoE 以太网连接”). 然后,按下面的方式给第一个 PPPoE 设备 eth0 增加 “/etc/network/interfaces" 文件里的条目.

allow-hotplug eth0
iface eth0 inet manual
 pre-up /sbin/ifconfig eth0 up
 up ifup ppp0=dsl
 down ifdown ppp0=dsl
 post-down /sbin/ifconfig eth0 down
# The following is used internally only
iface dsl inet ppp
 provider dsl-provider

The functionality of the ifupdown package can be improved beyond what was described in 第 5.5 节 “使用 ifupdown 进行基本网络配置(旧)” with the advanced knowledge.

The functionalities described here are completely optional. I, being lazy and minimalist, rarely bother to use these.

[小心] 小心

If you could not set up network connection by information in 第 5.5 节 “使用 ifupdown 进行基本网络配置(旧)”, you make situation worse by using information below.

The ifplugd package is an older automatic network configuration tool which can manage only Ethernet connections. This solves unplugged/replugged Ethernet cable issues for mobile PC etc. If you have NetworkManager or Wicd (see 第 5.2 节 “现代的桌面网络配置”) installed, you do not need this package.

This package runs a daemon and replaces auto or allow-hotplug functionalities (see 表 5.10 “"/etc/network/interfaces" 里面的节列表”) and starts interfaces upon their connection to the network.

Here is how to use the ifplugd package for the internal Ethernet port, e.g. eth0.

  1. Remove stanza in "/etc/network/interfaces": "auto eth0" or "allow-hotplug eth0".

  2. Keep stanza in "/etc/network/interfaces": "iface eth0 inet …" and "mapping …".

  3. Install the ifplugd package.

  4. Run "sudo dpkg-reconfigure ifplugd".

  5. Put eth0 as the "static interfaces to be watched by ifplugd".

Now, the network reconfiguration works as you desire.

  • Upon power-on or upon hardware discovery, the interface is not brought up by itself.

  • Upon finding the Ethernet cable, the interface is brought up.

  • Upon some time after unplugging the Ethernet cable, the interface is brought down automatically.

  • Upon plugging in another Ethernet cable, the interface is brought up under the new network environment.

[提示] 提示

The arguments for the ifplugd(8) command can set its behaviors such as the delay for reconfiguring interfaces.

We skipped explaining the mapping stanza in the "/etc/network/interfaces" in 第 5.5.2 节 “"/etc/network/interfaces" 基本语法” to avoid complication. This stanza has the following syntax.

mapping <interface_name_glob>
 script <script_name>
 map <script_input1>
 map <script_input2>
 map ...

This provides advanced features to the "/etc/network/interfaces" file by automating the choice of the configuration with the mapping script specified by <script_name>.

Let's follow the execution of the following.

$ sudo ifup eth0

When the "<interface_name_glob>" matches "eth0", this execution produces the execution of the following command to configure eth0 automatically.

$ sudo ifup eth0=$(echo -e '<script_input1> \n <script_input2> \n ...' | <script_name> eth0)

Here, script input lines with "map" are optional and can be repeated.

[注意] 注意

The glob for mapping stanza works like shell filename glob (see 第 1.5.6 节 “Shell glob”).

以下是如何在几个网络配置中进行手动切换,而无需像 第 5.5.13 节 “网络重新配置基础” 中那样重写 “/etc/network/interfaces” 文件。

对于你需要访问的所有网络配置,你需要在 “/etc/network/interfaces” 文件中像下面那样创建一个单独的节。

auto lo
iface lo inet loopback

iface config1 inet dhcp

iface config2 inet static
 address 192.168.11.100
 netmask 255.255.255.0
 gateway 192.168.11.1
 dns-domain example.com
 dns-nameservers 192.168.11.1

iface pppoe inet manual
 pre-up /sbin/ifconfig eth0 up
 up ifup ppp0=dsl
 down ifdown ppp0=dsl
 post-down /sbin/ifconfig eth0 down

# The following is used internally only
iface dsl inet ppp
 provider dsl-provider

iface pots inet ppp
 provider provider

Please note the network configuration name which is the token after iface does not use the token for the network interface name. Also, there are no auto stanza nor allow-hotplug stanza to start the network interface eth0 automatically upon events.

现在,你可以切换网络配置了。

让我们通过 DHCP 将你的 PC 移动到局域网。你可以通过下列命令开启由网络配置名称(逻辑接口名称)config1 指定的网络接口(物理接口) eth0

$ sudo ifup eth0=config1
Password:
...

eth0 接口已开启,由 DHCP 配置并连接到了局域网。

$ sudo ifdown eth0=config1
...

eth0 接口已关闭并断开局域网连接。

让我们通过静态 IP 使你的 PC 移动到局域网。你可以通过下列命令开启由网络配置名称 config2 指定的网络接口 eth0

$ sudo ifup eth0=config2
...

开启 eth0 接口,使用静态 IP 配置并连接到局域网。像 dns-* 这样的额外参数会配置 “/etc/resolv.conf” 的内容。如果安装了 resolvconf,“/etc/resolv.conf” 会更容易管理。

$ sudo ifdown eth0=config2
...

eth0 接口再次关闭并断开局域网连接。

让我们将你的 PC 移动到 PPPoE 服务器的 BB-modem 上的一个端口。你可以通过下列命令开启由网络配置名称 pppoe 指定的网络接口 eth0

$ sudo ifup eth0=pppoe
...

eth0 接口已开启,由 PPPoE 配置直接连接到 ISP。

$ sudo ifdown eth0=pppoe
...

eth0 接口再次关闭并断开连接。

让我们将你的 PC 移动到使用 POTS 和 modem 的位置,而非局域网或 BB-modem。你可以通过下列命令开启由网络配置名称 ppp0 指定的网络接口 eth0

$ sudo ifup ppp0=pots
...

开启 ppp0接口,并使用 PPP 连接到互联网。

$ sudo ifdown ppp0=pots
...

关闭 ppp0 接口并断开网络。

你应该检查 “/etc/network/run/ifstate” 文件,查看ifupdown 系统当前网络配置的状态。

[警告] 警告

如果你有多个网络接口,你可能需要调整 eth*ppp* 等的末尾数字。

ifupdown 系统会自动运行安装在 “/etc/network/*/” 中的脚本,而且会传递环境变量给脚本。


Here, each environment variable, "$IF_<OPTION>", is created from the name for the corresponding option such as <option1> and <option2> by prepending "$IF_", converting the case to the upper case, replacing hyphens to underscores, and discarding non-alphanumeric characters.

[提示] 提示

See 第 5.5.2 节 “"/etc/network/interfaces" 基本语法” for <address_family>, <method_name>, <option1> and <option2>.

The ifupdown-extra package (see 第 5.5.14 节 “ifupdown-extra 包”) uses these environment variables to extend the functionality of the ifupdown package. The ifmetric package (see 第 5.6.2 节 “The ifmetric package”) installs the "/etc/network/if-up.d/ifmetric" script which sets the metric via the "$IF_METRIC" variable. The guessnet package (see 第 5.6.8 节 “Mapping with guessnet”), which provides simple and powerful framework for the auto-selection of the network configuration via the mapping mechanism, also uses these.

[注意] 注意

For more specific examples of custom network configuration scripts using these environment variables, you should check example scripts in "/usr/share/doc/ifupdown/examples/*" and scripts used in ifscheme and ifupdown-scripts-zg2 packages. These additional scripts have some overlaps of functionalities with basic ifupdown-extra and guessnet packages. If you install these additional scripts, you should customize these scripts to avoid interferences.

Instead of manually choosing configuration as described in 第 5.6.6 节 “手动的可切换网络配置”, you can use the mapping mechanism described in 第 5.6.5 节 “The mapping stanza” to select network configuration automatically with custom scripts.

The guessnet-ifupdown(8) command provided by the guessnet package is designed to be used as a mapping script and provides powerful framework to enhance the ifupdown system.

  • You list test condition as the value for guessnet options for each network configuration under iface stanza.

  • Mapping choses the iface with first non-ERROR result as the network configuration.

This dual usage of the "/etc/network/interfaces" file by the mapping script, guessnet-ifupdown, and the original network configuration infrastructure, ifupdown, does not cause negative impacts since guessnet options only export extra environment variables to scripts run by the ifupdown system. See details in guessnet-ifupdown(8).

[注意] 注意

When multiple guessnet option lines are required in "/etc/network/interfaces", use option lines started with guessnet1, guessnet2, and so on, since the ifupdown package does not allow starting strings of option lines to be repeated.

Generic network optimization is beyond the scope of this documentation. I touch only subjects pertinent to the consumer grade connection.


The Maximum Transmission Unit (MTU) value can be determined experimentally with ping(8) with "-M do" option which sends ICMP packets with data size starting from 1500 (with offset of 28 bytes for the IP+ICMP header) and finding the largest size without IP fragmentation.

尝试下列例子

$ ping -c 1 -s $((1500-28)) -M do www.debian.org
PING www.debian.org (194.109.137.218) 1472(1500) bytes of data.
From 192.168.11.2 icmp_seq=1 Frag needed and DF set (mtu = 1454)

--- www.debian.org ping statistics ---
0 packets transmitted, 0 received, +1 errors

Try 1454 instead of 1500

You see ping(8) succeed with 1454.

This process is Path MTU (PMTU) discovery (RFC1191) and the tracepath(8) command can automate this.

[提示] 提示

The above example with PMTU value of 1454 is for my previous FTTP provider which used Asynchronous Transfer Mode (ATM) as its backbone network and served its clients with the PPPoE. The actual PMTU value depends on your environment, e.g., 1500 for the my new FTTP provider.


In addtion to these basic guide lines, you should know the following.

  • Any use of tunneling methods (VPN etc.) may reduce optimal MTU further by their overheads.

  • The MTU value should not exceed the experimentally determined PMTU value.

  • The bigger MTU value is generally better when other limitations are met.

Here are examples for setting the MTU value from its default 1500 to 1454.

For the DHCP (see 第 5.5.4 节 “使用 DHCP 的网络接口”), you can replace pertinent iface stanza lines in the "/etc/network/interfaces" with the following.

iface eth0 inet dhcp
 pre-up /sbin/ifconfig $IFACE mtu 1454

For the static IP (see 第 5.5.5 节 “使用静态 IP 地址的网络接口”), you can replace pertinent iface stanza lines in the "/etc/network/interfaces" with the following.

iface eth0 inet static
 address 192.168.11.100
 netmask 255.255.255.0
 gateway 192.168.11.1
 mtu 1454
 dns-domain example.com
 dns-nameservers 192.168.11.1

For the direct PPPoE (see 第 5.4.5 节 “使用 pppoeconf 的 PPPoE 以太网连接”), you can replace pertinent "mtu" line in the "/etc/ppp/peers/dsl-provider" with the following.

mtu 1454

The maximum segment size (MSS) is used as an alternative measure of packet size. The relationship between MSS and MTU are the following.

  • MSS = MTU - 40 for IPv4

  • MSS = MTU - 60 for IPv6

[注意] 注意

The iptables(8) (see 第 5.9 节 “Netfilter infrastructure”) based optimization can clamp packet size by the MSS and is useful for the router. See "TCPMSS" in iptables(8).

The TCP throughput can be maximized by adjusting TCP buffer size parameters as described in "TCP Tuning Guide" and "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 provides infrastructure for stateful firewall and network address translation (NAT) with Linux kernel modules (see 第 3.3.1 节 “内核模块初始化”).


Main user space program of netfilter is iptables(8). You can manually configure netfilter interactively from shell, save its state with iptables-save(8), and restore it via init script with iptables-restore(8) upon system reboot.

Configuration helper scripts such as shorewall ease this process.

See documentations at http://www.netfilter.org/documentation/ (or in "/usr/share/doc/iptables/html/").

[提示] 提示

Although these were written for Linux 2.4, both iptables(8) command and netfilter kernel function apply for Linux 2.6 and 3.x kernel series.