在CentOS上使用strongSwan搭建IPsec V P N服務


 

一、需求背景介紹:

 

客戶業務在往騰訊雲遷移的過程中,因為兩邊的數據需要同步,所以需要建立站點到站點的IPsec V**連接,由於某些公司沒有V**設備或者其他雲不支持V** Gateway產品的時候,就需要我們自己搭建IPsec V**服務。

 

本文以strongSwan為例介紹如何在本地站點中加載IPsec V**配置,配置信息如下:

 

騰訊雲VPC的網段:172.16.10.0/16 本地IDC的網段:10.0.0.0/16 strongSwan的公網IP地址:140.143.129.162 V** Gateway的公網IP地址:115.159.144.75 操作系統版本:騰訊雲CVM CentOS Linux release 7.5 strongSwan軟件版本:Linux strongSwan U5.7.1

 

二、本地IDC端配置

 

1、安裝strongSwan

 

(1)運行以下命令安裝strongSwan。

 

[root@VM_2_2_centos ~]# yum install strongswan -y

 

(2)運行以下命查看安裝的軟件版本。

 

[root@VM_2_2_centos ~]# strongswan version

 

2、配置strongSwan

 

(1)運行以下命令打開ipsec.conf配置文件。

 

[root@VM_2_2_centos ~]# vi /etc/strongswan/ipsec.conf

 

(2)參考以下配置,更改ipsec.conf的配置。

 

# ipsec.conf - strongSwan IPsec configuration file # basic configuration config setup # strictcrlpolicy=yes # uniqueids = no charondebug="all" conn %default ikelifetime=1440m keylife=60m rekeymargin=3m keyingtries=0 keyexchange=ikev1 #ike版本 authby=secret conn toshanghai left=%defaultroute leftid=140.143.120.162 #本地端V**網關公網IP leftsubnet=10.0.0.0/16 #本地端私有網絡地址,如果要確保VPC網段都能通,需要添加整段VPC地址 right=115.159.144.75 #對端V**網關公網IP rightsubnet=172.16.0.0/16 #對端私有網絡地址 auto=start #進程主動時立即建立 IPsec 安全連接 type=tunnel ike=3des-md5-modp1024 esp=3des-md5

 

(3)運行以下命令打開ipsec.secrets配置文件。

 

[root@VM_2_2_centos ~]# vi /etc/strongswan/ipsec.secrets

 

(4)添加如下配置。

 

#本地公網出口IP   #對端公網出口IP     #雙方約定的秘鑰
140.143.120.162 115.159.144.75 : PSK qcloud123

 

(5)/etc/sysctl.conf系統配置。

 

[root@VM_2_2_centos ~]# vim /etc/sysctl.conf #配置轉發,默認是0 net.ipv4.ip_forward = 1 #關閉重定向,防止惡意用戶可以使用IP重定向來修改遠程主機中的路由表 net.ipv4.conf.all.accept_redirects = 0 net.ipv4.conf.all.send_redirects = 0

 

#使配置生效
[root@VM_2_2_centos ~]# sysctl -p

 

3、啟動服務

 

(1)strongswan start啟動IPsec V**服務。

 

[root@VM_2_2_centos ~]# strongswan start

 

(2)IPSec V**監聽在UDP的500和4500兩個端口,其中500是用來IKE密鑰交換協商,4500的NAT-T是nat穿透的。

 

[root@VM_2_2_centos ~]# netstat -tunpl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1024/sshd udp 0 0 0.0.0.0:4500 0.0.0.0:* 15283/charon udp 0 0 0.0.0.0:500 0.0.0.0:* 15283/charon udp 0 0 0.0.0.0:68 0.0.0.0:* 15283/charon udp 0 0 10.0.2.2:123 0.0.0.0:* 1226/ntpd udp 0 0 127.0.0.1:123 0.0.0.0:* 1226/ntpd udp6 0 0 :::4500 :::* 15283/charon udp6 0 0 :::500 :::* 15283/charon

 

4、設置開機自動啟動

 

[root@VM_2_2_centos ~]# chkconfig strongswan on

 

5、添加路由

 

在IDC核心網關上添加到對端172.16.0.0/16的路由,下一跳指向strongswan的IP 10.0.2.2。

 

Destination subnet: 172.16.0.0/16 nexthop: 10.0.2.2

 

三、騰訊雲側配置

 

因為騰訊雲V** Gateway已經產品化,所以用戶可以快速在控制台里創建,具體操作如下:

 

1.創建V** Gateway

 

2.創建對端網關

 

3.創建V**通道

 

(1)第一步:選擇對應的V**網關和對端網關,預共享秘鑰與對端一致。

 

(2)第二步:填寫本端網段和對端網段。

 

(3)第三步:IKE配置,需與對端一致,注意目前騰訊雲只支持IKE V1版本,V2版本暫不支持。

 

(4)第四步:IPsec配置,需與對端一致。

 

4.騰訊雲側添加路由

 

添加去往對端10.0.0.0/16的路由,指向V** Gateway。

 

四、測試驗證

 

1.在騰訊雲側查看V**通道狀態

 

顯示“已聯調”狀態,說明V**通道已經正常建立。

 

2.在IDC端的strongSwan上驗證

 

(1)運行strongswan status,查看IPsec V**狀態已經建立。

 

[root@VM_2_2_centos ~]# strongswan status Security Associations (1 up, 0 connecting): tencent-V**-aws[1]: ESTABLISHED 2 hours ago, 10.0.2.2[140.143.120.162]...115.159.144.75[115.159.144.75] tencent-V**-aws{3}: INSTALLED, TUNNEL, reqid 1, ESP in UDP SPIs: c9e368a4_i aa7549f9_o tencent-V**-aws{3}: 10.0.0.0/16 === 172.16.0.0/16

 

(2)運行ip xfrm policy,查看路由策略。

 

[root@VM_2_2_centos ~]# ip xfrm policy src 10.0.0.0/16 dst 172.16.0.0/16 dir out priority 383615 ptype main tmpl src 10.0.2.2 dst 115.159.144.75 proto esp spi 0xaa7549f9 reqid 1 mode tunnel src 172.16.0.0/16 dst 10.0.0.0/16 dir fwd priority 383615 ptype main tmpl src 115.159.144.75 dst 10.0.2.2 proto esp reqid 1 mode tunnel src 172.16.0.0/16 dst 10.0.0.0/16 dir in priority 383615 ptype main tmpl src 115.159.144.75 dst 10.0.2.2 proto esp reqid 1 mode tunnel src 0.0.0.0/0 dst 0.0.0.0/0 socket in priority 0 ptype main src 0.0.0.0/0 dst 0.0.0.0/0 socket out priority 0 ptype main src 0.0.0.0/0 dst 0.0.0.0/0 socket in priority 0 ptype main src 0.0.0.0/0 dst 0.0.0.0/0 socket out priority 0 ptype main src ::/0 dst ::/0 socket in priority 0 ptype main src ::/0 dst ::/0 socket out priority 0 ptype main src ::/0 dst ::/0 socket in priority 0 ptype main src ::/0 dst ::/0 socket out priority 0 ptype main

 

3.雙向ping測試

 

(1)從IDC往騰訊雲172.16.2.2測試,ping通。

 

(2)從騰訊雲往IDC 10.0.2.14測試,ping通。

 

4.雙向速度測試

 

使用iperf壓測工具,騰訊雲VPC Gateway和strongSwan的帶寬都設置的是5Mbps,壓測速度能達到5Mbps就符合預期。

 

4.1 從騰訊雲172.16.2.2往IDC 10.0.2.14壓測。

 

(1)10.0.2.14上以server模塊啟動iperf,-s 以server模式啟動,-i 以秒為單位顯示報告間隔。

 

[root@VM_2_14_centos ~]# iperf -s -i 2 ------------------------------------------------------------ Server listening on TCP port 5001 TCP window size: 85.3 KByte (default) ------------------------------------------------------------ [ 4] local 10.0.2.14 port 5001 connected with 172.16.2.2 port 41074 [ ID] Interval Transfer Bandwidth [ 4] 0.0- 2.0 sec 1.67 MBytes 6.99 Mbits/sec [ 4] 2.0- 4.0 sec 1.11 MBytes 4.67 Mbits/sec [ 4] 4.0- 6.0 sec 1.18 MBytes 4.94 Mbits/sec [ 4] 6.0- 8.0 sec 1.15 MBytes 4.81 Mbits/sec [ 4] 8.0-10.0 sec 1.12 MBytes 4.69 Mbits/sec [ 4] 0.0-10.3 sec 6.38 MBytes 5.19 Mbits/sec

 

(2)172.16.2.2上以client模式運行iperf,-c host以client模式啟動,host是server端地址,-i 以秒為單位顯示報告間隔。

 

測試騰訊雲>>>IDC的速度能達到5Mbps,符合預期。

 

[root@VM_2_2_centos ~]# iperf -c 10.0.2.14 -i 2 ------------------------------------------------------------ Client connecting to 10.0.2.14, TCP port 5001 TCP window size: 45.0 KByte (default) ------------------------------------------------------------ [ 3] local 172.16.2.2 port 41074 connected with 10.0.2.14 port 5001 [ ID] Interval Transfer Bandwidth [ 3] 0.0- 2.0 sec 2.00 MBytes 8.39 Mbits/sec [ 3] 2.0- 4.0 sec 1.12 MBytes 4.72 Mbits/sec [ 3] 4.0- 6.0 sec 1.12 MBytes 4.72 Mbits/sec [ 3] 6.0- 8.0 sec 1.12 MBytes 4.72 Mbits/sec [ 3] 8.0-10.0 sec 1.00 MBytes 4.19 Mbits/sec [ 3] 0.0-10.0 sec 6.38 MBytes 5.34 Mbits/sec

 

4.2 從IDC 10.0.2.14往騰訊雲172.16.2.2壓測。

 

(1)172.16.2.2上以server模塊啟動iperf,-s 以server模式啟動,-i 以秒為單位顯示報告間隔。

 

[root@VM_2_2_centos ~]# iperf -s -i 2 ------------------------------------------------------------ Server listening on TCP port 5001 TCP window size: 85.3 KByte (default) ------------------------------------------------------------ [ 4] local 172.16.2.2 port 5001 connected with 10.0.2.14 port 56336 [ ID] Interval Transfer Bandwidth [ 4] 0.0- 2.0 sec 1.57 MBytes 6.57 Mbits/sec [ 4] 2.0- 4.0 sec 1.13 MBytes 4.75 Mbits/sec [ 4] 4.0- 6.0 sec 1.11 MBytes 4.65 Mbits/sec [ 4] 6.0- 8.0 sec 1.17 MBytes 4.92 Mbits/sec [ 4] 8.0-10.0 sec 1.14 MBytes 4.78 Mbits/sec [ 4] 0.0-10.5 sec 6.38 MBytes 5.10 Mbits/sec

 

(2)10.0.2.14上以client模式運行iperf,-c host以client模式啟動,host是server端地址,-i 以秒為單位顯示報告間隔。

 

測試IDC>>>騰訊雲的速度能達到5Mbps,符合預期。

 

[root@VM_2_14_centos ~]# iperf -c 172.16.2.2 -i 2 ------------------------------------------------------------ Client connecting to 172.16.2.2, TCP port 5001 TCP window size: 45.0 KByte (default) ------------------------------------------------------------ [ 3] local 10.0.2.14 port 56336 connected with 172.16.2.2 port 5001 [ ID] Interval Transfer Bandwidth [ 3] 0.0- 2.0 sec 1.75 MBytes 7.34 Mbits/sec [ 3] 2.0- 4.0 sec 1.25 MBytes 5.24 Mbits/sec [ 3] 4.0- 6.0 sec 1.12 MBytes 4.72 Mbits/sec [ 3] 6.0- 8.0 sec 1.12 MBytes 4.72 Mbits/sec [ 3] 8.0-10.0 sec 1.12 MBytes 4.72 Mbits/sec [ 3] 0.0-10.2 sec 6.38 MBytes 5.24 Mbits/sec

 

最后附上strongSwan配置參考文檔鏈接:

 

https://wiki.strongswan.org/projects/strongswan/wiki/UserDocumentation


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM