openvpn
时间:2010-06-24 来源:pk-feiyang
d获取并安装Openvpn:
首先检查系统是否安装lzo实时压缩工具
$rpm -qa | grep lzo
如果没有安装可以在http://www.oberhumer.com/opensource/lzo/找到并安装,安装方法详见压缩包中的 INSTALL文件,当然也可以用rpm包安装,记住一定要安装lzo-devel开头的那个包,因为OpenVPN需要使用lzo的头文件。
wget http://down1.chinaunix.net/distfiles/lzo-2.02.tar.gz
tar -xzvf lzo-2.02.tar.gz
cd lzo-2.02
./configure
make
make install
###$wget http://mesh.dl.sourceforge.net/sourceforge/openvpn/openvpn-2.0_rc16.tar.gz
//与lzo结合有问题
##$tar -zxvf openvpn-2.0_rc16.tar.gz
##$cd openvpn-2.0_rc16
wget http://openvpn.net/release/openvpn-2.1.1.tar.gz
$./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指令获得