📄 491-493.html
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Linux, Fourth Edition:Configuring a TCP/IP Network</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0789717468//-->
<!--TITLE=Special Edition Using Linux, Fourth Edition//-->
<!--AUTHOR=Jack Tackett//-->
<!--AUTHOR=Jr.//-->
<!--AUTHOR=Steve Burnett//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Que//-->
<!--CHAPTER=24//-->
<!--PAGES=491-493//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="488-491.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="493-495.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Returning to the Burwell network, the following is an example from the laptop linux2, with a SLIP link up and running:
</P>
<!-- CODE SNIP //-->
<PRE>
$ <B>/sbin/route</B>
Kernel routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
slip.burwell.c * 255.255.255.255 UH 0 0 0 sl0
127.0.0.0 * 255.0.0.0 U 0 0 100 lo
default slip.burwell.c * UG 0 0 1 sl0
</PRE>
<!-- END CODE SNIP //-->
<P>The table entry for the loopback is the same as before, and there are two new entries. The first specifies a route to slip.burwell.com. The other new entry specifies a default route by using slip.burwell.com as a gateway.
</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>Every machine connected to a network must have a default route in its routing table. The default route is used when no other routing table entry matches the destination for a packet.<HR></FONT>
</BLOCKQUOTE>
<P><FONT SIZE="+1"><B>Adding Static Routes</B></FONT></P>
<P>You add routes to the routing table by running the <TT>route</TT> program with the <TT>add</TT> argument. The command-line argument syntax for the <TT>route add</TT> command is</P>
<!-- CODE SNIP //-->
<PRE>
route add [ -net | -host ] <I>addr</I> [gw <I>gateway</I> ] [metric <I>cost</I>]
[netmask <I>mask</I>] [dev <I>device</I>]
</PRE>
<!-- END CODE SNIP //-->
<P>Table 24.5 describes the command-line arguments that the <TT>route add</TT> command uses.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 24.5</B> Command-Line Arguments Used by route add
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="20%" ALIGN="LEFT">Argument
<TH WIDTH="80%" ALIGN="LEFT">Description
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD VALIGN="TOP"><TT>-net | -host</TT>
<TD>Forces the specified address to be treated as a network or host address.
<TR>
<TD VALIGN="TOP"><I><TT>addr</TT> </I>
<TD>The destination address for the new route. This can be an IP address, host name, or network name.
<TR>
<TD VALIGN="TOP"><TT>gw <I>gateway</I></TT>
<TD>Specifies that any packets for this address be routed through the specified gateway.
<TR>
<TD><TT>metric <I>cost</I></TT>
<TD>Sets the metric field in the routing table.
<TR>
<TD VALIGN="TOP"><TT>netmask</TT> <TT><I>mask</TT></I>
<TD>Specifies the netmask of the route being added. The <TT>route</TT> program will guess what this is, so you don’t need to specify it under normal circumstances.
<TR>
<TD VALIGN="TOP"><TT>dev <I>device</I></TT>
<TD>Forces <TT>route</TT> to associate the new route with the specified network interface device. Again, <TT>route</TT> usually guesses correctly what device to use for the new route, so you don’t have to use this often.
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>CAUTION: </B><BR>When adding a gateway route to the routing table, you must make sure that the specified gateway is reachable. You usually have to add a static route for the gateway before adding the route by using the gateway.<HR></FONT>
</BLOCKQUOTE>
<P><FONT SIZE="+1"><B>Looking at Routing Examples</B></FONT></P>
<P>Now for some examples, starting with the loopback interface. After configuring the loopback interface with <TT>ifconfig,</TT> you need to add a route to it, as in the following:</P>
<!-- CODE SNIP //-->
<PRE>
# <B>route add 127.0.0.1</B>
</PRE>
<!-- END CODE SNIP //-->
<P>Nothing else is needed because <TT>route</TT> compares the address given to it with the addresses for the known interfaces and assigns the loopback interface to the new route. The following example shows how to set the routing for the SLIP link on the Burwell linux2 machine after the SLIP link is established and <TT>ifconfig</TT> is used to activate the interface:</P>
<!-- CODE SNIP //-->
<PRE>
# <B>route add slip.burwell.com</B>
# <B>route add default gw slip.burwell.com</B>
</PRE>
<!-- END CODE SNIP //-->
<P>The first command adds a static route for the host slip.burwell.com; the second one tells the kernel to use slip.burwell.com as a gateway for all packets with unknown destinations.
</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>CAUTION: </B><BR>Make sure that any host names you use with the <TT>route</TT> command are in the /etc/hosts file so that <TT>route</TT> can find the IP addresses for them; otherwise, <TT>route</TT> fails.<HR></FONT>
</BLOCKQUOTE>
<P>If you’re subnetting your network by splitting the IP address in the middle of an octet, you’ll have to specify the required netmask when running route. For example, if you have a class C network and have four subnets using the first two bits of the last octet, you need to run <TT>route</TT> like this:</P>
<!-- CODE SNIP //-->
<PRE>
# <B>route add hostname netmask 255.255.255.192</B>
</PRE>
<!-- END CODE SNIP //-->
<P>This ensures that <TT>route</TT> puts the right netmask in the routing table entry.</P>
<P>For Ethernet and other broadcast network interfaces, you need to add routes that tell the kernel what network can be reached via each configured interface. After using <TT>ifconfig</TT> to bring up the eth0 network interface on linux1.burwell.com as you did previously, you need to run <TT>route</TT> to install the route to the network on that interface:</P>
<!-- CODE SNIP //-->
<PRE>
# <B>route add -net 166.82.1.0</B>
</PRE>
<!-- END CODE SNIP //-->
<P>That may not look like enough to set the routing table entry correctly, because no interface is indicated; however, route manages to find the interface by comparing the IP address on the command line to the IP address of each network interface. It assigns the route to the interface that matches it. In this case, eth0 has been assigned the address 166.82.1.21 with a netmask of 255.255.255.0. This matches the network address given in the route command, so route installs a route to the network 166.82.1.0 by using interface eth0, as follows:
</P>
<!-- CODE SNIP //-->
<PRE>
$ <B>route</B>
Kernel routing table
Destination Gateway Genmask Flags Metric Ref UseIface
166.82.1.0 * 255.255.255.0 UN 0 0 0 eth0
127.0.0.0 * 255.0.0.0 U 0 0 100 lo
</PRE>
<!-- END CODE SNIP //-->
<P>To tell linux1 how to reach the other subnet, you need two more routing table entries to be safe:
</P>
<!-- CODE SNIP //-->
<PRE>
# <B>route add gateway.burwell.com</B>
# <B>route add -net 166.82.2.0 gw gateway.burwell.com</B>
</PRE>
<!-- END CODE SNIP //-->
<P>This adds a static route to gateway.burwell.com and then adds a network route for 166.82.2.0 by using gateway.burwell.com as the gateway for that network, as shown in the following:
</P>
<!-- CODE SNIP //-->
<PRE>
$ <B>route</B>
Kernel routing table
Destination Gateway Genmask Flags Metric Ref UseIface
gateway.burwell * 255.255.255.0 UH 0 0 0 eth0
166.82.1.0 * 255.255.255.0 UN 0 0 0 eth0
166.82.2.0 gateway.burwell 255.255.255.0 UN 0 0 0 eth0
127.0.0.0 * 255.0.0.0 U 0 0 100 lo
</PRE>
<!-- END CODE SNIP //-->
<P>This shows the static route you added for gateway.burwell.com and the gatewayed route to the 166.82.2.0 network.
</P>
<P><FONT SIZE="+1"><B>Deleting Routes with the <I>route</I> Command
</B></FONT></P>
<P>You delete routes by calling <TT>route</TT> with the <TT>del</TT> option and specifying the destination address of the route you want to delete. For example,</P>
<!-- CODE SNIP //-->
<PRE>
# <B>route del -net 166.82.2.0</B>
</PRE>
<!-- END CODE SNIP //-->
<P>deletes the network route for network 166.82.2.0.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="488-491.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="493-495.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -