2013-03-04

Ubuntu架設VPN Server

VPN在現今環境是很實用的,可以讓你在家裡也能存取公司內部網路的資源,這樣就可遠端工作啦~(但順不順暢還是得靠網路速度和硬體夠不夠強@@)

這邊用Ubuntu Server 12.04來說明如何架設。



首先硬體方面,安裝兩張網路卡比較好,網路卡設定:
eth0(內部連線) eth1(對外連線)
IP 192.168.1.10 i.i.i.i(視情況而定)
Gateway 不指定(因為不對外) g.g.g.g(視情況)
Mask 255.255.255.0 255.255.255.0(視情況)
DNS (有內部dns server可在此指定) 8.8.8.8(視情況)

如果是全新安裝,那在安裝時候預設網路要選擇eth1,在DHCP會失敗沒關係(如果對外是DHCP連線就沒此問題),在下一步會讓你手動輸入對外網路連線資訊,就照著實際情況輸入好,這樣網路就會通。在選擇要安裝的套件先只選擇OpenSSH Server就好,其他就照著說明裝完吧。

裝好之後的設定步驟:

  1. 變身Root
    1. sudo -i
    2. (輸入你帳號的密碼)
  2. 安裝pptpd
    1. apt-get install pptpd
  3. 編輯/etc/pptpd.conf
    1. vi /etc/pptpd.conf
  4. 幾乎都不用動,只需編輯最下面的localip與remoteip。
    這裡說明一下,localip指的是vpn server的真實IP,也就是可連線到internet的IP;而remoteip就是你要指派給用戶端的IP範圍,跟DHCP的意思差不多。
    1. ###############################################################################
    2. # $Id$
    3. #
    4. # Sample Poptop configuration file /etc/pptpd.conf
    5. #
    6. # Changes are effective when pptpd is restarted.
    7. ###############################################################################
    8. # TAG: ppp
    9. # Path to the pppd program, default '/usr/sbin/pppd' on Linux
    10. #
    11. #ppp /usr/sbin/pppd
    12. # TAG: option
    13. # Specifies the location of the PPP options file.
    14. # By default PPP looks in '/etc/ppp/options'
    15. #
    16. #下面這行的檔案等等也要做修改。
    17. option /etc/ppp/pptpd-options
    18. # TAG: debug
    19. # Turns on (more) debugging to syslog
    20. #
    21. #debug
    22. # TAG: stimeout
    23. # Specifies timeout (in seconds) on starting ctrl connection
    24. #
    25. # stimeout 10
    26. # TAG: noipparam
    27. # Suppress the passing of the client's IP address to PPP, which is
    28. # done by default otherwise.
    29. #
    30. #noipparam
    31. # TAG: logwtmp
    32. # Use wtmp(5) to record client connections and disconnections.
    33. #
    34. logwtmp
    35. # TAG: bcrelay
    36. # Turns on broadcast relay to clients from interface
    37. #
    38. #bcrelay eth1
    39. # TAG: localip
    40. # TAG: remoteip
    41. # Specifies the local and remote IP address ranges.
    42. #
    43. # Any addresses work as long as the local machine takes care of the
    44. # routing. But if you want to use MS-Windows networking, you should
    45. # use IP addresses out of the LAN address space and use the proxyarp
    46. # option in the pppd options file, or run bcrelay.
    47. #
    48. # You can specify single IP addresses seperated by commas or you can
    49. # specify ranges, or both. For example:
    50. #
    51. # 192.168.0.234,192.168.0.245-249,192.168.0.254
    52. #
    53. # IMPORTANT RESTRICTIONS:
    54. #
    55. # 1. No spaces are permitted between commas or within addresses.
    56. #
    57. # 2. If you give more IP addresses than MAX_CONNECTIONS, it will
    58. # start at the beginning of the list and go until it gets
    59. # MAX_CONNECTIONS IPs. Others will be ignored.
    60. #
    61. # 3. No shortcuts in ranges! ie. 234-8 does not mean 234 to 238,
    62. # you must type 234-238 if you mean this.
    63. #
    64. # 4. If you give a single localIP, that's ok - all local IPs will
    65. # be set to the given one. You MUST still give at least one remote
    66. # IP for each simultaneous client.
    67. #
    68. # (Recommended)
    69. #localip 192.168.0.1
    70. #remoteip 192.168.0.234-238,192.168.0.245
    71. # or
    72. #localip 192.168.0.234-238,192.168.0.245
    73. #remoteip 192.168.1.234-238,192.168.1.245
    74. #伺服器真實IP,依實際情況修改
    75. localip i.i.i.i
    76. #要指派給用戶端的內部IP範圍,依實際情況修改
    77. remoteip 192.168.1.241-245
  5. 編輯/etc/ppp/pptpd-options檔案,把ms-dns打開,還有做一些連線認證的設定
    1. ###############################################################################
    2. # $Id$
    3. #
    4. # Sample Poptop PPP options file /etc/ppp/pptpd-options
    5. # Options used by PPP when a connection arrives from a client.
    6. # This file is pointed to by /etc/pptpd.conf option keyword.
    7. # Changes are effective on the next connection. See "man pppd".
    8. #
    9. # You are expected to change this file to suit your system. As
    10. # packaged, it requires PPP 2.4.2 and the kernel MPPE module.
    11. ###############################################################################
    12. # Authentication
    13. # Name of the local system for authentication purposes
    14. # (must match the second field in /etc/ppp/chap-secrets entries)
    15. name pptpd
    16. # Optional: domain name to use for authentication
    17. # domain mydomain.net
    18. # Strip the domain prefix from the username before authentication.
    19. # (applies if you use pppd with chapms-strip-domain patch)
    20. #chapms-strip-domain
    21. # Encryption
    22. # Debian: on systems with a kernel built with the package
    23. # kernel-patch-mppe >= 2.4.2 and using ppp >= 2.4.2, ...
    24. # {{{
    25. #################################################################
    26. #注意這底下的設定,沒打開或是開錯了都有可能造成client端無法通過認證連線VPN
    27. refuse-pap
    28. refuse-chap
    29. refuse-mschap
    30. # Require the peer to authenticate itself using MS-CHAPv2 [Microsoft
    31. # Challenge Handshake Authentication Protocol, Version 2] authentication.
    32. require-mschap-v2
    33. # Require MPPE 128-bit encryption
    34. # (note that MPPE requires the use of MSCHAP-V2 during authentication)
    35. require-mppe-128
    36. # }}}
    37. ############################################
    38. # Network and Routing
    39. # If pppd is acting as a server for Microsoft Windows clients, this
    40. # option allows pppd to supply one or two DNS (Domain Name Server)
    41. # addresses to the clients. The first instance of this option
    42. # specifies the primary DNS address; the second instance (if given)
    43. # specifies the secondary DNS address.
    44. # Attention! This information may not be taken into account by a Windows
    45. # client. See KB311218 in Microsoft's knowledge base for more information.
    46. #ms-dns 10.0.0.1
    47. #ms-dns 10.0.0.2
    48. #dns server依實際情況設定
    49. ms-dns 8.8.8.8
    50. ms-dns 8.8.4.4
    51. # If pppd is acting as a server for Microsoft Windows or "Samba"
    52. # clients, this option allows pppd to supply one or two WINS (Windows
    53. # Internet Name Services) server addresses to the clients. The first
    54. # instance of this option specifies the primary WINS address; the
    55. # second instance (if given) specifies the secondary WINS address.
    56. #ms-wins 10.0.0.3
    57. #ms-wins 10.0.0.4
    58. # Add an entry to this system's ARP [Address Resolution Protocol]
    59. # table with the IP address of the peer and the Ethernet address of this
    60. # system. This will have the effect of making the peer appear to other
    61. # systems to be on the local ethernet.
    62. # (you do not need this if your PPTP server is responsible for routing
    63. # packets to the clients -- James Cameron)
    64. proxyarp
    65. # Debian: do not replace the default route
    66. nodefaultroute
    67. # Logging
    68. # Enable connection debugging facilities.
    69. # (see your syslog configuration for where pppd sends to)
    70. #debug
    71. # Print out all the option values which have been set.
    72. # (often requested by mailing list to verify options)
    73. #dump
    74. # Miscellaneous
    75. # Create a UUCP-style lock file for the pseudo-tty to ensure exclusive
    76. # access.
    77. lock
    78. # Disable BSD-Compress compression
    79. nobsdcomp
  6. 編輯/etc/ppp/chap-secrets,這個檔案設定可用VPN連線的使用者,跟系統裡的帳號無關,採用明碼設定,所以一定要記得權限只有root可讀寫(600),內容就照底下範例:
    1. # Secrets for authentication using CHAP
    2. # client server secret IP addresses
    3. # 帳號名 勿動 密碼 允許連線的IP
    4. vpn-user pptpd userpassword *
  7. 設定好後重新啟動pptpd
    1. /etc/init.d/pptpd restart
試著連線看看,OK~可以連線成功,但....嘿嘿...

怎麼連上去之後甚麼網站都去不了?

因為上面的設定只是讓外部網路使用者能以PPTP協定連線進來該台主機並取得內部IP,但預設NAT功能並沒打開,所以想要連其他IP或網站是不可能瘩!!!

所以我們繼續說明NAT設定...


  1. 首先開啟封包轉遞(IP forward功能)
    1. vi /etc/sysctl.conf
    1. # 將底下這個設定值修改正確即可! (本來值為 0 ,將它改為 1 即可)
    2. net.ipv4.ip_forward = 1

    改完存檔後輸入 sysctl -p 讓設定馬上生效。
  2. 輸入底下指令打開IP轉送
    1. iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth1 -j MASQUERADE

    上面指令是將192.168.1.x網段透過eth1(也就是外網)轉發出去
  3. 上面設定完之後,透過VPN連線進來的User們應該就可以上網和連線到別台主機了!!但是這樣Port其實是全開,有點危險,所以有機會要再設定一下iptables防火牆

----

經過參考鳥哥的網頁之後,可照著底下的步驟,可使這台也充當IP分享器和簡易防火牆的功能,照底下步驟設定的話,上面的步驟就可以省囉~
    1. mkdir -p /usr/local/iptables
    2. vi /usr/local/iptables/iptables.rule
  1. 增加底下所有,並視情況修改
    1. #!/bin/bash
    2. #設定參數
    3. EXTIF="eth1"
    4. INIF="eth0"
    5. INNET="192.168.1.0/24"
    6. export EXTIF INIF INNET
    7. #重設防火牆規則
    8. iptables -F
    9. iptables -X
    10. iptables -Z
    11. iptables -P INPUT DROP
    12. iptables -P OUTPUT ACCEPT
    13. iptables -P FORWARD ACCEPT
    14. iptables -A INPUT -i lo -j ACCEPT
    15. iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    16. #允許某些ICMP封包進入
    17. ##AICMP="0 3 3/4 4 8 11 12 14 16 18"
    18. #AICMP="0 8 17 18"
    19. #for tyicmp in $AICMP
    20. #do
    21. # #iptables -A INPUT -i $EXTIF -p icmp --icmp-type $tyicmp -j ACCEPT
    22. #iptables -A INPUT -i $INIF -p icmp --icmp-type $tyicmp -j ACCEPT
    23. #done
    24. #內部網路全部允許
    25. iptables -A INPUT -i $INIF -s $INNET -j ACCEPT
    26. #僅允許ssh與pptpd進入
    27. iptables -A INPUT -p TCP --dport 22 --sport 1024:65534 -j ACCEPT #SSH
    28. iptables -A INPUT -p TCP --dport 1723 --sport 1024:65534 -j ACCEPT #PPTP
    29. #iptables -A INPUT -p TCP --dport 53 --sport 1024:65534 -j ACCEPT #DNS
    30. #iptables -A INPUT -p UDP --dport 53 --sport 1024:65534 -j ACCEPT #DNS
    31. #更新pptp之後,突然變得無法連線了嗎?因為新版pptpd要開GRE才能通的關係,所以必須打開47才能連線!!系統更新後就是為了這個卡超久
    32. iptables -A INPUT -p 47 -j ACCEPT
    33. #清除NAT Table
    34. iptables -F -t nat
    35. iptables -X -t nat
    36. iptables -Z -t nat
    37. iptables -t nat -P PREROUTING ACCEPT
    38. iptables -t nat -P POSTROUTING ACCEPT
    39. iptables -t nat -P OUTPUT ACCEPT
    40. #開啟IP轉發
    41. iptables -t nat -A POSTROUTING -s $INNET -o $EXTIF -j MASQUERADE
    42. #存檔
    43. iptables-save
  2. 修改權限僅root可讀取寫入執行
    1. chmod 700 /usr/local/iptables/iptables.*
  3. 這樣的步驟每次開機都要手動執行一次/usr/local/iptables/iptables.rule,太麻煩了,我們讓他每次開機就自動執行:
    1. vi /etc/rc.local

    在exit 0前面加上下面那行
    1. /usr/local/iptables/iptables.rule

    存檔即可。

沒有留言:

張貼留言