openvpn連線後無法上網
阿新 • • 發佈:2019-02-12
安裝配置參照基礎流程,之後會出現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. 如果你沒有安裝過這個支援,你需要先做如下步驟:
- wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
- rpm -Uvh epel-release-6*.rpm
安裝 OpenVPN:
- yum -y install openvpn easy-rsa
如果你上面步驟出錯可以嘗試如下方案之後再重新嘗試安裝:
執行命令來升級repo 以便可以使用 HTTP 而不是 HTTPS:
-
sudo sed -i "s/mirrorlist=https/mirrorlist=http/" /etc/yum.repos.d/epel.repo
然後確認一下更新:
- yum check-update
找到資料夾 /etc/openvpn 建立一個 server.conf, 把下面內容貼上進來就好了(如果你熟悉vim你也可以vim/etc/openvpn/server.conf):
- port 1194
- proto udp
- dev tun
- ca ca.crt
- cert server.crt
- key server.key
- dh dh2048.pem
- server 10.8.0.0 255.255.255.0
- ifconfig-pool-persist ipp.txt
-
push "redirect-gateway def1 bypass-dhcp"
- push "dhcp-option DNS 8.8.8.8"
- push "dhcp-option DNS 8.8.4.4"
- keepalive 10 120
- comp-lzo
- max-clients 100
- user nobody
- group nobody
- persist-key
- persist-tun
- status /var/log/openvpn-status.log
- log-append /var/log/openvpn.log
- verb 3
現在來建立一個伺服器證書.
- mkdir -p /etc/openvpn/easy-rsa/keys
- cp -r /usr/share/easy-rsa/2.0/* /etc/openvpn/easy-rsa/
- cd /etc/openvpn/easy-rsa
- source ./vars
- ./clean-all
- ./build-ca
- ./build-key-server server
- ./build-dh
所有的選項讓它預設就好如果有出現 [y/n] 的詢問就輸入 y 然後回車.
下一步我們來做一個客戶端的證書:
- ./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:
- cd keys/
- cp dh2048.pem ca.crt server.crt server.key /etc/openvpn
修改一下 /etc/sysctl.conf 檔案裡面的IP Forwarding
- net.ipv4.ip_forward = 0
改為
- net.ipv4.ip_forward = 1
把設定好的東西提交一下:
- sysctl -p
編輯 Iptables, SERVER-IP 這邊記住要替換成你自己的伺服器 IP 地址.
- iptables -t nat -A POSTROUTING -o venet0 -j SNAT --to SERVER-IP
- iptables -A FORWARD -i venet0 -o tun0 -m state --state RELATED,ESTABLISHED -j ACCEPT
- iptables -A FORWARD -i tun0 -o venet0 -j ACCEPT
上面其實就完成了所有 VPS OpenVZ的設定, 但是要做一個獨立執行OpenVPN伺服器還需要一下步驟:
- iptables -A FORWARD -s 10.8.0.0/24 -j ACCEPT
- iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
- iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE (有問題)
儲存設定然後重啟iptables:
- service iptables save
- service iptables restart
開啟OpenVPN 伺服器 然後讓它開機啟動:
- service openvpn start
- chkconfig openvpn on
以上服務端的設定就完成了,我們現在來配置客戶端
你可以下載Openvpn的客戶端軟體再這裡下載對應的版本就好了 the official site,
然後從伺服器上拷貝如下三個檔案:
- /etc/openvpn/ca.crt
- /etc/openvpn/easy-rsa/keys/client1.crt
- /etc/openvpn/easy-rsa/keys/client1.key
到你本地的電腦上(window電腦在安裝好OpenVPN軟體後可以把如上證書拷貝到如下資料夾裡C:\Program Files\OpenVPN\config )
然後在這個資料夾建立一個 client.ovpn 檔案內如如下, 其他不用改只要把SERVER-IP 改成你伺服器的 IP
- client
- dev tun
- proto udp
- remote SERVER-IP 1194
- resolv-retry infinite
- nobind
- persist-key
- persist-tun
- ca ca.crt
- cert client1.crt
- key client1.key
- ns-cert-type server
- comp-lzo
- verb 3
- sndbuf 0
- rcvbuf 0