第 5 章 網絡設置

目录

5.1. 基本網絡架構
5.1.1. 主機名解析
5.1.2. The network interface name
5.1.3. The network address range for the LAN
5.1.4. The network device support
5.2. The modern network configuration for desktop
5.2.1. GUI network configuration tools
5.3. The legacy network connection and configuration
5.4. The network connection method (legacy)
5.4.1. The DHCP connection with the Ethernet
5.4.2. The static IP connection with the Ethernet
5.4.3. The PPP connection with pppconfig
5.4.4. The alternative PPP connection with wvdialconf
5.4.5. The PPPoE connection with pppoeconf
5.5. The basic network configuration with ifupdown (legacy)
5.5.1. The command syntax simplified
5.5.2. The basic syntax of "/etc/network/interfaces"
5.5.3. The loopback network interface
5.5.4. The network interface served by the DHCP
5.5.5. The network interface with the static IP
5.5.6. The basics of wireless LAN interface
5.5.7. The wireless LAN interface with WPA/WPA2
5.5.8. The wireless LAN interface with WEP
5.5.9. The PPP connection
5.5.10. The alternative PPP connection
5.5.11. The PPPoE connection
5.5.12. The network configuration state of ifupdown
5.5.13. The basic network reconfiguration
5.5.14. The ifupdown-extra package
5.6. The advanced network configuration with ifupdown (legacy)
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. The manually switchable network configuration
5.6.7. Scripting with the ifupdown system
5.6.8. Mapping with guessnet
5.7. The low level network configuration
5.7.1. Iproute2 commands
5.7.2. Safe low level network operations
5.8. Network optimization
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 节 “The udev system”), 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 and newer can manage the network connection via management daemon software such as NetworkManager (NM) (network-manager and associated packages) or Wicd (wicd and associated packages).

  • They come with their own GUI and command-line programs as their user interfaces.

  • They come with their own daemon as their backend system.

  • They allow easy connection of your system to the Internet.

  • They allow easy management of wired and wireless network configuration.

  • They allow us to configure network independent of the legacy ifupdown package.

[注意] 注意

Do not use these automatic network configuration tools for servers. These are aimed primarily for mobile desktop users on laptops.

These modern network configuration tools need to be configured properly to avoid conflicting with the legacy ifupdown package and its configuration file "/etc/network/interfaces".

[提示] 提示

Under systemd, the network may be configured in /etc/systemd/network/ instead. See systemd-resolved(8), resolved.conf(5), and 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.

Official documentations for NM and Wicd on Debian are provided in "/usr/share/doc/network-manager/README.Debian" and "/usr/share/doc/wicd/README.Debian", respectively.

Essentially, the network configuration for desktop is done as follows.

  1. Make desktop user, e.g. foo, belong to group "netdev" by the following (Alternatively, do it automatically via D-bus under modern desktop environments such as GNOME and KDE).

    $ sudo adduser foo netdev
  2. Keep configuration of "/etc/network/interfaces" as simple as in the following.

    auto lo
    iface lo inet loopback
  3. Restart NM or Wicd by the following.

    $ sudo /etc/init.d/network-manager restart
    $ sudo /etc/init.d/wicd restart
  4. Configure your network via GUI.

[注意] 注意

Only interfaces which are not listed in "/etc/network/interfaces" are managed by NM or Wicd to avoid conflict with ifupdown.

[提示] 提示

If you wish to extend network configuration capabilities of NM, please seek appropriate plug-in modules and supplemental packages such as network-manager-openconnect, network-manager-openvpn-gnome, network-manager-pptp-gnome, mobile-broadband-provider-info, gnome-bluetooth, etc. The same goes for those of Wicd.

[小心] 小心

These automatic network configuration tools may not be compatible with esoteric configurations of legacy ifupdown in "/etc/network/interfaces" such as ones in 第 5.5 节 “The basic network configuration with ifupdown (legacy)” and 第 5.6 节 “The advanced network configuration with ifupdown (legacy)”. Check BTS of network-manager and BTS of wicd for current issues and limitations.

When the method described in 第 5.2 节 “The modern network configuration for desktop” does not suffice your needs, you should use the legacy network connection and configuration method which combines many simpler tools.

The legacy network connection is specific for each method (see 第 5.4 节 “The network connection method (legacy)”).

There are 2 types of programs for the low level network configuration on Linux (see 第 5.7.1 节 “Iproute2 commands”).

  • Old net-tools programs (ifconfig(8), …) are from the Linux NET-3 networking system. Most of these are obsolete now.

  • New Linux iproute2 programs (ip(8), …) are the current Linux networking system.

Although these low level networking programs are powerful, they are cumbersome to use. So high level network configuration systems have been created.

The ifupdown package is the de facto standard for such high level network configuration system on Debian. It enables you to bring up network simply by doing , e.g., "ifup eth0". Its configuration file is the "/etc/network/interfaces" file and its typical contents are the following.

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

The resolvconf package was created to supplement ifupdown system to support smooth reconfiguration of network address resolution by automating rewrite of resolver configuration file "/etc/resolv.conf". Now, most Debian network configuration packages are modified to use resolvconf package (see "/usr/share/doc/resolvconf/README.Debian").

Helper scripts to the ifupdown package such as ifplugd, guessnet, ifscheme, etc. are created to automate dynamic configuration of network environment such as one for mobile PC on wired LAN. These are relatively difficult to use but play well with existing ifupdown system.

These are explained in detail with examples (see 第 5.5 节 “The basic network configuration with ifupdown (legacy)” and 第 5.6 节 “The advanced network configuration with ifupdown (legacy)”).

[小心] 小心

The connection test methods described in this section are meant for testing purposes. It is not meant to be used directly for the daily network connection. You are advised to use NM, Wicd, or the ifupdown package instead (see 第 5.2 节 “The modern network configuration for desktop” and 第 5.5 节 “The basic network configuration with ifupdown (legacy)”).

The typical network connection method and connection path for a PC can be summarized as the following.


Here is the summary of configuration scripts for each connection method.


The network connection acronyms mean the following.


[注意] 注意

The WAN connection services via cable TV are generally served by DHCP or PPPoE. The ones by ADSL and FTTP are generally served by PPPoE. You have to consult your ISP for exact configuration requirements of the WAN connection.

[注意] 注意

When BB-router is used to create home LAN environment, PCs on LAN are connected to the WAN via BB-router with network address translation (NAT). For such case, PC's network interfaces on the LAN are served by static IP or DHCP from the BB-router. BB-router must be configured to connect the WAN following the instruction by your ISP.

The configuration script pppconfig configures the PPP connection interactively just by selecting the following.

  • The telephone number

  • The ISP user name

  • The ISP password

  • The port speed

  • The modem communication port

  • The authentication method

表 5.6. List of configuration files for the PPP connection with pppconfig

file function
/etc/ppp/peers/<isp_name> The pppconfig generated configuration file for pppd specific to <isp_name>
/etc/chatscripts/<isp_name> The pppconfig generated configuration file for chat specific to <isp_name>
/etc/ppp/options The general execution parameter for pppd
/etc/ppp/pap-secret Authentication data for the PAP (security risk)
/etc/ppp/chap-secret Authentication data for the CHAP (more secure)

[小心] 小心

The "<isp_name>" value of "provider" is assumed if pon and poff commands are invoked without arguments.

You can test configuration using low level network configuration tools as the following.

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

See "/usr/share/doc/ppp/README.Debian.gz".

The traditional TCP/IP network setup on the Debian system uses ifupdown package as a high level tool. There are 2 typical cases.

These traditional setup methods are quite useful if you wish to set up advanced configuration; find details in the following.

The ifupdown package provides the standardized framework for the high level network configuration in the Debian system. In this section, we learn the basic network configuration with ifupdown with simplified introduction and many typical examples.

After preparing the system by 第 5.4.1 节 “The DHCP connection with the Ethernet”, the network interface served by the DHCP is configured by creating the configuration entry in the "/etc/network/interfaces" file as the following.

allow-hotplug eth0
iface eth0 inet dhcp

When the Linux kernel detects the physical interface eth0, the allow-hotplug stanza causes ifup to bring up the interface and the iface stanza causes ifup to use DHCP to configure the interface.

The wireless LAN (WLAN for short) provides the fast wireless connectivity through the spread-spectrum communication of unlicensed radio bands based on the set of standards called IEEE 802.11.

The WLAN interfaces are almost like normal Ethernet interfaces but require some network ID and encryption key data to be provided when they are initialized. Their high level network tools are exactly the same as that of Ethernet interfaces except interface names are a bit different like eth1, wlan0, ath0, wifi0, … depending on the kernel drivers used.

[提示] 提示

The wmaster0 device is the master device which is an internal device used only by SoftMAC with new mac80211 API of Linux.

Here are some keywords to remember for the WLAN.


The actual choice of protocol is usually limited by the wireless router you deploy.

You need to install the wpasupplicant package to support the WLAN with the new WPA/WPA2.

In case of the DHCP served IP on WLAN connection, the "/etc/network/interfaces" file entry should be as the following.

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

See "/usr/share/doc/wpasupplicant/README.modes.gz".

You need to install the wireless-tools package to support the WLAN with the old WEP. (Your consumer grade router may still be using this insecure infrastructure but this is better than nothing.)

[小心] 小心

Please note that your network traffic on WLAN with WEP may be sniffed by others.

In case of the DHCP served IP on WLAN connection, the "/etc/network/interfaces" file entry should be as the following.

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

See "/usr/share/doc/wireless-tools/README.Debian".

You need to configure the PPP connection first as described before (see 第 5.4.3 节 “The PPP connection with pppconfig”). Then, add the "/etc/network/interfaces" file entry for the primary PPP device ppp0 as the following.

iface ppp0 inet ppp
 provider <isp_name>

For PC connected directly to the WAN served by the PPPoE, you need to configure system with the PPPoE connection as described before (see 第 5.4.5 节 “The PPPoE connection with pppoeconf”). Then, add the "/etc/network/interfaces" file entry for the primary PPPoE device eth0 as the following.

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 节 “The basic network configuration with ifupdown (legacy)” 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 节 “The basic network configuration with ifupdown (legacy)”, 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 节 “The modern network configuration for desktop”) installed, you do not need this package.

This package runs a daemon and replaces auto or allow-hotplug functionalities (see 表 5.10 “List of stanzas in "/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.

The ifupdown package offers advanced network configuration using the network configuration name and the network interface name. I use a terminology being slightly different from the one used in ifup(8) and interfaces(5).


Basic network configuration commands in 第 5.5.1 节 “The command syntax simplified” require the network configuration name token of the iface stanza to match the network interface name in the "/etc/network/interfaces".

Advanced network configuration commands enables separation of the network configuration name and the network interface name in the "/etc/network/interfaces" as the following.


We skipped explaining the mapping stanza in the "/etc/network/interfaces" in 第 5.5.2 节 “The basic syntax of "/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”).

Here is how to switch manually among several network configurations without rewriting the "/etc/network/interfaces" file as in 第 5.5.13 节 “The basic network reconfiguration” .

For all the network configuration you need to access, you create a separate stanza in "/etc/network/interfaces" file as the following.

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.

Now you are ready to switch the network configuration.

Let's move your PC to a LAN served by the DHCP. You bring up the network interface (the physical interface) eth0 by assigning the network configuration name (the logical interface name) config1 to it by the following.

$ sudo ifup eth0=config1
Password:
...

The interface eth0 is up, configured by DHCP and connected to LAN.

$ sudo ifdown eth0=config1
...

The interface eth0 is down and disconnected from LAN.

Let's move your PC to a LAN served by the static IP. You bring up the network interface eth0 by assigning the network configuration name config2 to it by the following.

$ sudo ifup eth0=config2
...

The interface eth0 is up, configured with static IP and connected to LAN. The additional parameters given as dns-* configures "/etc/resolv.conf" contents. This "/etc/resolv.conf" is better manged if the resolvconf package is installed.

$ sudo ifdown eth0=config2
...

The interface eth0 is down and disconnected from LAN, again.

Let's move your PC to a port on BB-modem connected to the PPPoE served service. You bring up the network interface eth0 by assigning the network configuration name pppoe to it by the following.

$ sudo ifup eth0=pppoe
...

The interface eth0 is up, configured with PPPoE connection directly to the ISP.

$ sudo ifdown eth0=pppoe
...

The interface eth0 is down and disconnected, again.

Let's move your PC to a location without LAN or BB-modem but with POTS and modem. You bring up the network interface ppp0 by assigning the network configuration name pots to it by the following.

$ sudo ifup ppp0=pots
...

The interface ppp0 is up and connected to the Internet with PPP.

$ sudo ifdown ppp0=pots
...

The interface ppp0 is down and disconnected from the Internet.

You should check the "/etc/network/run/ifstate" file for the current network configuration state of the ifupdown system.

[警告] 警告

You may need to adjust numbers at the end of eth*, ppp*, etc. if you have multiple network interfaces.

The ifupdown system automatically runs scripts installed in "/etc/network/*/" while exporting environment variables to scripts.


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 节 “The basic syntax of "/etc/network/interfaces"” for <address_family>, <method_name>, <option1> and <option2>.

The ifupdown-extra package (see 第 5.5.14 节 “The ifupdown-extra package”) 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 节 “The manually switchable network configuration”, 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.

You may use low level network commands as follows safely since they do not change network configuration.


[提示] 提示

Some of these low level network configuration tools reside in "/sbin/". You may need to issue full command path such as "/sbin/ifconfig" or add "/sbin" to the "$PATH" list in your "~/.bashrc".

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 节 “The network interface served by the 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 节 “The network interface with the static 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 节 “The PPPoE connection with pppoeconf”), 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 节 “The kernel module initialization”).


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.