在阿里云 CentOS 7上使用strongswan搭建IKEv2 VPN
来源:互联网 发布:哑铃健身软件 编辑:程序博客网 时间:2024/06/09 14:43
1、安装strongswan
yum install strongswansystemctl enable strongswansystemctl start strongswan
2、创建证书
strongswan pki --gen --outform pem > ca.key.pemstrongswan pki --self --in ca.key.pem --dn "C=CN, O=iSoft, CN=iSoft wendell CA" --ca --lifetime 3650 --outform pem > ca.cert.pemstrongswan pki --gen --outform pem > server.key.pemstrongswan pki --pub --in server.key.pem --outform pem > server.pub.pem strongswan pki --pub --in server.key.pem | strongswan pki --issue --lifetime 3601 --cacert ca.cert.pem \ --cakey ca.key.pem --dn "C=CN, O=Chicago, CN=你的域名" \ --san="你的IP" --san="你的域名" --flag serverAuth --flag ikeIntermediate \ --outform pem > server.cert.pem
注:所填写的–dn都必须一致,在创建server.cert.pem 要写多个–san,如果有写一个的话,在连接的时候报 “ike身份验证凭证不可接受” 错误
如果只需要密码登录,上面的证书就创建完了
创建客户端证书
strongswan pki --gen --outform pem > client.key.pemstrongswan pki --pub --in client.key.pem --outform pem > client.pub.pemstrongswan pki --issue --lifetime 1200 --cacert ca.cert.pem --cakey ca.key.pem --in client.pub.pem --dn "C=CN, O=iSoft, CN=iSoft wendell CA" --outform pem > client.cert.pemopenssl pkcs12 -export -inkey client.key.pem -in client.cert.pem -name "iSoft wendell Client Cert" -certfile ca.cert.pem -caname "iSoft wendell CA" -out client.cert.p12
3、安装证书
cp -r ca.key.pem /etc/strongswan/ipsec.d/private/cp -r ca.cert.pem /etc/strongswan/ipsec.d/cacerts/cp -r server.cert.pem /etc/strongswan/ipsec.d/certs/cp -r server.pub.pem /etc/strongswan/ipsec.d/certs/cp -r server.key.pem /etc/strongswan/ipsec.d/private/cp -r client.cert.pem /etc/strongswan/ipsec.d/certs/cp -r client.key.pem /etc/strongswan/ipsec.d/private/
把 CA 证书(ca.cert.pem)、客户端证书(client.cert.pem)和 .p12 证书(client.cert.p12)用 FTP 复制出来给客户端用
4、配置VPN
vim /etc/strongswan/ipsec.conf
config setup uniqueids=never #允许多个客户端使用同一个证书conn %default #定义连接项, 命名为 %default 所有连接都会继承它 compress = yes #是否启用压缩, yes 表示如果支持压缩会启用. dpdaction = hold #当意外断开后尝试的操作, hold, 保持并重连直到超时. dpddelay = 30s #意外断开后尝试重连时长 dpdtimeout = 60s #意外断开后超时时长, 只对 IKEv1 起作用 inactivity = 300s #闲置时长,超过后断开连接. leftdns = 8.8.8.8,8.8.4.4 #指定服务端与客户端的dns, 多个用","分隔 rightdns = 8.8.8.8,8.8.4.4conn IKEv2-BASE leftca = "C=CN, O=iSoft, CN=iSoft wendell CA" #服务器端根证书DN名称 leftsendcert = always #是否发送服务器证书到客户端 rightsendcert = never #客户端不发送证书conn IKEv2-EAP keyexchange=ikev2 #默认的密钥交换算法, ike 为自动, 优先使用 IKEv2 left=%any #服务器端标识,%any表示任意 leftid=47.254.16.126 #服务器端ID标识 leftsubnet=0.0.0.0/0 #服务器端虚拟ip, 0.0.0.0/0表示通配. leftcert = server.cert.pem #服务器端证书 leftauth=pubkey #服务器校验方式,使用证书 right=%any #客户端标识,%any表示任意 rightsourceip = 10.1.0.0/24 #客户端IP地址分配范围 rightauth=eap-mschapv2 #eap-md5#客户端校验方式#KEv2 EAP(Username/Password) #rightauth=rsa #客户端校验方式,使用证书#IKEv2 Certificate #rightcert=clientCert.pem #客户端端证书#IKEv2 Certificate #eap_identity=%any # also=IKEv2-BASE eap_identity = %any #指定客户端eap id rekey = no #不自动重置密钥 fragmentation = yes #开启IKE 消息分片 auto = add #当服务启动时, 应该如何处理这个连接项. add 添加到连接表中.
注:leftid = 你的IP地址,这个IP地址,要跟上面那个证书的 –san=”你的IP”,保持一致
5、修改 dns 配置
/etc/strongswan/strongswan.d/charon.conf
charon { duplicheck.enable = no #同时连接多个设备,把冗余检查关闭. # windows 公用 dns dns1 = 8.8.8.8 dns2 = 8.8.4.4 #以下是日志输出, 生产环境请关闭. filelog { /var/log/charon.log { # add a timestamp prefix time_format = %b %e %T # prepend connection name, simplifies grepping ike_name = yes # overwrite existing files append = no # increase default loglevel for all daemon subsystems default = 1 # flush each line to disk flush_line = yes } }}
vim /etc/strongswan/strongswan.conf
1 # strongswan.conf - strongSwan configuration file 2 # 3 # Refer to the strongswan.conf(5) manpage for details 4 # 5 # Configuration changes should be made in the included files 6 7 charon { 8 load_modular = yes 9 duplicheck.enable = no 10 compress = yes 11 plugins { 12 include strongswan.d/charon/*.conf 13 } 14 dns1 = 8.8.8.8 15 nbns1 =8.8.4.4 16 } 17 18 include strongswan.d/*.conf
6、配置用户和密码
vim /etc/strongswan/ipsec.secrets
1 # ipsec.secrets - strongSwan IPsec secrets file 2 #使用证书验证时的服务器端私钥 3 #格式 : RSA <private key file> [ <passphrase> | %prompt ] 4 : RSA server.key.pem 5 6 #使用预设加密密钥, 越长越好 7 #格式 [ <id selectors> ] : PSK <secret> 8 %any %any : PSK "wendell" 9 10 #EAP 方式, 格式同 psk 相同 11 wendell %any : EAP "wendell" 12 13 #XAUTH 方式, 只适用于 IKEv1 14 #格式 [ <servername> ] <username> : XAUTH "<password>" 15 wendell %any : XAUTH "wendell"
7、开启内核转发
/etc/sysctl.conf
net.ipv4.ip_forward = 1net.ipv6.conf.all.forwarding=1
8、配置防火
vim /usr/lib/firewalld/services/strongswan.xml
<?xml version="1.0" encoding="utf-8"?><service> <short>Strongswan</short> <description>Strongswan VPN</description> <port protocol="udp" port="500,4500"/></service>
添加服务到当前区域, 开启转发
#以下命令没有指定 --zone 参数, 都是针对默认区域 public#为区域添加服务firewall-cmd --permanent --add-service=strongswanfirewall-cmd --permanent --add-service="ipsec"firewall-cmd --permanent --add-rich-rule='rule protocol value="esp" accept' # ESP (the encrypted data packets)firewall-cmd --permanent --add-rich-rule='rule protocol value="ah" accept' # AH (authenticated headers)firewall-cmd --permanent --add-port=500/udp #IKE (security associations)firewall-cmd --permanent --add-port=4500/udp # IKE NAT Traversal (IPsec between natted devices)#启用ip伪装firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.1.0.0/16" masquerade'#添加 nat 转发firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.1.0.0/16" forward-port port="4500" protocol="udp" to-port="4500"'firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.1.0.0/16" forward-port port="500" protocol="udp" to-port="500"'firewall-cmd --permanent --add-rich-rule='rule protocol value="esp" accept' # ESP (the encrypted data packets)firewall-cmd --permanent --add-rich-rule='rule protocol value="ah" accept' # AH (authenticated headers)#重新加载防火墙配置firewall-cmd --reloadfirewall-cmd --list-all
除了用 firewall-cmd 命令外, 还可以换种方法添加上面的规则.
vim /etc/firewalld/zones/public.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <zone> 3 <short>Public</short> 4 <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description> 5 <interface name="eth0"/> 6 <service name="ssh"/> 7 <service name="dhcpv6-client"/> 8 <service name="ipsec"/> 9 <service name="strongswan"/> 10 <port protocol="tcp" port="1723"/> 11 <port protocol="tcp" port="47"/> 12 <port protocol="tcp" port="1701"/> 13 <port protocol="tcp" port="22"/> 14 <masquerade/> 15 <rule family="ipv4"> 16 <source address="10.1.0.0/16"/> 17 <masquerade/> 18 </rule> 19 <rule family="ipv4"> 20 <source address="10.1.0.0/16"/> 21 <forward-port to-port="4500" protocol="udp" port="4500"/> 22 </rule> 23 <rule family="ipv4"> 24 <source address="10.1.0.0/16"/> 25 <forward-port to-port="500" protocol="udp" port="500"/> 26 </rule> 27 </zone>
至此, 服务端配置已完成.
firewall-cmd --reload #重新加载防火墙配置strongswan stop #使用strongswan 自身的命令停止服务systemctl start strongswan #使用 systemctl 命令启动服务
9、开放阿里云的端口
10、客户端配置
IOS:
先导入 CA 证书
将之前创建的 ca.cert.pem 用 ftp 导出 , 写邮件以附件的方式发到邮箱, 在 ios 浏览器登录邮箱, 下载附件, 安装 ca 证书.
- 使用 IKEv2 + EAP 认证
找到手机上 “设置->VPN->添加配置”, 选 IKEv2
描述: 随便填
服务器: 填url或ip
远程ID: 填url或ip
用户鉴定: 用户名
用户名: EAP 项用户名
密码: EAP 项密码
2. 使用 IKEv2 + 客户端证书 认证
把之前的 .p12 证书(里面包含ca证书)发到邮箱在手机上打开. 导入到手机(此时需要之前设置的证书密码).
找到手机上 “设置->VPN->添加配置”, 选 IKEv2
描述: 随便填
服务器: 填url或ip
远程ID: 填url或ip
用户鉴定: 证书
证书: 选择安装完的客户端证书
3. 使用 IKEv2 + 预设密钥 认证
找到手机上 “设置->VPN->添加配置”, 选 IKEv2
描述: 随便填
服务器: 填url或ip
远程ID: 填url或ip
用户鉴定: 无
使用证书: 关
密钥: PSK 项密钥
Windows 10
导入证书:
将 CA 根证书 ca.cert.pem 重命名为 ca.cert.crt
win+r 运行mmc;
文件>添加删除管理单元;
在可用的管理单元中选择”证书“,点击添加》确定;
在控制节点中展开证书》受信任的证书颁发机构》证书,右击所有任务》导入.
建立连接:
“控制面板”-“网络和共享中心”-“设置新的连接或网络”-“连接到工作区”-“使用我的 Internet 连接”
Internet 地址写服务器 IP 或 URL。
描述随便写。
用户名密码写之前配置的 EAP 的那个。
确定
转到 控制面板网络和 Internet网络连接
在新建的 VPN 连接上右键属性然后切换到“安全”选项卡
VPN 类型选 IKEv2
数据加密选“需要加密”
身份认证这里需要说一下,如果想要使用 EAP 认证的话就选择“Microsoft:安全密码(EAP-MSCHAP v2)”; 想要使用私人证书认证的话就选择“使用计算机证书”。
再切换到 “网络” 选项卡, 双击 “Internet 协议版本 4” 以打开属性窗口,
点击 “高级” 按钮, 勾选 “在远程网络上使用默认网关”, 确定退出.
VPN 配置完成
参考:
https://raymii.org/s/tutorials/IPSEC_vpn_with_CentOS_7.html
https://blog.itnmg.net/2015/04/03/centos7-ipsec-vpn/
http://blog.csdn.net/igame/article/details/52749079
- 在阿里云 CentOS 7上使用strongswan搭建IKEv2 VPN
- 在CentOS 7上使用strongSwan搭建IPsec VPN
- 使用Strongswan搭建IPSec/IKEv2 VPN
- 使用Strongswan搭建IPSec/IKEv2 VPN
- UBUNTU、CENTOS搭建IPSEC/IKEV2 VPN服务器全攻略----Strongswan
- 在 Ubuntu 上用 StrongSwan 安装 IKEv2 VPN 服务器
- Centos6.5 下Strongswan + IKEV2 VPN搭建
- 如何使用Strongswan和Freeradius建立IKEv2 VPN
- 香港云主机CentOS系统上安装strongSwan搭建IPsec VPN服务器
- strongswan 搭建vpn
- 搭建适用于Ubuntu/CentOS的IKEV2/L2TP的VPN
- 阿里云centos下vpn的搭建
- IOS iphone ipad CentOS7 安装配置 StrongSwan IPsec IKEv2 VPN
- 在 CentOS 7 上搭建 Cisco AnyConnect VPN
- CentOS上搭建VPN
- 在阿里云centOS上搭建 nginx tomcat mysql 环境
- 在centos上搭建阿里云python开发环境
- 使用 Strongswan 架设 Ipsec VPN
- TCP/IP协议族【第19章 域名系统DNS】
- asp.net 每天的某时刻定时执行任务
- 新手村 简单字符串 小书童——密码
- 这是个测试标题
- Vue.js学习常用的几个网站
- 在阿里云 CentOS 7上使用strongswan搭建IKEv2 VPN
- 转发和重定向的区别
- 数据结构:顺序结构和链式结构的数据类型定义
- ubuntu下一台mysal启动多实例
- java--经典50题part1
- python中numpy-choice函数
- Linux文件管理的链接文件问题
- the constructor pattern
- 防止sql注入方法