在日常生活中,或者在服務器中,有多個網卡配置多個地址,訪問不同的網絡段,這種情況是非常常見的現象,這個時候我們就需要添加多個路由來實現多網絡段的通行。
一、設置當前終端路由(再開機就沒效了)
1、顯示現有路由
route [root@localhost network-scripts]# route
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 5.5.5.0 2.2.2.252 255.255.255.0 UG 0 0 0 eth3 2.2.2.0 * 255.255.255.0 U 1 0 0 eth3 192.168.248.0 * 255.255.255.0 U 1 0 0 eth1 6.6.6.0 2.2.2.252 255.255.255.0 UG 0 0 0 eth3 1.1.1.0 * 255.255.255.0 U 1 0 0 eth2 4.4.4.0 2.2.2.252 255.255.255.0 UG 0 0 0 eth3 3.3.3.0 2.2.2.252 255.255.255.0 UG 0 0 0 eth3
2、添加路由
- 添加到網絡的路由
route add-net Destination/genmask gw Gateway [root@localhost network-scripts]# route add -net 3.3.3.0/24 gw 2.2.2.252
[root@localhost network-scripts]# route add -net 4.4.4.0/24 gw 2.2.2.252
[root@localhost network-scripts]# route add -net 5.5.5.0/24 gw 2.2.2.252
添加到主機的路由
route add –host Destination/genmask gw Gateway route add –host 192.168.168.119 gw 192.168.168.1
添加默認網關
route add default gw IP [root@localhost network-scripts]# route add default gw 6.6.6.250
3、刪除路由
- 刪除默認路由
route del default
刪除指定路由
route del –net Destination/genmask [root@localhost network-scripts]# route add -net 6.6.6.0/24 gw 5.5.5.250
路由相關的操作大致如此,比較簡單,不過與路由器相關的配置比較復雜,路由器在設置之前需要對網卡和相關配置進行設置,這個過程是比較痛苦的。
二:在linux下設置永久路由的方法:
1.在/etc/rc.local里添加
方法:
route add -net 192.168.3.0/24 dev eth0 route add -net 192.168.2.0/24 gw 192.168.2.254 route add –net 180.200.0.0 netmask 255.255.0.0 gw 10.200.6.201 dev eth0 metric 1
-host/-net:表示路由目標是主機還是網段;
netmask:表示路由目標為網段時才會使用到,表示路由目標網段的子網掩碼;
gw:命令關鍵字,后面跟下一跳網關;
dev:命令關鍵字,后面跟具體設備名,表示路由是從該設備出去。
metric:為路由指定所需躍點數的整數值(范圍是 1 ~ 9999),它用來在路由表里的多個路由中選擇與轉發包中的目標地址最為匹配的路由。所選的路由具有最少的躍點數。躍點數能夠反映躍點的數量、路徑的速度、路徑可靠性、路徑吞吐量以及管理
2.在/etc/sysconfig/network里添加到末尾(相當於添加默認網關)
方法:GATEWAY=gw-ip 或者 GATEWAY=gw-dev
3.寫入/etc/sysconig/static-routes文件,不過默認在/etc/sysconfig目錄中沒有這個文件需要我們手工創建然后進行添加
方法:
/etc/sysconfig/static-routes : any net 192.168.3.0/24 gw 192.168.3.254 any net 10.250.228.128 netmask 255.255.255.192 gw 10.250.228.129
三、路由器相關的實驗
與路由器相關的實驗,大多都是添加或添加路由表,可以使多個網段之間相互ping可以相通,接下來我將展示兩台PC機(PC1,PC2)和五台路由器(r1 ,r2,r3,r4,r5)之間相互ping的實驗。
1、在虛擬機編輯模式下添加虛擬網絡(2-7)以便以后添加網卡使用
2、規划5台路由以及2台PC的網址
PC1設為1.1.1.1/24 PC2設為6.6.6.6/24
route1左為1.1.1.251 route1右為2.2.2.251
route2左為2.2.2.252 route2右為3.3.3.252
route3左為3.3.3.253 route3右為4.4.4.253
route4左為4.4.4.254 route4右為5.5.5.254
route5左為5.5.5.250 route5右為6.6.6.250
PC1在左側,然后路由器按1-5從左到右一致排列,PC2在最右側。在 /etc/sysconfig/network-scripts目錄下修改新加網卡的默認配置,刪dhcp然后添加上述網址。
3、規划完相關地址后開始規划路由表
開了七台虛擬機,分別修改相關hostname為對應的機器名,在兩台PC機上設置器默然網關,
PC1 [root@pc1 ~]# route add default gw 1.1.1.251
PC2 [root@pc2 ~]# route add default gw 6.6.6.250
route1 [root@route1 ~]# route add -net 3.3.3.0/24 gw 2.2.2.252
[root@route1 ~]# route add -net 4.4.4.0/24 gw 2.2.2.252
[root@route1 ~]# route add -net 5.5.5.0/24 gw 2.2.2.252
[root@route1 ~]# route add -net 6.6.6.0/24 gw 2.2.2.252
route2,3 4,5都是按照上述方式選擇里自己最近的出口去連別的網段,在此我就不做過多展示。
4、還是以route1為例進行防火牆以及其他設置
[root@route1 ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1 [root@route1 ~]# sysctl -p
[root@route1 ~]# iptables –F
5、完成上述步驟網絡就已經通了,可以進行ping了。