Now it's time to put all of the previous theoretical knowledge into practice. Next, we will explore:
? Configuration file for network interface in Red Hat 7
? Using Ifconfig to check and configure interfaces
? Adding and editing static routes
? Using Netstat to monitor/troubleshoot network interfaces
A Key files and scripts
The actual process of network interface initialization (sometimes referred to as "Startup Interface") is controlled by a set of profiles and scripts, most of which are in the/ETC directory. These profiles tell Linux its IP address, host name, and domain name, and the script is responsible for initializing the network interface.
Unfortunately, there is still no uniform standard for file locations and naming conventions in different distributions (distribution). To provide a concrete example for this article, a description is made based on the widely used Red Hat 7.0 package. Keep in mind that if your distribution is not Red hat or is not based on Red Hat conventions (such as the Mandrake distribution), some of the files referenced here are in a different directory or have other names. However, the net effect is the same as the actual effect? Pass the network information stored in a series of configuration files to the script, and then initialize the interfaces and network routes with scripts.
Key files that involve initializing and configuring the network interface are:
? /etc/hosts (map host name to IP address)
? /etc/sysconfig/network (turn on or off networking, set host name and gateway)
? /etc/resolv.conf (Set the IP address of the name server or DNS server)
? /etc/rc.d/rc3.d/s10network (activates the configured Ethernet interface at boot time, called by the symbolic link in the run level directory/etc/rc.d/rcn.d/)
? A collection of some files in the/etc/sysconfig/network-scripts. These files include the primary configuration for network connectivity and symbolic links that provide interface status and control functionality.
/etc/hosts
/etc/hosts is a simple text file that links IP addresses to host names. Each computer in a TCP/IP network must have a unique IP address. The Hosts file only allows the user to associate the hostname with the IP address so that the name is used when accessing the computer, instead of entering a long string of digits. Each entry in the/etc/hosts contains an IP address, a blank, and a host name and/or alias in turn. The Well sign (#) indicates the beginning of the comment. For example:
#/etc/hosts
# Last Updated 12/3/2000
127.0.0.1 loopback localhost # loopback (lo0) name/address
192.168.1.5 www.163.com www
192.168.1.6 ftp.163.com FTP
192.168.1.7 www1.163.com WWW1
192.168.1.8 bbs.163.com BBS
In the Hosts file shown above, the IP address 192.168.1.5 maps to the host www.163.com and assigns it an alternate host name (or alias) www. Although DNS has superseded this host file, it is still used for the following reasons:
? Most systems have a small host table that contains the name and address information of a critical host on the local network. This table is used when DNS is not running, for example, during the initial system startup. Even if the local DNS server is running, there should be a small hosts file on each system that contains a host itself, a localhost entry, and any major gateways and server entries on the local area network.
? Small networks that are not connected to the Internet or other networks do not require DNS services. However, hosts on these networks also need to know how to locate other hosts on the same network.
/etc/sysconfig/network
The/etc/sysconfig/network is used to specify the desired network configuration information, and there are several scripts to use when booting. The file can contain one or more of the keyword/value pairs shown below:
Networking=yes|no-YES means you need to configure your network; no indicates that you do not need to configure your network.
Hostname=hostname? The fully qualified domain name of the host; This should match the host item in/etc/hosts for compatibility with older programs.
Gateway=gw-ip? The IP address of the network gateway.
Gatewaydev=gw-dev? The name of the gateway device (for example, eth0).
Nisdomain=dom-name? Represents the NIS domain, if any.
The following is an example of a/etc/sysconfig/network minimum configuration:
Networking=yes
Hostname=phoenix.syroidmanor.com
gateway=192.168.1.1
/etc/resolv.conf and/etc/rc.d/rc3.d/s10network
/etc/resolv.conf is one of the key files used by the network to determine host resolution. You can identify up to three name servers and, if the server that is listed first is not responding to the query, the next two will play a standby role. Domain entry defines the default domain name. Parser (Incidentally, the parser is not a separate process, but an example called by a network process threading) attaches the domain name listed here to any host query that does not contain a period.
#/etc/resolv.conf
# Domain Name Resolver config file
Domain 163.com
NameServer 192.168.1.7
NameServer 192.168.1.10
NameServer 165.142.268.19
In the example shown above, if the query submitted to the parser is a query to find the host Phoenix (note that there is no point), the field is appended to the request, which expands the query to phoenix.163.com. For more details and options to use, please enter Man resolv.conf.
/etc/rc.d/rc3.d/s10network is a symbolic link to a/etc/rc.d/init.d/network script. When the system reaches the run Level 3 o'clock, it is responsible for initializing all configured network interfaces. Here we don't want to take the time to discuss the logic behind the file in detail, because the file is essentially invoking the other scripts and programs mentioned in this section. However, if you are interested in the order in which various network components and services are initialized in S10network, you can use the Less/etc/rc.d/rc3.d/s10network command to peruse it.
/etc/sysconfig/network-scripts/Directory
Finally, you can usually find the following files in the/etc/sysconfig/network-scripts/directory:
? /etc/sysconfig/network-scripts/ifup
? /etc/sysconfig/network-scripts/ifdown
? /etc/sysconfig/network-scripts/network-functions
? /etc/sysconfig/network-scripts/ifcfg-interface-name
? /etc/sysconfig/network-scripts/ifcfg-interface-name:clone-name
? /etc/sysconfig/network-scripts/chat-interface-name
? /etc/sysconfig/network-scripts/dip-interface-name
? /etc/sysconfig/network-scripts/ifup-post
In the next few pages, we'll look at these key files briefly to see what they do and what they contain.
.../network-scripts/, the ifup and Ifdown items in the first part
/etc/sysconfig/network-scripts are actually symbolic links that point to/sbin/ifup and/sbin/ifdown respectively. These two scripts are the only scripts in the directory that should be called directly, and they call all other scripts as needed.
Ifup and Ifdown typically take only one parameter: a device name (for example, eth0). The system calls them with the parameter "boot" during the boot process so that devices that are not configured to initialize at system startup are not activated (see the following about the Onboot=no in the interface-name description).
Network-function is not a public file. It contains the functions required by several scripts in this directory. Specifically, it contains most of the code used to handle the configuration of an alternate interface.
.../network-scripts/, part two
configuration files Ifcfg-interface-name and ifcfg-interface-name:clone-name contain most of the details required to initialize the interface. The first file defines the interface, and the second file contains only the partial definitions associated with the alias (or substitution) interface. For example, the network address may be different, but others might be the same.
The items defined in the Ifcfg file depend on the interface type; The following values are common:
? Device=name, where name is the physical device name
? IPADDR=ADDR, where addr is the IP address
? Netmask=mask, where mask is the netmask value
? NETWORK=ADDR, where addr is the network address
? BROADCAST=ADDR, where addr is the broadcast address
? GATEWAY=ADDR, where addr is the gateway address
? Onboot=answer, where answer is "yes" (activating the device at boot time) or "No"
? Userctl=answer, where answer is "yes" (not the root user can control the device) or "No"
? Bootproto=proto, where Proto takes one of the following values: "None" (No protocol at boot) "BOOTP" (using the BOOTP protocol) or "DHCP" (using the DHCP protocol)
In addition, the following values are used for all SLIP (serial line IP) file is public:
? Persist=answer, where answer is "yes" (even if the modem has hung up the connection, keeping the device active) or "no" (inactive)
? Modemport=port, where port is the device name for the modem port (for example,/dev/modem)
? Linespeed=baud, where baud is the line speed of the modem
? Defabort=answer, where answer is "yes" (insert the default exception termination string when creating/editing the script for this interface) or "no" (without inserting the default exception termination string)
.../network-scripts/, Part III
The Chat-interface-name file is a conversation script for SLIP connections (chat scripts). Its function is to start the SLIP connection. For SLIP devices, DIP scripts are written based on this conversation script.
Chat-interface-name is a write-only script that is created by program Netcfg based on a conversation script. Do not modify the file.
When any network device (except the SLIP device) is initialized, the/etc/sysconfig/network-scripts/ifup-post is invoked. It invokes/etc/sysconfig/network-scripts/ifup-routes to start a static route that depends on the device, it also initiates any aliases configured for the device, and sets the hostname if the host name has not been set? This finds the host name that matches the IP address of the device. Finally, Ifup-post sends a signal (Sigio) to any program that requests notification of network events.
Two Ifconfig Program
The ifconfig command sets, checks, or monitors the configuration values of network interfaces. It can also be used to set the "state" of an interface? "Up" (boot) or "down" (off). A simple call to Ifconfig is:
Ifconfig interface-name ip-address Up|down
This activates the specified interface and assigns the provided IP address to it.
Ifconfig has many options available (metric, MTU, Pointtopoint, and so on; For more information, see the Help page) to explicitly set unique interface parameters, but generally, providing interface names (for example, eth0), IP addresses, and netmask is sufficient. For example:
Ifconfig eth0 192.168.1.5 netmask 255.255.255.0 up
The IP assigned to the interface eth0 is 192.168.1.5, and the netmask is 255.255.255.0 and "Start the interface" or initialize it. Similarly, if the interface is "closed", enter ifconfig eth0 down; you do not need to specify an IP and netmask.
Using Ifconfig to check the interface
Running a ifconfig with no parameters causes the program to display the status of all network interfaces. To check the state of a particular interface, append the name of the interface after ifconfig. For example:
[Root@localhost root]#/sbin/ifconfig eth0
Eth0 Link encap:ethernet hwaddr 00:10:5a:00:8
inet addr:192.168.1.20 bcast:192.168.1.255 mask:255.255.255.0
Up broadcast RUNNING multicast mtu:1500 metric:1
RX packets:9625272 errors:0 dropped:0 overruns:0 frame:0
TX packets:6997276 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
Interrupt:19 Base address:0xc800
The output above shows the MAC address (HWADDR), Assigned IP address (inet addr), broadcast address (bcast), and Netmask (Mask). You can also see that the interface is in the up state with an MTU of 1500 and Metric 1. The next two lines give statistics on the number of packets received (RX) and sent (TX) and the number of errors, discards, and overflow packets. The last two lines show the number of conflicting packets, the Send Queue size (Txqueuelen) and IRQ, and the base address of the card.
Three Configuring routing
Let's look at how to configure a route by looking at the network interface that has not yet been configured for the gateway. As you can see, using the route command with no parameters displays the kernel routing table.
[Root@localhost root]#/sbin/route
Kernel IP Routing Table
Destination Gateway genmask Flags Metric Ref use Iface
127.0.0.0 127.0.0.1 255.0.0.0 U 0 0 0 Lo
192.168.1.0 192.168.1.5 255.255.255.0 U 0 0 0 eth0
The first entry is the loopback route to the localhost, which is created automatically when LO is configured. The second item is the route through the interface eth0 to the network 192.168.1.0. Address 192.168.1.5 is not a remote gateway address. It is the address assigned to Phoenix Eth0.
Note the logo for each item. They all set the U (boot) flag, which means that they are ready to be used, but none of them have a G (gateway) flag set. The G flag is not set because these two routes are routed through the local interface rather than through the external gateway.
The above example contains only one network route, 192.168.1.0. Thus Phoenix can only communicate with hosts located in the 192.168.1.0 network.
adding static routes
The smallest routing table allows only hosts in the same network to communicate with each other. To communicate with a remote host, you must add a route through an external gateway to the routing table. One way to achieve this is by using the/sbin/route command. Take the example in the previous page, we will now add the routing 192.168.1.1 to the network configuration.
[root@localhost root]#/sbin/route Add default 192.168.1.1 1
In the example above, the first argument after the route command is the keyword Add. The first keyword on the route command is either add or del (delete route). The next value is the destination address, which is the address that is reached through the route. If the keyword default is used for the destination address, the default route is created. Use the default route as long as there is no specific route to the destination; Typically, this is the only item you need in the routing table. If there is only one gateway in the network, the default route is used to boot all data traffic to the remote network through that gateway.
The next parameter on this command line is the gateway address. This address must be the gateway address of the network directly connected to this computer. In the network path to the remote destination, TCP/IP routing specifies the next hop (next-hop). This next relay must be directly accessible to the local machine, so it must be directly connected to the network on which the computer resides.
Note: Because most routes are added early in the system startup process, it is recommended that you replace the host name with the IP address of the number. Doing so ensures that the routing configuration does not depend on the state of the name server. And make sure that you always use the full numeric address (4 bytes), and that if you don't use the full IP address, the route can only guess some of the IP addresses, which may cause incorrect configuration.
In the route command on the previous page, the last parameter is the number 1, called the routing metric (routing metric). This metric parameter is not required when routing is removed, but it is required by many systems when adding routes. Although metrics are required, route uses it only to determine whether the route is through a direct-attached interface or through an external gateway. If the metric is 0, this route is established through the native interface and does not set the G flag, and if the metric value is greater than 0, the route is established with G flags and the gateway address is considered external. Static routes do not use other metric values. The real need for multiple metric values is dynamic routing.
To display a new routing table, enter/sbin/route or use the NETSTAT-RN command
[Root@localhost root]# Netstat-rn
Kernel IP Routing Table
Destination Gateway genmask Flags MSS Window Irtt iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 Lo
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
Finally, do a test to prove that everything is running as we talked about, ping a host on another network, and you should be able to receive a response. If you do not receive a response, review your configuration.
To familiarize yourself with route other options and parameters, enter man route.
Four Netstat program
If you manage a TCP/IP network of any size, the Netstat program is a valuable tool. It can display the kernel routing table, the status of the active network connection, and some useful statistics for each installed network interface.
Like most Linux management command-line programs, netstat can select the amount of detail and/or range of information displayed by the additional options or flags that follow it. Some common options are:
-A? Displays information for all connections, including those that are listening
-I? Show statistics for all configured network devices
-C? Continuously Update network status (once per second) until aborted (^C)
-R? Show Kernel routing table
-n? Display remote and local addresses in number (original) format instead of resolved names
-T? Show only TCP socket information (excluding any UCP socket information)
-V? Display version information for netstat
Enter the man netstat to get a complete list of all available flags and to specify the purpose of each logo. Note that you can also combine these flags, so entering NETSTAT-RN will display the System routing table (r) for local and remote hosts (n) in the original IP address format.
Show Active Network Connections
Netstat supports a set of options for displaying active or inactive sockets:-T,-u,-w, and-X displays active TCP, UDP, RAW, or UNIX socket connections, respectively. If you add the-a flag, a socket is also displayed that waits for a connection (in other words, is listening). This will show you all the servers that are now running on the system.
For example, entering Netstat-ta on a host Phoenix displays the following:
[Root@localhost root]# Netstat-ta
Active Internet connections (servers and established)
Proto recv-q send-q Local address Foreign
TCP 0 PHOENIX.SYROIDMANOR:SSH 192.168.1.5:1132 established
TCP 0 0 *:ssh *:* LISTEN
TCP 0 0 phoenix.syroidmano:1028 HYDRAS.SYRO:NETBIOS-SSN established
TCP 0 0 phoenix.syroidmano:1027 RAIDSERVER:NETBIOS-SSN established
TCP 0 0 *:p rinter *:* LISTEN
TCP 0 0 *:auth *:* LISTEN
TCP 0 0 *:1024 *:* LISTEN
TCP 0 0 *:sunrpc *:* LISTEN
The above output shows that most servers are only waiting for incoming connections (LISTEN). However, the first line shows the connection between the host Phoenix and the IP address 192.168.1.5, and the third and fourth lines show two NetBIOS connections (Samba SMB sharing).
View the routing table with Netstat
When using the-R flag, netstat displays the routing table in the kernel, which is similar to the input/sbin/route:
[Root@localhost root]# Netstat-nr
Kernel IP Routing Table
Destination Gateway genmask Flags MSS Window Irtt iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 Lo
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
The-N option forces netstat to output addresses in the form of a dotted four-group IP number, rather than as a host and network name. This option is especially useful when you do not want to address queries over the network (for example, with DNS or NIS servers).
The second column shows the gateway that is pointed to in the route item. If the gateway is not used, an asterisk is displayed. The third column is the network mask for the route. The kernel makes routing "generic" by comparing the IP address of the packet to the destination IP address of the route and genmask the IP address of the packet.
The fourth column shows the flags for the route: U means active, H represents the host, G represents the gateway, D represents the dynamic route, and M says it has been modified.
[Root@localhost root]# Netstat-nr
Kernel IP Routing Table
Destination Gateway genmask Flags MSS Window Irtt iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 Lo
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
The next three columns display the MSS, Window, and Irtt, which will be applied to the TCP connection established through the route. The MSS (Maximum Segment size) represents the maximum fragment size, and is the maximum size that the kernel constructs to send datagrams over that route. Window indicates that the system receives the maximum burst of data from a remote host at a time.
The acronym IRTT represents "initial round-trip time (initial round trip Tim)". The TCP protocol ensures that data is sent reliably between hosts and is sent back if the data has been lost. The TCP protocol keeps a count of the time spent on datagrams sent to the remote endpoint and the acknowledgment received, in order to know the time to wait before the datagram is supposed to be sent back; The TCP protocol uses the time used for the first connection to establish the value of the initial round trip time. For most types of networks, the default is enough, but for some slow networks (especially some amateur packet radio networks), this time is too short to cause unnecessary postbacks. You can use the route command to set the Irtt value. In the above routing table, these fields are all zero values, indicating that the default values are being used.
Finally, this field represents the network interface used by the displayed route.
Use Netstat to display statistics used by some network interfaces
Call Netstat with the-I option to display some useful statistics for all configured interfaces? This is a very useful tool for troubleshooting network failures. With this command, it is easy to check the status of the connection and whether the connection is "normal".
[Root@localhost root]# Netstat-i
Kernel Interface Table
Eth0 Link encap:ethernet hwaddr 00:10:5a:00:8
inet addr:192.168.1.20 bcast:192.168.1.255 mask:255.255.255.0
Up broadcast RUNNING multicast mtu:1500 metric:1
RX packets:10554374 errors:0 dropped:0 overruns:0 frame:0
TX packets:8528339 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
Interrupt:19 Base address:0xc800
Lo Link encap:local Loopback
inet addr:127.0.0.1 mask:255.0.0.0
Up loopback RUNNING mtu:3924 metric:1
RX packets:5612 errors:0 dropped:0 overruns:0 frame:0
TX packets:5612 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
The
RX packets and TX packets rows show the packets that have been received or the number of packets that have been sent, the packets that have gone wrong, the missing packets, and the RX/TX statistics that have been spilled. The most common interface errors are due to improper configuration, so if you encounter some difficulties, it is best to double-check all settings for diagnostics.
If the interface is already started, there should be no packets queued to be sent (Txqueuelen)? If so, there may be a problem with the network cable or NIC. First swap a spare cable, and then check the connection again. The RX/TX error should be near zero. If there are too many TX errors, the network is saturated or there is a problem with the physical connection, or if there are too many RX errors, the network is saturated, there is a problem with the physical connection, or the host is overloaded. If you encounter an excessively high rate of conflict (the rate of conflict is the percentage of the output packet, rather than the total number of Send/Receive packets), it may also indicate that the network is saturated, and that the netstat-i is performed from another host on the same subnet. Command and compare the results to confirm this.
To resolve errors in the network, it is necessary to carefully and systematically analyze all aspects of the interface (Hardware and software).