本文是在Fedora Core 2環境下配置的,由于機器在網絡中不是在網關的位置,是以使用NAT方式來通路内網,否則還涉及到配置網絡裝置的路由。假設我的××× 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
如果沒有安裝可以在[url]http://www.oberhumer.com/opensource/lzo/[/url]找到并安裝,安裝方法詳見壓縮包中的INSTALL檔案,當然也可以用rpm包安裝,記住一定要安裝lzo-devel開頭的那個包,因為Open×××需要使用lzo的頭檔案。
$wget [url]http://mesh.dl.sourceforge.net/sourceforge/openvpn/openvpn-2.0_rc16.tar.gz[/url]
$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了,為了友善,我們使用Open×××包自帶的腳本生成。
#mkdir /etc/openvpn
#cp -r easy-rsa /etc/openvpn #切換到Open×××源代碼目錄執行
修改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
# Open××× package upgrade.
# This variable should point to
# the top level of the easy-rsa
# tree.
export D=`pwd`
# 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 Open××× ORG"
# 你的機關
export KEY_OU="Open××× Service"
# 你的郵件位址
export KEY_EMAIL="[email][email protected][/email]"
-------------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 Open××× ORG]:
Organizational Unit Name (eg, section) [Open××× Service]:
Common Name (eg, your name or your server's hostname) []:Open××× Root CA
Email Address [[email][email protected][/email]]:
# 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
..+..............................................................+....................................................................+....+........+.........+....................................................+.+..................................................................................................................................................................+.......................................+.................................+.............+.................................................................................+.......................................................+.............................++*++*++*
建立并簽發××× Server使用的CA
# ./build-key-server server # server 為建立後的檔案名,分别為server.crt server.key
......................++++++
...............++++++
writing new private key to 'server.key'
Country Name (2 letter code) [CN]:
Common Name (eg, your name or your server's hostname) []:Server No.1
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 Open××× ORG'
organizationalUnitName:PRINTABLE:'Open××× Service'
commonName :PRINTABLE:'Server No.1'
emailAddress :IA5STRING:'[email][email protected][/email]'
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
接下來為××× Client頒發CA憑證,如果以後要為其他Client頒發證書,直接使用build-key指令簽發新證書。
# ./build-key elm
........++++++
....................++++++
writing new private key to 'elm.key'
Common Name (eg, your name or your server's hostname) []:ELM #注意Common Name最好不要相同,如果相同[server要加duplicate-cn選項],那麼Email位址也不能相同
commonName :PRINTABLE:'ELM'
Certificate is to be certified until Feb 26 14:45:36 2015 GMT (3650 days)
為防止惡意攻擊(如DOS、UDP port flooding),我們生成一個"HMAC firewall"
#openvpn --genkey --secret keys/ta.key
生成證書吊銷鍊檔案,防止日後有人丢失證書,被非法使用者接入×××
#./make-crl vpncrl.pem
到現在為止,一切準備就緒,下面開始寫配置檔案,為了縮小篇幅,把原有注釋都去掉了。
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
#Open×××使用的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
#這是一條指令的合集,如果你是Open×××的老使用者,就知道這條指令的來由
#這條指令等效于:
# mode server #Open×××工作在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 #說明Open×××使用的位址池(用于配置設定給客戶),分别是起始位址、結束位址
# 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
#通過××× 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"
#×××啟動後,在××× Server上增加的路由,×××停止後自動删除
;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的預設網關指向×××,讓Client的所有Traffic都通過×××走
;push "redirect-gateway"
#DHCP的一些選項,具體檢視Manual
;push "dhcp-option DNS 10.8.0.1"
;push "dhcp-option WINS 10.8.0.1"
#如果可以讓××× Client之間互相通路直接通過openvpn程式轉發,
#不用發送到tun或者tap裝置後重新轉發,優化Client to Client的通路效率
client-to-client
#如果Client使用的CA的Common Name有重複了,或者說客戶都使用相同的CA
#和keys連接配接×××,一定要打開這個選項,否則隻允許一個人連接配接×××
;duplicate-cn
#NAT後面使用×××,如果×××長時間不通信,NAT Session可能會失效,
#導緻×××連接配接丢失,為防止之類事情的發生,keepalive提供一個類似于ping的機制,
#下面表示每10秒通過×××的Control通道ping對方,如果連續120秒無法ping通,
#認為連接配接丢失,并重新啟動×××,重新連接配接
#(對于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檢測逾時後,重新啟動×××,不重新讀取keys,保留第一次使用的keys
persist-key
#通過keepalive檢測逾時後,重新啟動×××,一直保持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 .
建立Open×××啟動腳本,可以在源代碼目錄中找到,在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端一緻
# 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嚴格一緻
#設定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位址
# 這樣無需人為重新啟動,即可重新接入×××
resolv-retry infinite
# 在本機不邦定任何端口監聽incoming資料,Client無需此操作,除非一對一的×××有必要
nobind
# 運作openvpn使用者的身份,舊版本在win下需要把這兩行注釋掉,新版本無需此操作
#在Client端增加路由,使得所有通路内網的流量都經過×××出去
#當然也可以在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裝置
# 如果你使用HTTP代理連接配接××× 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 #]
# 對于無線裝置使用×××的配置,看看就明白了
# 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是同一個檔案
# easy-rsa/build-key生成的key pair檔案,上面生成key部分中有提到,不同客戶使用不同的keys修改以下兩行配置并使用他們的keys即可。
cert elm.crt
key elm.key
# Server使用build-key-server腳本什成的,在x509 v3擴充中加入了ns-cert-type選項
# 防止××× client使用他們的keys + DNS hack欺騙vpn client連接配接他們假冒的××× Server
# 因為他們的CA裡沒有這個擴充
ns-cert-type server
# 和Server配置裡一緻,ta.key也一緻,注意最後參數使用的是1
tls-auth ta.key 1
# 壓縮選項,和Server嚴格一緻
# Set log file verbosity.
verb 4
--------------Cut Here---------------------
Linux下Client的Open×××的安裝方法一樣,隻是配置檔案和keys上的不同,隻要把client.conf ca.crt elm.crt elm.key ta.key複制到/etc/openvpn目錄即可啟動×××。
Win下Open×××的安裝,WIN下有圖形界面的Open×××-GUI程式,下載下傳位址[url]http://openvpn.se[/url]
下載下傳安裝,預設下一步就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,Open××× Client可以通路内網了。