項目背景:
最近做的一個項目中需要總公司和分公司之間內網互通;
總公司內網段:192.168.0.0/16
分公司內網段:172.16.0.0/16
考慮到專線的成本問題,最後公司決定使用IPsec。
簡化網絡拓撲:
配置:
1. 配置各路由器端口IP地址,使相鄰路由器相互ping通
R1:
R1(config)#int e0/0
R1(config-if)#ip address 12.1.1.1 255.255.255.0
R1(config-if)#no shutdown
R1(config-if)#exit
R1(config)#int e0/1
R1(config-if)#ip address 13.1.1.1 255.255.255.0
R1(config-if)#no shutdown
R1(config-if)#exit
R2:
R2(config)#int e0/0
R2(config-if)#ip address 12.1.1.2 255.255.255.0
R2(config-if)#no shutdown
R2(config-if)#exit
R2(config)#int e0/1
R2(config-if)#ip address 192.168.2.1 255.255.255.0
R2(config-if)#no shutdown
R2(config-if)#exit
R2(config)#int loopback0
R2(config-if)#ip address 192.168.1.1 255.255.255.0
R2(config-if)#exit
R3:
R3(config)#int e0/0
R3(config-if)#ip address 13.1.1.3 255.255.255.0
R3(config-if)#no shutdown
R3(config-if)#exit
R3(config)#int e0/1
R3(config-if)#ip address 172.16.2.1 255.255.255.0
R3(config-if)#no shutdown
R3(config-if)#exit
R3(config)#int loopback0
R3(config-if)#ip address 172.16.1.1 255.255.255.0
R3(config-if)#exit
R4:
R4(config)#int e0/0
R4(config-if)#ip address 192.168.2.2 255.255.255.0
R4(config-if)#no shutdown
R4(config-if)#exit
R5:
R5(config)#int e0/0
R5(config-if)#ip address 172.16.2.2 255.255.255.0
R5(config-if)#no shutdown
R5(config-if)#exit
2. 在R2、R3之間配置IPsec VPN,在環回接口之間建立連接
R2:
//配置感興趣流,使Site1內網的流量能夠通過該路由器到Site2
R2(config)#access-list 100 permit ip 192.168.0.0 0.0.255.255 172.16.0.0 0.0.255.255
R2(config)#crypto isakmp policy 10 //配置加密策略
R2(config-isakmp)#encryption 3des
R2(config-isakmp)#hash md5
R2(config-isakmp)#authentication pre-share
R2(config-isakmp)#group 2
R2(config-isakmp)#crypto isakmp key 6 cisco123 address 13.1.1.3 //密鑰爲cisco123,13.1.1.3爲對端連接ISP的外網IP地址
R2(config)#crypto ipsec transform-set xxx esp-3des esp-md5-hmac
R2(cfg-crypto-trans)#mode tunnel //site-to-site使用tunnel模式
R2(cfg-crypto-trans)#crypto map linktosite 10 ipsec-isakmp
% NOTE: This new crypto map will remain disabled until a peer
and a valid access list have been configured.
R2(config-crypto-map)#set transform-set xxx
R2(config-crypto-map)#set peer 13.1.1.3 //對等體IP
R2(config-crypto-map)#match address 100 //匹配的感興趣流爲ACL 100
R2(config-crypto-map)#int e0/0 //將設置好的加密圖應用到接口e0/0上
R2(config-if)#crypto map linktosite
*Mar 1 00:12:00.439: %CRYPTO-6-ISAKMP_ON_OFF: ISAKMP is ON
R2(config-if)#exit
R2(config)ip route 0.0.0.0 0.0.0.0 12.1.1.1 //該路由器的所有流量下一跳爲12.1.1.1
R3:
//配置感興趣流,使Site2內網的流量能夠通過該路由器到Site1
R3(config)#access-list 100 permit ip 172.16.0.0 0.0.255.255 192.168.0.0 0.0.255.255
R3(config)#crypto isakmp policy 10 //配置加密策略
R3(config-isakmp)#encryption 3des
R3(config-isakmp)#hash md5
R3(config-isakmp)#authentication pre-share
R3(config-isakmp)#group 2
R3(config-isakmp)#crypto isakmp key 6 cisco123 address 12.1.1.2 //密鑰爲cisco123,13.1.1.3爲對端連接ISP的外網IP地址
R3(config)#crypto ipsec transform-set xxx esp-3des esp-md5-hmac
R3(cfg-crypto-trans)#mode tunnel //site-to-site使用tunnel模式
R3(cfg-crypto-trans)#crypto map linktosite 10 ipsec-isakmp
% NOTE: This new crypto map will remain disabled until a peer
and a valid access list have been configured.
R3(config-crypto-map)#set transform-set xxx
R3(config-crypto-map)#set peer 12.1.1.2 //對等體IP
R3(config-crypto-map)#match address 100 //匹配的感興趣流爲ACL 100
R3(config-crypto-map)#int e0/0 //將設置好的加密圖應用到接口e0/0上
R3(config-if)#crypto map linktosite
*Mar 1 00:12:00.439: %CRYPTO-6-ISAKMP_ON_OFF: ISAKMP is ON
R3(config-if)#exit
R3(config)ip route 0.0.0.0 0.0.0.0 13.1.1.1 //該路由器的所有流量下一跳爲13.1.1.1
至此IPsec配置完成,我們來用環回地址相互ping一下試試:
IPsec的建立需要流量的觸發,如果ping不通,多試一次,對端也相互ping一下。
但是你以爲就配置完了嗎?哈哈哈,這時我們在R3上ping192.168.2.2發現是不通的,接下來我們繼續。
3. Site1、Site2分別配置OSPF路由協議
Site1:
R2:
R2(config)#router ospf 1
R2(config-router)#router-id 2.2.2.2
R2(config-router)#network 192.168.1.0 0.0.0.255 area 0
R2(config-router)#network 192.168.2.0 0.0.0.255 area 0
R2(config-router)#exit
R4:
R4(config)#router ospf 1
R4(config-router)#router-id 4.4.4.4
R4(config-router)#network 192.168.2.0 0.0.0.255 area 0
R4(config-router)#exit
R4(config)#ip route 172.16.0.0 255.255.0.0 192.168.1.1 //所有到172.16.0.0網段的流量下一跳爲192.168.1.1
Site2:
R3:
R3(config)#router ospf 1
R3(config-router)#router-id 3.3.3.3
R3(config-router)#network 172.16.1.0 0.0.0.255 area 0
R3(config-router)#network 172.16.2.0 0.0.0.255 area 0
R3(config-router)#exit
R5:
R3(config)#router ospf 1
R3(config-router)#router-id 5.5.5.5
R3(config-router)#network 172.16.2.0 0.0.0.255 area 0
R5(config-router)#exit
R5(config)#ip route 192.168.0.0 255.255.0.0 172.16.1.1 //所有到192.168.0.0網段的流量下一跳爲172.16.1.1
我們在R4上ping R5內網試試:
測試成功。
後記:
在配置IPsec時我的體會:一定要清楚流量的流向,源IP和目標IP,發現ping不通時,多檢查路由表,想清楚下一跳的關係。
爲什麼使用環回接口配置IPsec呢?
因爲環回接口有物理接口不具備的穩定性呀。
參考資料:
IPSec 理論知識:https://blog.csdn.net/bingyu9875/article/details/61614797
本文原創,轉載請註明出處