在 CentOS 7 中使用 strongSwan 搭建 IKEv2 VPN

[TOCM]

本文介绍在 CentOS 7 系统中使用 strongSwan 开源软件搭建 IKEv2 方式的 VPN 配置。

本文涉及到的各项系统及软件版本:

  • 服务端系统:CentOS 7.4 64位
  • 客户端系统:Windows 10 专业版,版本号 1809
  • 软件:strongSwan 版本 5.7.2

一、安装 strongSwan


使用 EPEL 源安装 strongSwan,因为 EPEL 包含 strongSwan 最新版本,EPEL 更新比较快,如果系统中没有,则执行下面命令安装 EPEL 源。

1、安装 EPEL 源
  1. yum -y install epel-release

注意,这不是必须的,你也可以使用其它源安装。

2、安装 openssl
  1. yum -y install openssl

注意,这不是必须的,请根据实际情况调整。

3、安装 strongswan
  1. yum -y install strongswan

注意,这是必须的。

4、设置开机启动
  1. systemctl enable strongswan

二、创建证书


1、创建 CA 根证书

1.1 创建一个私钥:
  1. strongswan pki --gen --outform pem > ca.key.pem
1.2 基于这个私钥自己签一个 CA 根证书:
  1. strongswan pki --self --in ca.key.pem --dn "C=CN, O=123si, CN=123si StrongSwan CA" --ca --lifetime 3650 --outform pem > ca.cert.pem

命令参数介绍:

参数 描述
--self 表示自签证书
--in 是输入的私钥
--dn 是判别名
C 表示国家名,同样还有 ST 州/省名,L 地区名,STREET(全大写)街道名
O 组织名称
CN 友好显示的通用名
--ca 表示生成 CA 根证书
--lifetime 为有效期, 单位是天

2、创建服务器端证书

2.1 创建一个私钥:
  1. strongswan pki --gen --outform pem > server.key.pem
2.2 用我们刚才自签的 CA 证书给自己发一个服务器证书:
  1. # 用私钥创建公钥
  2. strongswan pki --pub --in server.key.pem --outform pem > server.pub.pem
  3. # 用刚创建的公钥,创建服务器证书
  4. strongswan pki --issue --lifetime 3650 --cacert ca.cert.pem --cakey ca.key.pem --in server.pub.pem --dn "C=CN, O=123si, CN=48.85.166.86" --san="48.85.166.86" --flag serverAuth --flag ikeIntermediate --outform pem > server.cert.pem

命令参数介绍:

--issue--cacert--cakey就是表明要用刚才自签的 CA 证书来签这个服务器证书。

--dn--san--flag是一些客户端方面的特殊要求:

  • iOS 客户端要求 CN 也就是通用名必须是你的服务器的 URL 或 IP 地址;
  • Windows 7 不但要求了上面,还要求必须显式说明这个服务器证书的用途(用于与服务器进行认证),--flag serverAuth
  • 非 iOS 的 Mac OS X 要求了“IP 安全网络密钥互换居间(IP Security IKE Intermediate)”这种增强型密钥用法(EKU),--flag ikdeIntermediate
  • Android 和 iOS 都要求服务器别名(serverAltName)就是服务器的 URL 或 IP 地址,--san

3、创建客户端证书

3.1 创建一个私钥:
  1. strongswan pki --gen --outform pem > client.key.pem
3.2 然后用刚才自签的 CA 证书来签客户端证书:
  1. # 用私钥创建公钥
  2. strongswan pki --pub --in client.key.pem --outform pem > client.pub.pem
  3. # 用刚创建的公钥,创建客户端证书
  4. strongswan pki --issue --lifetime 3650 --cacert ca.cert.pem --cakey ca.key.pem --in client.pub.pem --dn "C=CN, O=123si, CN=48.85.166.86" --outform pem > client.cert.pem

4、打包证书为 pkcs12

  1. openssl pkcs12 -export -inkey client.key.pem -in client.cert.pem -name "123si StrongSwan Client Cert" -certfile ca.cert.pem -caname "123si StrongSwan CA" -out client.cert.p12

执行命令后,会提示输入两次密码,这个密码是在导入证书到其他系统时需要验证的。没有这个密码即使别人拿到了证书也没法使用。

三、安装证书


  1. cp -r ca.key.pem /etc/strongswan/ipsec.d/private/
  2. cp -r ca.cert.pem /etc/strongswan/ipsec.d/cacerts/
  3. cp -r server.cert.pem /etc/strongswan/ipsec.d/certs/
  4. cp -r server.pub.pem /etc/strongswan/ipsec.d/certs/
  5. cp -r server.key.pem /etc/strongswan/ipsec.d/private/
  6. cp -r client.cert.pem /etc/strongswan/ipsec.d/certs/
  7. cp -r client.key.pem /etc/strongswan/ipsec.d/private/

把 CA 证书(ca.cert.pem)、客户端证书(client.cert.pem)和 .p12 证书(client.cert.p12)用 FTP 复制出来给客户端用。

四、配置 VPN


1、修改主配置文件 ipsec.conf

配置文件ipsec.conf官方介绍链接:ipsec.conf: conn Reference

编辑配置文件:

  1. vim /etc/strongswan/ipsec.conf

配置文件常用设置说明:

  1. config setup
  2. # 是否缓存证书吊销列表
  3. # <em>cachecrls = yes</em>
  4. # 是否严格执行证书吊销规则
  5. # strictcrlpolicy=yes
  6. # 如果同一个用户在不同的设备上重复登录,yes 断开旧连接,创建新连接;no 保持旧连接,并发送通知;never 同 no,但不发送通知。
  7. uniqueids=no
  8. # 配置根证书,如果不使用证书吊销列表,可以不用这段。命名为 %default 所有配置节都会继承它
  9. # ca %default
  10. # 证书吊销列表 URL,可以是 LDAP,HTTP,或文件路径
  11. # crluri = <uri>
  12. # 定义连接项,命名为 %default 所有连接都会继承它
  13. conn %default
  14. # 是否启用压缩,yes 表示如果支持压缩会启用
  15. compress = yes
  16. # 当意外断开后尝试的操作,hold,保持并重连直到超时
  17. dpdaction = hold
  18. # 意外断开后尝试重连时长
  19. dpddelay = 30s
  20. # 意外断开后超时时长,只对 IKEv1 起作用
  21. dpdtimeout = 60s
  22. # 闲置时长,超过后断开连接
  23. inactivity = 300s
  24. # 数据传输协议加密算法列表
  25. esp = aes256-sha256,aes256-sha1,3des-sha1!
  26. # 密钥交换协议加密算法列表
  27. ike = aes256-sha1-modp1024,aes128-sha1-modp1024,3des-sha1-modp1024!
  28. # 默认的密钥交换算法,ike 为自动,优先使用 IKEv2
  29. keyexchange = ike
  30. # 服务端公网 IP,可以是魔术字 %any,表示从本地 IP 地址表中取
  31. left = %any
  32. # 客户端 IP,同上
  33. right = %any
  34. # 指定服务端与客户端的 DNS,多个用“,”分隔
  35. leftdns = 8.8.8.8,8.8.4.4
  36. rightdns = 8.8.8.8,8.8.4.4
  37. # 服务端用于 ike 认证时使用的端口,默认为 500,如果使用了 nat 转发,则使用 4500
  38. # leftikeport = <port>
  39. # 服务器端虚拟 IP 地址
  40. # leftsourceip = %config
  41. # 客户端虚拟 IP 段
  42. rightsourceip = 10.0.0.0/24
  43. # 服务器端子网,魔术字 0.0.0.0/0 。如果为客户端分配虚拟 IP 地址的话,那表示之后要做 iptables 转发,那么服务器端就必须是用魔术字
  44. leftsubnet = 0.0.0.0/0
  45. # rightsubnet = <ip subnet>[[<proto/port>]][,...]
  46. conn IKEv2-BASE
  47. # 服务器端根证书 DN 名称
  48. leftca = "C=CN, O=123si, CN=123si StrongSwan CA"
  49. # 服务器证书,可以是 PEM 或 DER 格式
  50. leftcert = server.cert.pem
  51. # 不指定客户端证书路径
  52. # rightcert = <path>
  53. # 指定服务器证书的公钥
  54. leftsigkey = server.pub.pem
  55. # rightsigkey = <raw public key> | <path to public key>
  56. # 是否发送服务器证书到客户端
  57. leftsendcert = always
  58. # 客户端不发送证书
  59. rightsendcert = never
  60. # 服务端认证方法,使用证书
  61. leftauth = pubkey
  62. # 客户端认证使用 EAP 扩展认证,貌似 eap-mschapv2 比较通用
  63. rightauth = eap-mschapv2
  64. # 服务端 ID,可以任意指定,默认为服务器证书的 subject,还可以是魔术字 %any,表示什么都行
  65. leftid = vpn.itnmg.net
  66. # 客户端 id,任意
  67. rightid = %any
  68. # ios, mac os, win7+, linux
  69. conn IKEv2-EAP
  70. also = IKEv2-BASE
  71. # 指定客户端 eap id
  72. eap_identity = %any
  73. # 不自动重置密钥
  74. rekey = no
  75. # 开启 IKE 消息分片
  76. fragmentation = yes
  77. # 当服务启动时,应该如何处理这个连接项。add 添加到连接表中。
  78. auto = add

我的配置:

  1. # ipsec.conf - strongSwan IPsec configuration file
  2. # basic configuration
  3. config setup
  4. # strictcrlpolicy=yes
  5. uniqueids = never
  6. # Add connections here.
  7. # Sample VPN connections
  8. #conn sample-self-signed
  9. # leftsubnet=10.1.0.0/16
  10. # leftcert=selfCert.der
  11. # leftsendcert=never
  12. # right=192.168.0.2
  13. # rightsubnet=10.2.0.0/16
  14. # rightcert=peerCert.der
  15. # auto=start
  16. #conn sample-with-ca-cert
  17. # leftsubnet=10.1.0.0/16
  18. # leftcert=myCert.pem
  19. # right=192.168.0.2
  20. # rightsubnet=10.2.0.0/16
  21. # rightid="C=CH, O=Linux strongSwan CN=peer name"
  22. # auto=start
  23. conn %default
  24. compress = yes
  25. esp = aes256-sha256,aes256-sha1,3des-sha1!
  26. ike = aes256-sha256-modp2048,aes256-sha1-modp2048,aes128-sha1-modp2048,3des-sha1-modp2048,aes256-sha256-modp1024,aes256-sha1-modp1024,aes128-sha1-modp1024,3des-sha1-modp1024!
  27. keyexchange = ike
  28. keyingtries = 1
  29. leftdns = 8.8.8.8,8.8.4.4
  30. rightdns = 8.8.8.8,8.8.4.4
  31. conn IKEv2-BASE
  32. # 服务器端根证书 DN 名称
  33. leftca = "C=CN, O=123si, CN=123si StrongSwan CA"
  34. # 是否发送服务器证书到客户端
  35. leftsendcert = always
  36. # 客户端不发送证书
  37. rightsendcert = never
  38. conn IKEv2-EAP
  39. leftca = "C=CN, O=123si, CN=123si StrongSwan CA"
  40. leftcert = server.cert.pem
  41. leftsendcert = always
  42. rightsendcert = never
  43. leftid = 48.85.166.86
  44. left = %any
  45. right = %any
  46. leftauth = pubkey
  47. rightauth = eap-mschapv2
  48. leftfirewall = yes
  49. leftsubnet = 0.0.0.0/0
  50. rightsourceip = 10.1.0.0/16
  51. fragmentation = yes
  52. rekey = no
  53. eap_identity = %any
  54. auto = add

2、修改 DNS 配置

strongSwan v5.1.2 之后,所有插件配置都分散在/etc/strongswan/strongswan.d/目录中。

编辑配置文件:

  1. vim /etc/strongswan/strongswan.d/charon.conf

设置 Windows 公用 DNS,去掉dns1dns2前面的井号(#)。

修改如下:

  1. # Options for the charon IKE daemon.
  2. charon {
  3. # Accept unencrypted ID and HASH payloads in IKEv1 Main Mode.
  4. # accept_unencrypted_mainmode_messages = no
  5. # Maximum number of half-open IKE_SAs for a single peer IP.
  6. # block_threshold = 5
  7. # Whether Certificate Revocation Lists (CRLs) fetched via HTTP or LDAP
  8. # should be saved under a unique file name derived from the public key of
  9. # the Certification Authority (CA) to /etc/ipsec.d/crls (stroke) or
  10. # /etc/swanctl/x509crl (vici), respectively.
  11. # cache_crls = no
  12. # Whether relations in validated certificate chains should be cached in
  13. # memory.
  14. # cert_cache = yes
  15. # Send Cisco Unity vendor ID payload (IKEv1 only).
  16. # cisco_unity = no
  17. # Close the IKE_SA if setup of the CHILD_SA along with IKE_AUTH failed.
  18. # close_ike_on_child_failure = no
  19. # Number of half-open IKE_SAs that activate the cookie mechanism.
  20. # cookie_threshold = 10
  21. # Delete CHILD_SAs right after they got successfully rekeyed (IKEv1 only).
  22. # delete_rekeyed = no
  23. # Delay in seconds until inbound IPsec SAs are deleted after rekeyings
  24. # (IKEv2 only).
  25. # delete_rekeyed_delay = 5
  26. # Use ANSI X9.42 DH exponent size or optimum size matched to cryptographic
  27. # strength.
  28. # dh_exponent_ansi_x9_42 = yes
  29. # Use RTLD_NOW with dlopen when loading plugins and IMV/IMCs to reveal
  30. # missing symbols immediately.
  31. # dlopen_use_rtld_now = no
  32. # DNS server assigned to peer via configuration payload (CP).
  33. # Windows 公用 DNS
  34. dns1 = 8.8.8.8
  35. # DNS server assigned to peer via configuration payload (CP).
  36. # Windows 公用 DNS
  37. dns2 = 8.8.4.4
  38. # Enable Denial of Service protection using cookies and aggressiveness
  39. # checks.
  40. # dos_protection = yes
  41. # Compliance with the errata for RFC 4753.
  42. # ecp_x_coordinate_only = yes
  43. # Free objects during authentication (might conflict with plugins).
  44. # flush_auth_cfg = no
  45. # Whether to follow IKEv2 redirects (RFC 5685).
  46. # follow_redirects = yes
  47. # Maximum size (complete IP datagram size in bytes) of a sent IKE fragment
  48. # when using proprietary IKEv1 or standardized IKEv2 fragmentation, defaults
  49. # to 1280 (use 0 for address family specific default values, which uses a
  50. # lower value for IPv4). If specified this limit is used for both IPv4 and
  51. # IPv6.
  52. # fragment_size = 1280
  53. # Name of the group the daemon changes to after startup.
  54. # group =
  55. # Timeout in seconds for connecting IKE_SAs (also see IKE_SA_INIT DROPPING).
  56. # half_open_timeout = 30
  57. # Enable hash and URL support.
  58. # hash_and_url = no
  59. # Allow IKEv1 Aggressive Mode with pre-shared keys as responder.
  60. # i_dont_care_about_security_and_use_aggressive_mode_psk = no
  61. # Whether to ignore the traffic selectors from the kernel's acquire events
  62. # for IKEv2 connections (they are not used for IKEv1).
  63. # ignore_acquire_ts = no
  64. # A space-separated list of routing tables to be excluded from route
  65. # lookups.
  66. # ignore_routing_tables =
  67. # Maximum number of IKE_SAs that can be established at the same time before
  68. # new connection attempts are blocked.
  69. # ikesa_limit = 0
  70. # Number of exclusively locked segments in the hash table.
  71. # ikesa_table_segments = 1
  72. # Size of the IKE_SA hash table.
  73. # ikesa_table_size = 1
  74. # Whether to close IKE_SA if the only CHILD_SA closed due to inactivity.
  75. # inactivity_close_ike = no
  76. # Limit new connections based on the current number of half open IKE_SAs,
  77. # see IKE_SA_INIT DROPPING in strongswan.conf(5).
  78. # init_limit_half_open = 0
  79. # Limit new connections based on the number of queued jobs.
  80. # init_limit_job_load = 0
  81. # Causes charon daemon to ignore IKE initiation requests.
  82. # initiator_only = no
  83. # Install routes into a separate routing table for established IPsec
  84. # tunnels.
  85. # install_routes = yes
  86. # Install virtual IP addresses.
  87. # install_virtual_ip = yes
  88. # The name of the interface on which virtual IP addresses should be
  89. # installed.
  90. # install_virtual_ip_on =
  91. # Check daemon, libstrongswan and plugin integrity at startup.
  92. # integrity_test = no
  93. # A comma-separated list of network interfaces that should be ignored, if
  94. # interfaces_use is specified this option has no effect.
  95. # interfaces_ignore =
  96. # A comma-separated list of network interfaces that should be used by
  97. # charon. All other interfaces are ignored.
  98. # interfaces_use =
  99. # NAT keep alive interval.
  100. # keep_alive = 20s
  101. # Plugins to load in the IKE daemon charon.
  102. # load =
  103. # Determine plugins to load via each plugin's load option.
  104. # load_modular = no
  105. # Initiate IKEv2 reauthentication with a make-before-break scheme.
  106. # make_before_break = no
  107. # Maximum number of IKEv1 phase 2 exchanges per IKE_SA to keep state about
  108. # and track concurrently.
  109. # max_ikev1_exchanges = 3
  110. # Maximum packet size accepted by charon.
  111. # max_packet = 10000
  112. # Enable multiple authentication exchanges (RFC 4739).
  113. # multiple_authentication = yes
  114. # WINS servers assigned to peer via configuration payload (CP).
  115. # nbns1 =
  116. # WINS servers assigned to peer via configuration payload (CP).
  117. # nbns2 =
  118. # UDP port used locally. If set to 0 a random port will be allocated.
  119. # port = 500
  120. # UDP port used locally in case of NAT-T. If set to 0 a random port will be
  121. # allocated. Has to be different from charon.port, otherwise a random port
  122. # will be allocated.
  123. # port_nat_t = 4500
  124. # Whether to prefer updating SAs to the path with the best route.
  125. # prefer_best_path = no
  126. # Prefer locally configured proposals for IKE/IPsec over supplied ones as
  127. # responder (disabling this can avoid keying retries due to
  128. # INVALID_KE_PAYLOAD notifies).
  129. # prefer_configured_proposals = yes
  130. # By default public IPv6 addresses are preferred over temporary ones (RFC
  131. # 4941), to make connections more stable. Enable this option to reverse
  132. # this.
  133. # prefer_temporary_addrs = no
  134. # Process RTM_NEWROUTE and RTM_DELROUTE events.
  135. # process_route = yes
  136. # Delay in ms for receiving packets, to simulate larger RTT.
  137. # receive_delay = 0
  138. # Delay request messages.
  139. # receive_delay_request = yes
  140. # Delay response messages.
  141. # receive_delay_response = yes
  142. # Specific IKEv2 message type to delay, 0 for any.
  143. # receive_delay_type = 0
  144. # Size of the AH/ESP replay window, in packets.
  145. # replay_window = 32
  146. # Base to use for calculating exponential back off, see IKEv2 RETRANSMISSION
  147. # in strongswan.conf(5).
  148. # retransmit_base = 1.8
  149. # Maximum jitter in percent to apply randomly to calculated retransmission
  150. # timeout (0 to disable).
  151. # retransmit_jitter = 0
  152. # Upper limit in seconds for calculated retransmission timeout (0 to
  153. # disable).
  154. # retransmit_limit = 0
  155. # Timeout in seconds before sending first retransmit.
  156. # retransmit_timeout = 4.0
  157. # Number of times to retransmit a packet before giving up.
  158. # retransmit_tries = 5
  159. # Interval in seconds to use when retrying to initiate an IKE_SA (e.g. if
  160. # DNS resolution failed), 0 to disable retries.
  161. # retry_initiate_interval = 0
  162. # Initiate CHILD_SA within existing IKE_SAs (always enabled for IKEv1).
  163. # reuse_ikesa = yes
  164. # Numerical routing table to install routes to.
  165. # routing_table =
  166. # Priority of the routing table.
  167. # routing_table_prio =
  168. # Whether to use RSA with PSS padding instead of PKCS#1 padding by default.
  169. # rsa_pss = no
  170. # Delay in ms for sending packets, to simulate larger RTT.
  171. # send_delay = 0
  172. # Delay request messages.
  173. # send_delay_request = yes
  174. # Delay response messages.
  175. # send_delay_response = yes
  176. # Specific IKEv2 message type to delay, 0 for any.
  177. # send_delay_type = 0
  178. # Send strongSwan vendor ID payload
  179. # send_vendor_id = no
  180. # Whether to enable Signature Authentication as per RFC 7427.
  181. # signature_authentication = yes
  182. # Whether to enable constraints against IKEv2 signature schemes.
  183. # signature_authentication_constraints = yes
  184. # The upper limit for SPIs requested from the kernel for IPsec SAs.
  185. # spi_max = 0xcfffffff
  186. # The lower limit for SPIs requested from the kernel for IPsec SAs.
  187. # spi_min = 0xc0000000
  188. # Number of worker threads in charon.
  189. # threads = 16
  190. # Name of the user the daemon changes to after startup.
  191. # user =
  192. crypto_test {
  193. # Benchmark crypto algorithms and order them by efficiency.
  194. # bench = no
  195. # Buffer size used for crypto benchmark.
  196. # bench_size = 1024
  197. # Time in ms during which crypto algorithm performance is measured.
  198. # bench_time = 50
  199. # Test crypto algorithms during registration (requires test vectors
  200. # provided by the test-vectors plugin).
  201. # on_add = no
  202. # Test crypto algorithms on each crypto primitive instantiation.
  203. # on_create = no
  204. # Strictly require at least one test vector to enable an algorithm.
  205. # required = no
  206. # Whether to test RNG with TRUE quality; requires a lot of entropy.
  207. # rng_true = no
  208. }
  209. host_resolver {
  210. # Maximum number of concurrent resolver threads (they are terminated if
  211. # unused).
  212. # max_threads = 3
  213. # Minimum number of resolver threads to keep around.
  214. # min_threads = 0
  215. }
  216. leak_detective {
  217. # Includes source file names and line numbers in leak detective output.
  218. # detailed = yes
  219. # Threshold in bytes for leaks to be reported (0 to report all).
  220. # usage_threshold = 10240
  221. # Threshold in number of allocations for leaks to be reported (0 to
  222. # report all).
  223. # usage_threshold_count = 0
  224. }
  225. processor {
  226. # Section to configure the number of reserved threads per priority class
  227. # see JOB PRIORITY MANAGEMENT in strongswan.conf(5).
  228. priority_threads {
  229. }
  230. }
  231. # Section containing a list of scripts (name = path) that are executed when
  232. # the daemon is started.
  233. start-scripts {
  234. }
  235. # Section containing a list of scripts (name = path) that are executed when
  236. # the daemon is terminated.
  237. stop-scripts {
  238. }
  239. tls {
  240. # List of TLS encryption ciphers.
  241. # cipher =
  242. # List of TLS key exchange methods.
  243. # key_exchange =
  244. # List of TLS MAC algorithms.
  245. # mac =
  246. # List of TLS cipher suites.
  247. # suites =
  248. }
  249. x509 {
  250. # Discard certificates with unsupported or unknown critical extensions.
  251. # enforce_critical = yes
  252. }
  253. }

3、配置用户名与密码

编辑配置文件:

  1. 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. # 格式 [ <id selectors> ] : PSK <secret>
  7. %any %any : PSK "abcdef123456"
  8. # EAP 方式, 格式同 psk 相同
  9. UserName1 %any : EAP "UserPassword1"
  10. UserName2 %any : EAP "UserPassword2"
  11. # XAUTH 方式, 只适用于 IKEv1
  12. # 格式 [ <servername> ] <username> : XAUTH "<password>"
  13. UserName1 %any : XAUTH "UserPassword1"
  14. UserName2 %any : XAUTH "UserPassword2"

4、开启内核转发

编辑系统配置文件:

  1. vim /etc/sysctl.conf

在末尾处,添加如下配置项:

  1. net.ipv4.ip_forward = 1
  2. net.ipv6.conf.all.forwarding = 1

或者,执行下面命令添加到系统配置文件。

  1. echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
  2. echo "net.ipv6.conf.all.forwarding = 1" >> /etc/sysctl.conf

最后重新加载系统参数,使上面的配置生效,执行命令:

  1. sysctl -p

五、配置防火墙


配置 CentOS 7 系统默认防火墙 FirewallD。

注意,以下命令没有指定--zone=public参数,都是针对默认区域public

1、为区域添加服务
  1. firewall-cmd --permanent --add-service="ipsec"
2、允许 AH 和 ESP 身份验证协议和加密协议通过防火墙
  1. # ESP (the encrypted data packets)
  2. firewall-cmd --permanent --add-rich-rule='rule protocol value="esp" accept'
  3. # AH (authenticated headers)
  4. firewall-cmd --permanent --add-rich-rule='rule protocol value="ah" accept'
3、开放 500 和 4500 端口
  1. # IKE (security associations)
  2. firewall-cmd --permanent --add-port=500/udp
  3. # IKE NAT Traversal (IPsec between natted devices)
  4. firewall-cmd --permanent --add-port=4500/udp
4、启用 IP 伪装
  1. firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.1.0.0/16" masquerade'
5、添加 nat 转发
  1. firewall-cmd --permanen --add-rich-rule='rule family="ipv4" source address="10.1.0.0/16" forward-port port="4500" protocol="udp" to-port="4500"'
  2. firewall-cmd --permanen --add-rich-rule='rule family="ipv4" source address="10.1.0.0/16" forward-port port="500" protocol="udp" to-port="500"'
6、重新加载防火墙配置
  1. firewall-cmd --reload
7、显示所有公共区域(public)
  1. firewall-cmd --list-all

查看上面命令执行结果。

六、strongSwan 服务操作


1、使用 strongswan 自身的命令
  1. # 停止服务
  2. strongswan stop
  3. # 查看是否连接了客户端
  4. strongswan status
  5. # 查看命令帮助
  6. strongswan --help
2、使用 systemctl 命令
  1. # 设置开机启动 strongswan 服务
  2. systemctl enable strongswan
  3. # 启动服务
  4. systemctl start strongswan
  5. # 停止服务
  6. systemctl stop strongswan
  7. # 重启服务
  8. systemctl restart strongswan
  9. # 查看服务状态
  10. systemctl status strongswan

注意,如果使用strongswan restart命令重启 strongSwan 后,再用systemctl status strongswan命令得不到正确的运行状态。

至此,服务端配置已完成。

七、客户端配置


IOS 系统

先导入 CA 证书,将之前创建的ca.cert.pem用 FTP 导出,写邮件以附件的方式发到邮箱, 在 IOS 浏览器登录邮箱,下载附件,安装 CA 证书。

1、使用 IKEv2 + EAP 认证

找到手机上“设置->VPN->添加配置”,选 IKEv2 。

  • 描述:随便填
  • 服务器:填 URL 或 IP
  • 远程 ID:ipsec.conf 中的 leftid
  • 用户鉴定:用户名
  • 用户名:EAP 项用户名
  • 密码:EAP 项密码
2、使用 IKEv2 + 客户端证书 认证

把之前的 .p12 证书(里面包含 CA 证书)发到邮箱在手机上打开。导入到手机(此时需要之前设置的证书密码)。

找到手机上“设置->VPN->添加配置’,选 IKEv2 。

  • 描述:随便填
  • 服务器:填 URL 或 IP
  • 远程ID:ipsec.conf 中的 leftid
  • 用户鉴定:证书
  • 证书:选择安装完的客户端证书
3、使用 IKEv2 + 预设密钥 认证

找到手机上“设置->VPN->添加配置”,选 IKEv2 。

  • 描述:随便填
  • 服务器:填 URL 或 IP
  • 远程ID:ipsec.conf 中的 leftid
  • 用户鉴定:无
  • 使用证书:关
  • 密钥:PSK 项密钥

Windows 10

导入证书:

  • 将 CA 根证书 ca.cert.pem 重命名为 ca.cert.crt
  • 双击 ca.cert.crt 开始安装证书;
  • 点击安装证书;
  • “存储位置”选择“本地计算机”,下一步;
  • 选择“将所有的证书都放入下列存储区”,点浏览,选择“受信任的根证书颁发机构”,确定,下一步,完成;

建立连接:

  • “控制面板”-“网络和共享中心”-“设置新的连接或网络”-“连接到工作区”-“使用我的 Internet 连接”;
  • Internet 地址写服务器 IP 或 URL;
  • 描述随便写;
  • 用户名密码写之前配置的 EAP 的那个;
  • 确定;
  • 转到 控制面板网络和 Internet 网络连接;
  • 在新建的 VPN 连接上右键属性然后切换到“安全”选项卡;
  • VPN 类型选 IKEv2 ;
  • 数据加密选“需要加密”;
  • 身份认证这里需要说一下,如果想要使用 EAP 认证的话就选择“Microsoft : 安全密码(EAP-MSCHAP v2)”;想要使用私人证书认证的话就选择“使用计算机证书”;
  • 再切换到“网络”选项卡,双击“Internet 协议版本 4”以打开属性窗口,这里说一下,如果你使用的是老版本的 Win10,可能会打不开属性窗口,这是已知的 Bug,升级最新版本即可解决;
  • 点击“高级”按钮,勾选“在远程网络上使用默认网关”,确定退出;

Windows 7 导入证书略有不同

  • 开始菜单搜索“cmd”,打开后输入 MMC(Microsoft 管理控制台);
  • “文件”-“添加/删除管理单元”,添加“证书”单元;
  • 证书单元的弹出窗口中一定要选“计算机账户”,之后选“本地计算机”,确定;
  • 在左边的“控制台根节点”下选择“证书”-“受信任的根证书颁发机构”-“证书”,右键“所有任务”-“导入”打开证书导入窗口;
  • 选择 CA 证书 ca.cert.crt 导入即可;

注意,千万不要双击 .p12 证书导入!因为那样会导入到当前用户而不是本机计算机中。

八、可能遇到的问题

  1. 关于 Windows 10 系统中 VPN 能正常连接,但不能打开网页的情况。

    这与“VPN 连接”属性中的“接口跃点数”设置有关。该设置用于设置网络接口的优先级,使用 cmd 执行命令route print查看路由表,知道其他接口的跃点数后,我们只要将“VPN 连接”的“接口跃点数”设置低于它们就可以了。设置好后,网络请求会优先使用“VPN 连接”。

    接口跃点数

    图为:接口跃点数的设置。

    route print

    图为:route print命令执行结果。

    图中“接口列表”第一列,用红色圈出的部分为跃点数,将“VPN 连接”的“接口跃点数”设置为 10 即可。

九、参考文献


(完)