1、安装必要的工具

  1. yum install vim net-tools wget unzip -y

2. 下载安装脚本

  1. wget -O StackScript.zip http://files.cnblogs.com/files/think8848/StackScript.zip

3. 解压文件

  1. unzip StackScript.zip

4. 执行安装文件

  1. chmod +x StackScript
  2.  
  3. ./StackScript

StackScript实际上市判断你的系统版本,然后下载安装脚本执行安装,centos安装的脚本如下:

  1. #!/bin/sh
  2. #
  3. # Script for automatic setup of an IPsec VPN server on CentOS/RHEL 6 and 7.
  4. # Works on any dedicated server or virtual private server (VPS) except OpenVZ.
  5. #
  6. # DO NOT RUN THIS SCRIPT ON YOUR PC OR MAC!
  7. #
  8. # The latest version of this script is available at:
  9. # https://github.com/hwdsl2/setup-ipsec-vpn
  10. #
  11. # Copyright (C) 2015-2017 Lin Song <linsongui@gmail.com>
  12. # Based on the work of Thomas Sarlandie (Copyright 2012)
  13. #
  14. # This work is licensed under the Creative Commons Attribution-ShareAlike 3.0
  15. # Unported License: http://creativecommons.org/licenses/by-sa/3.0/
  16. #
  17. # Attribution required: please include my name in any derivative and let me
  18. # know how you have improved it!
  19.  
  20. # =====================================================
  21.  
  22. # Define your own values for these variables
  23. # - IPsec pre-shared key, VPN username and password
  24. # - All values MUST be placed inside 'single quotes'
  25. # - DO NOT use these special characters within values: \ " '
  26.  
  27. YOUR_IPSEC_PSK=''
  28. YOUR_USERNAME=''
  29. YOUR_PASSWORD=''
  30.  
  31. # Important notes: https://git.io/vpnnotes
  32. # Setup VPN clients: https://git.io/vpnclients
  33.  
  34. # =====================================================
  35.  
  36. export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  37. SYS_DT="$(date +%F-%T)"
  38.  
  39. exiterr() { echo "Error: $1" >&2; exit 1; }
  40. exiterr2() { exiterr "'yum install' failed."; }
  41. conf_bk() { /bin/cp -f "$1" "$1.old-$SYS_DT" 2>/dev/null; }
  42. bigecho() { echo; echo "## $1"; echo; }
  43.  
  44. check_ip() {
  45. IP_REGEX='^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$'
  46. printf '%s' "$1" | tr -d '\n' | grep -Eq "$IP_REGEX"
  47. }
  48.  
  49. vpnsetup() {
  50.  
  51. if ! grep -qs -e "release 6" -e "release 7" /etc/redhat-release; then
  52. exiterr "This script only supports CentOS/RHEL 6 and 7."
  53. fi
  54.  
  55. if [ -f /proc/user_beancounters ]; then
  56. exiterr "OpenVZ VPS is not supported. Try OpenVPN: github.com/Nyr/openvpn-install"
  57. fi
  58.  
  59. if [ "$(id -u)" != 0 ]; then
  60. exiterr "Script must be run as root. Try 'sudo sh $0'"
  61. fi
  62.  
  63. net_iface=${VPN_NET_IFACE:-'eth0'}
  64. def_iface="$(route 2>/dev/null | grep '^default' | grep -o '[^ ]*$')"
  65. [ -z "$def_iface" ] && def_iface="$(ip -4 route list 0/0 2>/dev/null | grep -Po '(?<=dev )(\S+)')"
  66.  
  67. def_iface_state=$(cat "/sys/class/net/$def_iface/operstate" 2>/dev/null)
  68. if [ -n "$def_iface_state" ] && [ "$def_iface_state" != "down" ]; then
  69. if ! grep -qs raspbian /etc/os-release; then
  70. case "$def_iface" in
  71. wl*)
  72. exiterr "Wireless interface '$def_iface' detected. DO NOT run this script on your PC or Mac!"
  73. ;;
  74. esac
  75. fi
  76. net_iface="$def_iface"
  77. fi
  78.  
  79. net_iface_state=$(cat "/sys/class/net/$net_iface/operstate" 2>/dev/null)
  80. if [ -z "$net_iface_state" ] || [ "$net_iface_state" = "down" ] || [ "$net_iface" = "lo" ]; then
  81. printf "Error: Network interface '%s' is not available.\n" "$net_iface" >&2
  82. if [ -z "$VPN_NET_IFACE" ]; then
  83. cat 1>&2 <<EOF
  84. Unable to detect the default network interface. Manually re-run this script with:
  85. sudo VPN_NET_IFACE="your_default_interface_name" sh "$0"
  86. EOF
  87. fi
  88. exit 1
  89. fi
  90.  
  91. [ -n "$YOUR_IPSEC_PSK" ] && VPN_IPSEC_PSK="$YOUR_IPSEC_PSK"
  92. [ -n "$YOUR_USERNAME" ] && VPN_USER="$YOUR_USERNAME"
  93. [ -n "$YOUR_PASSWORD" ] && VPN_PASSWORD="$YOUR_PASSWORD"
  94.  
  95. if [ -z "$VPN_IPSEC_PSK" ] && [ -z "$VPN_USER" ] && [ -z "$VPN_PASSWORD" ]; then
  96. bigecho "VPN credentials not set by user. Generating random PSK and password..."
  97. VPN_IPSEC_PSK="$(LC_CTYPE=C tr -dc 'A-HJ-NPR-Za-km-z2-9' < /dev/urandom | head -c 16)"
  98. VPN_USER=vpnuser
  99. VPN_PASSWORD="$(LC_CTYPE=C tr -dc 'A-HJ-NPR-Za-km-z2-9' < /dev/urandom | head -c 16)"
  100. fi
  101.  
  102. if [ -z "$VPN_IPSEC_PSK" ] || [ -z "$VPN_USER" ] || [ -z "$VPN_PASSWORD" ]; then
  103. exiterr "All VPN credentials must be specified. Edit the script and re-enter them."
  104. fi
  105.  
  106. if printf '%s' "$VPN_IPSEC_PSK $VPN_USER $VPN_PASSWORD" | LC_ALL=C grep -q '[^ -~]\+'; then
  107. exiterr "VPN credentials must not contain non-ASCII characters."
  108. fi
  109.  
  110. case "$VPN_IPSEC_PSK $VPN_USER $VPN_PASSWORD" in
  111. *[\\\"\']*)
  112. exiterr "VPN credentials must not contain these special characters: \\ \" '"
  113. ;;
  114. esac
  115.  
  116. bigecho "VPN setup in progress... Please be patient."
  117.  
  118. # Create and change to working dir
  119. mkdir -p /opt/src
  120. cd /opt/src || exiterr "Cannot enter /opt/src."
  121.  
  122. bigecho "Installing packages required for setup..."
  123.  
  124. yum -y install wget bind-utils openssl \
  125. iproute gawk grep sed net-tools || exiterr2
  126.  
  127. bigecho "Trying to auto discover IP of this server..."
  128.  
  129. cat <<'EOF'
  130. In case the script hangs here for more than a few minutes,
  131. press Ctrl-C to abort. Then edit it and manually enter IP.
  132. EOF
  133.  
  134. # In case auto IP discovery fails, enter server's public IP here.
  135. PUBLIC_IP=${VPN_PUBLIC_IP:-''}
  136.  
  137. # Try to auto discover IP of this server
  138. [ -z "$PUBLIC_IP" ] && PUBLIC_IP=$(dig @resolver1.opendns.com -t A -4 myip.opendns.com +short)
  139.  
  140. # Check IP for correct format
  141. check_ip "$PUBLIC_IP" || PUBLIC_IP=$(wget -t 3 -T 15 -qO- http://ipv4.icanhazip.com)
  142. check_ip "$PUBLIC_IP" || exiterr "Cannot detect this server's public IP. Edit the script and manually enter it."
  143.  
  144. bigecho "Adding the EPEL repository..."
  145.  
  146. epel_url="https://dl.fedoraproject.org/pub/epel/epel-release-latest-$(rpm -E '%{rhel}').noarch.rpm"
  147. yum -y install epel-release || yum -y install "$epel_url" || exiterr2
  148.  
  149. bigecho "Installing packages required for the VPN..."
  150.  
  151. yum -y install nss-devel nspr-devel pkgconfig pam-devel \
  152. libcap-ng-devel libselinux-devel curl-devel \
  153. flex bison gcc make ppp xl2tpd || exiterr2
  154.  
  155. OPT1='--enablerepo=*server-optional*'
  156. OPT2='--enablerepo=*releases-optional*'
  157. if grep -qs "release 6" /etc/redhat-release; then
  158. yum -y remove libevent-devel
  159. yum "$OPT1" "$OPT2" -y install libevent2-devel fipscheck-devel || exiterr2
  160. else
  161. yum -y install systemd-devel iptables-services || exiterr2
  162. yum "$OPT1" "$OPT2" -y install libevent-devel fipscheck-devel || exiterr2
  163. fi
  164.  
  165. bigecho "Installing Fail2Ban to protect SSH..."
  166.  
  167. yum -y install fail2ban || exiterr2
  168.  
  169. bigecho "Compiling and installing Libreswan..."
  170.  
  171. SWAN_VER=3.22
  172. swan_file="libreswan-$SWAN_VER.tar.gz"
  173. swan_url1="https://github.com/libreswan/libreswan/archive/v$SWAN_VER.tar.gz"
  174. swan_url2="https://download.libreswan.org/$swan_file"
  175. if ! { wget -t 3 -T 30 -nv -O "$swan_file" "$swan_url1" || wget -t 3 -T 30 -nv -O "$swan_file" "$swan_url2"; }; then
  176. exiterr "Cannot download Libreswan source."
  177. fi
  178. /bin/rm -rf "/opt/src/libreswan-$SWAN_VER"
  179. tar xzf "$swan_file" && /bin/rm -f "$swan_file"
  180. cd "libreswan-$SWAN_VER" || exiterr "Cannot enter Libreswan source dir."
  181. [ "$SWAN_VER" = "3.22" ] && sed -i '/^#define LSWBUF_CANARY/s/-2$/((char) -2)/' include/lswlog.h
  182. cat > Makefile.inc.local <<'EOF'
  183. WERROR_CFLAGS =
  184. USE_DNSSEC = false
  185. EOF
  186. NPROCS="$(grep -c ^processor /proc/cpuinfo)"
  187. [ -z "$NPROCS" ] && NPROCS=1
  188. make "-j$((NPROCS+1))" -s base && make -s install-base
  189.  
  190. # Verify the install and clean up
  191. cd /opt/src || exiterr "Cannot enter /opt/src."
  192. /bin/rm -rf "/opt/src/libreswan-$SWAN_VER"
  193. if ! /usr/local/sbin/ipsec --version 2>/dev/null | grep -qF "$SWAN_VER"; then
  194. exiterr "Libreswan $SWAN_VER failed to build."
  195. fi
  196.  
  197. bigecho "Creating VPN configuration..."
  198.  
  199. L2TP_NET=${VPN_L2TP_NET:-'192.168.42.0/24'}
  200. L2TP_LOCAL=${VPN_L2TP_LOCAL:-'192.168.42.1'}
  201. L2TP_POOL=${VPN_L2TP_POOL:-'192.168.42.10-192.168.42.250'}
  202. XAUTH_NET=${VPN_XAUTH_NET:-'192.168.43.0/24'}
  203. XAUTH_POOL=${VPN_XAUTH_POOL:-'192.168.43.10-192.168.43.250'}
  204. DNS_SRV1=${VPN_DNS_SRV1:-'8.8.8.8'}
  205. DNS_SRV2=${VPN_DNS_SRV2:-'8.8.4.4'}
  206.  
  207. # Create IPsec (Libreswan) config
  208. conf_bk "/etc/ipsec.conf"
  209. cat > /etc/ipsec.conf <<EOF
  210. version 2.0
  211.  
  212. config setup
  213. virtual-private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12,%v4:!$L2TP_NET,%v4:!$XAUTH_NET
  214. protostack=netkey
  215. interfaces=%defaultroute
  216. uniqueids=no
  217.  
  218. conn shared
  219. left=%defaultroute
  220. leftid=$PUBLIC_IP
  221. right=%any
  222. encapsulation=yes
  223. authby=secret
  224. pfs=no
  225. rekey=no
  226. keyingtries=5
  227. dpddelay=30
  228. dpdtimeout=120
  229. dpdaction=clear
  230. ike=3des-sha1,3des-sha2,aes-sha1,aes-sha1;modp1024,aes-sha2,aes-sha2;modp1024,aes256-sha2_512
  231. phase2alg=3des-sha1,3des-sha2,aes-sha1,aes-sha2,aes256-sha2_512
  232. sha2-truncbug=yes
  233.  
  234. conn l2tp-psk
  235. auto=add
  236. leftprotoport=17/1701
  237. rightprotoport=17/%any
  238. type=transport
  239. phase2=esp
  240. also=shared
  241.  
  242. conn xauth-psk
  243. auto=add
  244. leftsubnet=0.0.0.0/0
  245. rightaddresspool=$XAUTH_POOL
  246. modecfgdns1=$DNS_SRV1
  247. modecfgdns2=$DNS_SRV2
  248. leftxauthserver=yes
  249. rightxauthclient=yes
  250. leftmodecfgserver=yes
  251. rightmodecfgclient=yes
  252. modecfgpull=yes
  253. xauthby=file
  254. ike-frag=yes
  255. ikev2=never
  256. cisco-unity=yes
  257. also=shared
  258. EOF
  259.  
  260. # Specify IPsec PSK
  261. conf_bk "/etc/ipsec.secrets"
  262. cat > /etc/ipsec.secrets <<EOF
  263. %any %any : PSK "$VPN_IPSEC_PSK"
  264. EOF
  265.  
  266. # Create xl2tpd config
  267. conf_bk "/etc/xl2tpd/xl2tpd.conf"
  268. cat > /etc/xl2tpd/xl2tpd.conf <<EOF
  269. [global]
  270. port = 1701
  271.  
  272. [lns default]
  273. ip range = $L2TP_POOL
  274. local ip = $L2TP_LOCAL
  275. require chap = yes
  276. refuse pap = yes
  277. require authentication = yes
  278. name = l2tpd
  279. pppoptfile = /etc/ppp/options.xl2tpd
  280. length bit = yes
  281. EOF
  282.  
  283. # Set xl2tpd options
  284. conf_bk "/etc/ppp/options.xl2tpd"
  285. cat > /etc/ppp/options.xl2tpd <<EOF
  286. +mschap-v2
  287. ipcp-accept-local
  288. ipcp-accept-remote
  289. ms-dns $DNS_SRV1
  290. ms-dns $DNS_SRV2
  291. noccp
  292. auth
  293. mtu 1280
  294. mru 1280
  295. proxyarp
  296. lcp-echo-failure 4
  297. lcp-echo-interval 30
  298. connect-delay 5000
  299. EOF
  300.  
  301. # Create VPN credentials
  302. conf_bk "/etc/ppp/chap-secrets"
  303. cat > /etc/ppp/chap-secrets <<EOF
  304. "$VPN_USER" l2tpd "$VPN_PASSWORD" *
  305. EOF
  306.  
  307. conf_bk "/etc/ipsec.d/passwd"
  308. VPN_PASSWORD_ENC=$(openssl passwd -1 "$VPN_PASSWORD")
  309. cat > /etc/ipsec.d/passwd <<EOF
  310. $VPN_USER:$VPN_PASSWORD_ENC:xauth-psk
  311. EOF
  312.  
  313. bigecho "Updating sysctl settings..."
  314.  
  315. if ! grep -qs "hwdsl2 VPN script" /etc/sysctl.conf; then
  316. conf_bk "/etc/sysctl.conf"
  317. if [ "$(getconf LONG_BIT)" = "64" ]; then
  318. SHM_MAX=68719476736
  319. SHM_ALL=4294967296
  320. else
  321. SHM_MAX=4294967295
  322. SHM_ALL=268435456
  323. fi
  324. cat >> /etc/sysctl.conf <<EOF
  325.  
  326. # Added by hwdsl2 VPN script
  327. kernel.msgmnb = 65536
  328. kernel.msgmax = 65536
  329. kernel.shmmax = $SHM_MAX
  330. kernel.shmall = $SHM_ALL
  331.  
  332. net.ipv4.ip_forward = 1
  333. net.ipv4.conf.all.accept_source_route = 0
  334. net.ipv4.conf.all.accept_redirects = 0
  335. net.ipv4.conf.all.send_redirects = 0
  336. net.ipv4.conf.all.rp_filter = 0
  337. net.ipv4.conf.default.accept_source_route = 0
  338. net.ipv4.conf.default.accept_redirects = 0
  339. net.ipv4.conf.default.send_redirects = 0
  340. net.ipv4.conf.default.rp_filter = 0
  341. net.ipv4.conf.$net_iface.send_redirects = 0
  342. net.ipv4.conf.$net_iface.rp_filter = 0
  343.  
  344. net.core.wmem_max = 12582912
  345. net.core.rmem_max = 12582912
  346. net.ipv4.tcp_rmem = 10240 87380 12582912
  347. net.ipv4.tcp_wmem = 10240 87380 12582912
  348. EOF
  349. fi
  350.  
  351. bigecho "Updating IPTables rules..."
  352.  
  353. # Check if IPTables rules need updating
  354. ipt_flag=0
  355. IPT_FILE="/etc/sysconfig/iptables"
  356. if ! grep -qs "hwdsl2 VPN script" "$IPT_FILE" \
  357. || ! iptables -t nat -C POSTROUTING -s "$L2TP_NET" -o "$net_iface" -j MASQUERADE 2>/dev/null \
  358. || ! iptables -t nat -C POSTROUTING -s "$XAUTH_NET" -o "$net_iface" -m policy --dir out --pol none -j MASQUERADE 2>/dev/null; then
  359. ipt_flag=1
  360. fi
  361.  
  362. # Add IPTables rules for VPN
  363. if [ "$ipt_flag" = "1" ]; then
  364. service fail2ban stop >/dev/null 2>&1
  365. iptables-save > "$IPT_FILE.old-$SYS_DT"
  366. iptables -I INPUT 1 -p udp --dport 1701 -m policy --dir in --pol none -j DROP
  367. iptables -I INPUT 2 -m conntrack --ctstate INVALID -j DROP
  368. iptables -I INPUT 3 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
  369. iptables -I INPUT 4 -p udp -m multiport --dports 500,4500 -j ACCEPT
  370. iptables -I INPUT 5 -p udp --dport 1701 -m policy --dir in --pol ipsec -j ACCEPT
  371. iptables -I INPUT 6 -p udp --dport 1701 -j DROP
  372. iptables -I FORWARD 1 -m conntrack --ctstate INVALID -j DROP
  373. iptables -I FORWARD 2 -i "$net_iface" -o ppp+ -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
  374. iptables -I FORWARD 3 -i ppp+ -o "$net_iface" -j ACCEPT
  375. iptables -I FORWARD 4 -i ppp+ -o ppp+ -s "$L2TP_NET" -d "$L2TP_NET" -j ACCEPT
  376. iptables -I FORWARD 5 -i "$net_iface" -d "$XAUTH_NET" -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
  377. iptables -I FORWARD 6 -s "$XAUTH_NET" -o "$net_iface" -j ACCEPT
  378. # Uncomment if you wish to disallow traffic between VPN clients themselves
  379. # iptables -I FORWARD 2 -i ppp+ -o ppp+ -s "$L2TP_NET" -d "$L2TP_NET" -j DROP
  380. # iptables -I FORWARD 3 -s "$XAUTH_NET" -d "$XAUTH_NET" -j DROP
  381. iptables -A FORWARD -j DROP
  382. iptables -t nat -I POSTROUTING -s "$XAUTH_NET" -o "$net_iface" -m policy --dir out --pol none -j MASQUERADE
  383. iptables -t nat -I POSTROUTING -s "$L2TP_NET" -o "$net_iface" -j MASQUERADE
  384. echo "# Modified by hwdsl2 VPN script" > "$IPT_FILE"
  385. iptables-save >> "$IPT_FILE"
  386. fi
  387.  
  388. bigecho "Creating basic Fail2Ban rules..."
  389.  
  390. if [ ! -f /etc/fail2ban/jail.local ] ; then
  391. cat > /etc/fail2ban/jail.local <<'EOF'
  392. [ssh-iptables]
  393. enabled = true
  394. filter = sshd
  395. action = iptables[name=SSH, port=ssh, protocol=tcp]
  396. logpath = /var/log/secure
  397. EOF
  398. fi
  399.  
  400. bigecho "Enabling services on boot..."
  401.  
  402. if grep -qs "release 6" /etc/redhat-release; then
  403. chkconfig iptables on
  404. chkconfig fail2ban on
  405. else
  406. systemctl --now mask firewalld 2>/dev/null
  407. systemctl enable iptables fail2ban 2>/dev/null
  408. fi
  409. if ! grep -qs "hwdsl2 VPN script" /etc/rc.local; then
  410. if [ -f /etc/rc.local ]; then
  411. conf_bk "/etc/rc.local"
  412. else
  413. echo '#!/bin/sh' > /etc/rc.local
  414. fi
  415. cat >> /etc/rc.local <<'EOF'
  416.  
  417. # Added by hwdsl2 VPN script
  418. (sleep 15
  419. modprobe -q pppol2tp
  420. service ipsec restart
  421. service xl2tpd restart
  422. echo 1 > /proc/sys/net/ipv4/ip_forward)&
  423. EOF
  424. fi
  425.  
  426. bigecho "Starting services..."
  427.  
  428. # Restore SELinux contexts
  429. restorecon /etc/ipsec.d/*db 2>/dev/null
  430. restorecon /usr/local/sbin -Rv 2>/dev/null
  431. restorecon /usr/local/libexec/ipsec -Rv 2>/dev/null
  432.  
  433. # Reload sysctl.conf
  434. sysctl -e -q -p
  435.  
  436. # Update file attributes
  437. chmod +x /etc/rc.local
  438. chmod 600 /etc/ipsec.secrets* /etc/ppp/chap-secrets* /etc/ipsec.d/passwd*
  439.  
  440. # Apply new IPTables rules
  441. iptables-restore < "$IPT_FILE"
  442.  
  443. # Fix xl2tpd on CentOS 7, if kernel module "l2tp_ppp" is unavailable
  444. if grep -qs "release 7" /etc/redhat-release; then
  445. if ! modprobe -q l2tp_ppp; then
  446. sed -i '/^ExecStartPre/s/^/#/' /usr/lib/systemd/system/xl2tpd.service
  447. systemctl daemon-reload
  448. fi
  449. fi
  450.  
  451. # Restart services
  452. modprobe -q pppol2tp
  453. service fail2ban restart 2>/dev/null
  454. service ipsec restart 2>/dev/null
  455. service xl2tpd restart 2>/dev/null
  456.  
  457. cat <<EOF
  458.  
  459. ================================================
  460.  
  461. IPsec VPN server is now ready for use!
  462.  
  463. Connect to your new VPN with these details:
  464.  
  465. Server IP: $PUBLIC_IP
  466. IPsec PSK: $VPN_IPSEC_PSK
  467. Username: $VPN_USER
  468. Password: $VPN_PASSWORD
  469.  
  470. Write these down. You'll need them to connect!
  471.  
  472. Important notes: https://git.io/vpnnotes
  473. Setup VPN clients: https://git.io/vpnclients
  474.  
  475. ================================================
  476.  
  477. EOF
  478.  
  479. }
  480.  
  481. ## Defer setup until we have the complete script
  482. vpnsetup "$@"
  483.  
  484. exit 0

5、安装完,会打印Server IP, IPseck PSK, Username, Password信息,我们可以换成自己配置的

  1. //先修改PSK,将PSK后面的字符替换掉
  2. vim /etc/ipsec.secrets
  3.  
  4. // 修改用户名和密码 ,将用户名和密码修改为自已想要的字符
  5. vim /etc/ppp/chap-secrets
  6.  
  7. //重启IPsec和xl2tpd服务
  8. systemctl restart ipsec xl2tpd

6、现在可以尝试连接vpn了,连接方法如下:

  win10 and win8

  1. 右键单击系统托盘中的无线/网络图标。
  2. 选择打开网络和共享中心。
  3. 点击设置一个新的连接或网络。
  4. 选择连接到工作区,然后单击下一步。
  5. 单击使用我的Internet连接(VPN)。
  6. Your VPN Server IPInternet地址栏中输入。
  7. 在目的地名称字段中输入任何你喜欢的,然后点击创建。
  8. 返回到网络和共享中心。在左侧,点击更改适配器设置。
  9. 右键单击新的VPN条目,然后选择“ 属性”。
  10. 单击安全选项卡。选择“使用IPsec的第2层隧道协议(L2TP / IPSec)”作为VPN的类型。
  11. 点击允许这些协议。一定要选择“挑战握手认证协议(CHAP)”复选框。
  12. 点击高级设置按钮。
  13. 选择使用预共享密钥进行验证,并输入Your VPN IPsec PSK的关键。
  14. 单击确定关闭高级设置。
  15. 单击确定以保存VPN连接详细信息。

  win7,vista和xp

  1. 点击开始菜单并进入控制面板。
  2. 转到网络和Internet部分。
  3. 点击网络和共享中心。
  4. 点击设置一个新的连接或网络。
  5. 选择连接到工作区,然后单击下一步。
  6. 单击使用我的Internet连接(VPN)。
  7. Your VPN Server IPInternet地址栏中输入。
  8. 在目的地名称字段中输入任何你喜欢的东西。
  9. 检查现在不要连接; 只需设置它,以便以后可以连接复选框。
  10. 点击下一步。
  11. Your VPN Username在用户名字段中输入。
  12. Your VPN Password在密码字段中输入。
  13. 选中记住此密码复选框。
  14. 点击创建,然后关闭。
  15. 返回到网络和共享中心。在左侧,点击更改适配器设置。
  16. 右键单击新的VPN条目,然后选择“ 属性”。
  17. 单击选项选项卡,并取消选中包括Windows登录域。
  18. 单击安全选项卡。选择“使用IPsec的第2层隧道协议(L2TP / IPSec)”作为VPN的类型。
  19. 点击允许这些协议。一定要选择“挑战握手认证协议(CHAP)”复选框。
  20. 点击高级设置按钮。
  21. 选择使用预共享密钥进行验证,并输入Your VPN IPsec PSK的关键。
  22. 单击确定关闭高级设置。
  23. 单击确定以保存VPN连接详细信息。

  注意:如果连接不上,需要更改注册表,点击查看详细

  1. cmd命令行执行
  2.  
  3. //对于Windows Vista,7,8.x和10
  4. REG ADD HKLM\SYSTEM\CurrentControlSet\Services\PolicyAgent /v AssumeUDPEncapsulationContextOnSendRule /t REG_DWORD /d 0x2 /f
  5.  
  6. //仅适用于Windows XP
  7. REG ADD HKLM\SYSTEM\CurrentControlSet\Services\IPSec /v AssumeUDPEncapsulationContextOnSendRule /t REG_DWORD /d 0x2 /f
  8.  
  9. //虽然不常见,但某些Windows系统禁用IPsec加密,导致连接失败。要重新启用它,请运行以下命令并重新启动您的PC
  10. REG ADD HKLM\SYSTEM\CurrentControlSet\Services\RasMan\Parameters /v ProhibitIpSec /t REG_DWORD /d 0x0 /f

  OS X

  1. 打开系统偏好设置并转到网络部分。
  2. 点击窗口左下角的+按钮。
  3. 从“ 接口”下拉菜单中选择“ VPN
  4. 从“ VPN类型”下拉菜单中选择“ IPSec上的L2TP”。
  5. 输入你喜欢的服务名称。
  6. 点击创建。
  7. 输入Your VPN Server IP的服务器地址。
  8. 输入Your VPN Username的帐户名称。
  9. 点击认证设置按钮。
  10. 在“ 用户验证”部分中,选择“ 密码”单选按钮并进入Your VPN Password
  11. 在“ 机器验证”部分中,选择“ 共享机密”单选按钮并进入Your VPN IPsec PSK
  12. 点击OK
  13. 选中菜单栏中显示VPN状态复选框。
  14. 单击“ 高级”按钮,确保选中“ 发送所有通过VPN连接的通信”复选框。
  15. 单击“ TCP / IP”选项卡,并确保“ 配置IPv6”部分中的“ 仅限本地链接”。
  16. 单击确定关闭高级设置,然后单击应用以保存VPN连接信息。

  Android

  1. 启动设置应用程序。
  2. 点击无线和网络部分中的更多...。
  3. 点击VPN
  4. 点击添加VPN配置文件或屏幕右上方的+图标。
  5. 在名称字段中输入您喜欢的任何内容。
  6. 在类型下拉菜单中选择L2TP / IPSec PSK
  7. Your VPN Server IP在服务器地址栏中输入。
  8. 输入Your VPN IPsec PSK的预共享的IPSec密钥场。
  9. 点按保存。
  10. 点击新的VPN连接。
  11. Your VPN Username在用户名字段中输入。
  12. Your VPN Password在密码字段中输入。
  13. 选中保存帐户信息复选框。
  14. 点击连接。

  ios

  1. 进入设置 - >常规 - > VPN
  2. 点击添加VPN配置...。
  3. 点按类型。选择L2TP并返回。
  4. 点按“ 说明”,然后输入您喜欢
  5. 点击服务器并输入Your VPN Server IP
  6. 点击帐户并输入Your VPN Username
  7. 点击密码并输入Your VPN Password
  8. 点击秘密并进入Your VPN IPsec PSK
  9. 确保发送所有通信开关打开。
  10. 点按完成。
  11. 滑动VPN开关。

  Linux

  1. #####要设置VPN客户端,请首先安装以下软件包####
  2. # Ubuntu & Debian
  3. apt-get update
  4. apt-get -y install strongswan xl2tpd
  5.  
  6. # CentOS & RHEL
  7. yum -y install epel-release
  8. yum -y install strongswan xl2tpd
  9.  
  10. # Fedora
  11. yum -y install strongswan xl2tpd
  12.  
  13. #####创建VPN变量(用实际值替换)####
  14. VPN_SERVER_IP='your_vpn_server_ip'
  15. VPN_IPSEC_PSK='your_ipsec_pre_shared_key'
  16. VPN_USER='your_vpn_username'
  17. VPN_PASSWORD='your_vpn_password'
  18.  
  19. #####配置strongSwan#####
  20. cat > /etc/ipsec.conf <<EOF
  21. # ipsec.conf - strongSwan IPsec configuration file
  22.  
  23. # basic configuration
  24.  
  25. config setup
  26. # strictcrlpolicy=yes
  27. # uniqueids = no
  28.  
  29. # Add connections here.
  30.  
  31. # Sample VPN connections
  32.  
  33. conn %default
  34. ikelifetime=60m
  35. keylife=20m
  36. rekeymargin=3m
  37. keyingtries=1
  38. keyexchange=ikev1
  39. authby=secret
  40. ike=aes128-sha1-modp1024,3des-sha1-modp1024!
  41. esp=aes128-sha1-modp1024,3des-sha1-modp1024!
  42.  
  43. conn myvpn
  44. keyexchange=ikev1
  45. left=%defaultroute
  46. auto=add
  47. authby=secret
  48. type=transport
  49. leftprotoport=17/1701
  50. rightprotoport=17/1701
  51. right=$VPN_SERVER_IP
  52. EOF
  53.  
  54. cat > /etc/ipsec.secrets <<EOF
  55. : PSK "$VPN_IPSEC_PSK"
  56. EOF
  57.  
  58. chmod 600 /etc/ipsec.secrets
  59.  
  60. # For CentOS/RHEL & Fedora ONLY
  61. mv /etc/strongswan/ipsec.conf /etc/strongswan/ipsec.conf.old 2>/dev/null
  62. mv /etc/strongswan/ipsec.secrets /etc/strongswan/ipsec.secrets.old 2>/dev/null
  63. ln -s /etc/ipsec.conf /etc/strongswan/ipsec.conf
  64. ln -s /etc/ipsec.secrets /etc/strongswan/ipsec.secrets
  65.  
  66. #####Configure xl2tpd#####
  67. cat > /etc/xl2tpd/xl2tpd.conf <<EOF
  68. [lac myvpn]
  69. lns = $VPN_SERVER_IP
  70. ppp debug = yes
  71. pppoptfile = /etc/ppp/options.l2tpd.client
  72. length bit = yes
  73. EOF
  74.  
  75. cat > /etc/ppp/options.l2tpd.client <<EOF
  76. ipcp-accept-local
  77. ipcp-accept-remote
  78. refuse-eap
  79. require-chap
  80. noccp
  81. noauth
  82. mtu 1280
  83. mru 1280
  84. noipdefault
  85. defaultroute
  86. usepeerdns
  87. connect-delay 5000
  88. name $VPN_USER
  89. password $VPN_PASSWORD
  90. EOF
  91.  
  92. chmod 600 /etc/ppp/options.l2tpd.client
  93.  
  94. VPN客户端设置现在完成。按照以下步骤进行连接。
  95.  
  96. 注意:每次尝试连接到VPN时都必须重复以下所有步骤
  97. ###创建xl2tpd控制文件:###
  98. mkdir -p /var/run/xl2tpd
  99. touch /var/run/xl2tpd/l2tp-control
  100.  
  101. ###重新启动服务###
  102. service strongswan restart
  103. service xl2tpd restart
  104.  
  105. ###启动IPsec连接###
  106. # Ubuntu & Debian
  107. ipsec up myvpn
  108.  
  109. # CentOS/RHEL & Fedora
  110. strongswan up myvpn
  111.  
  112. ###启动L2TP连接###
  113. echo "c myvpn" > /var/run/xl2tpd/l2tp-control
  114.  
  115. 运行ifconfig并检查输出。你现在应该看到一个新的界面ppp0
  116.  
  117. 检查您现有的默认路由
  118. ip route
  119.  
  120. 在输出中找到这一行:default via X.X.X.X ...。写下这个网关IP,用于下面的两个命令
  121. 从新的默认路由中排除您的VPN服务器的IP(用实际值替换)
  122. route add YOUR_VPN_SERVER_IP gw X.X.X.X
  123.  
  124. 如果您的VPN客户端是远程服务器,则还必须从新的默认路由中排除本地PC的公共IP,以防止您的SSH会话被断开(用实际值替换):
  125. route add YOUR_LOCAL_PC_PUBLIC_IP gw X.X.X.X
  126.  
  127. 添加一个新的默认路由,以开始通过VPN服务器路由流量:
  128. route add default dev ppp0
  129.  
  130. VPN连接现在完成。确认您的流量正在正确路由:
  131. wget -qO- http://ipv4.icanhazip.com; echo
  132.  
  133. 上述命令应该返回Your VPN Server IP
  134.  
  135. 要通过VPN服务器停止路由流量
  136. route del default dev ppp0
  137.  
  138. 断开连接
  139. # Ubuntu & Debian
  140. echo "d myvpn" > /var/run/xl2tpd/l2tp-control
  141. ipsec down myvpn
  142.  
  143. # CentOS/RHEL & Fedora
  144. echo "d myvpn" > /var/run/xl2tpd/l2tp-control
  145. strongswan down myvpn

7、这一个帐户明显不够用了,所以还是要找如何能多人共享方法,经过一阵折腾,终于找到方法了

  1. 1. /etc/ppp/chap-secrets 中添加第二个用户,格式为: "username" l2tpd "password" *
  2.  
  3. 2. 执行 openssl passwd -1 "password" 得到一串MD5加密的字符串,如: $1$5NwpneO7$YD82DhVJxCQpQ4zahB1N01
  4.  
  5. 3. 复制这一段字符串到 /etc/ipsec.d/passwd 中,新行如下: username:$1$5NwpneO7$YD82DhVJxCQpQ4zahB1N01:xauth-psk ,红色是手工添加的。
  6.  
  7. 4. systemctl restart ipsec xl2tpd

8、vpn使用的端口,注意开放

  1. (注意是:udp):500 4500 1701

9、这个脚本安装vpn的时候会添加很多iptables的规则,然而我并没有用它的规则,因为进入文件/etc/sysconfig/iptables,把里面的内容删除了

,然后自己添加一条规则

  1. iptables -t nat -A POSTROUTING -s 192.168.42.0/24 -o eth0 -j MASQUERADE

如果不添加这条规则,会导致客户端连接vpn 之后无法连接外网,加上后,客户单会通过vpn 服务器连接外网

CentOS7部署l2tp/IPsec服务的更多相关文章

  1. [原]CentOS 7.2 1511部署L2TP/IPsec服务器及客户端

    快过年了,感觉从去年开始,我们公司就变成了“别人的公司”,基本上提前一星期就放假了,好开心.正好可以利用这一段时间,把前段时间一些疑惑的问题解决下:) 然而挡在面前的一个拦路虎是:很多时候不能愉快的G ...

  2. Centos7部署kubernetes API服务(四)

    1.准备软件包 [root@linux-node1 bin]# pwd /usr/local/src/kubernetes/server/bin [root@linux-node1 bin]# cp ...

  3. centos7 配置PPTP、L2TP、IPSec服务

    首先,推荐跑下面的脚本: https://github.com/BoizZ/PPTP-L2TP-IPSec-VPN-auto-installation-script-for-CentOS-7 这个脚本 ...

  4. L2TP/IPSec一键安装脚本

    本脚本适用环境:系统支持:CentOS6+,Debian7+,Ubuntu12+内存要求:≥128M更新日期:2017 年 05 月 28 日 关于本脚本:名词解释如下L2TP(Layer 2 Tun ...

  5. 架设基于StrongSwan的L2tp/IPSec VPN服务器

    架设基于StrongSwan的L2tp/IPSec VPN服务器 参考: http://agit8.turbulent.ca/bwp/2011/01/setting-up-a-vpn-server-w ...

  6. PPTPD/L2TP/IPSec VPN一键安装包 For CentOS 6

    一.一键安装PPTPD VPN 本教程适用于Openv VPS.Xen VPS或者KVM VPS. 1.首先运行如下命令: cat /dev/net/tun 返回的必须是: cat: /dev/net ...

  7. 如何在 Debian / Ubuntu 服务器上架设 L2TP / IPSec VPN

    本站的 Rio 最近在一台 Ubuntu 和一台 Debian 主机上配置了 L2TP / IPSec VPN,并在自己的博客上做了记录.原文以英文写就,我把它大致翻译了一下,结合我和 Rio 在设置 ...

  8. centos7 部署ssserver

    centos7 部署shadowsocks服务端 为什么要选centos7? 以后centos7 肯定是主流,在不重要的环境还是尽量使用新系统吧 centos7 的坑 默认可能会有firewall 或 ...

  9. centos7 部署 docker ce

    =============================================== 2019/4/9_第1次修改                       ccb_warlock === ...

随机推荐

  1. 某ISP的流氓行径 劫持用户HTTP请求插入js代码

    最近公司搞的项目有用户反应点击任意链接后偶尔会跳到一个“莫名奇妙”的网站………… 喏,就是这个咯.

  2. 停课day1

    一早上只做了一个calculator 还是参照题解,好惭愧 f[1]=0; flag[1]=true;    for (int i=2,N=num[n];i<p;i++) {        fo ...

  3. 浅析JavaScript的垃圾回收机制

    JavaScript语言是一门优秀的脚本语言.其中包含脚本语言的灵活性外还拥有许多高级语言的特性.例如充许构建和实例化一个对象,垃圾回收机制(GC:Garbage Collecation).通常我们使 ...

  4. I/O多路转接-epoll

    By francis_hao    Aug 5,2017   APUE讲多路转接的章节介绍了select.pselect和poll函数.而epoll是linux内核在2.5.44引入的.在glibc ...

  5. yaf学习网站

    http://www.01happy.com/php-yaf-ext-business/

  6. Codeforces 937.C Save Energy!

    C. Save Energy! time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  7. Spring学习--xml 中 Bean 的自动装配

    Spring IOC 容器可以自动装配 Bean. 只要在 <bean> 的 autowire 属性里指定自动装配的模式. byName(根据名称自动装配):必须将目标 Bean 的名称和 ...

  8. Java中中英文对齐输出问题,以及Java中的格式化输出

    一 中英文对齐输出问题 问题,要求控制台输出如下: abcefg  def 森林 阿狗 其实就是要求对齐输出,各种查找java的格式化输出,然后发现只要一个简单的“\t”就可以实现. 代码如下: Sy ...

  9. 完美兼容IE,chrome,ff的设为首页、加入收藏及保存到桌面js代码

    <script  type="text/javascript"> //设为首页 function SetHome(obj,url){     try{         ...

  10. Lucene6去掉了Filter但是可以用BooleanQuery实现Filter查询

    Lucene在6.0版本之后彻底废除了Filter的使用,采用BooleanQuery来实现Filter的功能,核心代码如下: TermQuery termQuery = new TermQuery( ...