1. 程式人生 > >CentOS 7 安裝VPN Server 和 Client

CentOS 7 安裝VPN Server 和 Client

1. CentOS 7 安裝VPN Server

    modprobe ppp-compress-18 && echo yes
  • 1
  • 2
  • 是否開啟了TUN,有的虛擬機器主機需要開啟,返回結果為cat: /dev/net/tun: File descriptor in bad state。就表示通過。

    cat /dev/net/tun
    
    • 1
    • 2
  • 安裝ppp和pptpd

    yum -y install ppp pptpd
    
    • 1
    • 2
  • 配置pptpd.conf

    vim  /etc/pptpd.conf  
    
    localip 192.168.9.1
    remoteip 172.168.9.101-200
    #Localip這行是給vpn伺服器設定一個隧道ip
    #Remoteip是自動分配給客戶端的ip地址範圍。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 配置options.pptpd

    vim /etc/ppp/options.pptpd      #在末尾新增dns
    #先去掉debug前面的#,開啟日誌記錄
    ms-dns  202.96.209.5  
    ms-dns  8.8.8.8
    logfile /var/log/pptpd.log
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 配置連線VPN客戶端要用到的帳號密碼。

    vim /etc/ppp/chap-secrets    #格式很通俗易懂。
    
    #client為帳號,server是pptpd服務,secret是密碼,*表示是分配任意的ip
    #Secrets for authentication using CHAP
    #client     server     secret          IP addresses
     vpn        pptpd       vpn             *
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 配置sysctl.conf

    vi /etc/sysctl.conf
    #新增一行    net.ipv4.ip_forward = 1    
    #到末尾即可,然後儲存,這個很重要,系統路由模式功能。
    sysctl -p    
    #執行這個命令會輸出上面新增的那一行資訊,意思是使核心修改生效
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 配置iptables的轉發規則

    這個時候把iptables關閉的話是可以連線VPN了,之所以要把iptables關閉是因為沒有開放VPN的埠,客戶如果直接連線的話是不允許的。這裡還需要設定iptables的轉發規則,讓你的客戶端連線上之後能訪問外網。

iptables -t nat -A
POSTROUTING -s 192.168.9.0/24 -j MASQUERADE
  • 1
  • 在centos 7 中由於使用firewalld 來替換了iptables,所以需要用如下命令來配置轉發規則
firewall-cmd --permanent --direct --passthrough ipv4 -t nat -I POSTROUTING -o eth1 -j MASQUERADE -s 192.168.9.0/24
  • 1

目標網路vpn的內部網路為192.168.9.0/24,vpn伺服器有2個網絡卡,其中一個網絡卡連私有網路(eth0),ip是10.46.89.192,另外一個連線公網(eth1),ip地址是120.76.25.191。現在把所有 192.168.9.0 網段過來的請求,路由到外網。 這樣就能正常地路由所涉及的網路了。

  • 配置iptables的埠

    iptables -I INPUT -p tcp --dport 1723 -j ACCEPT
    iptables -I INPUT -p tcp --dport 47 -j ACCEPT
    iptables -I INPUT -p gre -j ACCEPT
    
    • 1
    • 2
    • 3
    • 4

在centos 7 中,用firewalld 來取代了iptables

firewall-cmd --zone=public --add-port=1723/tcp --permanent
firewall-cmd --zone=public --add-port=47/tcp --permanent
firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -p gre -j ACCEPT
firewall-cmd --permanent --direct --add-rule ipv6 filter INPUT 0 -p gre -j ACCEPT
firewall-cmd --reload   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 系統自動啟動pptp的vpn server

    systemctl start pptpd
    systemctl enable pptpd
    
    • 1
    • 2
    • 3

2. CentOS 7 安裝VPN Client

  • 安裝ppp pptp pptp-setup

        # yum install ppp pptp pptp-setup  
    
    • 1
    • 2
  • 建立VPN連線

    pptpsetup --create vpn --server 10.46.89.192 --username vpn --password vpn --start
    
    • 1
    • 2
  • 載入模組

    # modprobe ppp_mppe  
    
    • 1
    • 2
  • 連線VPN連線

    # pppd call vpn  //這裡的vpn是上面建立vpn連線  
    
    • 1
    • 2
  • 如果報以下錯誤:

    Connect: ppp0 <--> /dev/pts/3  
    CHAP authentication succeeded  
    LCP terminated by peer (MPPE required but peer refused)  
    Modem hangup  
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 解決辦法

        # vim /etc/ppp/peers/vpn  //vpn是上面建立的連線  
        檔案尾部,加上以下內容  
        require-mppe-128  
    
    • 1
    • 2
    • 3
    • 4
  • 如果報以下錯誤:

    Refusing MPPE stateful mode offered by peer  
    MPPE required but peer negotiation failed  
    
    • 1
    • 2
    • 3
  • 解決方法: 
    說明服務端不支援MPPE加密,pptpsetup時不需要使用–encrypt選項。或者去掉上面的,require-mppe-128

  • 連線成功(連線成功地後,客戶端和服務端都會有ppp0這樣的東西。)

    # ifconfig |grep ppp  
    ppp0 Link encap:Point-to-Point Protocol   
    
    # route  
    Kernel IP routing table  
    Destination Gateway Genmask Flags Metric Ref Use Iface  
    li684-32.member 192.168.10.1 255.255.255.255 UGH 0 0 0 eth0  
    192.168.0.100 * 255.255.255.255 UH 0 0 0 ppp0  
    192.168.10.0 * 255.255.255.0 U 0 0 0 eth0  
    192.168.122.0 * 255.255.255.0 U 0 0 0 virbr0  
    link-local * 255.255.0.0 U 1002 0 0 eth0  
    default 192.168.10.1 0.0.0.0 UG 0 0 0 eth0  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 設定預設路由

    ip route replace default dev ppp0

centos 7 上安裝的問題和解決辦法

  • 按照上述步驟安裝後,在vpn的客戶端可以ping 通外網地址,但是無法通過curl www.sina.com.cn 獲取內容,出現《失敗:沒有到主機的路由》
  • 解決辦法 
    1. 在server端 關閉 firewalld 。
    2. 在server端 指定路由,並且放在 /etc/rc.d/rc.local裡面隨著系統啟動 

      #vpn server 的路由定義 
      iptables -t nat -A POSTROUTING -s 192.168.9.0/24 -o eth1 -j MASQUERADE 
    3. 在客戶端這裡 配置隨系統啟動的自動撥號和增加預設路由的命令。也是放在/etc/rc.d/rc.local裡面 

      #自動撥號vpn 
      pppd call vpn 
      sleep 2 
      ip route replace default dev ppp0 

      應該有更好的方法來處理這個問題,但是一直沒有找到。特別是centos 7 上面使用了firewalld 來取代了 iptables