A计算机必须拥有三块网卡,分别与三个网段相连。
假设:
eth0与172.16.0.0相连,
eth1与10.0.0.0相连,
eth2与192.168.1.0相连。
step 1
首先来配置eth0。给这个网络接口分配地址172.16.1.1,运行下列命令:
# ifconfig eth0 172.16.1.1 netmask 255.255.0.0
为了使这个地址不再计算机重新启动后消失,
编辑/etc/sysconfig/network-scripts/ifcfg-eth0文件,修改为如下格式:
DEVICE = eth0
ONBOOT = yes
BROADCAST = 172.16.255.255
NETWORK = 172.16.0.0
NETMASK = 255.255.0.0
IPADDR = 172.16.1.1
增加一条静态路由:
# route add -net 172.16.0.0 netmask 255.255.0.0
这样系统中就增加了一条静态路由:
# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface
172.16.0.0 * 255.255.0.0 U 0 0 0 eth0
step 2
配置eth1,eth1与10.0.0.0网段相连,分配给它的地址是10.254.254.254, 如同step 1
再增加一条静态路由:
# route add -net 10.0.0.0 netmask 255.0.0.0
网络中当前的路由表为
# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface
172.16.0.0 * 255.255.0.0 U 0 0 0 eth0
10.0.0.0 * 255.0.0.0 U 0 0 0 eth1
step 3
最后配置eth3,它连接192.168.1.0网段,分配的IP地址是192.168.1.254, 同step 1
再增加一条静态路由:
# route add -net 192.168.1.0 netmask 255.255.255.0
这样网络中就有三条静态路由记录了:
# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface
172.16.0.0 * 255.255.0.0 U 0 0 0 eth0
10.0.0.0 * 255.0.0.0 U 0 0 0 eth1
192.168.1.0 * 255.255.255.0 U 0 0 0 eth2
step 4
还要为系统增加一条缺省路由,因为缺省的路由是把所有的数据包都发往它的上一级网关(假设地址是172.16.1.100,这个地址依赖于使用的网络而定,由网络管理员分配),因此增加如下的缺省路由记录:
# route add default gw 172.16.1.100
这样系统的静态路由表建立完成,它的内容是
# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface
172.16.0.0 * 255.255.0.0 U 0 0 0 eth0
10.0.0.0 * 255.0.0.0 U 0 0 0 eth1
192.168.1.0 * 255.255.255.0 U 0 0 0 eth2
default 172.16.1.100 0.0.0.0 UG 0 0 0 eth0
PS:机器从新启动之后路由表内容还在吗?肯定是不在了,所以要把配置写到文件里头,可以建立文件/etc/sysconfig/static-routes,静态路由格式像下面这样:
any net 10.0.0.0 netmask 255.255.0.0 gw 10.254.254.254
这是把10.0.0.0/8这个网段的ip全部经过10.254.254.254走。
Setp 5
要增加系统的IP转发功能。这个功能由/proc/sys/net/ipv4目录下的ip_forward文件控制,执行如下命令打开ip转发功能:
echo 1 > /proc/sys/net/ipv4/ip_forward
或修改/etc/sysctl.conf文件
这样我们的路由器基本上是配置好了
最后,测试路由器的工作情况。