1. 程式人生 > >openvpn連線後無法上網

openvpn連線後無法上網

安裝配置參照基礎流程,之後會出現vpn可以連線,但是卻無法上網。糾結了一天。

原因是路由轉發的問題:

就是把iptables的轉發規則中的 -o eth0 去掉,即:
iptables -t nat -A POSTROUTING -s 10.8.0.0/24  -j MASQUERADE
然後
/etc/init.d/iptables save
還不行就清空所有規則
iptables -F , iptables -X iptables -Z, 然後iptables -t nat -A POSTROUTING -s 10.8.0.0/24  -j MASQUERADE, /etc/init.d/iptables save
搞定!

你可以從 EPEL倉庫獲取OpenVPN. 如果你沒有安裝過這個支援,你需要先做如下步驟:

  1. wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm  
  2. rpm -Uvh epel-release-6*.rpm  

安裝 OpenVPN:

  1. yum -y install openvpn easy-rsa  

如果你上面步驟出錯可以嘗試如下方案之後再重新嘗試安裝:

執行命令來升級repo 以便可以使用 HTTP 而不是 HTTPS:

  1. sudo sed -i "s/mirrorlist=https/mirrorlist=http/" /etc/yum.repos.d/epel.repo  

然後確認一下更新:
  1. yum check-update  

找到資料夾 /etc/openvpn 建立一個 server.conf, 把下面內容貼上進來就好了(如果你熟悉vim你也可以vim/etc/openvpn/server.conf):

  1. port 1194  
  2. proto udp  
  3. dev tun  
  4. ca ca.crt  
  5. cert server.crt  
  6. key server.key  
  7. dh dh2048.pem  
  8. server 10.8.0.0 255.255.255.0  
  9. ifconfig-pool-persist ipp.txt  
  10. push "redirect-gateway def1 bypass-dhcp"  
  11. push "dhcp-option DNS 8.8.8.8"  
  12. push "dhcp-option DNS 8.8.4.4"  
  13. keepalive 10 120  
  14. comp-lzo  
  15. max-clients 100  
  16. user nobody  
  17. group nobody  
  18. persist-key  
  19. persist-tun  
  20. status /var/log/openvpn-status.log  
  21. log-append /var/log/openvpn.log  
  22. verb 3  

現在來建立一個伺服器證書. 
  1. mkdir -p /etc/openvpn/easy-rsa/keys  
  2. cp -r /usr/share/easy-rsa/2.0/* /etc/openvpn/easy-rsa/  
  3. cd /etc/openvpn/easy-rsa  
  4. source ./vars  
  5. ./clean-all  
  6. ./build-ca  
  7. ./build-key-server server  
  8. ./build-dh  

所有的選項讓它預設就好如果有出現 [y/n] 的詢問就輸入 y 然後回車.

下一步我們來做一個客戶端的證書:

  1. ./build-key client1  

Everything is similar. If you want multiple clients, the operation is repeated several times, changing client1 on client2, client3 and so on.

如果你想做多個客戶端認證參考這個步驟就好了比如想要做client2, client3 按照client1的步驟就可以。

拷貝所有的key:

  1. cd keys/  
  2. cp dh2048.pem ca.crt server.crt server.key /etc/openvpn  

修改一下 /etc/sysctl.conf 檔案裡面的IP Forwarding

  1. net.ipv4.ip_forward = 0

改為

  1. net.ipv4.ip_forward = 1

把設定好的東西提交一下:

  1. sysctl -p  

編輯 Iptables, SERVER-IP 這邊記住要替換成你自己的伺服器 IP 地址.

  1. iptables -t nat -A POSTROUTING -o venet0 -j SNAT --to SERVER-IP  
  2. iptables -A FORWARD -i venet0 -o tun0 -m state --state RELATED,ESTABLISHED -j ACCEPT  
  3. iptables -A FORWARD -i tun0 -o venet0 -j ACCEPT  

上面其實就完成了所有 VPS OpenVZ的設定, 但是要做一個獨立執行OpenVPN伺服器還需要一下步驟:

  1. iptables -A FORWARD -s 10.8.0.0/24 -j ACCEPT  
  2. iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT  
  3. iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE  (有問題)

儲存設定然後重啟iptables: 

  1. service iptables save  
  2. service iptables restart  

開啟OpenVPN 伺服器 然後讓它開機啟動: 

  1. service openvpn start  
  2. chkconfig openvpn on  

以上服務端的設定就完成了,我們現在來配置客戶端

你可以下載Openvpn的客戶端軟體再這裡下載對應的版本就好了 the official site

然後從伺服器上拷貝如下三個檔案:

  1. /etc/openvpn/ca.crt  
  2. /etc/openvpn/easy-rsa/keys/client1.crt   
  3. /etc/openvpn/easy-rsa/keys/client1.key  

到你本地的電腦上(window電腦在安裝好OpenVPN軟體後可以把如上證書拷貝到如下資料夾裡C:\Program Files\OpenVPN\config 

然後在這個資料夾建立一個 client.ovpn 檔案內如如下, 其他不用改只要把SERVER-IP 改成你伺服器的 IP  

  1. client  
  2. dev tun  
  3. proto udp  
  4. remote SERVER-IP 1194  
  5. resolv-retry infinite  
  6. nobind  
  7. persist-key  
  8. persist-tun  
  9. ca ca.crt  
  10. cert client1.crt  
  11. key client1.key  
  12. ns-cert-type server  
  13. comp-lzo  
  14. verb 3  
  15. sndbuf 0  
  16. rcvbuf 0