在 Ubuntu 13.10 服務器上安裝和配置 OpenVPN 服務器


虛擬專用網絡 ((((虛擬專用網絡) 是一種創建安全網絡連接的網絡技術 通過公共網絡 互聯網等

打開VPN 開源 虛擬專用網絡 詹姆斯喬南的惡魔。 打開VPN 強大而靈活 VPN 守護進程.. 打開VPN 支持 SSL / TLS 安全性, 以太網橋接, TCPUDP 隧道 由代理交付或 網絡地址解讀它支持動態 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

生成 DiffieHellman 參數

輸入以下命令生成 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 客戶端 在你的電腦。