2017-07-30

[筆記]使用Ubuntu 16.04架設L2TP Over IPSec VPN

Ubuntu在16.04版本之後的套件庫,拿掉了openswan,所以要改用strongswan來設定IPSec,跟之前openswan有些許的不同,參考了這篇之後架設成功,所以趕緊筆記一下~

注意,底下所有指令都用root身分執行,所以就不再打sudo了!

1.系統部分

首先安裝必要套件:
  1. apt-get install strongswan xl2tpd ppp lsof



再來設定轉發相關(打指令,可全部複製貼上):
  1. echo "net.ipv4.ip_forward = 1" | tee -a /etc/sysctl.conf
  2. echo "net.ipv4.conf.all.accept_redirects = 0" | tee -a /etc/sysctl.conf
  3. echo "net.ipv4.conf.all.send_redirects = 0" | tee -a /etc/sysctl.conf
  4. echo "net.ipv4.conf.default.rp_filter = 0" | tee -a /etc/sysctl.conf
  5. echo "net.ipv4.conf.default.accept_source_route = 0" | tee -a /etc/sysctl.conf
  6. echo "net.ipv4.conf.default.send_redirects = 0" | tee -a /etc/sysctl.conf
  7. echo "net.ipv4.icmp_ignore_bogus_error_responses = 1" | tee -a /etc/sysctl.conf
  8.  
  9. sysctl -p



2.IPSec部分

接著要來設定strongswan,編輯 /etc/ipsec.conf:
  1. config setup
  2.  
  3. conn L2TP-PSK-noNAT
  4. authby=secret
  5. #shared secret. Use rsasig for certificates.
  6. auto=add
  7. #the ipsec tunnel should be started and routes created when the ipsec daemon itself starts.
  8. keyingtries=3
  9. #Only negotiate a conn. 3 times.
  10. ikelifetime=8h
  11. keylife=1h
  12. ike=aes256-sha1,aes128-sha1,3des-sha1
  13. type=transport
  14. #because we use l2tp as tunnel protocol
  15. left=1.2.3.4 #別忘了要改成VPN Server的真實IP喔
  16. #fill in server IP above
  17. leftprotoport=17/1701
  18.  
  19. right=%any
  20. rightprotoport=17/%any
  21. dpddelay=10
  22. # Dead Peer Dectection (RFC 3706) keepalives delay
  23.  
  24. dpdtimeout=20
  25. # 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.
  26.  
  27. dpdaction=clear
  28. # When a DPD enabled peer is declared dead, what action should be taken. clear means the eroute and SA with both be cleared.


再來修改預先共用金鑰,編輯 /etc/ipsec.secrets,將PASSWORD改成你要的密碼:
  1. # This file holds shared secrets or RSA private keys for authentication.
  2. # RSA private key for this host, authenticating it to any other host
  3. # which knows the public part.
  4. # 將底下PASSWORD改成你要的密碼,別忘了""不可刪除
  5. %any : PSK "PASSWORD"


3.L2TP部分

編輯 /etc/xl2tpd/xl2tpd.conf:
  1. [global]
  2. ipsec saref = yes
  3. saref refinfo = 30
  4. ;debug avp = yes
  5. ;debug network = yes
  6. ;debug state = yes
  7. ;debug tunnel = yes
  8. [lns default]
  9. ip range = 192.168.1.x-192.168.1.y ;這裡是要配發給client的內部IP區段
  10. local ip = 192.168.1.z ;這裡是VPN Server的內部IP
  11. require chap = yes
  12. refuse pap = yes
  13. require authentication = yes
  14. ;ppp debug = yes
  15. pppoptfile = /etc/ppp/options.xl2tpd
  16. length bit = yes


再來編輯 /etc/ppp/options.xl2tpd:
  1. require-mschap-v2
  2. ms-dns 8.8.8.8
  3. ms-dns 8.8.4.4
  4. auth
  5. mtu 1200
  6. mru 1000
  7. crtscts
  8. hide-password
  9. modem
  10. name l2tpd
  11. proxyarp
  12. lcp-echo-interval 30
  13. lcp-echo-failure 4


增加使用者,編輯 /etc/ppp/chap-secrets:
  1. # Secrets for authentication using CHAP
  2. # client server secret IP addresses
  3. username l2tpd PASSWORD *

別忘了server要填l2tp才行

到這邊就設定完成了,可重啟服務:
  1. ipsec update
  2. ipsec reload
  3. ipsec restart
  4. service xl2tpd restart


設定防火牆,並開放NAT功能,讓連進來的client也可連上網路,請參考底下(來源:鳥哥):
  1. mkdir -p /usr/local/virus/iptables
  2. vim /usr/local/virus/iptables/iptables.rule
  3. ################## 增加底下 ###################
  4. #!/bin/bash
  5. # 請先輸入您的相關參數,不要輸入錯誤了!
  6. EXTIF="eth0" # 這個是可以連上 Public IP 的網路介面
  7. INIF="eth1" # 內部 LAN 的連接介面;若無則寫成 INIF=""
  8. INNET="192.168.1.0/24" # 若無內部網域介面,請填寫成 INNET=""
  9. export EXTIF INIF INNET
  10. # 第一部份,針對本機的防火牆設定!##########################################
  11. # 1. 先設定好核心的網路功能:
  12. echo "1" > /proc/sys/net/ipv4/tcp_syncookies
  13. echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
  14. for i in /proc/sys/net/ipv4/conf/*/{rp_filter,log_martians}; do
  15. echo "1" > $i
  16. done
  17. for i in /proc/sys/net/ipv4/conf/*/{accept_source_route,accept_redirects,send_redirects}; do
  18. echo "0" > $i
  19. done
  20. # 2. 清除規則、設定預設政策及開放 lo 與相關的設定值
  21. PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin; export PATH
  22. iptables -F
  23. iptables -X
  24. iptables -Z
  25. iptables -P INPUT DROP
  26. iptables -P OUTPUT ACCEPT
  27. iptables -P FORWARD ACCEPT
  28. iptables -A INPUT -i lo -j ACCEPT
  29. iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  30. # 3. 啟動額外的防火牆 script 模組
  31. if [ -f /usr/local/virus/iptables/iptables.deny ]; then
  32. sh /usr/local/virus/iptables/iptables.deny
  33. fi
  34. if [ -f /usr/local/virus/iptables/iptables.allow ]; then
  35. sh /usr/local/virus/iptables/iptables.allow
  36. fi
  37. if [ -f /usr/local/virus/httpd-err/iptables.http ]; then
  38. sh /usr/local/virus/httpd-err/iptables.http
  39. fi
  40. # 4. 允許某些類型的 ICMP 封包進入
  41. AICMP="0 3 3/4 4 11 12 14 16 18"
  42. for tyicmp in $AICMP
  43. do
  44. iptables -A INPUT -i $EXTIF -p icmp --icmp-type $tyicmp -j ACCEPT
  45. done
  46. # 5. 載入允許進入的service表,用額外檔案以方便單獨設定,就不用每次都要下拉編輯
  47. if [ -f /usr/local/virus/iptables/iptables.services ]; then
  48. sh /usr/local/virus/iptables/iptables.services
  49. fi
  50. # 第二部份,針對後端主機的防火牆設定!###############################
  51. # 1. 先載入一些有用的模組
  52. modules="ip_tables iptable_nat ip_nat_ftp ip_nat_irc ip_conntrack ip_conntrack_ftp ip_conntrack_irc"
  53. for mod in $modules
  54. do
  55. testmod=`lsmod | grep "^${mod} " | awk '{print $1}'`
  56. if [ "$testmod" == "" ]; then
  57. modprobe $mod
  58. fi
  59. done
  60. # 2. 清除 NAT table 的規則吧!
  61. iptables -F -t nat
  62. iptables -X -t nat
  63. iptables -Z -t nat
  64. iptables -t nat -P PREROUTING ACCEPT
  65. iptables -t nat -P POSTROUTING ACCEPT
  66. iptables -t nat -P OUTPUT ACCEPT
  67. # 3. 若有內部介面的存在 (雙網卡) 開放成為路由器,且為 IP 分享器!
  68. if [ "$INIF" != "" ]; then
  69. iptables -A INPUT -i $INIF -j ACCEPT
  70. echo "1" > /proc/sys/net/ipv4/ip_forward
  71. if [ "$INNET" != "" ]; then
  72. for innet in $INNET
  73. do
  74. iptables -t nat -A POSTROUTING -s $innet -o $EXTIF -j MASQUERADE
  75. done
  76. fi
  77. fi
  78. # 如果你的 MSN 一直無法連線,或者是某些網站 OK 某些網站不 OK,
  79. # 可能是 MTU 的問題,那你可以將底下這一行給他取消註解來啟動 MTU 限制範圍
  80. # iptables -A FORWARD -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss \
  81. # --mss 1400:1536 -j TCPMSS --clamp-mss-to-pmtu
  82. # 4. NAT 伺服器後端的 LAN 內對外之伺服器設定
  83. # iptables -t nat -A PREROUTING -p tcp -i $EXTIF --dport 80 \
  84. # -j DNAT --to-destination 192.168.1.210:80 # WWW
  85. # 5. 特殊的功能,包括 Windows 遠端桌面所產生的規則,假設桌面主機為 1.2.3.4
  86. # iptables -t nat -A PREROUTING -p tcp -s 1.2.3.4 --dport 6000 \
  87. # -j DNAT --to-destination 192.168.100.10
  88. # iptables -t nat -A PREROUTING -p tcp -s 1.2.3.4 --sport 3389 \
  89. # -j DNAT --to-destination 192.168.100.20
  90. # 6. 最終將這些功能儲存下來吧!
  91. # /etc/init.d/iptables save
  92. # 在Ubuntu不一定有用就是了,就不用啦,反正每次開機都會執行一次
  93. # iptables-save
  94. ########################### END OF iptables.rule #####################
  95.  
  96. vim /usr/local/virus/iptables/iptables.allow
  97. ################### 新增底下 #####################
  98. #!/bin/bash
  99. # 底下則填寫你允許進入本機的其他網域或主機啊!
  100. #iptables -A INPUT -i $EXTIF -s 1.2.3.4 -j ACCEPT
  101. ########################## END OF iptables.allow #####################
  102. vim /usr/local/virus/iptables/iptables.services
  103. ################### 新增底下 ####################
  104. #!/bin/bash
  105. # 將妳要對外開啟的服務設定在此
  106. # iptables -A INPUT -p TCP -i $EXTIF --dport 21 --sport 1024:65534 -j ACCEPT # FTP
  107. # iptables -A INPUT -p TCP -i $EXTIF --dport 22 --sport 1024:65534 -j ACCEPT # SSH
  108. # iptables -A INPUT -p TCP -i $EXTIF --dport 25 --sport 1024:65534 -j ACCEPT # SMTP
  109. # iptables -A INPUT -p UDP -i $EXTIF --dport 53 --sport 1024:65534 -j ACCEPT # DNS
  110. # iptables -A INPUT -p TCP -i $EXTIF --dport 53 --sport 1024:65534 -j ACCEPT # DNS
  111. # iptables -A INPUT -p TCP -i $EXTIF --dport 80 --sport 1024:65534 -j ACCEPT # WWW
  112. # iptables -A INPUT -p TCP -i $EXTIF --dport 110 --sport 1024:65534 -j ACCEPT # POP3
  113. # iptables -A INPUT -p TCP -i $EXTIF --dport 443 --sport 1024:65534 -j ACCEPT # HTTPS
  114.  
  115. ## !!!!!!這裡是 L2TP Over IPSec VPN 務必開放的Port!!!!!!!! ##
  116. iptables -A INPUT -p UDP -i $EXTIF --dport 500 -j ACCEPT
  117. iptables -A INPUT -p UDP -i $EXTIF --dport 4500 -j ACCEPT
  118. iptables -A INPUT -p UDP -i $EXTIF --dport 1701 -j ACCEPT
  119. ########################## END OF iptables.services #####################
  120. chmod 700 /usr/local/virus/iptables/iptables.*


好了之後,編輯/etc/rc.local,增加一行
  1. /usr/local/virus/iptables/iptables.rule
重新開機就可以囉~

1 則留言:

  1. 大大您好...我近期根據您的文件配置一個L2TP with IPSec Server,但我配置之後透過Win7去新增一個VPN Client撥接,如果我去Win7的VPN撥接設定一組L2TP"預先共用金鑰"後無法撥接成功,但如果設定"使用憑證進行驗證"就可以,請教這哪邊出了問題?

    回覆刪除