Running Linux, 5th Edition - Matthias Kalle Dalheimer [250]
For most configurations, an rc.inet1 file similar to the following should work. You will, of course, have to edit this for your own system. Do not use the sample IP and network addresses listed here; they may correspond to an actual machine on the Internet:
#!/bin/sh
# This is /etc/init.d/rc.inet1 - Configure the TCP/IP interfaces
# First, configure the loopback device
HOSTNAME=`hostname`
/sbin/ifconfig lo 127.0.0.1 # uses default netmask 255.0.0.0
/sbin/route add 127.0.0.1 # a route to point to the loopback device
# Next, configure the Ethernet device. If you're only using loopback or
# SLIP, comment out the rest of these lines.
# Edit for your setup.
IPADDR="128.17.75.20" # REPLACE with your IP address
NETMASK="255.255.255.0" # REPLACE with your subnet mask
NETWORK="128.17.75.0" # REPLACE with your network address
BROADCAST="128.17.75.255" # REPLACE with your broadcast address
GATEWAY="128.17.75.98" # REPLACE with your default gateway address
# Configure the eth0 device to use information above.
/sbin/ifconfig eth0 ${IPADDR} netmask ${NETMASK} broadcast ${BROADCAST}
# Add a route for our own network.
/sbin/route add ${NETWORK}
# Add a route to the default gateway.
/sbin/route add default gw ${GATEWAY} metric 1
# End of Ethernet Configuration
As you can see, the format of the ifconfig command is
ifconfig interface device options...
For example:
ifconfig lo 127.0.0.1
assigns the lo (loopback) device the IP address 127.0.0.1, and
ifconfig eth0 127.17.75.20
assigns the eth0 (first Ethernet) device the address 127.17.75.20.
In addition to specifying the address, Ethernet devices usually require that the subnetwork mask be set with the netmask option and that the broadcast address be set with broadcast.
The format of the route command, as used here, is:
route add [ -net | -host ] destination [ gw gateway ]
[ metric metric ]options
where destination is the destination address for this route (or the keyword default), gateway is the IP address of the gateway for this route, and metric is the metric number for the route (discussed later).
We use route to add entries to the routing table. You should add a route for the loopback device (as seen earlier), for your local network, and for your default gateway. For example, if our default gateway is 128.17.75.98, we would use the command:
route add default gw 128.17.75.98
route takes several options. Using -net or -host before destination will tell route that the destination is a network or specific host, respectively. (In most cases, routes point to networks, but in some situations you may have an independent machine that requires its own route. You would use -host for such a routing table entry.)
The metric option specifies a metric value for this route. Metric values are used when there is more than one route to a specific location, and the system must make a decision about which to use. Routes with lower metric values are preferred. In this case, we set the metric value for our default route to 1, which forces that route to be preferred over all others.
How could there possibly be more than one route to a particular location? First of all, you may use multiple route commands in rc.inet1 for a particular destination—if you have more than one gateway to a particular network, for example. However, your routing tables may dynamically acquire additional entries in them if you run routed (discussed later). If you run routed, other systems may broadcast routing information to machines on the network, causing extra routing table entries to be created on your machine. By setting the metric value for your default route to 1, you ensure that any new routing table entries will not supersede the preference of your default gateway.
You should read the manual pages for ifconfig and route, which describe the syntax of these commands in detail.