1、在azure上创建ubuntu虚拟机 选择v15.04 server 版本

2、添加端口号

3、远程桌面到ubuntu

命令行 输入 sudo su  输入创建 ubuntu虚拟机 时候的 密码

切换到root身份。

4、开始创建 ipsec/ikev2 vpn 服务器

【注】 我选择的是  Xen、KVM 的vps类型

【注】修改后 文件 不生效 则重新启动虚拟机

【注】win7客户端连接vpn 需要证书安装到  计算机账户的 受信任的根证书颁发机构

具体如下:
1.下载脚本:

wget https://raw.githubusercontent.com/quericy/one-key-ikev2-vpn/master/one-key-ikev2.sh

2.运行(如果有需要使用自己已有的根证书,请将私钥命名为ca.cert,将根证书命名为ca.cert.pem,放到脚本的相同目录下再运行该脚本,没有证书的话将自动生成自签名证书咯):

chmod +x one-key-ikev2.sh

bash one-key-ikev2.sh

3.等待自动配置部分内容后,选择vps类型(OpenVZ还是Xen、KVM),选错将无法成功连接,请务必核实服务器的类型。输入服务器ip或者绑定的域名(连接vpn时服务器地址将需要与此保持一致),以及证书的相关信息(C,O,CN),使用自己的根证书的话,C,O,CN的值需要与根证书一致,为空将使用默认值(default value),确认无误后按任意键继续

4.输入两次pkcs12证书的密码(可以为空)

5.看到install success字样即表示安装成功。默认用户名密码将以黄字显示,可根据提示自行修改文件中的用户名密码。(WindowsPhone8.1的用户请将用户名myUserNames修改为%any ,否则可能会由于域的问题无法连接,具体参见这篇文章中的说明)

6.将提示信息中的证书文件ca.cert.pem拷贝到客户端,修改后缀名为.cer后导入。ios设备使用Ikev1无需导入证书,而是需要在连接时输入共享密钥,共享密钥即是提示信息中的黄字PSK.

服务器重启后默认ipsec不会自启动,请自行添加,或使用命令手动开启:

 
ipsec start

连上服务器后无法链接外网:

 
vim /etc/sysctl.conf

修改net.ipv4.ip_forward=1后保存并关闭文件 然后使用以下指令刷新sysctl:

sysctl -p

如遇报错信息,请重新打开/etc/syctl并将报错的那些代码用#号注释,保存后再刷新sysctl直至不会报错为止。

bash脚本源码(点击展开)

  1. #! /bin/bash
  2. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  3. export PATH
  4. #===============================================================================================
  5. # System Required: CentOS6.x (32bit/64bit) or Ubuntu
  6. # Description: Install IKEV2 VPN for CentOS and Ubuntu
  7. # Author: quericy
  8. # Intro: http://quericy.me/blog/699
  9. #===============================================================================================
  10.  
  11. clear
  12. echo "#############################################################"
  13. echo "# Install IKEV2 VPN for CentOS6.x (32bit/64bit) or Ubuntu"
  14. echo "# Intro: http://quericy.me/blog/699"
  15. echo "#"
  16. echo "# Author:quericy"
  17. echo "#"
  18. echo "#############################################################"
  19. echo ""
  20.  
  21. # Install IKEV2
  22. function install_ikev2(){
  23. rootness
  24. disable_selinux
  25. get_my_ip
  26. get_system
  27. yum_install
  28. pre_install
  29. download_files
  30. setup_strongswan
  31. get_key
  32. configure_ipsec
  33. configure_strongswan
  34. configure_secrets
  35. iptables_set
  36. ipsec start
  37. success_info
  38. }
  39.  
  40. # Make sure only root can run our script
  41. function rootness(){
  42. if [[ $EUID -ne 0 ]]; then
  43. echo "Error:This script must be run as root!" 1>&2
  44. exit 1
  45. fi
  46. }
  47.  
  48. # Disable selinux
  49. function disable_selinux(){
  50. if [ -s /etc/selinux/config ] && grep 'SELINUX=enforcing' /etc/selinux/config; then
  51. sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
  52. setenforce 0
  53. fi
  54. }
  55.  
  56. # Get IP address of the server
  57. function get_my_ip(){
  58. echo "Preparing, Please wait a moment..."
  59. IP=`curl -s checkip.dyndns.com | cut -d' ' -f 6 | cut -d'<' -f 1`
  60. if [ -z $IP ]; then
  61. IP=`curl -s ifconfig.me/ip`
  62. fi
  63. }
  64.  
  65. # Ubuntu or CentOS
  66. function get_system(){
  67. get_system_str=`cat /etc/issue`
  68. echo "$get_system_str" |grep -q "CentOS"
  69. if [ $? -eq 0 ]
  70. then
  71. system_str="0"
  72. else
  73. echo "$get_system_str" |grep -q "Ubuntu"
  74. if [ $? -eq 0 ]
  75. then
  76. system_str="1"
  77. else
  78. echo "This Script must be running at the CentOS or Ubuntu!"
  79. exit 1
  80. fi
  81. fi
  82.  
  83. }
  84.  
  85. # Pre-installation settings
  86. function pre_install(){
  87. echo "#############################################################"
  88. echo "# Install IKEV2 VPN for CentOS6.x (32bit/64bit) or Ubuntu"
  89. echo "# Intro: http://quericy.me/blog/699"
  90. echo "#"
  91. echo "# Author:quericy"
  92. echo "#"
  93. echo "#############################################################"
  94. echo ""
  95. echo "please choose the type of your VPS(Xen、KVM: 1 , OpenVZ: 2):"
  96. read -p "your choice(1 or 2):" os_choice
  97. if [ "$os_choice" = "1" ]; then
  98. os="1"
  99. os_str="Xen、KVM"
  100. else
  101. if [ "$os_choice" = "2" ]; then
  102. os="2"
  103. os_str="OpenVZ"
  104. else
  105. echo "wrong choice!"
  106. exit 1
  107. fi
  108. fi
  109. echo "please input the ip (or domain) of your VPS:"
  110. read -p "ip or domain(default_vale:${IP}):" vps_ip
  111. if [ "$vps_ip" = "" ]; then
  112. vps_ip=$IP
  113. fi
  114. echo "please input the cert country(C):"
  115. read -p "C(default value:com):" my_cert_c
  116. if [ "$my_cert_c" = "" ]; then
  117. my_cert_c="com"
  118. fi
  119. echo "please input the cert organization(O):"
  120. read -p "O(default value:myvpn):" my_cert_o
  121. if [ "$my_cert_o" = "" ]; then
  122. my_cert_o="myvpn"
  123. fi
  124. echo "please input the cert common name(CN):"
  125. read -p "CN(default value:VPN CA):" my_cert_cn
  126. if [ "$my_cert_cn" = "" ]; then
  127. my_cert_cn="VPN CA"
  128. fi
  129. echo "####################################"
  130. get_char(){
  131. SAVEDSTTY=`stty -g`
  132. stty -echo
  133. stty cbreak
  134. dd if=/dev/tty bs=1 count=1 2> /dev/null
  135. stty -raw
  136. stty echo
  137. stty $SAVEDSTTY
  138. }
  139. echo "Please confirm the information:"
  140. echo ""
  141. echo -e "the type of your server: [\033[32;1m$os_str\033[0m]"
  142. echo -e "the ip(or domain) of your server: [\033[32;1m$vps_ip\033[0m]"
  143. echo -e "the cert_info:[\033[32;1mC=${my_cert_c}, O=${my_cert_o}\033[0m]"
  144. echo ""
  145. echo "Press any key to start...or Press Ctrl+C to cancel"
  146. char=`get_char`
  147. #Current folder
  148. cur_dir=`pwd`
  149. cd $cur_dir
  150. }
  151.  
  152. #install necessary lib
  153. function yum_install(){
  154. if [ "$system_str" = "0" ]; then
  155. yum -y update
  156. yum -y install pam-devel openssl-devel make gcc
  157. else
  158. apt-get -y update
  159. apt-get -y install libpam0g-dev libssl-dev make gcc
  160. fi
  161. }
  162.  
  163. # Download strongswan
  164. function download_files(){
  165. if [ -f strongswan.tar.gz ];then
  166. echo -e "strongswan.tar.gz [\033[32;1mfound\033[0m]"
  167. else
  168. if ! wget http://download.strongswan.org/strongswan.tar.gz;then
  169. echo "Failed to download strongswan.tar.gz"
  170. exit 1
  171. fi
  172. fi
  173. tar xzf strongswan.tar.gz
  174. if [ $? -eq 0 ];then
  175. cd $cur_dir/strongswan-*/
  176. else
  177. echo ""
  178. echo "Unzip strongswan.tar.gz failed! Please visit http://quericy.me/blog/699 and contact."
  179. exit 1
  180. fi
  181. }
  182.  
  183. # configure and install strongswan
  184. function setup_strongswan(){
  185. if [ "$os" = "1" ]; then
  186. ./configure --enable-eap-identity --enable-eap-md5 \
  187. --enable-eap-mschapv2 --enable-eap-tls --enable-eap-ttls --enable-eap-peap \
  188. --enable-eap-tnc --enable-eap-dynamic --enable-eap-radius --enable-xauth-eap \
  189. --enable-xauth-pam --enable-dhcp --enable-openssl --enable-addrblock --enable-unity \
  190. --enable-certexpire --enable-radattr --enable-tools --enable-openssl --disable-gmp
  191.  
  192. else
  193. ./configure --enable-eap-identity --enable-eap-md5 \
  194. --enable-eap-mschapv2 --enable-eap-tls --enable-eap-ttls --enable-eap-peap \
  195. --enable-eap-tnc --enable-eap-dynamic --enable-eap-radius --enable-xauth-eap \
  196. --enable-xauth-pam --enable-dhcp --enable-openssl --enable-addrblock --enable-unity \
  197. --enable-certexpire --enable-radattr --enable-tools --enable-openssl --disable-gmp --enable-kernel-libipsec
  198.  
  199. fi
  200. make; make install
  201. }
  202.  
  203. # configure cert and key
  204. function get_key(){
  205. cd $cur_dir
  206. if [ -f ca.pem ];then
  207. echo -e "ca.pem [\033[32;1mfound\033[0m]"
  208. else
  209. echo -e "ca.pem [\033[32;1mauto create\032[0m]"
  210. echo "auto create ca.pem ..."
  211. ipsec pki --gen --outform pem > ca.pem
  212. fi
  213.  
  214. if [ -f ca.cert.pem ];then
  215. echo -e "ca.cert.pem [\033[32;1mfound\033[0m]"
  216. else
  217. echo -e "ca.cert.pem [\032[33;1mauto create\032[0m]"
  218. echo "auto create ca.cert.pem ..."
  219. ipsec pki --self --in ca.pem --dn "C=${my_cert_c}, O=${my_cert_o}, CN=${my_cert_cn}" --ca --outform pem >ca.cert.pem
  220. fi
  221. if [ ! -d my_key ];then
  222. mkdir my_key
  223. fi
  224. mv ca.pem my_key/ca.pem
  225. mv ca.cert.pem my_key/ca.cert.pem
  226. cd my_key
  227. ipsec pki --gen --outform pem > server.pem
  228. ipsec pki --pub --in server.pem | ipsec pki --issue --cacert ca.cert.pem \
  229. --cakey ca.pem --dn "C=${my_cert_c}, O=${my_cert_o}, CN=${vps_ip}" \
  230. --san="${vps_ip}" --flag serverAuth --flag ikeIntermediate \
  231. --outform pem > server.cert.pem
  232. ipsec pki --gen --outform pem > client.pem
  233. ipsec pki --pub --in client.pem | ipsec pki --issue --cacert ca.cert.pem --cakey ca.pem --dn "C=${my_cert_c}, O=${my_cert_o}, CN=VPN Client" --outform pem > client.cert.pem
  234. echo "configure the pkcs12 cert password(Can be empty):"
  235. openssl pkcs12 -export -inkey client.pem -in client.cert.pem -name "client" -certfile ca.cert.pem -caname "${my_cert_cn}" -out client.cert.p12
  236. echo "####################################"
  237. get_char(){
  238. SAVEDSTTY=`stty -g`
  239. stty -echo
  240. stty cbreak
  241. dd if=/dev/tty bs=1 count=1 2> /dev/null
  242. stty -raw
  243. stty echo
  244. stty $SAVEDSTTY
  245. }
  246. echo "Press any key to install ikev2 VPN cert"
  247. cp -r ca.cert.pem /usr/local/etc/ipsec.d/cacerts/
  248. cp -r server.cert.pem /usr/local/etc/ipsec.d/certs/
  249. cp -r server.pem /usr/local/etc/ipsec.d/private/
  250. cp -r client.cert.pem /usr/local/etc/ipsec.d/certs/
  251. cp -r client.pem /usr/local/etc/ipsec.d/private/
  252.  
  253. }
  254.  
  255. # configure the ipsec.conf
  256. function configure_ipsec(){
  257. cat > /usr/local/etc/ipsec.conf<<-EOF
  258. config setup
  259. uniqueids=never
  260.  
  261. conn iOS_cert
  262. keyexchange=ikev1
  263. fragmentation=yes
  264. left=%defaultroute
  265. leftauth=pubkey
  266. leftsubnet=0.0.0.0/0
  267. leftcert=server.cert.pem
  268. right=%any
  269. rightauth=pubkey
  270. rightauth2=xauth
  271. rightsourceip=10.31.2.0/24
  272. rightcert=client.cert.pem
  273. auto=add
  274.  
  275. conn android_xauth_psk
  276. keyexchange=ikev1
  277. left=%defaultroute
  278. leftauth=psk
  279. leftsubnet=0.0.0.0/0
  280. right=%any
  281. rightauth=psk
  282. rightauth2=xauth
  283. rightsourceip=10.31.2.0/24
  284. auto=add
  285.  
  286. conn networkmanager-strongswan
  287. keyexchange=ikev2
  288. left=%defaultroute
  289. leftauth=pubkey
  290. leftsubnet=0.0.0.0/0
  291. leftcert=server.cert.pem
  292. right=%any
  293. rightauth=pubkey
  294. rightsourceip=10.31.2.0/24
  295. rightcert=client.cert.pem
  296. auto=add
  297.  
  298. conn windows7
  299. keyexchange=ikev2
  300. ike=aes256-sha1-modp1024!
  301. rekey=no
  302. left=%defaultroute
  303. leftauth=pubkey
  304. leftsubnet=0.0.0.0/0
  305. leftcert=server.cert.pem
  306. right=%any
  307. rightauth=eap-mschapv2
  308. rightsourceip=10.31.2.0/24
  309. rightsendcert=never
  310. eap_identity=%any
  311. auto=add
  312.  
  313. EOF
  314. }
  315.  
  316. # configure the strongswan.conf
  317. function configure_strongswan(){
  318. cat > /usr/local/etc/strongswan.conf<<-EOF
  319. charon {
  320. load_modular = yes
  321. duplicheck.enable = no
  322. compress = yes
  323. plugins {
  324. include strongswan.d/charon/*.conf
  325. }
  326. dns1 = 8.8.8.8
  327. dns2 = 8.8.4.4
  328. nbns1 = 8.8.8.8
  329. nbns2 = 8.8.4.4
  330. }
  331. include strongswan.d/*.conf
  332. EOF
  333. }
  334.  
  335. # configure the ipsec.secrets
  336. function configure_secrets(){
  337. cat > /usr/local/etc/ipsec.secrets<<-EOF
  338. : RSA server.pem
  339. : PSK "myPSKkey"
  340. : XAUTH "myXAUTHPass"
  341. myUserName %any : EAP "myUserPass"
  342. EOF
  343. }
  344.  
  345. # iptables set
  346. function iptables_set(){
  347. sysctl -w net.ipv4.ip_forward=1
  348. if [ "$os" = "1" ]; then
  349. iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  350. iptables -A FORWARD -s 10.31.0.0/24 -j ACCEPT
  351. iptables -A FORWARD -s 10.31.1.0/24 -j ACCEPT
  352. iptables -A FORWARD -s 10.31.2.0/24 -j ACCEPT
  353. iptables -A INPUT -i eth0 -p esp -j ACCEPT
  354. iptables -A INPUT -i eth0 -p udp --dport 500 -j ACCEPT
  355. iptables -A INPUT -i eth0 -p tcp --dport 500 -j ACCEPT
  356. iptables -A INPUT -i eth0 -p udp --dport 4500 -j ACCEPT
  357. iptables -A INPUT -i eth0 -p udp --dport 1701 -j ACCEPT
  358. iptables -A INPUT -i eth0 -p tcp --dport 1723 -j ACCEPT
  359. iptables -A FORWARD -j REJECT
  360. iptables -t nat -A POSTROUTING -s 10.31.0.0/24 -o eth0 -j MASQUERADE
  361. iptables -t nat -A POSTROUTING -s 10.31.1.0/24 -o eth0 -j MASQUERADE
  362. iptables -t nat -A POSTROUTING -s 10.31.2.0/24 -o eth0 -j MASQUERADE
  363. else
  364. iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  365. iptables -A FORWARD -s 10.31.0.0/24 -j ACCEPT
  366. iptables -A FORWARD -s 10.31.1.0/24 -j ACCEPT
  367. iptables -A FORWARD -s 10.31.2.0/24 -j ACCEPT
  368. iptables -A INPUT -i venet0 -p esp -j ACCEPT
  369. iptables -A INPUT -i venet0 -p udp --dport 500 -j ACCEPT
  370. iptables -A INPUT -i venet0 -p tcp --dport 500 -j ACCEPT
  371. iptables -A INPUT -i venet0 -p udp --dport 4500 -j ACCEPT
  372. iptables -A INPUT -i venet0 -p udp --dport 1701 -j ACCEPT
  373. iptables -A INPUT -i venet0 -p tcp --dport 1723 -j ACCEPT
  374. iptables -A FORWARD -j REJECT
  375. iptables -t nat -A POSTROUTING -s 10.31.0.0/24 -o venet0 -j MASQUERADE
  376. iptables -t nat -A POSTROUTING -s 10.31.1.0/24 -o venet0 -j MASQUERADE
  377. iptables -t nat -A POSTROUTING -s 10.31.2.0/24 -o venet0 -j MASQUERADE
  378. fi
  379. if [ "$system_str" = "0" ]; then
  380. service iptables save
  381. else
  382. iptables-save > /etc/iptables.rules
  383. cat > /etc/network/if-up.d/iptables<<EOF
  384. #!/bin/sh
  385. iptables-restore < /etc/iptables.rules
  386. EOF
  387. chmod +x /etc/network/if-up.d/iptables
  388. fi
  389. }
  390.  
  391. # echo the success info
  392. function success_info(){
  393. echo "#############################################################"
  394. echo -e "#"
  395. echo -e "# [\033[32;1mInstall Successful\033[0m]"
  396. echo -e "# There is the default login info of your VPN"
  397. echo -e "# UserName:\033[33;1m myUserName\033[0m"
  398. echo -e "# PassWord:\033[33;1m myUserPass\033[0m"
  399. echo -e "# PSK:\033[33;1m myPSKkey\033[0m"
  400. echo -e "# you can change UserName and PassWord in\033[32;1m /usr/local/etc/ipsec.secrets\033[0m"
  401. echo -e "# you must copy the cert \033[32;1m ${cur_dir}/my_key/ca.cert.pem \033[0m to the client and install it."
  402. echo -e "#"
  403. echo -e "#############################################################"
  404. echo -e ""
  405. }
  406.  
  407. # Initialization step
  408. install_ikev2
  1.  
  1.  

如需Debian系统的IKEV2一键安装脚本,可参考magic282童鞋的一键脚本:
https://github.com/magic282/One-Key-L2TP-IKEV2-Setup

转载自quericy Eden*博客

参考

UBUNTU、CENTOS搭建IPSEC/IKEV2 VPN服务器全攻略

CENTOS/UBUNTU一键安装IPSEC/IKEV2 VPN服务器的更多相关文章

  1. CentOS Linux VPS安装IPSec+L2TP VPN

    CentOS Linux VPS安装IPSec+L2TP VPN 时间 -- :: 天使羊波波闪耀光芒 相似文章 () 原文 http://www.live-in.org/archives/818.h ...

  2. 转:CentOS/Debian/Ubuntu一键安装LAMP(Apache/MySQL/PHP)环境

    CentOS/Debian/Ubuntu一键安装LAMP(Apache/MySQL/PHP) 今天遇到一个网友提到需要在Linux VPS服务器中安装LAMP(Apache/MySQL/PHP)网站环 ...

  3. CentOS下一键安装Openstack

    CentOS下一键安装Openstack 系统环境:Oracle VirtualBox 4.38CentOS-6.5-x86_64-bin-DVD1.iso 安装前需要修改 /etc/hosts文件, ...

  4. 使用Cobbler批量部署Linux和Windows:CentOS/Ubuntu批量安装(二)

    通过前面服务端的部署,已经配置好了 Cobbler Server 端,接下来开始进行 CentOS/Ubuntu 的批量安装,在进行 CentOS/Ubuntu 批量安装时,也需要通过Cobbler来 ...

  5. 在阿里云服务器上(centos 8) 安装自己的MQTT服务器 (mosquitto)

    layout: post title: 在阿里云服务器上(centos 8) 安装自己的MQTT服务器 (mosquitto) subtitle: date: 2020-3-2 author: Dap ...

  6. (转)CentOS下一键安装GitLab

    [环境准备]OS: CentOS 6.3 x86_64 [安装要求]如果有条件,提供一台全新的Server(仅仅只安装了一些系统的软件包),可以直接使用一键安装脚本(gitlab-install-el ...

  7. centos 7 一键安装gitlab

    # cat /etc/redhat-release CentOS release 6.5 (Final) # strings /lib64/libc.so.6 |grep GLIBC_ 首先升级 如果 ...

  8. 如何在CentOS 7上安装Memcached(缓存服务器)

    首先更新本地软件包索引,然后使用以下yum命令从官方CentOS存储库安装Memcached. yum update yum install memcached 接下来,我们将安装libmemcach ...

  9. CentOS全自动一键安装PHP,MySQL,phpmyadmin与Nginx

    运行install_nginx.sh即可 1,需要修改install_nginx.sh中的相应路径: #存放源代码和本脚本的目录 compile_dir=/root/nginx_compile,需要修 ...

随机推荐

  1. jenkins+gerrit

    Verified 功能 http://www.cnblogs.com/zhanchenjin/p/5032218.html

  2. em

    macro jumptocaller(){    JumpToLocation(GetSymbolLocation((GetCurSymbol ())))}

  3. 阿里云OneinStack,Linux下tomcat命令

    阿里云OneinStack,Linux下tomcat命令 Linux下如何查看tomcat是否启动在Linux系统下,重启Tomcat使用命令操作的首先,进入Tomcat下的bin目录cd /usr/ ...

  4. linux内核常识

    找到了一个阅读linux源码的在线工具woboq,但是不能很好地用来阅读live555代码. 比如,我们要阅读epoll实现,可以访问此网址:https://code.woboq.org/linux/ ...

  5. python学习之安装模块

    安装pip下载python模块 yum -y install python-pip pip install pandas (pandas要安装的模块名) 默认使用的官方python源,这个在国内访问很 ...

  6. hdu 3966 Aragorn's Story 树链剖分 按点

    Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. chrome 开发者工具详解

    Google Chrome一共提供了8大组工具: Elements: 允许我们从浏览器的角度看页面,也就是说我们可以看到chrome渲染页面所需要的的HTML.CSS和DOM(Document Obj ...

  8. 在FireFox中安装Selenium IDE

    第二步:点击查看更多,查找Selenium IDE,安装 第三步:安装好后,在顶部的工具栏里点击"工具",弹出的选项框里出现Selenium IDE,安装完毕.

  9. [luogu2982][USACO10FEB]慢下来Slowing down(树状数组+dfs序)

    题目描述 Every day each of Farmer John's N (1 <= N <= 100,000) cows conveniently numbered 1..N mov ...

  10. 【前端开发系列】—— 别说你不会Ajax

    之前一直都是用封装好的Ajax,所以一直很好奇它是如何使用和实现的.这里正好就进行一下学习,下面是Ajax的一个时间图. 设置触发条件 这里模拟一个使用场景,就是在用户登陆时,异步的对用户名以及密码进 ...