LINUX配置路由功能

 

 

 

 

配置:关闭防火墙

linux1    地址1: 192.168.10.10/24 地址2:192.168.20.10/24(不指定网关,做为路由,自己就是网关)

linux2    地址1: 192.168.20.20/24 地址2:192.168.80.10/24(不指定网关,做为路由,自己就是网关)

win2003    地址:192.168.10.100/24  网关 192.168.10.10

winxp  地址1: 192.168.80.100/24  网关 192.168.80.10

 

一.启用Linux路由功能

1.配置文件在 /etc/sysctl.conf  里的 net.ipv4.ip_forwartd=1启用路由功能

[root@localhost ~]# cat /proc/sys/net/ipv4/ip_forward 

0     ipv4/ip_forward =0 表示没有启用路由功能

[root@localhost ~]# echo '1'>/proc/sys/net/ipv4/ip_forward  启用路由功能(零时生效)

[root@localhost ~]# cat /proc/sys/net/ipv4/ip_forward 
1
[root@localhost ~]# vi /etc/sysctl.conf   配置 net.ipv4.ip_forwartd=1启用路由功能
[root@localhost ~]# service network start
[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296

sysctl -p   (显示内核参数)  sysctl配置与显示在/proc/sys目录中的内核参数.可以用sysctl来设置或重新设置联网功能,如IP转发、IP碎片去除以及源路由检查等。用户只需要编辑/etc/sysctl.conf文件,即可手工或自动执行由sysctl控制的功能。

二.设置路由转发,实现win2003和XP互ping

1.查看路由表和添加静态路由 (创建一个route-ethX 的文件,其中ethX一定是网卡名称,linux路由器上有多个网卡,那属于那一个网卡呢!属于转发出去的那个。如图)

在linux1/etc/sysconfig/nework-scripts/route-eth1 创建一个route-eth1的文件,添加如下:

ADDRESS1=192.168.80.0

NETMASK1=255.255.255.0

GATEWAY1=192.168.20.20

或者: route add -net  192.168.80.0 netmask 255.255.255.0  gw 192.168.20.20

在linux2/etc/sysconfig/nework-scripts/route-eth0 创建一个route-eth0的文件,添加如下:

ADDRESS1=192.168.10.0

NETMASK1=255.255.255.0

GATEWAY1=192.168.20.10

或者: route add -net  192.168.10.0 netmask 255.255.255.0  gw 192.168.20.10

如果添加的是默认路由,在配置/etc/sysconfig/nework-scripts/ifcfg-ethX的GATEWAY值。

2.重启服务

[root@www ~]# service network restart

正在关闭接口 eth0: [确定]
正在关闭接口 eth1: [确定]
关闭环回接口: [确定]
禁用 IPv4 包转送: net.ipv4.ip_forward = 0
[确定]
弹出环回接口: [确定]
弹出界面 eth0: [确定]
弹出界面 eth1: [确定]

3.查看路由设置
[root@www ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.20.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
192.168.80.0 192.168.20.20 255.255.255.0 UG 0 0 0 eth1
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1

[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.20.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.80.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
192.168.10.0 192.168.20.10 255.255.255.0 UG 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1

4.(win2003)测试走过和路由 

C:\Documents and Settings\Administrator>pathping 192.168.80.100

Tracing route to WINXP [192.168.80.100]
over a maximum of 30 hops:
0 win2003 [192.168.10.100]
1 192.168.10.10
2 192.168.20.20
3 WINXP [192.168.80.100]

5.(winxp)测试走过和路由 

C:\Documents and Settings\Administrator>pathping 192.168.10.100

Tracing route to 192.168.10.100 over a maximum of 30 hops

0 192.168.80.100
1 192.168.80.10
2 192.168.20.10
3 192.168.10.100

三.实验:策略路由,根据端口或IP地址有方向的选择要走的路径(电信与联通)

 

1.对两台LINUX添加块网卡,并设置成VMnet3 网段。 如图

2.本实验是根据上一实验进行的,取消静态路由设置。

2. 安装iproute软件。       

3.要求:192.168.80.100数据包到192.168.10.100走VMnet2的电信通道,  到192.168.10.0网段走VMnet3的联通。

4.在linux2上创建路由表

[root@localhost ~]# gedit /etc/sysconfig/network-scripts/route-eth0(注意文件名,是在eth1接口上)

ADDRESS1=192.168.10.100(具体到PC)

NETMASK1=255.255.255.255

GATEWAY1=192.168.20.10

[root@localhost ~]# gedit /etc/sysconfig/network-scripts/route-eth2(注意文件名,是在eth2接口上)

ADDRESS1=192.168.10.0

NETMASK1=255.255.255.0

GATEWAY1=192.168.30.10

[root@localhost ~]# service network restart
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.10.100 192.168.20.10 255.255.255.255 UGH 0 0 0 eth0
192.168.20.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.80.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
192.168.30.0 0.0.0.0 255.255.255.0 U 0 0 0 eth2
192.168.10.0 192.168.30.10 255.255.255.0 UG 0 0 0 eth2
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth2

5.在linux1上创建策略路由表和策略

创建策略路由表

[root@www ~]# rpm -qa | grep iproute  查看是否安装 iproute

iproute-2.6.18-10.el5

[root@www ~]# cat /etc/iproute2/rt_tables   查看所有路由表
# reserved values
255 local
254 main     
 route -n  显示的就是这个表。
253 default
0 unspec
# local
#1 inr.ruhep

[root@www ~]# gedit /etc/iproute2/rt_tables  

# reserved values
255 local
254 main
253 default
0 unspec

251 dianxin    #电信
252 liantong   #联通
# local
#1 inr.ruhep

添加静态路由到表中。 说明到80网段有两个路径到。

[root@www ~]# ip route add 192.168.80.0/24 via 192.168.20.20 table 251     
[root@www ~]# ip route add 192.168.80.0/24 via 192.168.30.20 table 252
[root@www ~]# ip route show table 251
192.168.80.0/24 via 192.168.20.20 dev eth1 
[root@www ~]# ip route show table 252
192.168.80.0/24 via 192.168.30.20 dev eth2

 

 

两种显示:ip route show table 251  或  ip route show table dainxin 显示路由表

配置文件,永久生效。

在linux1/etc/sysconfig/nework-scripts/route-eth1   192.168.80.0/24 via 192.168.20.20 table 251     

在linux1/etc/sysconfig/nework-scripts/route-eth2   192.168.80.0/24 via 192.168.30.20 table 252


[root@www ~]# ip rule show  显示路由规则优先级
0: from all lookup 255 
32766: from all lookup main 
32767: from all lookup default 
创建规则:说明100地址回去的路径走“table 251”的路由表。pref 是优先级,独立 IP地址的优先级当然更高。
[root@www ~]# ip rule add from 192.168.10.100/32 table 251 pref 10
[root@www ~]# ip rule add from 192.168.10.0/24 table 252 pref 100

[root@www ~]# ip rule show
0: from all lookup 255 
10: from 192.168.10.100 lookup dianxin 
100: from 192.168.10.0/24 lookup liantong 
32766: from all lookup main 
32767: from all lookup default

6.测试

C:\Documents and Settings\Administrator>pathping 192.168.80.100

Tracing route to WINXP [192.168.80.100]
over a maximum of 30 hops:
0 win2003 [192.168.10.100]
1 192.168.10.10
2 192.168.20.20
3 WINXP [192.168.80.100]

更换IP地址测试

C:\Documents and Settings\Administrator>pathping 192.168.80.100

Tracing route to WINXP [192.168.80.100]

over a maximum of 30 hops:
0 win2003 [192.168.10.111]
1 192.168.10.10
2 192.168.30.20   
说明走的是不同端口
3 WINXP [192.168.80.100]

=====================================================================

 Linux 最多可以支持 255 张路由表,其中有 3 张表是内置的,不能删除。

表255 本地路由表(Local table):本地接口地址,广播地址,已及NAT地址都放在这个表。该路由表由系统自动维护,管理员不能直接修改。

表254 主路由表(Main table) :如果没有指明路由所属的表,所有的路由都默认都放在这个表里。旧的路由工具(如route)所添加的路由都会加到这个表。

表253 默认路由表 (Default table):这个表的作用不清楚,一般也用不到

表 0 保留

 

默认的路由策略

通过  ip rule ls 可以看到 Linux 默认的路由策略:

0:                            from all lookup local

32766:                   from all lookup main

32767:                   from all lookup default

可以看到:

1.       有三条策略,它们的优先级分别是  0,32766 和 32767;

2.       这些策略都是根据源地址来选择路由表,它们适用于任何源地址的情况(from all);

3.       0 级的策略优先级最高,它选定 local 表,这适应于目的地址是本机地址或者广播地址的情况。

4.       32766 级的策略选定 main 表,它适用于目的地址为普通地址的情况,这张表也就是通过工具 ‘route’ 看到的那张路由表。

转载于:https://www.cnblogs.com/dapaitou2006/p/6564622.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值