首页 > 代码库 > 配置LINUX为路由

配置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

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

 

配置LINUX为路由