CentOS 7 搭建 IKEv2 VPN 服务
更新系统安装
安装EPEL
1 2
| yum -y install epel-release
|
安装依赖组件
1 2
| yum -y install gpm-devel pam-devel openssl-devel make gcc
|
安装Strongswan
1 2 3 4
| yum -y install strongswan
alias ipsec='strongswan'
|
生成CA私钥,并使用此私钥自签名生成CA证书
1 2 3
| cd /etc/strongswan && mkdir ca && cd ca ipsec pki --gen --outform pem > ca.pem ipsec pki --self --in ca.pem --dn "C=[国家名], O=[组织名], CN=[通用名]" --ca --lifetime 3652 --outform pem > ca.cert.pem
|
注:配置中的 C=[国家名], O=[组织名] 值都相同
生成服务器私钥,然后从私钥中导出公钥,用CA证书签发服务器证书
1 2
| ipsec pki --gen --outform pem > server.pem ipsec pki --pub --in server.pem | ipsec pki --issue --lifetime 3652 --cacert ca.cert.pem --cakey ca.pem --dn "C=[国家名], O=[组织名], CN=[IP 或 domain]" --san="[IP 或 domain]" --flag serverAuth --flag ikeIntermediate --outform pem > server.cert.pem
|
生成客户端私钥,然后从私钥中导出公钥,用CA证书签发客户端证书
1 2
| ipsec pki --gen --outform pem > client.pem ipsec pki --pub --in client.pem | ipsec pki --issue --lifetime 3652 --cacert ca.cert.pem --cakey ca.pem --dn "C=[国家名], O=[组织名], CN=[通用名]" --outform pem > client.cert.pem
|
生成 pkcs12 证书,用来导入手机或电脑。此处需输入密码,此密码用于安装CA证书时使用
1
| openssl pkcs12 -export -inkey client.pem -in client.cert.pem -name "[此处与第7步中CN同值]" -certfile ca.cert.pem -caname "[CA 证书别名]" -out client.cert.p12
|
strongswam导入证书
1 2 3 4 5 6 7 8
|
cp -r ca.cert.pem /etc/strongswan/ipsec.d/cacerts/ cp -r server.cert.pem /etc/strongswan/ipsec.d/certs/ cp -r server.pem /etc/strongswan/ipsec.d/private/ cp -r client.cert.pem /etc/strongswan/ipsec.d/certs/ cp -r client.pem /etc/strongswan/ipsec.d/private/
|
修改strongSwan各组件的通用配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| cat >/etc/strongswan/strongswan.conf <<EOF charon { load_modular = yes duplicheck.enable = no compress = yes plugins { include strongswan.d/charon/*.conf } dns1 = 8.8.8.8 dns2 = 8.8.4.4 nbns1 = 8.8.8.8 nbns2 = 8.8.4.4
filelog { /var/log/strongswan.log { time_format = %b %e %T default = 5 append = no flush_line = yes } } }
include strongswan.d/*.conf EOF
|
修改IPsec相关的配置,定义IKE版本、验证方式、加密方式、连接属性等等
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| cat >/etc/strongswan/ipsec.conf <<EOF config setup protostack=netkey listen = [IP 或 domain] uniqueids = never
conn %default left=%any leftsubnet=0.0.0.0/0 right=%any rightsourceip=10.10.10.0/24 dpdaction=clear
conn IKEv1-CERT-XAUTH keyexchange=ikev1 fragmentation=yes leftauth=pubkey leftcert=server.cert.pem rightauth=pubkey rightauth2=xauth rightcert=client.cert.pem auto=add
conn IKEv1-PSK-XAUTH keyexchange=ikev1 leftauth=psk rightauth=psk rightauth2=xauth auto=add aggressive=yes
conn IKEv2-CERT keyexchange=ikev2 leftauth=pubkey leftcert=server.cert.pem rightauth=pubkey rightcert=client.cert.pem auto=add
conn IKEv2-EAP keyexchange=ikev2 ike=aes256-sha1-modp1024,aes128-sha1-modp1024,3des-sha1-modp1024! esp=aes256-sha256,aes256-sha1,3des-sha1! rekey=no leftauth=pubkey leftcert=server.cert.pem leftsendcert=always leftid=[IP 或 domain] rightauth=eap-mschapv2 rightsendcert=never eap_identity=%any fragmentation=yes auto=add EOF
|
定义各类密钥,例如:私钥、预共享密钥、用户账户和密码
1 2 3 4 5 6
| cat >/etc/strongswan/ipsec.secrets <<EOF : RSA server.pem : PSK "[xxxxxxx]" [username] : XAUTH "[password]" [username] : EAP "[password]" EOF
|
设置ipsec.secrets文件权限
设置网卡转发
1 2 3 4
| cat >/etc/sysctl.conf <<EOF net.ipv4.ip_forward = 1 EOF sysctl -p
|
配置防火墙,本处使用的firewalld
1 2 3 4 5
| yum install firewall firewall-config firewall-cmd --permanent --add-service=ipsec firewall-cmd --permanent --zone=public --add-masquerade firewall-cmd --permanent --zone=public --add-rich-rule='rule family=ipv4 source address=10.8.0.0/24 masquerade' firewall-cmd --reload
|
配置服务启动
1 2 3
| chkconfig strongswan on chkconfig firewalld on systemctl start strongswan
|
strongswan 查看日志状态
链接VPN的方式
- Windows
- 10 以上的使用IKEv2的方式连接
- 10 以下推荐安装官方的strongswan APP链接
- IOS
IKEv2 链接
- Android
推荐安装官方的strongswan APP链接
注:推荐使用IKEv2 不推荐使用 IPSEC、IKE 等等