Previous Section
 < Day Day Up > 
Next Section


Network Interfaces and Routes: ifconfig and route

Your connection to a network is made by your system through a particular hardware interface, such as an Ethernet card or a modem. Data passing through this interface is then routed to your network. The ifconfig command configures your network interfaces, and the route command sets up network connections accordingly. If you configure an interface with a network configuration tool, such as redhat-config-network, you needn't use ifconfig or route. However, you can directly configure interfaces using ifconfig and route, if you want. Every time you start your system, the network interfaces and their routes must be established. This is done automatically for you when you boot up by ifconfig and route commands executed for each interface by the /etc/rc.d/init.d/network initialization file, which is executed whenever you start your system. If you are manually adding your own interfaces, you must set up the network script to perform the ifconfig and route operations for your new interfaces.

Network Startup Script: /etc/rc.d/init.d/network

On Red Hat, your network interface is started up using the network script in the /etc/rc.d/init.d directory. This script will activate your network interface cards (NICs) as well as implement configuration information such as gateway, host, and name server identities. You can manually shut down and start your network interface using this script and the restart, start, or stop options. You can run the script on Red Hat with the service command. The following commands shut down and then start up your network interface:

service network stop
service network start

If you are changing network configuration, you will have to restart your network interface for the changes to take effect:

service network restart

To test if your interface is working, use the ping command with an IP address of a system on your network, such as your gateway machine. The ping command continually repeats until you stop it with a CTRL-C.

ping 192.168.0.1

Interface Configuration Scripts: /etc/sysconfig/network-scripts

The /etc/rc.d/init.d/network file performs the startup operations by executing several specialized scripts located in the /etc/sysconfig/network-scripts directory. The network script uses a script in that directory called ifup to activate a network connection, and ifdown to shut it down. ifup and ifdown will invoke other scripts tailored to the kind of device being worked on, such as ifup-ppp for modems using the PPP protocol, or ifup-ipv6 for network devices that use IP Protocol version 6 addressing.

Note 

You can activate and deactivate network interfaces using the Network Device Control tool accessible from the System Tools menu.

The ifup and ifdown scripts make use of interface configuration files that bear the names of the network interfaces currently configured, such as ifcfg-eth0 for the first Ethernet device. These files define shell variables that hold information on the interface, such as whether to start them at boot time. For example, the ifcfg-eth0 file holds definitions for NETWORK, BROADCAST, and IPADDR, which are assigned the network, broadcast, and IP addresses that the device uses.

The ifdown and ifup scripts, in turn, hold the ifconfig and route commands to activate scripts using these variables defined in the interface configuration files. If you want to manually start up an interface with ifup, you simply use the interface configuration file as its argument. The following command starts up the second Ethernet card:

cd /etc/sysconfig/network-scripts
ifup ifcfg-eth1

Interface configuration files are automatically generated when you configure your network connections, such as with a distribution's network administrative tool or third-party tools like rp3 or Webmin. You can also manually edit these interface configuration files, making changes such as whether to start up the interface at boot or not (though using a configuration tool such as redhat-config-network is easier). A sample ifcfg-eth0 file is shown here using a static IP address. For DHCP set BOOTPROTO to dhcp.

/etc/sysconfig/network-scripts/ifcfg-eth0
Start example
DEVICE=eth0                     
BOOTPROTO=static
BROADCAST=192.168.0.255
IPADDR=192.168.0.1
NETMASK=255.255.255.0
NETWORK=192.168.0.0
ONBOOT=yes
End example

ifconfig

The ifconfig command takes as its arguments the name of an interface and an IP address, as well as options. The ifconfig command then assigns the IP address to the interface. Your system now knows that such an interface exists and that it references a particular IP address. In addition, you can specify whether the IP address is a host address or a network address. You can use a domain name for the IP address, provided the domain name is listed along with its IP address in the /etc/hosts file. The syntax for the ifconfig command is as follows:

# ifconfig interface -host_net_flag address options

The host_net_flag can be either -host or -net to indicate a host or network IP address. The -host flag is the default. The ifconfig command can have several options, which set different features of the interface, such as the maximum number of bytes it can transfer (mtu) or the broadcast address. The up and down options activate and deactivate the interface. In the next example, the ifconfig command configures an Ethernet interface:

# ifconfig eth0 192.168.0.1

For a simple configuration such as this, ifconfig automatically generates a standard broadcast address and netmask. The standard broadcast address is the network address with the number 255 for the host address. For a class C network, the standard netmask is 255.255.255.0, whereas for a class A network, the standard netmask is 255.0.0.0. If you are connected to a network with a particular netmask and broadcast address, however, you must specify them when you use ifconfig. The option for specifying the broadcast address is broadcast; for the network mask, it is netmask. Table 38-11 lists the different ifconfig options. In the next example, ifconfig includes the netmask and broadcast address:

# ifconfig eth0 192.168.0.1 broadcast 192.168.0.255 netmask 255.255.255.0
Table 38-11: The ifconfig Options

Option

Description

Interface

Name of the network interface, such as eth0 for the first Ethernet device or ppp0 for the first PPP device (modem)

up

Activates an interface; implied if IP address is specified

down

Deactivates an interface

allmulti

Turns on or off the promiscuous mode; preceding hyphen (-) turns it off; this allows network monitoring

mtu n

Maximum number of bytes that can be sent on this interface per transmission

dstaddr address

Destination IP address on a point-to-point connection

netmask address

IP network mask; preceding hyphen (-) turns it off

broadcast address

Broadcast address; preceding hyphen (-) turns it off

point-to-point address

Point-to-point mode for interface; if address is included, it is assigned to remote system

hw

Sets hardware address of interface

Address

IP address assigned to interface

Once you configure your interface, you can use ifconfig with the up option to activate it and with the down option to deactivate it. If you specify an IP address in an ifconfig operation, as in the preceding example, the up option is implied.

# ifconfig eth0 up

Point-to-point interfaces such as Parallel IP (PLIP), Serial Line IP (SLIP), and Point-to-Point Protocol (PPP) require you to include the pointopoint option. A PLIP interface name is identified with the name plip with an attached number. For example, plip0 is the first PLIP interface. SLIP interfaces use slip0. PPP interfaces start with ppp0. Point-to-point interfaces are those that usually operate between only two hosts, such as two computers connected over a modem. When you specify the pointopoint option, you need to include the IP address of the host. In the next example, a PLIP interface is configured that connects the computer at IP address 192.168.1.72 with one at 204.166.254.14. If domain addresses were listed for these systems in /etc/hosts, those domain names could be used in place of the IP addresses.

# ifconfig plip0 192.168.1.72 pointopoint 204.166.254.14

If you need to, you can also use ifconfig to configure your loopback device. The name of the loopback device is lo, and its IP address is the special address 127.0.0.1. The following example shows the configuration:

# ifconfig lo 127.0.0.1

The ifconfig command is useful for checking on the status of an interface. If you enter the ifconfig command along with the name of the interface, information about that interface is displayed:

# ifconfig eth0

To see if your loopback interface is configured, you can use ifconfig with the loopback interface name, lo:

# ifconfig lo
   
lo Link encap:Local Loopback
 inet addr:127.0.0.1 Bcast:127.255.255.255 Mask:255.0.0.0
 UP BROADCAST LOOPBACK RUNNING MTU:2000 Metric:1
 RX packets:0 errors:0 dropped:0 overruns:0
 TX packets:12 errors:0 dropped:0 overruns:0

Routing

A packet that is part of a transmission takes a certain route to reach its destination. On a large network, packets are transmitted from one computer to another until the destination computer is reached. The route determines where the process starts and to what computer your system needs to send the packet for it to reach its destination. On small networks, routing may be static-that is, the route from one system to another is fixed. One system knows how to reach another, moving through fixed paths. On larger networks and on the Internet, however, routing is dynamic. Your system knows the first computer to send its packet off to, and then that computer takes the packet from there, passing it on to another computer, which then determines where to pass it on. For dynamic routing, your system needs to know little. Static routing, however, can become complex because you have to keep track of all the network connections.

Your routes are listed in your routing table in the /proc/net/route file. To display the routing table, enter route with no arguments (the netstat -r command will also display the routing table):

# route
Kernel routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
loopback * 255.0.0.0 U 0 0 12 lo
pango1.train.com * 255.255.255.0 U 0 0 0 eth0

Each entry in the routing table has several fields, providing information such as the route destination and the type of interface used. The different fields are listed in Table 38-12.

Table 38-12: The ifconfig Options

Field

Description

Destination

Destination IP address of the route

Gateway

IP address or hostname of the gateway the route uses; * indicates no gateway is used

Genmask

The netmask for the route

Flags

Type of route: U = up, H = host, G = gateway, D = dynamic, M = modified

Metric

Metric cost of route

Ref

Number of routes that depend on this one

Window

TCP window for AX.25 networks

Use

Number of times used

Iface

Type of interface this route uses

You should have at least one entry in the routing table for the loopback interface. If not, you must route the loopback interface using the route command. The IP address for an interface has to be added to the routing table before you can use that interface. You add an address with the route command and the add option:

route add address

The next example adds the IP address for the loopback interface to the routing table:

route add 127.0.0.1

With the add argument, you can add routes either for networks with the -net option or with the -host option for IP interfaces (hosts). The -host option is the default. In addition, you can then specify several parameters for information, such as the netmask (netmask), the gateway (gw), the interface device (dev), and the default route (default). If you have more than one IP interface on your system, such as several Ethernet cards, you must specify the name of the interface using the dev parameter. If your network has a gateway host, you use the gw parameter to specify it. If your system is connected to a network, at least one entry should be in your routing table that specifies the default route. This is the route taken by a message packet when no other route entry leads to its destination. The following example is the routing of an Ethernet interface:

# route add 192.168.1.2 dev eth0

If your system has only the single Ethernet device as your IP interface, you could leave out the dev eth0 parameter:

# route add 192.168.1.2

You can delete any route you establish by invoking ifconfig with the del argument and the IP address of that route, as in this example:

# route del 192.168.1.2

For a gateway, you first add a route to the gateway interface, and then add a route specifying that it is a gateway. The address of the gateway interface in this example is 192.168.1.1:

# route add 192.168.1.1
# route add default gw 192.168.1.1

If you are using the gateway to access a subnet, add the network address for that network (in this example, 192.168.23.0):

# route add -net 192.168.23.0 gw dev eth1

To add another IP address to a different network interface on your system, use the ifconfig and route commands with the new IP address. The following command configures a second Ethernet card (eth1) with the IP address 192.168.1.3:

ifconfig eth1 192.168.1.3
route add 192.168.1.3 dev eth1


Previous Section
 < Day Day Up > 
Next Section
This HTML Help has been published using the chm2web software.