配置:關閉防火牆linux
linux1 地址1: 192.168.10.10/24 地址2:192.168.20.10/24(不指定網關,作爲路由,本身就是網關)cookie
linux2 地址1: 192.168.20.20/24 地址2:192.168.80.10/24(不指定網關,作爲路由,本身就是網關)tcp
win2003 地址:192.168.10.100/24 網關 192.168.10.10工具
winxp 地址1: 192.168.80.100/24 網關 192.168.80.10測試
一.啓用Linux路由功能spa
1.配置文件在 /etc/sysctl.conf 裏的 net.ipv4.ip_forwartd=1啓用路由功能rest
[root@localhost ~]# cat /proc/sys/net/ipv4/ip_forward blog
0 ipv4/ip_forward =0 表示沒有啓用路由功能接口
[root@localhost ~]# echo '1'>/proc/sys/net/ipv4/ip_forward 啓用路由功能(零時生效)ip
[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’ 看到的那張路由表。