8.2. 配置网络
基本知识必要的网络概念(以太网、IP地址、子网、广播)
Most modern local networks use the Ethernet protocol, where data is split into small blocks called frames and transmitted on the wire one frame at a time. Data speeds vary from 10 Mb/s for older Ethernet cards to 10 Gb/s in the newest cards (with the most common rate currently growing from 100 Mb/s to 1 Gb/s). The most widely used cables are called 10BASE-T, 100BASE-T, 1000BASE-T or 10GBASE-T depending on the throughput they can reliably provide (the T stands for “twisted pair”); those cables end in an RJ45 connector. There are other cable types, used mostly for speeds of 1 Gb/s and above.
An IP address is a number used to identify a network interface on a computer on a local network or the Internet. In the currently most widespread version of IP (IPv4), this number is encoded in 32 bits, and is usually represented as 4 numbers separated by periods (e.g. 192.168.0.1
), each number being between 0 and 255 (inclusive, which corresponds to 8 bits of data). The next version of the protocol, IPv6, extends this addressing space to 128 bits, and the addresses are generally represented as a series of hexadecimal numbers separated by colons (e.g., 2001:0db8:13bb:0002:0000:0000:0000:0020, or 2001:db8:13bb:2::20 for short).
A subnet mask (netmask) defines in its binary code which portion of an IP address corresponds to the network, the remainder specifying the machine. In the example of configuring a static IPv4 address given here, the subnet mask, 255.255.255.0
(24 “1”s followed by 8 “0”s in binary representation) indicates that the first 24 bits of the IP address correspond to the network address, and the other 8 are specific to the machine. In IPv6, for readability, only the number of “1”s is expressed; the netmask for an IPv6 network could, thus, be 64
.
The network address is an IP address in which the part describing the machine’s number is 0. The range of IPv4 addresses in a complete network is often indicated by the syntax, a.b.c.d/e, in which a.b.c.d is the network address and e is the number of bits affected to the network part in an IP address. The example network would thus be written: 192.168.0.0/24
. The syntax is similar in IPv6: 2001:db8:13bb:2::/64
.
A router is a machine that connects several networks to each other. All traffic coming through a router is guided to the correct network. To do this, the router analyzes incoming packets and redirects them according to the IP address of their destination. The router is often known as a gateway; in this configuration, it works as a machine that helps reach out beyond a local network (towards an extended network, such as the Internet).
The special broadcast address connects all the stations in a network. Almost never “routed”, it only functions on the network in question. Specifically, it means that a data packet addressed to the broadcast never passes through the router.
This chapter focuses on IPv4 addresses, since they are currently the most commonly used. The details of the IPv6 protocol are approached in 第 10.5 节 “IPV6”, but the concepts remain the same.
Since the network is automatically configured during the initial installation, the /etc/network/interfaces
file already contains a valid configuration. A line starting with auto
gives a list of interfaces to be automatically configured on boot by ifupdown and its /etc/init.d/networking
init script. This will often be eth0
, which refers to the first Ethernet card.
ALTERNATIVE NetworkManager
If Network Manager is particularly recommended in roaming setups (see 第 8.2.4 节 “Automatic Network Configuration for Roaming Users”), it is also perfectly usable as the default network management tool. You can create “System connections” that are used as soon as the computer boots either manually with a .ini
-like file in /etc/NetworkManager/system-connections/
or through a graphical tool (nm-connection-editor
). Just remember to deactivate all entries in /etc/network/interfaces
if you want Network Manager to handle them.
→ https://wiki.gnome.org/Projects/NetworkManager/SystemSettings/jessie
→ https://developer.gnome.org/NetworkManager/0.9/ref-settings.html
8.2.1. Ethernet Interface
If the computer has an Ethernet card, the IP network that is associated with it must be configured by choosing from one of two methods. The simplest method is dynamic configuration with DHCP, and it requires a DHCP server on the local network. It may indicate a desired hostname, corresponding to the hostname
setting in the example below. The DHCP server then sends configuration settings for the appropriate network.
例 8.1. DHCP configuration
- auto eth0
- iface eth0 inet dhcp
- hostname arrakis
A “static” configuration must indicate network settings in a fixed manner. This includes at least the IP address and subnet mask; network and broadcast addresses are also sometimes listed. A router connecting to the exterior will be specified as a gateway.
例 8.2. Static configuration
- auto eth0
- iface eth0 inet static
- address 192.168.0.3
- netmask 255.255.255.0
- broadcast 192.168.0.255
- network 192.168.0.0
- gateway 192.168.0.1
NOTE Multiple addresses
It is possible not only to associate several interfaces to a single, physical network card, but also several IP addresses to a single interface. Remember also that an IP address may correspond to any number of names via DNS, and that a name may also correspond to any number of numerical IP addresses.
As you can guess, the configurations can be rather complex, but these options are only used in very special cases. The examples cited here are typical of the usual configurations.
8.2.2. Connecting with PPP through a PSTN Modem
A point to point (PPP) connection establishes an intermittent connection; this is the most common solution for connections made with a telephone modem (“PSTN modem”, since the connection goes over the public switched telephone network).
A connection by telephone modem requires an account with an access provider, including a telephone number, username, password, and, sometimes the authentication protocol to be used. Such a connection is configured using the pppconfig
tool in the Debian package of the same name. By default, it sets up a connection named provider
(as in Internet service provider). When in doubt about the authentication protocol, choose PAP: it is offered by the majority of Internet service providers.
After configuration, it is possible to connect using the pon
command (giving it the name of the connection as a parameter, when the default value of provider
is not appropriate). The link is disconnected with the poff
command. These two commands can be executed by the root user, or by any other user, provided they are in the dip
group.
8.2.3. Connecting through an ADSL Modem
The generic term “ADSL modem” covers a multitude of devices with very different functions. The modems that are simplest to use with Linux are those that have an Ethernet interface (and not only a USB interface). These tend to be popular; most ADSL Internet service providers lend (or lease) a “box” with Ethernet interfaces. Depending on the type of modem, the configuration required can vary widely.
8.2.3.1. Modems Supporting PPPOE
Some Ethernet modems work with the PPPOE protocol (Point to Point Protocol over Ethernet). The pppoeconf
tool (from the package with the same name) will configure the connection. To do so, it modifies the /etc/ppp/peers/dsl-provider
file with the settings provided and records the login information in the /etc/ppp/pap-secrets
and /etc/ppp/chap-secrets
files. It is recommended to accept all modifications that it proposes.
Once this configuration is complete, you can open the ADSL connection with the command, pon dsl-provider
and disconnect with poff dsl-provider
.
TIP Starting ppp
at boot
PPP connections over ADSL are, by definition, intermittent. Since they are usually not billed according to time, there are few downsides to the temptation of keeping them always open. The standard means to do so is to use the init system.
The default init system on Jessie is systemd
. Adding an automatically restarting task for the ADSL connection is a simple matter of creating a “unit file” such as /etc/systemd/system/adsl-connection.service
, with contents such as the following:
- [Unit]
- Description=ADSL connection
- [Service]
- Type=forking
- ExecStart=/usr/sbin/pppd call dsl-provider
- Restart=always
- [Install]
- WantedBy=multi-user.target
Once this unit file has been defined, it needs to be enabled with systemctl enable adsl-connection
. Then the loop can be started manually with systemctl start adsl-connection
; it will also be started automatically on boot.
On systems not using systemd
(including Wheezy and earlier versions of Debian), the standard SystemV init works differently. On such systems, all that is needed is to add a line such as the following at the end of the /etc/inittab
file; then, any time the connection is disconnected, init
will reconnect it.
- adsl:2345:respawn:/usr/sbin/pppd call dsl-provider
For ADSL connections that auto-disconnect on a daily basis, this method reduces the duration of the interruption.
8.2.3.2. Modems Supporting PPTP
The PPTP (Point-to-Point Tunneling Protocol) protocol was created by Microsoft. Deployed at the beginning of ADSL, it was quickly replaced by PPPOE. If this protocol is forced on you, see 第 10.2.4 节 “PPTP”.
8.2.3.3. Modems Supporting DHCP
When a modem is connected to the computer by an Ethernet cable (crossover cable) you typically configure a network connection by DHCP on the computer; the modem automatically acts as a gateway by default and takes care of routing (meaning that it manages the network traffic between the computer and the Internet).
BACK TO BASICS Crossover cable for a direct Ethernet connection
Computer network cards expect to receive data on specific wires in the cable, and send their data on others. When you connect a computer to a local network, you usually connect a cable (straight or crossover) between the network card and a repeater or switch. However, if you want to connect two computers directly (without an intermediary switch or repeater), you must route the signal sent by one card to the receiving side of the other card, and vice-versa. This is the purpose of a crossover cable, and the reason it is used.
Note that this distinction has become almost irrelevant over time, as modern network cards are able do detect the type of cable present and adapt accordingly, so it won’t be unusual that both kinds of cable will work in a given location.
Most “ADSL routers” on the market can be used like this, as do most of the ADSL modems provided by Internet services providers.
8.2.4. Automatic Network Configuration for Roaming Users
Many Falcot engineers have a laptop computer that, for professional purposes, they also use at home. The network configuration to use differs according to location. At home, it may be a wifi network (protected by a WPA key), while the workplace uses a wired network for greater security and more bandwidth.
To avoid having to manually connect or disconnect the corresponding network interfaces, administrators installed the network-manager package on these roaming machines. This software enables a user to easily switch from one network to another using a small icon displayed in the notification area of their graphical desktop. Clicking on this icon displays a list of available networks (both wired and wireless), so they can simply choose the network they wish to use. The program saves the configuration for the networks to which the user has already connected, and automatically switches to the best available network when the current connection drops.
In order to do this, the program is structured in two parts: a daemon running as root handles activation and configuration of network interfaces and a user interface controls this daemon. PolicyKit handles the required authorizations to control this program and Debian configured PolicyKit in such a way so that members of the netdev group can add or change Network Manager connections.
Network Manager knows how to handle various types of connections (DHCP, manual configuration, local network), but only if the configuration is set with the program itself. This is why it will systematically ignore all network interfaces in /etc/network/interfaces
for which it is not suited. Since Network Manager doesn’t give details when no network connections are shown, the easy way is to delete from /etc/network/interfaces
any configuration for all interfaces that must be managed by Network Manager.
Note that this program is installed by default when the “Desktop Environment” task is chosen during initial installation.
ALTERNATIVE Configuration by “network profile”
More advanced users may want to try the guessnet package for automatic network configuration. A group of test scripts determine which network profile should be activated and configure it on the fly.
Users who prefer to manually select a network profile will prefer the netenv program, found in the package of the same name.