虛擬專用網絡 (虛擬專用網絡) 是一種創建安全網絡連接的網絡技術。 通過公共網絡 比如互聯網。
打開VPN 它是開源的 虛擬專用網絡 詹姆斯·約南的惡魔。 打開VPN 強大而靈活 VPN 守護程序. 打開VPN 支持 SSL/TLS 安全性, 以太網橋接, TCP 還 UDP 隧道 機構或 網絡地址解讀支持動態 IP 地址和 DHCP,可擴展到數十萬用戶,並可移植到大多數主要操作系統平台。
本教程中解釋了這些步驟 OpenVPN 服務器設置和配置 什麼時候 客戶 存在 Ubuntu 13.10 用於遠程訪問。
在 Ubuntu 13.10 上安裝 OpenVpn 服務器
要在終端上安裝 openvpn,請鍵入:
$ sudo apt-get install openvpn easy-rsa -y
從 openvpn 服務器複製所需的密鑰
要設置您自己的證書頒發機構 (CA) 並為您的 OpenVPN 服務器和多個客戶端生成證書和密鑰,首先將 easy-rsa 目錄複製到 /etc/openvpn。
$ mkdir /etc/openvpn/easy-rsa $ cp -rf /usr/share/easy-rsa/* /etc/openvpn/easy-rsa/
編輯 /etc/openvpn/easy-rsa/vars:
$ vi /etc/openvpn/easy-rsa/vars
還要更改值以匹配您的國家、州、城市、電子郵件 ID 等。
... export KEY_COUNTRY="BA" export KEY_PROVINCE="RS" export KEY_CITY="Banjaluka" export KEY_ORG="Lin Tut" export KEY_EMAIL="[email protected]" export KEY_CN=MyVPN export KEY_NAME=MyVPN export KEY_OU=MyVPN ...
通過鍵入以下內容為主要證書頒發機構 (CA) 生成證書和密鑰:
$ cd /etc/openvpn/easy-rsa/ $ cp openssl-1.0.0.cnf openssl.cnf $ source vars $ ./clean-all
通過運行以下命令生成 CA 證書和 CA 密鑰:
$ ./build-ca
示例輸出:
$ ./build-ca Generating a 1024 bit RSA private key ...............++++++ ...++++++ writing new private key to 'ca.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [BA]:----> Press Enter State or Province Name (full name) [RS]:----> Press Enter Locality Name (eg, city) [Banjaluka]:----> Press Enter Organization Name (eg, company) [Lint Tut]:----> Press Enter Organizational Unit Name (eg, section) [MyVPN]:----> Press Enter Common Name (eg, your name or your server's hostname) [MyVPN]:----> Press Enter Name [MyVPN]:----> Press Enter Email Address [[email protected]]:----> Press Enter
服務器證書
接下來,為您的服務器生成證書和私鑰。
$ ./build-key-server server
示例輸出:
./build-key-server server Generating a 1024 bit RSA private key ................++++++ ..........++++++ writing new private key to 'server.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [BA]:----> Press Enter State or Province Name (full name) [RS]:----> Press Enter Locality Name (eg, city) [Banjaluka]:----> Press Enter Organization Name (eg, company) [Lint Tut]:----> Press Enter Organizational Unit Name (eg, section) [MyVPN]:----> Press Enter Common Name (eg, your name or your server's hostname) [server]:----> Press Enter Name [MyVPN]:----> Press Enter Email Address [[email protected]]:----> Press Enter . Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:----> Press Enter An optional company name []:----> Press Enter Using configuration from /etc/openvpn/easy-rsa/openssl-1.0.0.cnf Check that the request matches the signature Signature ok The Subject's Distinguished Name is as follows countryName :PRINTABLE:'BA' stateOrProvinceName :PRINTABLE:'RS' localityName :PRINTABLE:'Banjaluka' organizationName :PRINTABLE:'Lint Tut' organizationalUnitName:PRINTABLE:'MyVPN' commonName :PRINTABLE:'server' name :PRINTABLE:'MyVPN' emailAddress :IA5STRING:'[email protected]' Certificate is to be certified until Mar 31 21:56:13 2024 GMT (3650 days) Sign the certificate? [y/n]:y ----> Type Y and Press Enter . 1 out of 1 certificate requests certified, commit? [y/n]y ----> Type Y and Press Enter Write out database with 1 new entries Data Base Updated
客戶證書
這個 VPN客戶端 還需要一個 驗證我的證書 到達 服務器通常,您會為每個客戶端創建不同的證書。要在終端中以 root 身份創建證書,請鍵入:
$ ./build-key client
生成 Diffie Hellman 參數
以下命令生成 DH 參數。
$ ./build-dh
進入/etc/openvpn/easy-rsa/keys/目錄,輸入如下命令,將上述文件傳送到/etc/openvpn/目錄下。
$ cd /etc/openvpn/easy-rsa/keys/ $ cp dh1024.pem ca.crt server.crt server.key /etc/openvpn/
接下來,我們需要將所有客戶端證書和密鑰複製到遠程 VPN 客戶端,以便對 VPN 服務器進行身份驗證。在這種情況下,我只為一個客戶端生成了證書和密鑰,因此我不得不將以下文件複製到 VPN 客戶端:
ca.crt 客戶端.crt 客戶端.key
上述文件應安全複製到您的 VPN 客戶端。複製密鑰時要小心。如果有人可以訪問您的密鑰,他們可以輕鬆破解它並獲得對您的虛擬專用網絡的完全訪問權限。
VPN服務器配置
拷貝文件 服務器配置文件 歸檔到 /etc/openvpn/ 目錄。
$ cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/
使用以下命令提取文件:
$ gzip -d /etc/openvpn/server.conf.gz
編輯文件 服務器配置文件,
$ vi /etc/openvpn/server.conf
找到並取消註釋以下行以通過 OpenVPN 服務器路由客戶端系統流量。
[...] push "redirect-gateway def1 bypass-dhcp" [...]
同時取消註釋並更改 DNS 服務器以反映您自己的 DNS 值。我們在這裡使用 Google 公共 DNS 服務器。
[...] push "dhcp-option DNS 208.67.222.222" push "dhcp-option DNS 208.67.220.220" [...]
取消註釋以下行。
[...] user nobody group nogroup [...]
保存並關閉文件。
接下來,您需要復制和編輯 client.conf 文件。您需要將此文件傳輸到您的 VPN 客戶端。首先,將文件複製到某處(例如 /家庭 目錄)。
$ cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf /home/
編輯文件 客戶端配置文件,
$ vi /home/client.conf
設置 VPN 服務器的主機名/IP 地址。
# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
remote 192.168.1.5 1194 # Enter IP on your OpenVPN server
配置 IP 轉發和路由
編輯 配置文件 文檔,
$ vi /etc/sysctl.conf
找到以下行並設置值“1” 啟用 IP 轉發。
# Controls IP packet forwarding net.ipv4.ip_forward = 1
運行以下命令申請 系統控制 種類。
$ sysctl -p
使用以下命令啟用 IP 轉發:
$ echo 1 > /proc/sys/net/ipv4/ip_forward
調整 iptables 以通過 VPN 正確轉發流量。
輸入以下命令通過OpenVPN一一轉發流量。
$ iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT $ iptables -A FORWARD -s 10.8.0.0/24 -j ACCEPT $ iptables -A FORWARD -j REJECT $ iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
安裝 iptables-persistent 並保存您的 iptables 規則。
$ apt-get install iptables-persistent
接受默認值並保存 iptables 規則。
如果您使用的是 UFW,則需要通過運行以下命令來允許 openvpn 端口 1194:
$ ufw allow 1194/udp $ ufw allow 1194/tcp
最後,啟動 openvpn 服務,並使用以下命令在每次重新啟動時自動啟動:
$ service openvpn start
驗證 VPN 接口 (tun0) 是使用 ifconfig 命令創建的。
$ ifconfig
示例輸出:

配置 OpenVPN 客戶端
最後,讓我們創建 server.ovpn 配置文件。為方便起見,您可以使用記事本(或任何其他簡單的文本編輯器工具)在本地計算機上創建它。在該文件中輸入以下內容:
client dev tun proto udp remote 192.168.1.5 1194 # - Your server IP and OpenVPN Port resolv-retry infinite nobind tun-mtu 1500 tun-mtu-extra 32 mssfix 1450 persist-key persist-tun ca ca.crt auth-user-pass comp-lzo reneg-sec 0 verb 3
然後使用 .ovpn 擴展名保存它。將此文件保存在安裝位置的 config 目錄中 OpenVPN客戶端 在你的電腦。