在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