在Debian5.0.2下源代码安装OpenVPN笔记 [使用Mysql+PAM认证] 第二版

来源:互联网 发布:数据库 大数据 排序 编辑:程序博客网 时间:2024/04/27 17:09


作者:何祖彬[RobinHe] Mail:zubin.he@gmail.com
BLOG: http://blog.csdn.net/zubin006
始于2009年9月5日

版本号:OpenVPN-V2.0-20090905,2009年9月5日首版 2009年9月16日更新至第二版


转载请注明出处,本文也是采用两篇网友的大部分内容,因此转载时,请也将下面“参考文章”的出处也一并加入!谢谢!
本文中难免有不足、遗漏、错别字和句子不通之处,如发现、或者有更新改变之处,请与我联系以及时修正和完善!

切记:你的OpenVPN的Server端选用的版号要跟Client要一致,否则就会造成Client端能正常连接,但是获取不到gateway,我栽了跟头。

系统信息:
OS : Debian 5.0.2
在第二版新增功能中我是使用Ubuntu 9.04 Server版

使用说明:
绿色加粗字体的绝大部分是输入的命令和系统输出显示的结果。

关键词:
VPN
OpenVPN
SSL
OpenSSL
MySql
PAM
pam_mysql
证书

参考文章:
Ubuntu下openvpn源码安装:作者:ileo
http://www.linuxidc.com/Linux/2008-09/15672.htm

OpenVNP Server在Linux系统安装配置HOWTO:作者:郭瑞[china.guorui@gmail.com]
http://life.chinaunix.net/bbsfile/month_0907/20090728_cde3783a7b6132013264eYUWNKnnSrdx.pdf

Linux 下 OpenVPN 安装和 Windows OpenVPN GUI 安装笔记
http://www.xiaohui.com/dev/server/20070514-install-openvpn.htm

OpenVPN服务器搭建详解
http://www.linuxha.cn/openvpn_learn_all.htm

一,下载所需的软件:
1,安装所需的编译工具:
#apt-get install gcc g++ make pkg-configlibpam0g-dev sasl2-bin

2,下载lzo库[http://www.oberhumer.com/opensource/lzo/]:
#mkdir /usr/local/src/openvpn
#cd /usr/local/src/openvpn
#wgethttp://www.oberhumer.com/opensource/lzo/download/lzo-2.03.tar.gz

说明:lzo是一个实用的无损压缩工具。[可选]

3,下载openssl[http://www.openssl.org/]:
#wgethttp://www.openssl.org/source/openssl-0.9.8k.tar.gz

说明:OpenVPN依赖OpenSSL库,用于加密,需要安装。

4,下载openvpn[http://www.openvpn.net/]:
#wgethttp://www.openvpn.net/release/openvpn-2.0.9.tar.gz

最新的rc版
#wget http://www.openvpn.net/release/openvpn-2.1_rc19.tar.gz


二,安装OpenVPN及相关软件:
1,安装lzo:
#tar -zxvf lzo-2.03.tar.gz
#cd lzo-2.03
#./configure --prefix=/usr/local/lzo
#make
#make install

在/etc/ld.so.conf中加入以下内容:
/lib
/usr/lib
/usr/local/lib
/usr/local/lzo/lib

/etc/ld.so.conf的结果:
#vi /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf
/lib
/usr/lib
/usr/local/lib
/usr/local/lzo/lib

使用/etc/ld.so.conf中的内容生效,即动态连接库生效:
#ldconfig

2,安装openssl:
#cd ..
#tar -zxvf openssl-0.9.8k.tar.gz
#cd openssl-0.9.8k
#./config --prefix=/usr/local/openssl
#make
#make install

3,安装openvpn:
#cd ..
#tar -zxvf openvpn-2.0.9.tar.gz
#cd openvpn-2.0.9
#./configure --prefix=/usr/local/openvpn--with-lzo-headers=/usr/local/lzo/include--with-lzo-lib=/usr/local/lzo/lib--with-ssl-headers=/usr/local/openssl/include--with-ssl-lib=/usr/local/openssl/lib


#make
#make install


三,配置OpenVPN Server
1,创建配置环境
# mkdir -p /usr/local/openvpn/etc
# cp -R /usr/local/src/openvpn/openvpn-2.0.9/easy-rsa/usr/local/openvpn/etc/.


#cd /usr/local/openvpn/etc/easy-rsa/2.0
#ls


结果是程序以及脚本,这里简要的说明一下:
vars                脚本,是用来创建环境变量,设置所需要的变量的脚本
clean-all           脚本,是创建生成ca证书及密钥文件所需要的文件及目录
build-ca            脚本,生成ca证书(交互)
build-dh            脚本,生成Diff-Hellman文件(交互)
build-key-server    脚本,生成服务器端密钥(交互)
build-key           脚本,生成客户端密钥(交互)
pkitool             脚本,直接使用vars的环境变量设置,直接生成证书(非交互)

2,生成CA证书及密钥[注意字符输入不要出错]
初始化系统环境变量,你可以看一下这个脚本的内容就知道它在干什么了:
#./vars
NOTE: If you run ./clean-all, I will be doing a rm -rf on/usr/local/openvpn/etc/easy-rsa/2.0/keys


请理解警告的意思。

#chmod +rwx *
#source ./vars


生成并初始keys文件夹
#./clean-all

修改vars文件,内容如下:
export KEY_COUNTRY="CN"
export KEY_PROVINCE="FuJian"
export KEY_CITY="PuTian"
export KEY_ORG="ITCHT"
export KEY_EMAIL="zubin@itcht.com"

修改完保存后,即可生成ca证书及密钥文件了:
#source ./vars

生成Root Ca证书, 用于签发Server和Client证书
#./build-ca
# ls keys

可以看到已经生成了ca.crt ca.key文件
生成Diffie-Hellman文件
#./build-dh
#ls -l keys/dh1024.pem

可以看到生成了1024位的Diffie-Hellman文件
生成服务器使用的VPN server Ca证书
#./build-key-server itcht-server
根据提示输入相关信息,
itcht-server是你为CA证书起的一个名字, 以server名字为例,生成的服务器使用的CA证书文件为:itcht-server.crt itcht-server.key
将生成的CA证书及密钥拷贝到/usr/local/openvpn/etc下:

#cp keys/ca.* /usr/local/openvpn/etc/.
#cp keys/itcht-server.* /usr/local/openvpn/etc/.
#cp keys/dh1024.pem /usr/local/openvpn/etc/.


生成客户端CA证书及密钥
生成客户端CA证书及密钥使用:build-key程序即可
#./build-key itcht-user1
根据提示输入相关信息,
将在keys目录下生成itcht-user1.crt itcht-user1.csr itcht-user1.key三个客户端证书
将ca.crt ca.key itcht-user1.crt itcht-user1.csritcht-user1.key五个文件打包,以备客户端vpn使用

#mkdir itcht-user1-key
#cp keys/ca.* ./itcht-user1-key/.
#cp keys/itcht-user1.* ./itcht-user1-key/.

#tar -czvf itcht-user1-key.tar.gz itcht-user1-key
上面这件打包文件要COPY到客户端时使用

生成openvpn配置文件
创建openvpn 配置文件最好的方法是先看openvpn 的样例文件,在源码目录下的sample-config-files下,本例为
/usr/local/src/openvpn/openvpn-2.0.9/sample-config-files
服务器端配置文件名: server.conf
客户端配置文件名为: client.conf
可以根据需要修改.
#cp/usr/local/src/openvpn/openvpn-2.0.9/sample-config-files/server.conf/usr/local/openvpn/etc/openvpn.conf
#mkdir -p /usr/local/openvpn/logs
#groupadd nobody
#vi/usr/local/openvpn/etc/openvpn.conf


#################################################
# Sample OpenVPN 2.0 config file for            #
# multi-client server.                          #
#                                               #
# This file is for the server side              #
# of a many-clients <-> one-server              #
# OpenVPN configuration.                        #
#                                               #
# OpenVPN also supports                         #
# single-machine <-> single-machine             #
# configurations (See the Examples page         #
# on the web site for more info).               #
#                                               #
# This config should work on Windows            #
# or Linux/BSD systems.  Remember on            #
# Windows to quote pathnames and use            #
# double backslashes, e.g.:                     #
# "C://Program Files//OpenVPN//config//foo.key" #
#                                               #
# Comments are preceded with '#' or ';'         #
#################################################

# Which local IP address should OpenVPN
# listen on? (optional)
;local a.b.c.d

# Which TCP/UDP port should OpenVPN listen on?
# If you want to run multiple OpenVPN instances
# on the same machine, use a different port
# number for each one.  You will need to
# open up this port on your firewall.
#port 1194
port 5194

# TCP or UDP server?
;proto tcp
#proto udp
proto tcp

# "dev tun" will create a routed IP tunnel,
# "dev tap" will create an ethernet tunnel.
# Use "dev tap0" if you are ethernet bridging
# and have precreated a tap0 virtual interface
# and bridged it with your ethernet interface.
# If you want to control access policies
# over the VPN, you must create firewall
# rules for the the TUN/TAP interface.
# On non-Windows systems, you can give
# an explicit unit number, such as tun0.
# On Windows, use "dev-node" for this.
# On most systems, the VPN will not function
# unless you partially or fully disable
# the firewall for the TUN/TAP interface.
;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 or higher,
# you may need to selectively disable the
# Windows firewall for the TAP adapter.
# Non-Windows systems usually don't need this.
;dev-node MyTap

# SSL/TLS root certificate (ca), certificate
# (cert), and private key (key).  Each client
# and the server must have their own cert and
# key file.  The server and all clients will
# use the same ca file.
#
# See the "easy-rsa" directory for a series
# of scripts for generating RSA certificates
# and private keys.  Remember to use
# a unique Common Name for the server
# and each of the client certificates.
#
# Any X509 key management system can be used.
# OpenVPN can also use a PKCS #12 formatted key file
# (see "pkcs12" directive in man page).
ca /usr/local/openvpn/etc/ca.crt
#cert server.crt
cert
/usr/local/openvpn/etc/itcht-server.crt
key
/usr/local/openvpn/etc/itcht-server.key # This file should be kept secret

# Diffie hellman parameters.
# Generate your own with:
#   openssl dhparam -out dh1024.pem 1024
# Substitute 2048 for 1024 if you are using
# 2048 bit keys.
dh /usr/local/openvpn/etc/dh1024.pem

# Configure server mode and supply a VPN subnet
# for OpenVPN to draw client addresses from.
# The server will take 10.8.0.1 for itself,
# the rest will be made available to clients.
# Each client will be able to reach the server
# on 10.8.0.1. Comment this line out if you are
# ethernet bridging. See the man page for more info.
#server 10.8.0.0 255.255.255.0
server 172.16.0.0 255.255.0.0

# Maintain a record of client <-> virtual IP address
# associations in this file.  If OpenVPN goes down or
# is restarted, reconnecting clients can be assigned
# the same virtual IP address from the pool that was
# previously assigned.
ifconfig-pool-persist ipp.txt

# Configure server mode for ethernet bridging.
# You must first use your OS's bridging capability
# to bridge the TAP interface with the ethernet
# NIC interface.  Then you must manually set the
# IP/netmask on the bridge interface, here we
# assume 10.8.0.4/255.255.255.0.  Finally we
# must set aside an IP range in this subnet
# (start=10.8.0.50 end=10.8.0.100) to allocate
# to connecting clients.  Leave this line commented
# out unless you are ethernet bridging.
;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100

# Push routes to the client to allow it
# to reach other private subnets behind
# the server.  Remember that these
# private subnets will also need
# to know to route the OpenVPN client
# address pool (10.8.0.0/255.255.255.0)
# back to the OpenVPN server.
;push "route 192.168.10.0 255.255.255.0"
push "route 172.16.0.0 255.255.0.0"
;push "route 192.168.20.0 255.255.255.0"

# To assign specific IP addresses to specific
# clients or if a connecting client has a private
# subnet behind it that should also have VPN access,
# use the subdirectory "ccd" for client-specific
# configuration files (see man page for more info).

# EXAMPLE: Suppose the client
# having the certificate common name "Thelonious"
# also has a small subnet behind his connecting
# machine, such as 192.168.40.128/255.255.255.248.
# First, uncomment out these lines:
;client-config-dir ccd
;route 192.168.40.128 255.255.255.248
# Then create a file ccd/Thelonious with this line:
#   iroute 192.168.40.128 255.255.255.248
# This will allow Thelonious' private subnet to
# access the VPN.  This example will only work
# if you are routing, not bridging, i.e. you are
# using "dev tun" and "server" directives.

# EXAMPLE: Suppose you want to give
# Thelonious a fixed VPN IP address of 10.9.0.1.
# First uncomment out these lines:
;client-config-dir ccd
;route 10.9.0.0 255.255.255.252
# Then add this line to ccd/Thelonious:
#   ifconfig-push 10.9.0.1 10.9.0.2

# Suppose that you want to enable different
# firewall access policies for different groups
# of clients.  There are two methods:
# (1) Run multiple OpenVPN daemons, one for each
#     group, and firewall the TUN/TAP interface
#     for each group/daemon appropriately.
# (2) (Advanced) Create a script to dynamically
#     modify the firewall in response to access
#     from different clients.  See man
#     page for more info on learn-address script.
;learn-address ./script

# If enabled, this directive will configure
# all clients to redirect their default
# network gateway through the VPN, causing
# all IP traffic such as web browsing and
# and DNS lookups to go through the VPN
# (The OpenVPN server machine may need to NAT
# the TUN/TAP interface to the internet in
# order for this to work properly).
# CAVEAT: May break client's network config if
# client's local DHCP server packets get routed
# through the tunnel.  Solution: make sure
# client's local DHCP server is reachable via
# a more specific route than the default route
# of 0.0.0.0/0.0.0.0.
;push "redirect-gateway"
push "redirect-gateway"

# Certain Windows-specific network settings
# can be pushed to clients, such as DNS
# or WINS server addresses.  CAVEAT:
# http://openvpn.net/faq.html#dhcpcaveats
;push "dhcp-option DNS 10.8.0.1"
push "dhcp-option DNS 218.85.157.99"
push "dhcp-option DNS 218.85.152.99"
;push "dhcp-option WINS 10.8.0.1"

# Uncomment this directive to allow different
# clients to be able to "see" each other.
# By default, clients will only see the server.
# To force clients to only see the server, you
# will also need to appropriately firewall the
# server's TUN/TAP interface.
;client-to-client
client-to-client

# Uncomment this directive if multiple clients
# might connect with the same certificate/key
# files or common names.  This is recommended
# only for testing purposes.  For production use,
# each client should have its own certificate/key
# pair.
#
# IF YOU HAVE NOT GENERATED INDIVIDUAL
# CERTIFICATE/KEY PAIRS FOR EACH CLIENT,
# EACH HAVING ITS OWN UNIQUE "COMMON NAME",
# UNCOMMENT THIS LINE OUT.
;duplicate-cn
duplicate-cn

# The keepalive directive causes ping-like
# messages to be sent back and forth over
# the link so that each side knows when
# the other side has gone down.
# Ping every 10 seconds, assume that remote
# peer is down if no ping received during
# a 120 second time period.
keepalive 10 120

# For extra security beyond that provided
# by SSL/TLS, create an "HMAC firewall"
# to help block DoS attacks and UDP port flooding.
#
# Generate with:
#   openvpn --genkey --secret ta.key
#
# The server and each client must have
# a copy of this key.
# The second parameter should be '0'
# on the server and '1' on the clients.
;tls-auth ta.key 0 # This file is secret

# Select a cryptographic cipher.
# This config item must be copied to
# the client config file as well.
;cipher BF-CBC        # Blowfish (default)
;cipher AES-128-CBC   # AES
;cipher DES-EDE3-CBC  # Triple-DES

# Enable compression on the VPN link.
# If you enable it here, you must also
# enable it in the client config file.
comp-lzo

# The maximum number of concurrently connected
# clients we want to allow.
;max-clients 100

# It's a good idea to reduce the OpenVPN
# daemon's privileges after initialization.
#
# You can uncomment this out on
# non-Windows systems.
;user nobody
user nobody
;group nobody
group nobody

# The persist options will try to avoid
# accessing certain resources on restart
# that may no longer be accessible because
# of the privilege downgrade.
persist-key
persist-tun

# Output a short status file showing
# current connections, truncated
# and rewritten every minute.
status /usr/local/openvpn/logs/openvpn-status.log

# By default, log messages will go to the syslog (or
# on Windows, if running as a service, they will go to
# the "/Program Files/OpenVPN/log" directory).
# Use log or log-append to override this default.
# "log" will truncate the log file on OpenVPN startup,
# while "log-append" will append to it.  Use one
# or the other (but not both).
;log         openvpn.log
log         /usr/local/openvpn/logs/openvpn.log
;log-append  openvpn.log
log-append  /usr/local/openvpn/logs/openvpn.log

# Set the appropriate level of log
# file verbosity.
#
# 0 is silent, except for fatal errors
# 4 is reasonable for general usage
# 5 and 6 can help to debug connection problems
# 9 is extremely verbose
verb 3

# Silence repeating messages.  At most 20
# sequential messages of the same message
# category will be output to the log.
;mute 20


以上为Server端配置文件内容。

切记:以上的文章中使用到的文件最好用约对路径来指定。否则在执行时,不在当前目录,就无法找到相关文件。造成启动失败。


启动OpenSVN Server:
#/usr/local/openvpn/sbin/openvpn--config /usr/local/openvpn/etc/openvpn.conf


附:我在Debian 3.1上,即内核2.4上启动时出错:No such file/dev/net/tun,检查确实是没有这个文件,上网找资料发现原来2.6早些版本以及之前的内核都没有预先加载
这个虚拟网卡模块,须先加载:
# mkdir /dev/net
# mknod /dev/net/tun c 10 200
# ls -l /dev/net/tun
crw-r--r-- 1 root root 10, 200 Sep  5 06:45 /dev/net/tun



tun是Linux中的虚拟网卡:在OpenVPN中还有一个dev tap的选项,也是虚拟网卡:
# "dev tun" 将创建可路由的 IP 隧道,
# "dev tap" 将创建以太网隧道。

四,WINDOWS XP客户端安装及设置:
下载客户端[切记版本要对上号,否则就会产生一些麻烦]:
http://www.openvpn.net/release/openvpn-2.0.9-install.exe
http://www.openvpn.net/release/openvpn-2.1_rc19-install.exe

安装之:
将之前产生的itcht-user1-key.tar.gz压缩文件解压到C:/Program Files/OpenVPN/config:
其中需将client.conf改为client.ovpn,并做相应的改变,主要是:
cert itcht-user1.crt
key itcht-user1.key


内容如下:
##############################################
# Sample client-side OpenVPN 2.0 config file #
# for connecting to multi-client server.     #
#                                            #
# This configuration can be used by multiple #
# clients, however each client should have   #
# its own cert and key files.                #
#                                            #
# On Windows, you might want to rename this  #
# file so it has a .ovpn extension           #
##############################################

# Specify that we are a client and that we
# will be pulling certain config file directives
# from the server.
client

# Use the same setting as you are using on
# the server.
# On most systems, the VPN will not function
# unless you partially or fully disable
# the firewall for the TUN/TAP interface.
;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

# Are we connecting to a TCP or
# UDP server?  Use the same setting as
# on the server.
;proto tcp
;proto udp
proto tcp

# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
;remote my-server-1 1194
remote 59.58.97.5 5194
;remote my-server-2 1194

# Choose a random host from the remote
# list for load-balancing.  Otherwise
# try hosts in the order specified.
;remote-random

# Keep trying indefinitely to resolve the
# host name of the OpenVPN server.  Very useful
# on machines which are not permanently connected
# to the internet such as laptops.
resolv-retry infinite

# Most clients don't need to bind to
# a specific local port number.
nobind

# Downgrade privileges after initialization (non-Windows only)
;user nobody
;group nobody

# Try to preserve some state across restarts.
persist-key
persist-tun

# If you are connecting through an
# HTTP proxy to reach the actual OpenVPN
# server, put the proxy server/IP and
# port number here.  See the man page
# if your proxy server requires
# authentication.
;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

# SSL/TLS parms.
# See the server config file for more
# description.  It's best to use
# a separate .crt/.key file pair
# for each client.  A single ca
# file can be used for all clients.
ca ca.crt
cert itcht-user1.crt
key itcht-user1.key

# Verify server certificate by checking
# that the certicate has the nsCertType
# field set to "server".  This is an
# important precaution to protect against
# a potential attack discussed here:
#  http://openvpn.net/howto.html#mitm
#
# To use this feature, you will need to generate
# your server certificates with the nsCertType
# field set to "server".  The build-key-server
# script in the easy-rsa folder will do this.
ns-cert-type server

# If a tls-auth key is used on the server
# then every client must also have the key.
;tls-auth ta.key 1

# Select a cryptographic cipher.
# If the cipher option is used on the server
# then you must also specify it here.
;cipher x

# Enable compression on the VPN link.
# Don't enable this unless it is also
# enabled in the server config file.
comp-lzo

# Set log file verbosity.
verb 3

# Silence repeating messages
;mute 20


五,连接OpenVPN服务器:
右击client.ovpn文件,选择"Start OpenVPN on thisconfig file":
系统就会有如下信息:
Sat Sep 05 07:50:25 2009 OpenVPN 2.1_rc19 i686-pc-mingw32 [SSL] [LZO2][PKCS11] built on Jul 16 2009
Sat Sep 05 07:50:25 2009 NOTE: OpenVPN 2.1 requires '--script-security2' or higher to call user-defined scripts or executables
Sat Sep 05 07:50:25 2009 LZO compression initialized
Sat Sep 05 07:50:25 2009 Control Channel MTU parms [ L:1544 D:140 EF:40EB:0 ET:0 EL:0 ]
Sat Sep 05 07:50:25 2009 Data Channel MTU parms [ L:1544 D:1450 EF:44EB:135 ET:0 EL:0 AF:3/1 ]
Sat Sep 05 07:50:25 2009 Local Options hash (VER=V4): '69109d17'
Sat Sep 05 07:50:25 2009 Expected Remote Options hash (VER=V4):'c0103fa8'
Sat Sep 05 07:50:25 2009 Attempting to establish TCP connection with59.58.97.5:5194
Sat Sep 05 07:50:25 2009 TCP connection established with 59.58.97.5:5194
Sat Sep 05 07:50:25 2009 Socket Buffers: R=[8192->8192]S=[8192->8192]
Sat Sep 05 07:50:25 2009 TCPv4_CLIENT link local: [undef]
Sat Sep 05 07:50:25 2009 TCPv4_CLIENT link remote: 59.58.97.5:5194
Sat Sep 05 07:50:26 2009 TLS: Initial packet from 59.58.97.5:5194,sid=7bf73d46 5fff2ee2
Sat Sep 05 07:50:29 2009 VERIFY OK: depth=1,/C=CN/ST=FuJian/L=PuTian/O=ITCHT/OU=ITCHT/CN=ITCHT_CA/name=ITCHT/emailAddress=zubin@itcht.com
Sat Sep 05 07:50:29 2009 VERIFY OK: nsCertType=SERVER
Sat Sep 05 07:50:29 2009 VERIFY OK: depth=0,/C=CN/ST=FuJian/L=PuTian/O=ITCHT/OU=ITCHT/CN=itcht-server/name=ITCHT/emailAddress=zubin@itcht.com
Sat Sep 05 07:50:36 2009 Data Channel Encrypt: Cipher 'BF-CBC'initialized with 128 bit key
Sat Sep 05 07:50:36 2009 Data Channel Encrypt: Using 160 bit messagehash 'SHA1' for HMAC authentication
Sat Sep 05 07:50:36 2009 Data Channel Decrypt: Cipher 'BF-CBC'initialized with 128 bit key
Sat Sep 05 07:50:36 2009 Data Channel Decrypt: Using 160 bit messagehash 'SHA1' for HMAC authentication
Sat Sep 05 07:50:36 2009 Control Channel: TLSv1, cipher TLSv1/SSLv3DHE-RSA-AES256-SHA, 1024 bit RSA
Sat Sep 05 07:50:36 2009 [itcht-server] Peer Connection Initiated with59.58.97.5:5194
Sat Sep 05 07:50:37 2009 SENT CONTROL [itcht-server]: 'PUSH_REQUEST'(status=1)
Sat Sep 05 07:50:37 2009 PUSH: Received control message:'PUSH_REPLY,route 172.16.0.0 255.255.0.0,redirect-gateway def1bypass-dhcp,dhcp-option DNS 218.85.157.99,dhcp-option DNS218.85.152.99,route 172.16.0.0 255.255.0.0,topology net30,ping10,ping-restart 120,ifconfig 172.16.0.6 172.16.0.5'
Sat Sep 05 07:50:37 2009 OPTIONS IMPORT: timers and/or timeouts modified
Sat Sep 05 07:50:37 2009 OPTIONS IMPORT: --ifconfig/up options modified
Sat Sep 05 07:50:37 2009 OPTIONS IMPORT: route options modified
Sat Sep 05 07:50:37 2009 OPTIONS IMPORT: --ip-win32 and/or--dhcp-option options modified
Sat Sep 05 07:50:37 2009 ROUTE default_gateway=192.168.0.1
Sat Sep 05 07:50:38 2009 TAP-WIN32 device [本地连接 7] opened://./Global/{1E148F56-651B-4309-9E55-894CF45AA118}.tap
Sat Sep 05 07:50:38 2009 TAP-Win32 Driver Version 9.6
Sat Sep 05 07:50:38 2009 TAP-Win32 MTU=1500
Sat Sep 05 07:50:38 2009 Notified TAP-Win32 driver to set a DHCPIP/netmask of 172.16.0.6/255.255.255.252 on interface{1E148F56-651B-4309-9E55-894CF45AA118} [DHCP-serv: 172.16.0.5,lease-time: 31536000]
Sat Sep 05 07:50:38 2009 Successful ARP Flush on interface [4]{1E148F56-651B-4309-9E55-894CF45AA118}
Sat Sep 05 07:50:43 2009 TEST ROUTES: 3/3 succeeded len=2 ret=1 a=0u/d=up
Sat Sep 05 07:50:43 2009 C:/WINDOWS/system32/route.exe ADD 59.58.97.5MASK 255.255.255.255 192.168.0.1
Sat Sep 05 07:50:43 2009 Route addition via IPAPI succeeded [adaptive]
Sat Sep 05 07:50:43 2009 C:/WINDOWS/system32/route.exe ADD 0.0.0.0 MASK128.0.0.0 172.16.0.5
Sat Sep 05 07:50:43 2009 Route addition via IPAPI succeeded [adaptive]
Sat Sep 05 07:50:43 2009 C:/WINDOWS/system32/route.exe ADD 128.0.0.0MASK 128.0.0.0 172.16.0.5
Sat Sep 05 07:50:43 2009 Route addition via IPAPI succeeded [adaptive]
Sat Sep 05 07:50:43 2009 C:/WINDOWS/system32/route.exe ADD 172.16.0.0MASK 255.255.0.0 172.16.0.5
Sat Sep 05 07:50:43 2009 Route addition via IPAPI succeeded [adaptive]
Sat Sep 05 07:50:43 2009 C:/WINDOWS/system32/route.exe ADD 172.16.0.0MASK 255.255.0.0 172.16.0.5
Sat Sep 05 07:50:43 2009 Route addition via IPAPI succeeded [adaptive]
Sat Sep 05 07:50:43 2009 Initialization Sequence Completed

看一下我的IP信息:
C:/ipconfig /all


六,服务端的NAT配置:
编辑:/etc/sysctl.conf,更改net.ipv4.ip_forward=0net.ipv4.ip_forward=1
#vi /etc/sysctl.conf
net.ipv4.ip_forward=1

或者:
#echo "1" > /proc/sys/net/ipv4/ip_forward

并执行如下指令[我的外网网卡是eth0,为两个网段做NAT]
#/sbin/iptables -A POSTROUTING -t nat -s192.168.0.0/255.255.255.0 -o eth0 -j MASQUERADE
#/sbin/iptables -A POSTROUTING -t nat -s 172.16.0.0/255.255.0.0 -o eth0-j MASQUERADE

这时,您可以用你连上OPENVPN的电脑打开www.123cha.com或者相关查本地IP的网站或工具,可以看到您的IP就不是你现在的IP了,应该是你OPENVPN服务器的IP了,恭喜你成功了。

后续:
继续研究并实作使用用户认证方式连接OpenVPN......




下面为后来完善认证功能的部分:



七:使用Mysql+PAM的用户认证:

启用这个功能前,得先安装MYSQL,最好是Apache+Mysql+PHP+phpMyAdmin,因为这样便用管理mysql中的帐号:
Linux+Apache+Mysql+PHP的安装,即简称为LAMP的安装,先前我有笔记:
原创-在Debian 5.0下的使用源代码安装LAMP笔记[完整版,带HTTPS] :
http://blog.csdn.net/zubin006/archive/2009/09/07/4529070.aspx


1,创建数据库以及用户密码表:

mysql>create database openvpn;

在phpMyAdmin执行以下的sql语句,创建表openvpnuser以及增加一条用户名为:test,密码也为test的记录:

-----------------------------------------------------------------------------------------------

-- phpMyAdmin SQL Dump
-- version 3.2.2-rc1
-- http://www.phpmyadmin.net
--
-- 主机: localhost
-- 生成日期: 2009 年 09 月 07 日 08:52
-- 服务器版本: 5.1.38
-- PHP 版本: 5.3.0

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- 数据库: `openvpn`
--

-- --------------------------------------------------------

--
-- 表的结构 `openvpnuser`
--

CREATE TABLE IF NOT EXISTS `openvpnuser` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` char(20) NOT NULL,
  `password` char(128) NOT NULL,
  `active` int(10) NOT NULL DEFAULT '1',
  `reg_date` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COMMENT='openvpn user table.'AUTO_INCREMENT=5 ;

--
-- 转存表中的数据 `openvpnuser`
--

INSERT INTO `openvpnuser` (`id`, `username`, `password`, `active`,`reg_date`) VALUES
(1, 'test', '*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29', 1, 0);


----------------------------------------------------------------------------------------------------------


2,新增数据库普通用户openvpn,让它对数据库openvpn具有完全权限。


3,安装并配置 pam_mysql 认证模块:

a,编译并安装 pam_mysql模块:
下载pam_mysql模块:
#wgethttp://prdownloads.sourceforge.net/pam-mysql/pam_mysql-0.7RC1.tar.gz
# tar -zxvf pam_mysql-0.7RC1.tar.gz
# cd pam_mysql-0.7RC1
# ./configure --with-mysql=/usr/local/mysql
# make

32位系统:
# cp .libs/pam_mysql.so /lib/security/.
64位系统:
# cp .libs/pam_mysql.so /lib64/security/.

b,配置 pam_mysql模块:

#touch /etc/pam.d/openvpn
#vi /etc/pam.d/openvpn

auth required /lib/security/pam_mysql.so user=openvpnpasswd=youdbpasswd host=localhost db=openvpn table=openvpnuserusercolumn=username passwdcolumn=password where=active=1 sqllog=0crypt=2 verbose=1
account required /lib/security/pam_mysql.so user=openvpnpasswd=youdbpasswd host=localhost db=openvpn table=openvpnuserusercolumn=username passwdcolumn=password where=active=1 sqllog=0crypt=2 verbose=1


c,利用saslauthd来测试pam_mysql是否工作正常[上面安装的sasl2-bin包]:

# vi /etc/default/saslauthd

# Should saslauthd run automatically on startup? (default: no)
#START=no
START=yes


启动saslauthd:
# /etc/init.d/saslauthd start

测试:

# testsaslauthd -u test -p test -s openvpn
0: OK "Success."


说明pam_mysql可以正常工作,即正常的利用mysql中的openvpn数据库的用户表进行用户名和密码的匹配。

相关日志有所体现:
/var/log/syslog
/var/log/auth.log

4,配置 openvpn 服务器及客户端有关认证的内容:

进入 openvpn 源代码目,在 plugin/auth-pam 下将 openvpn 认证模块编译出来:

# cd/usr/local/src/openvpn/openvpn-2.0.9/plugin/auth-pam
# make

32位系统:
# cp -f openvpn-auth-pam.so /lib/security/.
64位系统:
# cp -f openvpn-auth-pam.so /lib64/security/.


在openvpn服务器端的配置文件中加入认证模块:
# vi /usr/local/openvpn/etc/openvpn.conf
在最后一行加入:

plugin /lib/security/openvpn-auth-pam.soopenvpn           #64位系统做相关的改动。

重启一下OpenVPN,即杀掉进程号,然后:
启动OpenSVN Server:
#/usr/local/openvpn/sbin/openvpn--config /usr/local/openvpn/etc/openvpn.conf

在openvpn客户端最后一行加入:
auth-user-pass

重启客户端时就要求输入客户端,详细操作方式,即客户端使用手册:

-----------------------------------------------------------------------------------------------------------

使用手册-OpenVPN-2.1_rc19版.txt

ITCHT VPN 客户端安装手册 [基于OpenVPN]

说明:本手册将指导您一步一步的安装并连接上ITCHT OpenVPN服务器

一,OpenVPN客户端软件的安装:

1,下载下面的OpenVPN客户端:
http://www.openvpn.net/release/openvpn-2.0.9-install.exe
注意:由于服务器端是使用2.0.9版,因此客户端也得使用2.0.9版,否则如果使用最新的2.1_rc19版的话,您将无法获取到网关,也就无法使用OpenVPN上网。

2,安装:
a,双击openvpn-2.0.9-install.exe文件。
b,点击 : "Next >" ==> "I Agree" ==> "Next >" ==> "Install"
c,在点击 "Install" 后,系统会提示您进行"硬件安装",其实就是安装一张OpenVPN的虚拟网卡,请点击 "仍然继续(C)".
d,点击 : "Next >" ==> "Finish" 以完成OpenVPN客户端的安装。


二,连接上ITCHT OpenVPN服务器:

1,鼠标右键点击本目录的 : itchtvpn-user1.ovpn 。
2,选择 : "Start OpenVPN on this config file" 。

系统画面如下:
Enter Auth Username :
输入您的用户名,按Enter,
Enter Auth Password :
输入您的密码,按Enter,

系统会进行跟服务器的连接,直到出现:
Initialization Sequence Completed.
并且刚才装的虚拟网卡会自动获取到IP,即成功完成跟OpenVPN服务器的连接。

这时您上网都将通过本VPN服务器,并跟其它连入本VPN的用户形成一个虚拟局域网。

完...
祖彬于莆田
2009,09,08


---------------------------------------------------------------------------------------------------------------


使用手册-OpenVPN-2.1_rc19版.txt

ITCHT VPN 客户端安装手册 [基于OpenVPN]

说明:本手册将指导您一步一步的安装并连接上ITCHT OpenVPN服务器

一,OpenVPN客户端软件的安装:

1,下载下面的OpenVPN客户端:
http://www.openvpn.net/release/openvpn-2.1_rc19-install.exe
注意:由于服务器端是使用2.1_rc19版,因此客户端也得使用2.1_rc19版,否则如果使用旧的2.0.9版的话,您将无法获取到网关,也就无法使用OpenVPN上网。

2,安装:
a,双击openvpn-2.1_rc19-install.exe文件。
b,点击 : "Next >" ==> "I Agree" ==> "Next >" ==> "Install"
c,在点击 "Install" 后,系统会提示您进行"硬件安装",其实就是安装一张OpenVPN的虚拟网卡,请点击 "仍然继续(C)".
d,点击 : "Next >" ==> "Finish" 以完成OpenVPN客户端的安装。


二,连接上ITCHT OpenVPN服务器:

1,鼠标右键点击本目录的 : itchtvpn21-user1.ovpn
2,选择 : "Start OpenVPN on this config file" 。

系统画面如下:
Enter Auth Username :
输入您的用户名,按Enter,
Enter Auth Password :
输入您的密码,按Enter,

系统会进行跟服务器的连接,直到出现:
Initialization Sequence Completed.
并且刚才装的虚拟网卡会自动获取到IP,即成功完成跟OpenVPN服务器的连接。

这时您上网都将通过本VPN服务器,并跟其它连入本VPN的用户形成一个虚拟局域网。


在2.1_rc19中还有另一种方法连接到服务器的方法:
使用OpenVPN 2.1_rc19的 GUI方式:
1,将本目录的所有文件COPY到C:/Program Files/OpenVPN/config目录下。
2,点击 "开始" ==> "所有程序" ==> "OpenVPN" ==> "OpenVPNGUI",这时在你的电脑右下角会出现一个带红色的网络连接图标,叫"OpenVPN GUI"。
3,右键点击这个位于右下解的"OpenVPN GUI"图标,选"Connect"进行跟VPN服务器连接。会弹出输入用户名和密码。输入按ENTER即可。
4,右键点击这个位于右下解的"OpenVPN GUI"图标,选"Disconnect"将中断与服务器的连接。



完...
祖彬于莆田
2009,09,08


----------------------------------------------------------------------------------------------------



#注意,如果是安装openvpn-2.1_rc19时在客户端连接时,在Server端的/var/log/auth.log会出现如下错误信息:

Sep  6 23:29:19 d502 openvpn: PAM unable todlopen(/lib/security/pam_mysql.so): /lib/security/pam_mysql.so:undefined symbol: pam_set_data
Sep  6 23:29:19 d502 openvpn: PAM adding faulty module:/lib/security/pam_mysql.so

解决方案:

/lib/security/openvpn-auth-pam.so 这个文件要用 openvpn-2.0.9源代码文件编译出来的。原因也不是太懂。折磨了我好久。有空再找官网看一下相关文档:就是COPY下面的这个文件到/lib/security/openvpn-auth-pam.so
/usr/local/src/openvpn/openvpn-2.0.9/plugin/auth-pam/openvpn-auth-pam.so

这个问题解决后,在2.0.9客户端当VPN服务器连接断掉后,系统就无法工作,这个问题目在2.1_rc19就没有了。所以现在我就是在使用最新的2.1_rc19,再强调一次:你的OpenVPN的Server端选用的版号要跟Client要一致,否则就会造成Client端能正常连接,但是获取不到gateway,我栽了跟头。


本文终于 2009年09月05日...福建 莆田
于2009年09月16日晚祖彬于莆田更新到第二版...主要是增加了Mysql+PAM的用户认证功能以及修改了第一版中的错误地方。

原创粉丝点击