-->

Previous | Table of Contents | Next

Page 254

Using ifconfig

ifconfig is the tool used to set up and configure your network card. If you used the Red Hat installation package, you might already have this configured for you. You should, however, understand this command in the event you need to configure the network by hand after a system crash.

The purpose of ifconfig is to set up and configure the network interfaces. Typically, this is done early in the boot sequence so that any other network services that need to be started know how to communicate with the rest of the world.

The format of the ifconfig command is as follows:


ifconfig    interface    IP_address    options

NOTE
The ifconfig command takes parameters in a slightly different way than most commands. Each parameter should not be prefixed by a minus sign (-) unless you are turning that function off. If you are setting a parameter, such as the netmask, simply use netmask followed by the netmask you want to set—for example, netmask 255.255.255.0.

interface is the network device that you want to set up. If you are using Ethernet, this will be eth followed by a number designating which Ethernet card you are using. The numbering starts from zero (the first Ethernet card in your system will be eth0). IP_address is the address that you want to assign to your machine. Use dotted notation and remember not to assign it the network address or broadcast address.

These are the only required options for configuring a network device. ifconfig will use the default netmask and broadcast address based on the class of your IP_address setting. The default netmask and broadcast addresses, however, will only be correct if you are subnetting along an 8-bit boundary.

To set the netmask and broadcast address, use the parameters netmask and broadcast, respectively, followed by the address you want to set for them.

Here is a sample ifconfig:


ifconfig   eth0   192.168.42.2   netmask   255.255.255.0   broadcast   192.168.42.255

As with most other UNIX commands, there is no output if it is successful. To see what the currently configured cards are, simply run ifconfig without any parameters:


[root@vestax /root]# ifconfig

Page 255


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:3584  Metric:1

          RX packets:10984 errors:0 dropped:0 overruns:0

          TX packets:10984 errors:0 dropped:0 overruns:0



eth0      Link encap:10Mbps Ethernet  HWaddr 00:60:97:C3:D0:C9

          inet addr:192.168.42.2 Bcast:192.168.42.255 Mask:255.255.255.0

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:1995581 errors:1 dropped:1 overruns:0

          TX packets:1361726 errors:0 dropped:0 overruns:0

          Interrupt:11 Base address:0x6100

The first network card listed here is the lo device, which is the loopback device. I'm not terribly interested in that entry, although it should be there. The second entry shows how the eth0 network card is currently configured. The Link encap tells you that it is configured as a 10Mbps Ethernet card. The HWaddr tells you the hardware address of that particular Ethernet card (also known as the MAC address). On the second line of the output is the inet addr. This is the address you have configured the card to answer to. On the same line are the respective broadcast and netmask addresses. The third line of the output shows which options have been enabled on the card from the ifconfig command. You should recognize the BROADCAST option because you explicitly set it. The others are default options that are covered shortly. The fourth line contains information regarding the number of packets that have been received. In this example, about 1.9 million packets have been received, 1 packet had an error, 1 packet was dropped, and no packets were overruns. (Overruns are packets that are too long.) In the line below it is the same information for packets transmitted. The last line provides the configuration information for the hardware.

Optional Parameters to ifconfig

The following parameters are optional on the ifconfig command line:

up This tells ifconfig to activate the network interface and begin sending and receiving packets.
down This option enables you to shut the interface down after it has been activated. This is useful for shutting down an active card when troubleshooting network-related problems.
arp ARP (Address Resolution Protocol) enables you to map a network card's hardware address to its IP address. Protocols such as DHCP use this to find machines on a subnet without having to know an IP address first. Note that the ARP will work only within your immediate subnet and will not transfer through routers. By default, this option is turned on. To turn it off, place a minus sign in front of it (-arp).
mtu N The MTU (Maximum Transfer Unit) sets the size of each Ethernet packet to N, where N is the number of bytes in each packet. For Ethernet, this defaults to 1500, and you shouldn't change it unless you are sure about what you are doing.

Page 256

Using route

In order to communicate with machines outside your local area network, you need to use a router. This device is the link between your network and the rest of the world. When you need to communicate to a machine outside of your LAN, your host will send the message to the router, which will forward it on through the outside network. The same is true for packets coming in from the outside network. The router will receive the packet and forward it to your host.

If you used the Red Hat installation procedure for configuring your network, this has already been configured for you. For a host connected to the Internet, you should have at the very least three possible routes: a loopback, a route to your LAN, and a default route to your router. By running route without any parameters, you can see your current routing table. The format of the route command is


route cmd type target_ip netmask gateway options

where cmd is either add or del depending on whether you want to add or delete a route, respectively. If you use del, you then only need the target_ip parameter, which specifies the IP address for which you are routing.

If, on the other hand, you used the add command, you need to specify type to be either -net or -host, where -net is a network that you are routing to and -host is a specific host you are routing to.

The target_ip address specifies either the network or host IP address to which you are routing. There is a special keyword for this option: default. If you specify default instead of an actual IP address, all packets that do not have a specific route listed in the route table will be sent to this route.

netmask allows you to specify the netmask to the network you are routing to. Note that this applies only when using the -net option. The netmask option is used like this: netmask mask_number where mask_number is the actual netmask in dotted notation.

gateway specifies which gateway to use for sending packets to target_ip. For example, if your default route points to the Internet (a likely situation), then your gateway setting should point to your router connecting to the Internet. For example, if the router were 192.168.42.1, this option would be specified as gw 192.168.42.1. You can use hostnames if you want to, so long as they appear in the /etc/hosts file. (See the section "The Domain Name Service," later in this chapter, for details.)

The options available in addition to the ones already stated are as follows:

-n Uses numerical addresses instead of trying to resolve IP addresses to hostnames. This is used when invoking route without either the add or del parameter so you can see which routes are currently set.

Previous | Table of Contents | Next