当前位置: > 运维管理 >

Linux服务器静态路由配置

时间:2015-09-30 23:42来源:linux.it.net.cn 作者:IT
Linux服务器静态路由配置
  静态路由是在路由器中设置的固定的路由表。除非网络管理员干预,否则静态路由不会发生变化。由于静态路由不能对网络的改变作出反映,一般用于网络规模不大、拓扑结构固定的网络中。静态路由的优点是简单、高效、可靠。在所有的路由中,静态路由优先级最高。当动态路由与静态路由发生冲突时,以静态路由为准。Linux支持一下路由守护进程:
Routed :支持RIPI路由协议;
Gated:支持RIP、BGP、EGP、OSPF四种路由协议;
Zebra:支持RIP、BGP、OSPF三种路由协议;

1. 查看内核路由表
登陆Linux服务器查看路由表,使用命令:route
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0*255.255.255.0 U 000 eth0
127.0.0.1*255.0.0.0 U 000 lo
Default 192.168.1.10.0.0.0 UG 000 eth0

127.0.0.1即封闭回路地址,我们知道,使用TCP/IP协议的电脑,都会拥有一个IP地址,彼此间相互以IP地址确认对方,传递信息与数据。在有些情况下,我们为了进行某项测试(比如网卡是否正确安装),或者是没有另外一台电脑作为接收端。这时,我们可利用本机扮演信息的发送端和接收端,这就是所谓的封闭回路。也可以使用等价命令:netstat -r

表-1 是route命令输出选项说明

BIAO1

route命令格式:
route [-cfvnee] [选项]
主要选项如下。
-n:不显示域名。
-e:显示其他信息。
-v:显示详细信息。
-F:显示FIB信息。
-C:显示FIB缓存信息。
add:增加路由。
del:删除路由。
-net:路由到达的是一个网络,而不是一台主机。
-host:路由到达的是一台主机。
-netmask Nm:指定路由的子网掩码。
gw:指定路由的网关。

route命令举例:
添加到主机的路由
# route add -host 192.168.1.2 dev eth0:0
# route add -host 10.20.30.148 gw 10.20.30.40

添加到网络的路由
# route add -net 10.20.30.40 netmask 255.255.255.248 eth0
# route add -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41
# route add –net 192.168.1.0/24 eth1

删除路由

# route del -host 192.168.1.2 dev eth0:0
# route del -host 10.20.30.148 gw 10.20.30.40
# route del -net 10.20.30.40 netmask 255.255.255.248 eth0
# route del -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41
# route del -net 192.168.1.0/24 eth1
# route del default gw 192.168.1.1

2. Linux支持的三种路由类型

主机路由
主机路由的Fiags字段为H ,下面的例子中,本机通过ip地址192.168.1.1的路由器到达ip地址为10.0.0.10的主机。
Destination Gateway Genmask Flags Metric Ref Use Iface
-----------------------------------------------------------------
10.0.0.10 192.168.1.1 255.255.255.255 UH 0 0 0 eth0

网络路由
网络路由的Fiags字段为N ,下面的例子中,本机将发送到网络192.19.12的数据包转发到ip地址为192.168.1.1的路由器。
Destination Gateway Genmask Flags Metric Ref Use Iface
-------------------------------------------------------------------
192.19.12 192.168.1.1 255.255.255.0 UN 0 0 0 eth0

默认路由
默认路由的Fiags字段为G ,下面的例子中,默认路由的ip地址为192.168.1.1。

Destination Gateway Genmask Flags Metric Ref Use Iface
-------------------------------------------------------------
default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0

3. 应用实例配置静态路由

在配置网络时,要为机器指定接收数据包时该包要经过的路径。在Linux系统中,提供一个命令route,这个命令可以为ifconfig命令配置的网卡设置静态路由。这种设置工作通常在/etc/rc.d/rc.inet1中引入,在系统引导时进行。网络结构如下:

Linux 路由器包括三个网卡:

eht0 192.168.1.1
eht1 192.168.10.1
eht2 192.168.100.1

分别连接三个子网:
子网A 192.168.1.0/24
子网B 192.168.10.0/24
子网C 192.168.100.0/24

为了让三个子网中的计算机互相通信进行如下配置:
在Red Hat Linux 中默认的内核配置已经包含了路由功能,但默认并没有在系统启动时启用此功能。
# echo 1 > /proc/sys/net/ipv4/ip_foward

配置三个网络接口
#ifconfig eth0 down
#ifconfig eth1 down
#ifconfig eth2 down
#ifconfig eth0 192.168.1.1 up
#ifconfig eth1 192.168.10.1 up
#ifconfig eth2 192.168.100.1 up

添加路由选项
#route add -net 192.168.1.0/24 eth0
#route add -net 192.168.10.0/24 eth1
#route add -net 192.168.100.0/24 eth2

重新查看路由表

# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.100.0 * 255.255.255.0 U 0 0 0 eth2
192.168.10.0 * 255.255.255.0 U 0 0 0 eth1
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
127.0.0.1 * 255.0.0.0 U 0 0 0 l0

4. 测试静态路由
在子网C的一个linux 计算机上配置网络接口和网关:
#ifconfig 192.168.100.10 up
#route add default gw 192.168.100.1
#route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.100.0 * 255.255.255.0 U 0 0 0 et0
127.0.0.1 * 255.0.0.0 U 0 0 0 lo
Default 192.168.100.1 0.0.0.0 UG 0 0 0 eth0

在子网B的一个Windows 2000 计算机上配置网络接口和网关,如图2 。

图2 Windows 2000 计算机上配置网络接口和网关

从子网C中的一个Linux 计算机上进行路由测试:
首先ping子网B中ip地址为:192.168.10.83 的计算机
# ping 192.168.10.83 -c 3/ k% l6 t1 V/ P6 P( l
PING 192.168.10.83 (192.168.10.83)from 912.168.100.10:56(84) bytes of data.
64 bytes from 192.168.10.83: icmp_seq=0 ttl=64 time=0.113 ms
64 bytes from 192.168.10.83: icmp_seq=1 ttl=64 time=0.102 ms
64 bytes from 192.168.10.83: icmp_seq=2 ttl=64 time=0.099 ms
--- 192.168.10.83 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.099/0.104/0.113/0.013 ms, pipe 2
使用traceroute命令
# traceroute 192.168.10.83
traceroute to 192.168.10.83 (192.168.10.83), 30 hops max, 38 byte packets
1 192.168.10.83 (192.168.10.83) 4.316 ms 1.018 ms 0.419 ms
到此为止,Linux服务器的静态路由配置结束。

(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容