windows下利用OpenVPN搭建VPN服务器

更新时间:2024-04-13 04:07:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

windows下利用OpenVPN搭建VPN服务器

2008/01/07 5:02 | 鬼仔 | 技术文章 | 占个座先

信息来源:邪恶八进制信息安全团队(www.eviloctal.com) 文章作者:pt007[at]vip.sina.com 版权所有 转载需注明作者

注:文章首发I.S.T.O信息安全团队(http://blog.csdn.net/I_S_T_O),后由原创作者友情提交到邪恶八进制信息安全团队。

一、OpenVPN是一款功能强大,可跨平台(支持Win 2000/XP/2003, Linux, Mac OS X, Solaris, FreeBSD, NetBSD, 和 OpenBSD)使用的SSL VPN服务器软件(具体说明见官方主页官方主页)。

openvpn-2.1_beta16命令行版,下载地址

http://openvpn.net/download_action.php?openvpn-2.1_beta16-install.exe 也可以下图形版。 二、安装与配置

第一步:安装openvpn

这一部分是服务端跟客户端都要做的工作,操作完全相同

双击 openvpn-2.1_beta16-install.exe进行安装,点击NEXT,I Agree,NEXT之后开始选择安装路径,我手动修改为C:\\Program Files\\OpenVPN 。点击 Install 开始安装,安装过程中,弹出硬件安装窗口,点击仍然继续,安装虚拟网卡。点击 next,Finish 完成安装。

第二步: VPN服务器配置:

说明:架设OPENVPN服务器,服务器方面做的工作比较多,客户端相对来说就比较简单。 服务器采用RSA证书和密钥验证方式对客户端进行验证,默认情况下证书和用户是一对一的,多个用户使用同一证书会被踢出。所以首先要做的工作就是证书的制作。 在进行操作之前,首先进行初始化工作:

(1)修改C:\\Program Files\\OPENVPN\\easy-rsa\\vars.bat.sample的以下部分 set HOME=%ProgramFiles%\\OpenVPN\\easy-rsa set KEY_COUNTRY=US set KEY_PROVINCE=CA

set KEY_CITY=SanFrancisco set KEY_ORG=FortFunston

set KEY_EMAIL=mail@domain.com 请根据自身情况修改,也可以不修改,改为

set HOME=C:\\Program Files\\OPENVPN\\easy-rsa set KEY_COUNTRY=CN #(国家)

set KEY_PROVINCE=GuangDong #(省份) set KEY_CITY=ShenZhen #(城市) set KEY_ORG=oovc.com #(组织)

set KEY_EMAIL=admin@oovc.com #(邮件地址) 上面#开始的是注释,请不要写到文件中。 打开命令提示符:

开始-->运行...-->键入cmd,回车,进入命令提示符 或者 开始-->程序-->附件-->命令提示符

进入C:\\Program Files\\openvpn\\easy-rsa目录下: 命令如下:

(1)cd C:\\Program Files\\openvpn\\easy-rsa init-config vars clean-all

上面是初始化工作,以后,在进行证书制作工作时,仍旧需要进行初始化,但只需要进入openvpn\\easy-rsa目录,运行vars就可以了,不需要上面那些步骤了。 (2)下面开始证书的制作: 生成根证书:(请输入红字部分)

build-ca

build-dh 生成服务端密钥:

build-key-server server

生成客户端密钥 build-key client1

build-key client2 //可以继续配置第二个VPN客户端密钥

//生成的密钥存放于C:\\Program Files\\openvpn\\easy\\rsa\\keys目录下 接下来开始配置服务器和客户端:

(3)将生成的ca.crt,dh1024.pem,server.crt,server.key复制到C:\\Program Files\\OPENVPN\\KEY目录下,这四个文件是VPN服务端运行所需要的文件。

(4)ca.crt,client.crt,client.key是VPN客户端所需要的文件,复制到客户端C:\\Program Files\\OPENVPN\\KEY目录下

(5)在C:\\Program Files\\OpenVPN\\config目录下创建server.ovpn: 服务器端文件示例:(server.ovpn) local 192.168.3.1 #建立VPN的IP

port 443 #端口号,根据需要,自行修改,如果是用http代理连接,请不要修改 proto tcp-server #通过TCP协议连接 dev tap #win下必须设为tap

server 192.168.0.0 255.255.255.0 # 虚拟局域网网段设置,请根据需要自行修改,不支持和拔号网卡位于同一网段

push \表示client通过VPN SERVER上网 keepalive 20 180

ca \证书存放位置,请根据实际情况自行修改

cert \服务器证书存放位置,请根据实际情况自行修改

key \服务器密钥存放位置,请根据实际情况自行修改

dh \存放位置,请根据实际情况自行修改 push \

push \,请根据实际情况自行修改 mode server tls-server

status \记录文件存放位置,请根据实际情况自行修改 comp-lzo verb 4

(6)客户端设置:在客户端安装完成之后,需要将 ca.crt client.crt client.key 这三个文件拷贝到C:\\Program Files\\openvpn\\key目录下,这三个文件由服务端生成,所以,连接谁的服务器,就需要跟谁索取这三个文件

然后,编辑一个 client.ovpn的配置文件存放到C:\\Program Files\\openvpn\\config目录下,客户端就可以进行连接了。

客户端文件示例:(client.ovpn) client

dev tap #windows下面用tap,LINUX下用tun proto tcp-client

remote 192.168.3.1 443 #VPN服务器的域名或IP 端口 resolv-retry infinite nobind

#http-proxy 192.168.1.1 80 #这里填入你的代理服务器地址和端口 mute-replay-warnings

ca \

cert \这里改成每个客户端相应的证书

key \这里改成每个客户端相应的密钥 comp-lzo verb 4

status openvpn-status.log (7)其它设置:

上面的配置拔号成功后,VPN SERVER的IP:192.168.0.1 VPN client的IP:192.168.0.2

ping 192.168.0.1 //相互之间应能ping通

然后设置VPN SERVER上的”internet连接共享“来实现clinet通过VPN SERVER上网:(需要两块网卡)

(8)VPN服务端命令行启动:

\Files\\OpenVPN\\config\\server.ovpn\启动VPN到443端口 (9)VPN客户端命令行连接:

\Files\\OpenVPN\\config\\client.ovpn\

Linux下OpenVPN的建设详细教程

作者:未知 文章来源:网络 点击数: 更新时间:2007-9-3 23:13:07

到现在为止,一切准备就绪,下面开始写配置文件,为了缩小篇幅,把原有注释都去掉了。 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 \把这条路由发送给客户端,客户连接成功后自动加入路由表,省略了下一跳地址: 10.8.0.1

# else

# push \否则发送本条路由,这是一个主机路由,省略了子网掩码和下一跳地址,分别为: 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传递给客户机 #

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 \;push \

#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 \

#DHCP的一些选项,具体查看Manual

;push \;push \

#如果可以让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.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可以访问内网了。

OpenVPN之以太网桥接 2010-07-07 18:45 OpenVPN之以太网桥接

电脑-openvpn 2007-10-18 10:19:23 阅读401 评论0 字号:大中小 译者:温占考(Email: wzkwenzknet),来源:OpenVPN.net,转载请注明译者和出处及版权信息,并且不能用于商业用途,违者必究。

由于英语水平有限,如有错误,还望指正!

桥接概述

查看FAQ了解路由对比桥接的概述。

以太网桥接实质上是把一个以太网络接口和一个或多个虚拟TAP接口桥接在一个网桥接口上。以太网桥表现为使用软件模拟一个真实的以太网交换机。以太网桥可以理解为一种能够用来连接一个机器上的多个以太网接口(无论是物理的还是虚拟的)使它们共享一个IP子网的软件交换机。

把分布在两个独立网络的一个物理的以太网NIC和一个OpenVPN所使用的TAP接口桥接起来,这样就可以逻辑上合并两个以太网络,就像它们在一个以太网子网内。

配置桥接

这个例子将指导你配置一个OpenVPN服务器端的桥接。多个客户将会连接到这个网桥上,并且每个客户机的TAP接口将会分配到一个服务器端LAN网段的IP地址。

下面是两个实现客户IP地址分配的方法:

让OpenVPN使用server-bridge指示来管理其客户的IP地址池,或者 在LAN内配置DHCP服务器让其为VPN客户分配IP地址。 在这个例子中,我们将使用第一个方法即OpenVPN服务器管理其客户在LAN子网内的IP地址池,和DHCP服务器使用的地址池分开(如果存在的话)。两种方法在FAQ item都有详细描述。

在我们的例子中,我们将使用下面的网桥配置:

设置

网桥启动 参数

取值

以太网络接口 eth

eth0

本地IP地址 ip

192.168.8.4

本地子网掩码

eth_netmask

255.255.255.0

本地广播地址

eth_broadcast

192.168.8.255

VPN客户地址池

192.168.8.128 to 192.168.8.254

虚拟网桥接口 br br0

虚拟TAP接口 tap

tap0

第一步是按照HOWTO中的“开始使用VPN并且测试其连通性(Starting up the VPN and testing for initial connectivity)”章节。然后,根据你是在Linux或Windows下配置桥接继续配置。

在Linux上配置桥接服务器

首先,确认你已经安装了bridge-utils软件包。

编辑下面的bridge-start(启动网桥)脚本。根据你要桥接的物理网络接口设置br, tap, eth, eth_ip, eth_netmask, 和 eth_broadcast 参数。确认使用一个内部网络接口并且是连接在一个受防火墙保护的LAN内。你可以使用Linux的ifconfig命令获得网络接口的一些必要的信息来完成bridge-start(启动网桥)参数。

现在就运行bridge-start脚本。脚本将会创建一个永久[译者注:永久意为系统重新启动前永久,默认虚拟接口在OpenVPN退出后就消失]的tap0网络接口并且已经和以太网接口桥接成功了。

下一步,我们将要编辑OpenVPN server configuration file(OpenVPN服务器端配置文件)来使能桥接配置。

注释掉dev tun这行行并且替换成:

dev tap0

注释掉以server开始的行并且替换成:

server-bridge 192.168.8.4 255.255.255.0 192.168.8.128 192.168.8.254

现在设置Linux防火墙使之允许数据包通过新建的tap0 和 br0接口:

iptables -A INPUT -i tap0 -j ACCEPT

iptables -A INPUT -i br0 -j ACCEPT

iptables -A FORWARD -i br0 -j ACCEPT

OpenVPN桥接现在能够通过下面顺序来启动和停止:

运行 bridge-start 运行 openvpn 停止 openvpn

运行 bridge-stop

到目前为止,桥接的配置就完成了,你可以continue where you left off in the HOWTO(根据HOWTO继续完成剩下部分)。

在Windows XP中配置桥接服务器

这个配置在桥接端需要Windows XP或更高版本系统。据我所知,Windows 2000不支持桥接,然而Windows 2000系统可以是一个桥接网络的客户,而OpenVPN另一端的桥接是在Linux或Windows XP系统上实现的。

在Windows上安装OpenVPN时,安装程序将自动创建一个名为“本地连接2”的TAP-Win32适配器。转到控制面版中的网络连接下并且改名为“tap-bridge”。

接下来使用鼠标选择tap-bridge和以太网适配器,点击鼠标右键,并且选择桥接(Bridge Connections)。这样就在控制面板中创建了一个新的桥适配器图标。

在桥适配器的TCP/IP属性里配置IP地址192.168.8.4子网掩码255.255.255.0。

接下来,编辑OpenVPN server configuration file(OpenVPN服务端配置文件) 使能桥接配置。

注释dev tun这行且替换成:

dev tap

dev-node tap-bridge

注释以server开始的行并且替换成:

server-bridge 192.168.8.4 255.255.255.0 192.168.8.128 192.168.8.254

如果你的系统时XP SP2,进入防火墙配置控制面板,并且禁用桥适配器和TAP适配器上的包过滤功能。

到目前为止,桥接的配置就完成了,你可以continue where you left off in the HOWTO(根据HOWTO继续完成剩下部分)。

配置桥接客户端

在sample OpenVPN client configuration(OpenVPN客户配置)基础上进行配置。注释dev tun的行且替换成:

dev tap

最后,确认客户端的配置文件和服务器端的配置文件是否一致。需要着重注意的

是proto (udp or tcp)选项是否一致。同样如果使用了comp-lzo 和 fragment确认客户端和服务器端是否一致。

以太网桥接说明

使用以太网桥接配置时,第一步是构造以太网桥--一种虚拟网络接口是其他网络接口的一个容器,可以是物理的NIC也可以是虚拟的TAP接口。以太网桥接口必须在OpenVPN启动之前创建。

没有通用的网桥配置方法--每个OS有自己的配置方法(见下面例子)。

网桥接口一旦被创建,并且以太网接口加入到网桥中,OpenVPN就可以启动了。

网桥接口是一种由一个或多个以太网络接口组成的虚拟网络接口,这些以太网络接口可以是物理网络接口或OpenVPN所使用的虚拟TAP接口。

当你配置以太网桥接接口时,你需要手工设置网桥接口的IP地址和子网掩码并且不要在OpenVPN配置文件中使用ifconfig选项来配置。这是因为与TUN/TAP接口不一样,OpenVPN程序不能配置网桥接口的IP地址和子网掩码。

OpenVPN配置文件应该通过dev选项来指定已经加入网桥TAP接口,而不是指定网桥接口的名字。

对于Windows系统,使用dev-node选项来指定已经加入网桥TAP-Win32适配器(dev-node这个名字是网络连接控制面板上显示的名字)。 在Linux/BSD/Unix系统中,对于dev tap选项,使用你已经加入网桥的TUN/TAP的序号如dev tap0。

如果你的OpenVPN工作在点对点模式,去掉ifconfig选项,如果你使用的是客户/服务器模式,在服务器端使用server-bridge选项。

桥接的时候,你必须手工设置网桥接口的TCP/IP设置。例如在Linux中,可以使用ifconfig命令配置而在Windows XP中可以通过配置网络连接面板中网桥接口的TCP/IP属性来实现(在Windows XP和更高版本的网络连接面板中可以通过鼠标操作来实现桥接)。

确定只有桥接的TAP接口和内部以太网络接口是被防火墙保护的。不要把TAP接口和连接互联网的网络接口进行桥接,这样可能存在潜在的安全漏洞。 local 和 remote 所使用的地址不能是桥接子网内的地址--否则将会出现路由环路。

理解以太网桥接很重要的一点就是每个加入网桥的网络接口将会失去原有的配置如IP地址和子网掩码。只有网桥接口的TCP/IP设置有效。

手工配置网桥时常见的错误是在为网桥配置IP地址和子网掩码之前把主要的以太网络适配器加入到网桥中。这就导致主要的以太网卡“丢失”了原有的配置,但是所属的网桥接口还没有配置,所以这就造成了以太网络接口的连接就丢失了。

在大多数情况下,只是在服务器端配置一个可用的桥接,而不是在客户端配置。这样一来,当客户机连接上服务器后就成为了多宿主系统了,例如:它们仍然拥有原有的以太网接口,但是连接到OpenVPN服务器的上层连接,它们将会有一个新的TAP接口和服务器端的以太网接口桥接(并且有可能是所有连接到服务器的

客户机的TAP接口如果在服务器的配置文件里申明了client-to-client选项)。 说明 – Windows下的以太网桥接

Check out this HOWTO by Adam Pavelec.

Windows Notes 页面有更多关于以太网桥接的说明。

info@openvpn.net>. OpenVPN is a trademark of OpenVPN Solutions LLC.

摘自:http://www.yuanma.org/data/2006/1106/article_1786.htm

================================================================== 参考如上文章修改如下测试成功

我最后采用了网桥模式 问题解决(不知道路由模式是否可以) 配置文件

(认证部分省略) dev tap

server-bridge 10.0.8.4 255.255.255.0 10.0.8.50 10.0.8.100

push \(服务器真实内网ip子网为 10.0.0.0/24)

网络设置

ip addr add 10.0.8.4/24 dev tap0 #10.0.8.4(网桥IP地址) 客户端分配到的IP为10.0.8.50 ip link set tap0 up echo 1 > /proc/sys/net/ipv4/ip_forward #(打开IP转发) echo 1 > /proc/sys/net/ipv4/conf/eth0/proxy_arp #(打开ARP代理) eth0(内网网卡)

配置桥接客户端

在sample OpenVPN client configuration(OpenVPN客户配置)基础上进行配置。注释dev tun的行且替换成: dev tap

另 有兴趣的可以看看http://nimlabs.org/~nim/dirtynat.html这篇文章,写的很不错。

原贴地址为:http://bbs3.chinaunix.net/viewthread.php?tid=1092285

Dirty NAT tricks to get a VPN to work with clients also numbered

in the private address space

Nick Martin

The Problem

You have a corporate LAN. You want to set up a VPN (in this case OpenVPN) into the LAN for your road-warriors. However, your LAN is numbered with one of the very common private subnets, such as 192.168/16. Your

road-warriors often get addresses in the same private subnet from their coffee-shops, and this breaks things horribly. This is illustrated in the diagram below.

I'm going to use specific IP addresses and interface names on this page, since too often I find when people try to describe the general solution it actually makes it less clear and harder to apply to your own network.

The standard VPN solution will simply number the client node on to the corporate LAN (somewhere in 192.168/16). Now the client will be confused, since it will have a route for both 192.168.0/24 and 192.168/16. What will

likely happen is the client will simply not be able to access anything in the corporate LAN's 192.168.0/24 range.

This page assumes you are able to set up a bare VPN connection between the clients and the server (just two connected virtual Ethernet devices, as shown in the diagram), but are just not sure how to number it such that the client can actually talk to all the nodes in the LAN.

Discarded Solution #1: Renumber the LAN

The OpenVPN HOWTO suggests renumbering the corporate LAN. For many people, this is simply not an option. A typical dialog might go something like this: - Hello, friendly sysadmin! + What do you want? - I'd like to set up a VPN so I can work from the road. + Seems reasonable. What do you need from me. - Oh, just permission and uhh... renumbering the whole corporate LAN + What!?!? - We need to renumber the network. + I WILL DESTROY YOU! Discarded Solution #2: Client side static routing

Another solution people on various mailing lists have proposed is to number the client into the corporate LAN as normal, but alter the routing table on the client such that it has a static route to its gateway. The routing table on the client would look something like this: 192.168.0.1/32 dev eth0 192.168.0.0/16 dev tap0 default via 192.168.0.1 dev eth0 There are several problems with this approach. First, it requires manual intervention on the client side, and a client OS that supports such intervention. If you want to support Windows clients, this may not be the best approach.

The more major problem with this approach is that two IPs on the private LAN will be \(192.168.0.1) and the IP of the client itself (192.168.0.42). If there is an important server on 192.168.0.42 on the private LAN, the client will not be able to access it.

Solution: Dirty NAT tricks

The solution I settled on was to create a one-to-one NAT to remap all of corporate LAN to a different private netblock (10.22/16), and put the client into that range. To the hosts in the corporate network, the VPN client appears to be in 192.168/16 and to the client the corporate network seems to be 10.22/16. (The OpenVPN FAQ mentions this approach, but does not give detail as to how to set it up.) A diagram of the configuration is provided below.

Configuring OpenVPN

First, you should pick a subnet of the corporate LAN to put VPN clients on. I chose 192.168.8/24, which will be mapped to 10.22.8/24. OpenVPN should be configured to hand out addresses in the mapped range, and set the VPN host as the router to the rest of the mapped range. We have the following lines in the OpenVPN configuration: server-bridge 10.22.8.1 255.255.255.0 10.22.8.10 10.22.8.120 push \Once the OpenVPN link is configured, configure the TAP interface: ip addr add 10.22.8.1/24 dev tap0 ip link set tap0 up Now you should be able to bring an OpenVPN client up (say 10.22.8.10). You should be able to ping the server (as 10.22.8.1) from the client and vice versa.

Setting up the NAT

Next, set up the one-to-one NAT mapping the two private networks together: iptables -v -t nat -A PREROUTING -d 192.168.8.0/24 -j NETMAP --to 10.22.8.0/24 iptables -v -t nat -A PREROUTING -i tap0 -d 10.22.0.0/16 -j NETMAP --to 192.168.0.0/16 iptables -v -t nat -A POSTROUTING -o tap0 -s 192.168.0.0/16 -j NETMAP --to 10.22.0.0/16 iptables -v -t nat -A POSTROUTING -o eth0 -s 10.22.0.0/16 -j NETMAP --to 192.168.0.0/16 echo 1 > /proc/sys/net/ipv4/ip_forward More documentation on iptables and netfilter can be found here. Netfilter is a very complicated beast, but is well worth spending some time getting acquainted with.

At this stage, you should be able to send pings out from the VPN client to hosts on the private LAN (at the remapped address, of course), but the LAN machines won't know how to reply to the ping since ARP is not set up yet.

You can test this by logging in to another server (say 192.168.1.12) on the LAN and watching tcpdump. Ping the remapped address (10.22.1.12) from the client. You should see the ping requests come in to the server, followed by ARP packets asking who-has 192.168.8.10.

Setting up Proxy ARP

In order for the system to work correctly, the VPN server should respond to all ARP requests for 192.168.8/24 on eth0. However, in Linux version 2.2, the ability to instruct the kernel to reply to any ARP packet for addresses in a given subnet was removed, and replaced by automatic proxy ARP. For the automatic proxy ARP to work, the VPN server must think it has a different route to the virtual client subnet. The following should enable proxy ARP in the configuration required. ip addr add 192.168.8.1/24 dev tap0 echo 1 > /proc/sys/net/ipv4/conf/tap0/proxy_arp echo 1 > /proc/sys/net/ipv4/conf/eth0/proxy_arp Once this is set up, you should be able to get full communication between VPN clients and servers on the corporate LAN. Test this by pinging the remapped address of a machine on the LAN (from 10.22.8.10 to 10.22.1.12) from the client and the non-remapped client address from said machine (from 192.168.1.12 to 192.168.8.10).

Other details

As with any NAT, some things will break over this connection. Anything with IP addresses hard coded will not quite work right due to the remapping.

Also, you can not use the same DNS entries for things in the private LAN. DNS must return remapped addresses to VPN clients on remapped networks. The simplest solution is to run a DNS server on the VPN server that has edited copies of the zone files from the master DNS for the subnet. Ideally, there would be a simple proxy DNS server that did the remapping, but I have been unable to find such a thing.

OpenVPN在Windows下使用User/Pass验证

OpenVPN在Windows下使用User/Pass验证

对于Windows下使用User/Pass验证已经是很久以前的承诺了,因为一开始一直都是在找寻 使用CMD(bat)文件检查用户名/密码的方式,但是一直没有结果,最后使用C写了一个小程 序实现用户名/密码验证。

转载请注明出处,如有疑问访问: http://wenzk.cublog.cn 反馈。

Windows安装OpenVPN是很容易的,OpenVPN GUI下载网址:

http://openvpn.se/files/install_packages/openvpn-2.0.7-gui-1.0.3-install.exe

记得选上easy-rsa这部分脚本,安装完毕后,easy-rsa在C:\\Program Files\\OpenVPN\\目录下。

把easy-rsa目录下的vars.bat.sample改名为vars.bat,并且修改其内容: ================================== set KEY_COUNTRY=CN set KEY_PROVINCE=Liaoning set KEY_CITY=Shenyang set KEY_ORG=OpenVPN

set KEY_EMAIL=elm@elm.freetcp.com

================================== 其它部分就不用修改了,上面部分修改成你自己的配置。

把easy-rsa下的openssl.cnf.sample改成openssl.cnf。

然后进入cmd.exe

============================================= Microsoft Windows XP [版本 5.1.2600] (C) 版权所有 1985-2001 Microsoft Corp.

C:\\Documents and Settings\\Administrator>cd \easy-rsa\

C:\\Program Files\\OpenVPN\\easy-rsa>vars

C:\\Program Files\\OpenVPN\\easy-rsa>clean-all.bat 系统找不到指定的文件。 已复制 1 个文件。 已复制 1 个文件。

C:\\Program Files\\OpenVPN\\easy-rsa>

生成Root CA 格式: build-ca.bat

输出: keys/ca.crt keys/ca.key

======================================================================

C:\\Program Files\\OpenVPN\\easy-rsa>build-ca.bat Loading 'screen' into random state - done Generating a 1024 bit RSA private key

................................................................................ ....++++++

....................................++++++ writing new private key to 'keys\\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) [OpenVPN]:

Organizational Unit Name (eg, section) []:OpenVPN ORG

Common Name (eg, your name or your server's hostname) []:OpenVPN ROOTCA Email Address [elm@elm.freetcp.com]:

生成dh1024.pem文件,Server使用TLS必须使用的一个文件。 格式: build-dh.bat 输出: keys/dh1024.pem

============================================================================

C:\\Program Files\\OpenVPN\\easy-rsa>build-dh.bat Loading 'screen' into random state - done

Generating DH parameters, 1024 bit long safe prime, generator 2 This is going to take a long time

............................................................+.................+. ................................................................................ ................................................................................ ....+..................+...........................+..........................+.

.........................+............................+.+....................... ............................................+......+...+...............+........ ..+...........+............+.....................+...+.........................+ .....+..............................................................+........... ...............+....................................+.......................+... .....................................................+.......................... ..................................................+............................. ......................................+..............+.+........................ +..........................................................................+.... ................................................................+............... ......................................+...+.............................+....... ............+...........+................+...................................... .........+...........................................+.......................... ................................................................................ .+.......+....+..............+.................................................. .........................................................................+...... ..........+..................................................................... ................................................................................ ...........................+.................................................... ........+....................................................................... ...................................................+..............+.........+... ........................................+.........+...................+......... .............+.......+..........+............+................+................. ................................................................................ ................................................................................ .................................+.................................++*++*++*

C:\\Program Files\\OpenVPN\\easy-rsa>

下面开始生成Server使用的证书: 格式: build-key-server.bat

输出: keys/.crt .csr .key

================================================================================

C:\\Program Files\\OpenVPN\\easy-rsa>build-key-server.bat server01 Loading 'screen' into random state - done Generating a 1024 bit RSA private key ...............++++++ ...........++++++

writing new private key to 'keys\\server01.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) [OpenVPN]:

Organizational Unit Name (eg, section) []:OpenVPN ORG

Common Name (eg, your name or your server's hostname) []:Server01 Email Address [elm@elm.freetcp.com]:

Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []: Using configuration from openssl.cnf Loading 'screen' into random state - done 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:'OpenVPN' organizationalUnitName:PRINTABLE:'OpenVPN ORG' commonName :PRINTABLE:'Server01'

emailAddress :IA5STRING:'elm@elm.freetcp.com'

Certificate is to be certified until Jul 7 13:33:23 2016 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

C:\\Program Files\\OpenVPN\\easy-rsa>

下面生成ta.key文件

格式: openvpn --genkey --secret keys/ta.key 输出: keys/ta.key

=========================================================================

C:\\Program Files\\OpenVPN\\easy-rsa>openvpn --genkey --secret keys/ta.key

C:\\Program Files\\OpenVPN\\easy-rsa>

OK,那些keys就搞定了,下面开始写配置文件。 server01.ovpn内容:

----------------CUT Here------------- port 1194 ; proto tcp proto udp ; dev tap dev tun

;dev-node MyTap ca ca.crt

cert server01.crt

key server01.key # This file should be kept secret dh dh1024.pem

server 10.8.0.0 255.255.255.0 ifconfig-pool-persist ipp.txt client-to-client ;duplicate-cn keepalive 10 120

tls-auth ta.key 0 # This file is secret auth-user-pass-verify checkpsw.exe via-env client-cert-not-required username-as-common-name

;cipher BF-CBC # Blowfish (default) ;cipher AES-128-CBC # AES ;cipher DES-EDE3-CBC # Triple-DES comp-lzo ;max-clients 100 user nobody group nobody persist-key persist-tun status status.log

;log /var/log/openvpn.log

;log-append /var/log/TCP_openvpn.log verb 4 ;mute 20

----------------CUT Here-------------

把配置文件放到C:\\Program Files\\OpenVPN\\config\\目录下。

把easy-rsa\\keys\\下的 ca.crt server01.crt server01.key ta.key dh1024.pem 复制到server01.ovpn所在目录。

同时下载本人写的破烂验证程序(checkpsw.exe)[不要仍砖块]放到OpenVPN配置目录下 程序在Windows XP SP2下测试通过,其他系统如果有问题,可以用源文件进行编译 程序在压缩包里面:

文件: checkpsw.rar 大小: 9KB 下载: 下载

在checkpsw.exe目录下建立password.txt[用于存放用户名&密码]文件: password.txt文件格式:

用户名 密码 是否活动(0/1) 中间用空格隔开 Username Password Active -------------Cut Here--------------------- wzk wzk 1

-------------Cut Here---------------------

Server的配置已经结束,可以启动Server了,在右下角OpenVPN-gui上点右键,然后选择connected。 需要服务器启动后自动运行,修改 \控制面板\下面的 \管理工具\下的 \服务\把OpenVPN设置成自动启动。

Client的配置文件: client.ovpn

-------------Cut Here--------------------- client dev tun proto udp

remote 61.1.1.2 1194 ;remote my-server-2 1194

;remote-random

resolv-retry infinite nobind user nobody group nobody

route 192.168.0.0 255.255.252.0 persist-key persist-tun

;http-proxy-retry # retry on connection failures ;http-proxy [proxy server] [proxy port #]

ca ca.crt auth-user-pass

ns-cert-type server tls-auth ta.key 1 comp-lzo

# Set log file verbosity. verb 4

--------------Cut Here---------------------

并且把easy-rsa/keys下的ca.crt ta.key一起放到Client的 \\config目录下。

Client的配置已经结束,可以连接Server了,在右下角OpenVPN-gui上点右键,然后选择connected。

OK,整个配置就完成了。

#include #include #define MAX 1024

int checkpsw(char *username, char *password) { FILE *f;

char user[MAX+2], pass[MAX+2], active[MAX+2]; /* printf(\

if (!(f=fopen(\{ perror(\ }

printf(\

printf(\return(1);

while(!feof(f)) {

fscanf(f,\

printf(\

if (!strcmp(username,user) && !strcmp(password,pass) && !strcmp(active,\ { }

printf(\fclose(f); return (0);

/*

/*

} {

} fclose(f); return (1);

void main()

int status;

/* printf(\ }

checkpsw.exe OpenVPN用户名/密码验证程序

password.txt checkpsw.exe验证程序使用的用户名和密码程序 checkpsw.c checkpsw.exe源代码文件(TC)

password.txt格式

用户名 密码 是否活动(0/1) 中间用空格隔开 Username Password Active

-------------Cut Here--------------------- wzk wzk 1

-------------Cut Here---------------------

Blog: http://wenzk.cublog.cn

status=checkpsw(getenv(\return (status);

OPENVPN安装手册

来源:www.vfocus.net 作者:vfocus 发布时间:2009-08-18

by:http://blog.vfocus.net

一、在网上下载openvpn的安装包http://openvpn.net/上下载openvpn的最新版,时至今 日是openvpn-2.0.9.tar.gz。

网上都说还需要lzo这个压缩库。这个包是压缩的,我没有安装,在编译程序的时候只需要加上--disable-lzo就可以了。然后再配置文件里面把这项注释掉就可以了,这样所有要安装的就是一个包openvpn-2.0.9.tar.gz。 二、vpn服务端的网络结构

vpn服务端是有两个ip 其中eth0 172.16.6.79 提供vpn服务,由外部通过172.31.16.0/24 通过1194端口拨入,然后通过 eth1 192.168.253.79访问192.168.253.0/24去维护服务器 三、安装openvpn

tar zxvf openvpn-2.0.9.tar.gz cd openvpn-2.0.9

./configure –prefix=/usr/local/openvpn –-disable-lzo make;make install

四、生成openvpn证书

1、cd openvpn-2.0.9/ easy-rsa/ vi vars

根据自己的情况设置

根据自己的情况设置KEY_COUNTRY, KEY_PROVINCE, KEY_CITY, KEY_ORG, KEY_EMAIL,比如我的 export KEY_COUNTRY=CN export KEY_PROVINCE=Beijing export KEY_CITY=Beijing export KEY_ORG=abc

export KEY_EMAIL=abc@123.com 然后保存

. ./vars //注意两个点中间有空格 ./clean-all ./build-ca

build-ca 命令使用openssl 命令生成certificate authority (CA) certificate 和密钥:

./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) [CN]:

State or Province Name (full name) [Beijing]: Locality Name (eg, city) [Beijing]: Organization Name (eg, company) [BUAA]: Organizational Unit Name (eg, section) []:

Common Name (eg, your name or your server's hostname) []:zhang //这里随便输入即可 Email Address [zhangwenhao@wanmei.com]: 这些步骤一直按回车就可以了 2、为vpn server生成证书与密匙 ./build-key-server server

Common Name 处填server,其他默认,为上面编辑vars 文件时设置的值。\[y/n]\和\两处选y。 3、为vpn clients 生成证书与密匙 ./build-key client1 ./build-key client2 ./build-key client3

Common Name 处分别填client1 client2 client3,别的同vpn server 设置 4、生成diffie hellman 参数 ./build-dh

现在生成了所需的文件。目录keys 下生成的文件中,ca.crt 是所有vpn 机器都需要的。vpn server 需要ca.crt ca.key dh1024.pem server.crt server.key,拷贝到vpn server 的/etc/openvpn目录。vpn client1 需要ca.crt ca.key lient1.crt client1.key,拷贝到vpn client1 的/etc/openvpn 目录。client2,client3 和client1 同,只是client1.crt 和client1.key 分别用client2 和client3 的对

应文件。vpn client2 和vpn client3 也把对应的密钥文件拷贝到各自的/etc/openvpn 目录。 五、配置server端与client端

1、server.conf 使用代码包目录sample-config-files 里的server.conf 修改即可。配置文件如下()

################################################# ;local a.b.c.d 绑定的IP 地址,可以不填 port 1194 绑定的端口

proto tdp 选择udp 协议,也可用tcp

# \# \;dev tap

#使用tun 设备,路由IP 通道。FreeBSD 默认支持TUN/TAP 驱动。 dev tun

#下面4 处填上面为vpn server 生成的证书和密钥

ca /etc/openvpn/ca.crt (如果启动的时候带了cd路径,则这里不需要绝对路径) cert /etc/openvpn/server.crt

key /etc/openvpn/server.key # This file should be kept secret dh /etc/openvpn/dh1024.pem

#配置vpn 虚拟网段,会在vpn server 建立一条10.0.0.1 >10.0.0.2 的点对点虚拟链路。 server 10.8.0.0 255.255.255.0

#记录client 对应得虚拟地址,默认600 秒更新一次 ifconfig-pool-persist ipp.txt

#等价于ping 10 和ping-restart 120,对于vpn 机器有可能断开外网联线或者重起的, #或者vpn client 先于vpn server 运行的情况下很重要。Client 会重新连接。 keepalive 10 120

#开启压缩支持。如果编译的时候没有使用LZO 库则注释本行 #comp-lzo

#初始化完成后改变OpenVPN 进程的用户ID 为nobody user nobody

#同上,改变进程的组ID 为nobody。如果是Windows 系统,注释掉这两行 group nobody persist-key persist-tun

#默认每分钟更新状态文件,可以看到client 的真实IP 虚拟IP 等信息。 status openvpn-status.log verb 3

2、client端

client1.conf 使用源代码包目录sample-config-files 里的client.conf 修改即可 client dev tun

proto tcp #和server.conf 一致即可

remote 172.16.6.79 1194 #指定vpn server 的IP 地址和端口 resolv-retry infinite #如果解析vpn server 失败则无限次重试 nobind user nobody group nobody persist-key persist-tun ca ca.crt

cert client1.crt #这2 行填给client1 生成的密钥文件。client2 client3 相应更改。 key client1.key #comp-lzo

keepalive 10 120

如果有其他的客户端需要连过来 也采取同样的配置 3、分配client端的联入地址 给server.confi添加如下行: client-config-dir ccd

并在vpn server 的/etc/openvpn 目录建立ccd 子目录,并在ccd 目录下建立文件client1,client2,client3(这里的文件名对应”为vpn clients 产生证书和密钥”步骤里的Common Name),当对应得client 连接时,vpn server 会读去对应文件里的配置信息。文件client1 里输入:

ifconfig-push 10.8.0.5 10.8.0.6,即给vpn client1 指定10.8.0.5,对端10.8.0.6 的虚拟链路。 如果有其他的客户端也同样处理,如

client2 内容:ifconfig-push 10.8.0.9 10.8.0.10 client3 内容:ifconfig-push 10.8.0.13 10.8.0.14

分别拷贝server.conf,client1.conf,client2.conf,client3.conf 到vpn server 和对应的client。 4、启动vpn

分别在各个vpn 计算机启动vpn server 和vpn client 启动server的时候用 守护进程启动

./openvpn –daemon –config /etc/openvpn/server.conf 启动client的时候 用client的配置文件

./openvpn –daemon –config /etc/openvpn/client.conf

查看端口1194端口是否起来,如果两个都起来则查看ifconfig 看vpn网卡是否起来

到这一步,VPN 客户端和服务端可以相互通讯了。10.8.0.1 应该可以ping 通10.8.0.5,10.8.0.9,10.8.0.13。10.8.0.5,10.8.0.9,10.8.0.13 也可以ping 通10.8.0.1

但是此时还仅仅是vpn 客户端与服务器可以通,但此时,vpn的客户端还不能访问服务器端同网段内机器

5、 vpn client 访问vpn server 所在172.16.6.0/24 网段内计算机 第一种方法,添加如下行到server.conf push \

push 选项是把选项内容给连接的vpn 客户端执行,也就是在vpn 客户端执行“route 172.16.6.0 255.255.255.0”,即当client 连接时,添加路由到client 的路由表里。对于vpn client1,该选项相当于在vpn client1 的路由表里添加到网络172.16.6.0/24,下一跳为10.8.0.6的路由,这里10.8.0.6 为给vpn client1 分配的虚拟IP 对的服务器端IP,相当于路由到vpn server。

第二种方法,如果不改变 server.conf,在vpn client1 里route add –net 172.16.6.0 10.8.0.6 255.255.255.0 效果一样,也可route add –net 172.16.6.0 10.8.0.1 255.255.255.0 直接路由到vpn server 的本端IP 也可,但这样需要在所有的vpn 客户端改变路由表。

这样在vpn 客户端可以ping 通172.16.6.0/24 网段内部计算机了。到此,vpn客户端可以访问服务端同网段的服务器了。 6、总结

在vpn server 的/etc/rc.local 文件添加如下行:

/usr/local/openvpn/openvpn –daemon --config /etc/openvpn/server.conf --cd /etc/openvpn/ 在vpn client1 的/etc/rc.local 文件添加如下行:

/usr/local/openvpn/openvpn –daemon --config /etc/openvpn/client1.conf --cd /etc/openvpn/ 这样完成了所有的配置。下面总结如下:

vpn server 的目录/etc/openvpn 下有文件ca.crt,ca.key,dh1024.pem,server.crt,server.key和server.conf 以及子目录ccd。/etc/openvpn/ccd 目录下有文件client1,client2 和client3。 /etc/openvpn/server.conf 内容如下: ;local a.b.c.d port 1194 proto tcp dev tun ca ca.crt cert server.crt

key server.key # This file should be kept secret dh dh1024.pem

server 10.8.0.0 255.255.255.0 ifconfig-pool-persist ipp.txt keepalive 10 120 comp-lzo user nobody group nobody persist-key persist-tun

status openvpn-status.log verb 3

client-config-dir ccd

#使vpn clients 能访问vpn server 内部网段计算机 push \

#route 172.31.13.0 255.255.255.0 #使vpn server 能访问中文网计算机 /etc/openvpn/ccd/client1 内容如下: ifconfig-push 10.8.0.5 10.8.0.6 iroute 172.31.13.0 255.255.255.0

vpn client1 的目录/etc/openvpn 下有文件ca.crt,ca.key client1.crt,client1.key 和client1.conf。

/etc/openvpn/client1.conf 内容如下:

############################################## # Sample client-side OpenVPN 2.0 config file # # for connecting to multi-client server. # ############################################## client dev tun proto udp

remote 172.16.6.79 1194 resolv-retry infinite nobind user nobody group nobody persist-key persist-tun ca ca.crt cert client1.crt key client1.key comp-lzo verb 3

keepalive 10 120

VPN详细配制 OPENVPN

(2009-08-14 11:51:45) 转载

标签: 分类: 服务器配置

vpn crt program log ca it

有时回家想继续白天未完成的工作,在家里需要控制公司的电脑,但公司电脑在内网,先想到远程控制软件,外网控制内网像windows自带3389、radmin、pcanywhere都用不上了,用了几款马木级别的“自由远程管理系统”、灰鸽子、pcshare,在命令行下还凑和,但桌面控制就力不从心了,又试了TeamViewer,这款软件有绿色版使用简单功能却很强大,,但数据毕竟是通过服务器中转的,速度也凑和着可以了,就是有致使的弱点,当对方机器锁住后就连不了了,还老断线

又想到了VPN,但偶肯定不会因为要搭建VPN服务器把xp换了,但VPN软件也蛮多的,试过VPN-X是最方便好用的,不过需要java环境,试用版只支持两个用户,这也够我用了,最气人的是前几天升级到最新版本后服务器指示灯就是不变绿,没辙了,上网找破解版,竟然全是病毒

又继续找免费的VPN服务器,茫茫baidu中看到了OpenVPN,有了Open这字眼肯定是开源了,嘿嘿,就是他,不过看了下安装方法蛮复杂的,试了一下,效果非常的好,先组网再用3389,速度很快(ping大概60左右)

---------------------------------------------------------------------------------------- 下面着重介绍

OpenVPN了

简介:

OpenVPN是一款功能强大,可跨平台(支持Win 2000/XP/2003, Linux, Mac OS X, Solaris, FreeBSD, NetBSD, 和 OpenBSD)使用的SSL VPN服务器软件。 官方网址:http://openvpn.net/

下载地址:http://openvpn.net/download.html#stable 放了一份在网络硬盘里 http://loverosa.ys168.com/

文件夹openvpn下的openvpn-2.0.5-gui-1.0.3-install.rar,才999KB 目前最新版本是2.0.9,我使用的2.0.5

双击 openvpn-2.0.5-gui-1.0.3-install.exe进行安装,点击NEXT,I Agree,NEXT之后开始选择安装路径,我手动修改为C:\\Program Files\\OpenVPN 。

点击 Install 开始安装,安装过程中,弹出 硬件安装窗口,点击仍然继续,安装虚拟网卡。点击 next,Finish 完成安装。

说明: 架设OPENVPN服务器,服务器方面做的工作比较多,客户端相对来说就比较简单。 服务器采用RSA证书和密钥验证方式对客户端进行验证,默认情况下证书和用户是一对一的,多个用户使用同一证书会被踢出。所以首先要做的工作就是证书的制作。

在进行操作之前,首先进行初始化工作:

修改C:\\Program Files\\OPENVPN\\easy-rsa\\vars.bat.sample的以下部分 set HOME=%ProgramFiles%\\OpenVPN\\easy-rsa set KEY_COUNTRY=US set KEY_PROVINCE=CA set KEY_CITY=SanFrancisco set KEY_ORG=FortFunston set KEY_EMAIL=mail@domain.com

(请根据自身情况修改)改为

set HOME=C:\\Program Files\\OPENVPN\\easy-rsa set KEY_COUNTRY=CN #(国家) set KEY_PROVINCE=GuangDong #(省份) set KEY_CITY=ShenZhen #(城市) set KEY_ORG=iPanel #(组织) set KEY_EMAIL=kundy@163.com #(邮件地址)

打开命令行窗口(开始->运行->cmd),进入openvpn\\easy-rsa目录,开始初始化

*******第一次初始化************ C:

cd \回车 init-config vars clean-all

*******初始化结束************

上面是初始化工作,以后,在进行证书制作工作时,仍旧需要进行初始化,但只需要进入easy-rsa目录,运行vars就可以了,不需要上面所有的步骤了。

*******初始化(不是第一回)******** C:

cd \回车 vars clean-all

*******初始化结束************

下面开始证书的制作(这是最关键的了):

*******证书制作开始******** build-ca #生成根证书 build-dh

build-key-server server # 生成服务端密钥 build-key client #生成客户端密钥 *******证书制作结束********

生成的密钥存放于openvpn\\easy-rsa\\keys目录下

接下来开始配置服务器 将生成的 ca.crt dh1024.pem server.crt server.key

复制到C:\\Program Files\\OPENVPN\\KEY目录下,这四个文件是服务端运行所需要的文件。 在\\OpenVPN\\config目录下创建server.ovpn文件,可以打开记事本,把内容写进去之后,再另存为server.ovpn文件。以下是server.ovpn参数说明及示例

#******server.ovpn参数详解开始****** #申明本机使用的IP地址,也可以不说明 ;local 192.168.24.34

#申明使用的端口,默认443 port 443

#申明使用的协议,默认使用UDP,如果使用HTTP proxy,必须使用TCP协议 ;proto tcp proto udp

#申明使用的设备可选tap和tun,tap是二层设备,支持链路层协议。 #tun是ip层的点对点协议,限制稍微多一些,建议使用tun

dev tap ;dev tun

#OpenVPN使用的ROOT CA,使用build-ca生成的,用于验证客户是证书是否合法 ca ca.crt

#Server使用的证书文件 cert server.crt

#Server使用的证书对应的key,注意文件的权限,防止被盗 key server.key

#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 \把这条路由发送给客户端,客户连接成功后自动加入路由表,省略了下一跳地址: 10.8.0.1 # else

# push \否则发送本条路由,这是一个主机路由,省略了子网掩码和下一跳地址,分别为: 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传递给客户机 #

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 \;push \

#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 \

#DHCP的一些选项,具体查看Manual ;push \;push \

#如果可以让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

#******server.ovpn参数详解结束******

中间一段太复杂,建议参看下面示例写一个,路径建议加上引号,斜杆\\是双的

#******server.ovpn示例开始******** port 443 proto tcp-server dev tun

server 10.8.0.0 255.255.255.0 duplicate-cn keepalive 20 180

ca \cert \key \dh \push \mode server tls-server

status C:\\\\Program Files\\\\OPENVPN\\\\log\\\\openvpn-status.log comp-lzo verb 4

#******server.ovpn示例结束********

客户端设置: ca.crt client.crt client.key

是客户端所需要的文件,复制到客户端C:\\Program Files\\OPENVPN\\KEY目录下

在客户端安装完成之后,需要将 ca.crt client.crt client.key 这三个文件拷贝到 openvpn\\key目录下,这三个文件由服务端生成,所以,连接谁的服务器,就需要跟谁索取这三个文件 然后,编辑一个 client.ovpn的配置文件存放到 openvpn\\config目录下,客户端就可以进行连接了。

客户端文件示例:(client.ovpn)

#******client.ovpn参数详解开始****** 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.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 3

#******client.ovpn参数详解结束******

更复杂了,建议还是参照下面的示例写一个

#******client.ovpn示例开始******** client dev tun proto tcp-client

remote kundy.2288.org 443 resolv-retry infinite nobind

#http-proxy 192.168.1.1 80 mute-replay-warnings

ca \cert \key \comp-lzo verb 4

status openvpn-status.log

#******client.ovpn示例结束********

最后,鼠标右击ovpn文件,选择start openvpn...即可,或运行OpenVPN GUI,注意看日志

本文来源:https://www.bwwdw.com/article/jp0p.html

Top