Windows XP Windows 7 Windows 2003 Windows Vista Windows教程綜合 Linux 系統教程
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟件教程
 Windows教程網 >> Linux系統教程 >> Linux教程 >> Linux下OpenVPN的建設詳細教程

Linux下OpenVPN的建設詳細教程

日期:2017/2/7 16:59:03      編輯:Linux教程
本文是在Fedora Core 2環境下配置的,由於機器在網絡中不是在網關的位置,所以使用NAT方式來訪問內網,否則還涉及到配置網絡設備的路由。假設我的VPN Server有2塊網卡,eth0對外,IP:61.1.1.2 eth1對內,IP:192.168.1.2,內網地址:192.168.0.0/16
本文除配置文件中行首的"#"是注釋外,其他行首的"#"都是提示符,如果在非配置文件一行中第二次出現"#"說明後面的是注釋,書寫命令時可以省略。

獲取並安裝Openvpn:
首先檢查系統是否安裝lzo實時壓縮工具
$rpm -qa | grep lzo
如果沒有安裝可以在http://www.oberhumer.com/opensource/lzo/找到並安裝,安裝方法詳見壓縮包中的INSTALL文件,當然也可以用rpm包安裝,記住一定要安裝lzo-devel開頭的那個包,因為OpenVPN需要使用lzo的頭文件。
$wget http://mesh.dl.sourceforge.net/sourceforge/openvpn/openvpn-2.0_rc16.tar.gz
$tar -zxvf openvpn-2.0_rc16.tar.gz
$cd openvpn-2.0_rc16
$./configure
$make
$su
#make install
按照INSTALL文件中的說明,做如下操作:
#mknod /dev/net/tun c 10 200 #創建一個tun設備
#echo "alias char-major-10-200 tun" >> /etc/modprobe.conf
#echo 1 > /proc/sys/net/ipv4/ip_forward #打開系統的轉發功能
接下來就生成服務器客戶端需要使用的keys了,為了方便,我們使用OpenVPN包自帶的腳本生成。
#mkdir /etc/openvpn
#cp -r easy-rsa /etc/openvpn #切換到OpenVPN源代碼目錄執行
修改vars 文件
-------------CUT Here-------------
# easy-rsa parameter settings

# NOTE: If you installed from an RPM,
# don't edit this file in place in
# /usr/share/openvpn/easy-rsa --
# instead, you should copy the whole
# easy-rsa directory to another location
# (such as /etc/openvpn) so that your
# edits will not be wiped out by a future
# OpenVPN package upgrade.

# This variable should point to
# the top level of the easy-rsa
# tree.
export D=`pwd`

# This variable should point to
# the openssl.cnf file included
# with easy-rsa.
export KEY_CONFIG=$D/openssl.cnf

# Edit this variable to point to
# your soon-to-be-created key
# directory.
#
# WARNING: clean-all will do
# a rm -rf on this directory
# so make sure you define
# it correctly!
export KEY_DIR=$D/keys

# Issue rm -rf warning
echo NOTE: when you run ./clean-all, I will be doing a rm -rf on $KEY_DIR

# Increase this to 2048 if you
# are paranoid. This will slow
# down TLS negotiation performance
# as well as the one-time DH parms
# generation process.
export KEY_SIZE=1024

# These are the default values for fields
# which will be placed in the certificate.
# Don't leave any of these fields blank.
# 定義你所在的國家,2個字符
export KEY_COUNTRY=CN
# 你所在的省份
export KEY_PROVINCE=Liaoning
# 你所在的城市
export KEY_CITY=Shenyang
# 你所在的組織
export KEY_ORG="ELM OpenVPN ORG"
# 你的單位
export KEY_OU="OpenVPN Service"
# 你的郵件地址
export KEY_EMAIL="[email protected]"
-------------CUT Here-----------------
修改後保存,下面我們開始什成keys,以下為shell命令 "#" 為提示符
#. vars #使修改的變量生效
NOTE: when you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/keys
#./clean-all #初始化keys目錄,創建所需要的文件和目錄
#./build-ca #什成Root CA證書,用於簽發Server和Client證書,請保護好keys/ca.key文件。
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) [CN]: #如果無需修改,直接回車
State or Province Name (full name) [Liaoning]:
Locality Name (eg, city) [Shenyang]:
Organization Name (eg, company) [ELM OpenVPN ORG]:
Organizational Unit Name (eg, section) [OpenVPN Service]:
Common Name (eg, your name or your server's hostname) []:OpenVPN Root CA
Email Address [[email protected]]:

# ls keys
ca.crt ca.key index.txt serial
我們可以看到ca.crt ca.key文件已經什成了。
下面我們為服務器生成 Diffie-Hellman 文件
# ./build-dh #TLS server 需要使用的一個文件
Generating DH parameters, 1024 bit long safe prime, generator 2
This is going to take a long time
..+..............................................................+....................................................................+..
創建並簽發VPN Server使用的CA
# ./build-key-server server # server 為創建後的文件名,分別為server.crt server.key
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) [CN]:
State or Province Name (full name) [Liaoning]:
Locality Name (eg, city) [Shenyang]:
Organization Name (eg, company) [ELM OpenVPN ORG]:
Organizational Unit Name (eg, section) [OpenVPN Service]:
Common Name (eg, your name or your server's hostname) []:Server No.1
Email Address [[email protected]]:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /etc/openvpn/easy-rsa/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName :PRINTABLE:'CN'
stateOrProvinceName :PRINTABLE:'Liaoning'
localityName :PRINTABLE:'Shenyang'
organizationName :PRINTABLE:'ELM OpenVPN ORG'
organizationalUnitName:PRINTABLE:'OpenVPN Service'
commonName :PRINTABLE:'Server No.1'
emailAddress :IA5STRING:'[email protected]'
Certificate is to be certified until Feb 26 14:43:44 2015 GMT (3650 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

接下來為VPN Client頒發CA證書,如果以後要為其他Client頒發證書,直接使用build-key命令簽發新證書。
# ./build-key elm
Generating a 1024 bit RSA private key
........++++++
....................++++++
writing new private key to 'elm.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) [CN]:
State or Province Name (full name) [Liaoning]:
Locality Name (eg, city) [Shenyang]:
Organization Name (eg, company) [ELM OpenVPN ORG]:
Organizational Unit Name (eg, section) [OpenVPN Service]:
Common Name (eg, your name or your server's hostname) []:ELM #注意Common Name最好不要相同,如果相同[server要加duplicate-cn選項],那麼Email地址也不能相同
Email Address [[email protected]]:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /etc/openvpn/easy-rsa/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName :PRINTABLE:'CN'
stateOrProvinceName :PRINTABLE:'Liaoning'
localityName :PRINTABLE:'Shenyang'
organizationName :PRINTABLE:'ELM OpenVPN ORG'
organizationalUnitName:PRINTABLE:'OpenVPN Service'
commonName :PRINTABLE:'ELM'
emailAddress :IA5STRING:'[email protected]'
Certificate is to be certified until Feb 26 14:45:36 2015 GMT (3650 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
為防止惡意攻擊(如DOS、UDP port flooding),我們生成一個"HMAC firewall"
#openvpn --genkey --secret keys/ta.key
生成證書吊銷鏈文件,防止日後有人丟失證書,被非法用戶接入VPN
#./make-crl vpncrl.pem
Using configuration from /etc/openvpn/easy-rsa/openssl.cnf

到現在為止,一切准備就緒,下面開始寫配置文件,為了縮小篇幅,把原有注釋都去掉了。
Server使用的配置文件server.conf
----------------CUT Here-------------
#申明本機使用的IP地址,也可以不說明
;local a.b.c.d
#申明使用的端口,默認1194
port 1194
#申明使用的協議,默認使用UDP,如果使用HTTP proxy,必須使用TCP協議
;proto tcp
proto udp
#申明使用的設備可選tap和tun,tap是二層設備,支持鏈路層協議。
#tun是ip層的點對點協議,限制稍微多一些,本人習慣使用TAP設備
dev tap
;dev tun
#OpenVPN使用的ROOT CA,使用build-ca生成的,用於驗證客戶是證書是否合法
ca ca.crt
#Server使用的證書文件
cert server.crt
#Server使用的證書對應的key,注意文件的權限,防止被盜
key server.key # This file should be kept secret
#CRL文件的申明,被吊銷的證書鏈,這些證書將無法登錄
crl-verify vpncrl.pem
#上面提到的生成的Diffie-Hellman文件
dh dh1024.pem
#這是一條命令的合集,如果你是OpenVPN的老用戶,就知道這條命令的來由
#這條命令等效於:
# mode server #OpenVPN工作在Server模式,可以支持多client同時動態接入
# tls-server #使用TLS加密傳輸,本端為Server,Client端為tls-client
#
# if dev tun: #如果使用tun設備,等效於以下配置
# ifconfig 10.8.0.1 10.8.0.2 #設置本地tun設備的地址
# ifconfig-pool 10.8.0.4 10.8.0.251 #說明OpenVPN使用的地址池(用於分配給客戶),分別是起始地址、結束地址
# route 10.8.0.0 255.255.255.0 #增加一條靜態路由,省略下一跳地址,下一跳為對端地址,這裡是: 10.8.0.2
# if client-to-client: #如果使用client-to-client這個選項
# push "route 10.8.0.0 255.255.255.0" #把這條路由發送給客戶端,客戶連接成功後自動加入路由表,省略了下一跳地址: 10.8.0.1
# else
# push "route 10.8.0.1" #否則發送本條路由,這是一個主機路由,省略了子網掩碼和下一跳地址,分別為: 255.255.255.255 10.8.0.1
#
# if dev tap: #如果使用tap設備,則等效於以下命令
# ifconfig 10.8.0.1 255.255.255.0 #配置tap設備的地址
# ifconfig-pool 10.8.0.2 10.8.0.254 255.255.255.0 #客戶端使用的地址池,分別是起始地址、結束地址、子網掩碼
# push "route-gateway 10.8.0.1" #把環境變量route-gateway傳遞給客戶機
#
server 10.8.0.0 255.255.255.0 #等效於以上命令
#用於記錄某個Client獲得的IP地址,類似於dhcpd.lease文件,
#防止openvpn重新啟動後“忘記”Client曾經使用過的IP地址
ifconfig-pool-persist ipp.txt
#Bridge狀態下類似DHCPD的配置,為客戶分配地址,由於這裡工作在路由模式,所以不使用
;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100
#通過VPN Server往Client push路由,client通過pull指令獲得Server push的所有選項並應用
;push "route 192.168.10.0 255.255.255.0"
;push "route 192.168.20.0 255.255.255.0"
#VPN啟動後,在VPN Server上增加的路由,VPN停止後自動刪除
;route 10.9.0.0 255.255.255.252
#Run script or shell command cmd to validate client
#virtual addresses or routes. 具體查看manual
;learn-address ./script
#其他的一些需要PUSH給Client的選項
#
#使Client的默認網關指向VPN,讓Client的所有Traffic都通過VPN走
;push "redirect-gateway"
#DHCP的一些選項,具體查看Manual
;push "dhcp-option DNS 10.8.0.1"
;push "dhcp-option WINS 10.8.0.1"
#如果可以讓VPN Client之間相互訪問直接通過openvpn程序轉發,
#不用發送到tun或者tap設備後重新轉發,優化Client to Client的訪問效率
client-to-client
#如果Client使用的CA的Common Name有重復了,或者說客戶都使用相同的CA
#和keys連接VPN,一定要打開這個選項,否則只允許一個人連接VPN
;duplicate-cn
#NAT後面使用VPN,如果VPN長時間不通信,NAT Session可能會失效,
#導致VPN連接丟失,為防止之類事情的發生,keepalive提供一個類似於ping的機制,
#下面表示每10秒通過VPN的Control通道ping對方,如果連續120秒無法ping通,
#認為連接丟失,並重新啟動VPN,重新連接
#(對於mode server模式下的openvpn不會重新連接)。
keepalive 10 120
#上面提到的HMAC防火牆,防止DOS攻擊,對於所有的控制信息,都使用HMAC signature,
#沒有HMAC signature的控制信息不予處理,注意server端後面的數字肯定使用0,client使用1
tls-auth ta.key 0 # This file is secret
#對數據進行壓縮,注意Server和Client一致
comp-lzo
#定義最大連接數
;max-clients 100
#定義運行openvpn的用戶
user nobody
group nobody
#通過keepalive檢測超時後,重新啟動VPN,不重新讀取keys,保留第一次使用的keys
persist-key
#通過keepalive檢測超時後,重新啟動VPN,一直保持tun或者tap設備是linkup的,
#否則網絡連接會先linkdown然後linkup
persist-tun
#定期把openvpn的一些狀態信息寫到文件中,以便自己寫程序計費或者進行其他操作
status openvpn-status.log
#記錄日志,每次重新啟動openvpn後刪除原有的log信息
log /var/log/openvpn.log
#和log一致,每次重新啟動openvpn後保留原有的log信息,新信息追加到文件最後
;log-append openvpn.log
#相當於debug level,具體查看manual
verb 3
--------------Cut Here-----------------
把server.conf文件保存到/etc/opennvpn目錄中,並把使用easy-rsa下的腳本什成的key都復制到/etc/openvpn目錄下,命令如下:
#cd /etc/openvpn
#cp easy-rsa/keys/ca.crt .
#cp easy-rsa/keys/server.crt .
#cp easy-rsa/keys/server.key .
#cp easy-rsa/keys/dh1024.pem .
#cp easy-rsa/keys/ta.key .
#cp easy-rsa/keys/vpncrl.pem .
創建OpenVPN啟動腳本,可以在源代碼目錄中找到,在sample-scripts目錄下的openvpn.init文件,將其復制到/etc/init.d/目錄中,改名為openvpn
然後運行:
#chkconfig --add openvpn
#chkconfig openvpn on
立即啟動openenvpn
#/etc/init.d/openvpn start

接下來配置客戶端的配置文件client.conf:
Linux或Unix下使用擴展名為.conf Windows下使用的是.ovpn,並把需要使用的keys復制到配置文件所在目錄ca.crt elm.crt elm.key ta.key
-------------Cut Here---------------------
# 申明我們是一個client,配置從server端pull過來,如IP地址,路由信息之類“Server使用push指令push過來的”
client

#指定接口的類型,嚴格和Server端一致
dev tap
;dev tun

# Windows needs the TAP-Win32 adapter name
# from the Network Connections panel
# if you have more than one. On XP SP2,
# you may need to disable the firewall
# for the TAP adapter.
;dev-node MyTap

# 使用的協議,與Server嚴格一致
;proto tcp
proto udp

#設置Server的IP地址和端口,如果有多台機器做負載均衡,可以多次出現remote關鍵字

remote 61.1.1.2 1194
;remote my-server-2 1194

# 隨機選擇一個Server連接,否則按照順序從上到下依次連接
;remote-random

# 始終重新解析Server的IP地址(如果remote後面跟的是域名),
# 保證Server IP地址是動態的使用DDNS動態更新DNS後,Client在自動重新連接時重新解析Server的IP地址
# 這樣無需人為重新啟動,即可重新接入VPN
resolv-retry infinite

# 在本機不邦定任何端口監聽incoming數據,Client無需此操作,除非一對一的VPN有必要
nobind

# 運行openvpn用戶的身份,舊版本在win下需要把這兩行注釋掉,新版本無需此操作
user nobody
group nobody

#在Client端增加路由,使得所有訪問內網的流量都經過VPN出去
#當然也可以在Server的配置文件裡頭設置,Server配置裡頭使用的命令是
# push "route 192.168.0.0 255.255.255.0"
route 192.168.0.0 255.255.0.0

# 和Server配置上的功能一樣如果使用了chroot或者su功能,最好打開下面2個選項,防止重新啟動後找不到keys文件,或者nobody用戶沒有權限啟動tun設備
persist-key
persist-tun

# 如果你使用HTTP代理連接VPN Server,把Proxy的IP地址和端口寫到下面
# 如果代理需要驗證,使用http-proxy server port [authfile] [auth-method]
# 其中authfile是一個2行的文本文件,用戶名和密碼各占一行,auth-method可以省略,詳細信息查看Manual
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]

# 對於無線設備使用VPN的配置,看看就明白了
# Wireless networks often produce a lot
# of duplicate packets. Set this flag
# to silence duplicate packet warnings.
;mute-replay-warnings

# Root CA 文件的文件名,用於驗證Server CA證書合法性,通過easy-rsa/build-ca生成的ca.crt,和Server配置裡的ca.crt是同一個文件
ca ca.crt
# easy-rsa/build-key生成的key pair文件,上面生成key部分中有提到,不同客戶使用不同的keys修改以下兩行配置並使用他們的keys即可。
cert elm.crt
key elm.key

# Server使用build-key-server腳本什成的,在x509 v3擴展中加入了ns-cert-type選項
# 防止VPN client使用他們的keys + DNS hack欺騙vpn client連接他們假冒的VPN Server
# 因為他們的CA裡沒有這個擴展
ns-cert-type server

# 和Server配置裡一致,ta.key也一致,注意最後參數使用的是1
tls-auth ta.key 1

# 壓縮選項,和Server嚴格一致
comp-lzo

# Set log file verbosity.
verb 4
--------------Cut Here---------------------
Linux下Client的OpenVPN的安裝方法一樣,只是配置文件和keys上的不同,只要把client.conf ca.crt elm.crt elm.key ta.key復制到/etc/openvpn目錄即可啟動VPN。
Win下OpenVPN的安裝,WIN下有圖形界面的OpenVPN-GUI程序,下載地址http://openvpn.se
下載安裝,默認下一步就OK了,安裝完事後在托盤上出現一個新的圖標,把client.ovpn ca.crt elm.crt elm.key ta.key文件拷貝到C:\Program Files\openvpn\config目錄下,然後點右鍵connect就OK了。
對於LinuxServer使用NAT的一些說明:
首先要把系統的Forward打開,可以通過如下命令實現
echo 1 > /proc/sys/net/ipv4/ip_forward
或者使用
sysctl -w net.ipv4.ip_forward=1
或者修改/etc/sysctl.conf文件,增加
net.ipv4.ip_forward = 1
設定SNAT的規則,使用iptables命令
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth1 -j SNAT --to-source 192.168.1.2
OK,OpenVPN Client可以訪問內網了。
Copyright © Windows教程網 All Rights Reserved