一键设置L2TP脚本-Ubuntu14.04LTS

前端之家收集整理的这篇文章主要介绍了一键设置L2TP脚本-Ubuntu14.04LTS前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

亲测在VultrUltraVPSUbuntu 14.04 LTS成功搭建L2TPVPN

方法使用Linux自带的账户认证作为L2TP的认证。用户名默认为vpn_user,密码在脚本执行过程中,由执行者手动设定密码;PSKpsk,开机自动启动。

本脚本必须使用root账户执行。

  1. #!/bin/bash
  2. # Referring from https://raymii.org/s/tutorials/IPSEC_L2TP_vpn_with_Ubuntu_14.04.html
  3.  
  4. if [[ $EUID -ne 0 ]]; then
  5. echo 'Error:This script must be run as root!'
  6. exit 1
  7. fi
  8.  
  9. apt-get install -y openswan xl2tpd ppp lsof vim rng-tools curl
  10. SERVERIP=`curl -s -4 icanhazip.com`
  11. iptables -t nat -A POSTROUTING -j SNAT --to-source $SERVERIP -o eth0
  12.  
  13. echo 'net.ipv4.ip_forward = 1' | tee -a /etc/sysctl.conf
  14. echo 'net.ipv4.conf.all.accept_redirects = 0' | tee -a /etc/sysctl.conf
  15. echo 'net.ipv4.conf.all.send_redirects = 0' | tee -a /etc/sysctl.conf
  16. echo 'net.ipv4.conf.default.rp_filter = 0' | tee -a /etc/sysctl.conf
  17. echo 'net.ipv4.conf.default.accept_source_route = 0' | tee -a /etc/sysctl.conf
  18. echo 'net.ipv4.conf.default.send_redirects = 0' | tee -a /etc/sysctl.conf
  19. echo 'net.ipv4.icmp_ignore_bogus_error_responses = 1' | tee -a /etc/sysctl.conf
  20.  
  21. for vpn in /proc/sys/net/ipv4/conf/*; do echo 0 > $vpn/accept_redirects; echo 0 > $vpn/send_redirects; done
  22. sysctl -p
  23.  
  24. # start VPN onstart
  25. cp /etc/rc.local{,.bak}
  26. sed -i '/exit 0/d' /etc/rc.local
  27. cat >> /etc/rc.local<<EOF
  28. for vpn in /proc/sys/net/ipv4/conf/*; do echo 0 > $vpn/accept_redirects; echo 0 > $vpn/send_redirects; done
  29. iptables -t nat -A POSTROUTING -j SNAT --to-source $SERVERIP -o eth0
  30.  
  31. exit 0
  32. EOF
  33.  
  34. # config ipsec
  35. cp /etc/ipsec.conf{,.bak}
  36. cat >/etc/ipsec.conf <<EOF
  37. version 2 # conforms to second version of ipsec.conf specification
  38.  
  39. config setup
  40. dumpdir=/var/run/pluto/
  41. #in what directory should things started by setup (notably the Pluto daemon) be allowed to dump core?
  42.  
  43. nat_traversal=yes
  44. #whether to accept/offer to support NAT (NAPT,also known as 'IP Masqurade') workaround for IPsec
  45.  
  46. virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12,%v6:fd00::/8,%v6:fe80::/10
  47. #contains the networks that are allowed as subnet= for the remote client. In other words,the address ranges that may live behind a NAT router through which a client connects.
  48.  
  49. protostack=netkey
  50. #decide which protocol stack is going to be used.
  51.  
  52. force_keepalive=yes
  53. keep_alive=60
  54. # Send a keep-alive packet every 60 seconds.
  55.  
  56. conn L2TP-PSK-noNAT
  57. authby=secret
  58. #shared secret. Use rsasig for certificates.
  59.  
  60. pfs=no
  61. #Disable pfs
  62.  
  63. auto=add
  64. #the ipsec tunnel should be started and routes created when the ipsec daemon itself starts.
  65.  
  66. keyingtries=3
  67. #Only negotiate a conn. 3 times.
  68.  
  69. ikelifetime=8h
  70. keylife=1h
  71.  
  72. ike=aes256-sha1,aes128-sha1,3des-sha1
  73. phase2alg=aes256-sha1,3des-sha1
  74. # https://lists.openswan.org/pipermail/users/2014-April/022947.html
  75. # specifies the phase 1 encryption scheme,the hashing algorithm,and the diffie-hellman group. The modp1024 is for Diffie-Hellman 2. Why 'modp' instead of dh? DH2 is a 1028 bit encryption algorithm that modulo's a prime number,e.g. modp1028. See RFC 5114 for details or the wiki page on diffie hellmann,if interested.
  76.  
  77. type=transport
  78. #because we use l2tp as tunnel protocol
  79.  
  80. left=$SERVERIP
  81. #fill in server IP above
  82.  
  83. leftprotoport=17/1701
  84. right=%any
  85. rightprotoport=17/%any
  86.  
  87. dpddelay=10
  88. # Dead Peer Dectection (RFC 3706) keepalives delay
  89. dpdtimeout=20
  90. # length of time (in seconds) we will idle without hearing either an R_U_THERE poll from our peer,or an R_U_THERE_ACK reply.
  91. dpdaction=clear
  92. # When a DPD enabled peer is declared dead,what action should be taken. clear means the eroute and SA with both be cleared.
  93. EOF
  94.  
  95. #config ipsec.secrets
  96. cp /etc/ipsec.secrets{,.bak}
  97. cat >/etc/ipsec.secrets <<EOF
  98. $SERVERIP %any: PSK 'psk' #<<<<<<<<<<PSK>>>>>>>>>>#
  99. EOF
  100.  
  101. ipsec verify
  102.  
  103. cp /etc/xl2tpd/xl2tpd.conf{,.bak}
  104. cat > /etc/xl2tpd/xl2tpd.conf <<EOF
  105. [global]
  106. ipsec saref = yes
  107. saref refinfo = 30
  108.  
  109. ;debug avp = yes
  110. ;debug network = yes
  111. ;debug state = yes
  112. ;debug tunnel = yes
  113.  
  114. [lns default]
  115. ip range = 172.16.1.30-172.16.1.100
  116. local ip = 172.16.1.1
  117. require authentication = yes
  118. unix authentication = yes
  119. ;ppp debug = yes
  120. pppoptfile = /etc/ppp/options.xl2tpd
  121. length bit = yes
  122. EOF
  123.  
  124. # config options.xl2tpd
  125. cp /etc/ppp/options.xl2tpd{,.bak}
  126. cat >/etc/ppp/options.xl2tpd <<EOF
  127. ms-dns 8.8.8.8
  128. ms-dns 8.8.4.4
  129. auth
  130. mtu 1200
  131. mru 1000
  132. crtscts
  133. hide-password
  134. modem
  135. name l2tpd
  136. proxyarp
  137. lcp-echo-interval 30
  138. lcp-echo-failure 4
  139. login
  140. EOF
  141.  
  142. #config pam.d
  143. cp /etc/pam.d/ppp{,.bak}
  144. sed -i '/^auth/c\ auth required pam_nologin.so\ auth required pam_unix.so\ account required pam_unix.so\ session required pam_unix.so ' /etc/pam.d/ppp
  145.  
  146. #config pap-secrets
  147. cp /etc/ppp/pap-secrets{,.bak}
  148. cat >>/etc/ppp/pap-secrets <<EOF
  149. * l2tpd '' *
  150. EOF
  151.  
  152. useradd vpn_user
  153. echo -n 'set password for [vpn_user]:'
  154. passwd vpn_user
  155. /etc/init.d/ipsec restart
  156. /etc/init.d/xl2tpd restart
  157.  
  158. echo 'IP:' $SERVERIP
  159. echo 'UserName(set by useradd vpn_user): vpn_user'
  160. echo 'PSK(set by /etc/ipsec.secrets): psk'
  161.  
  162. exit 0

猜你在找的Ubuntu相关文章