一、原理 OpenVPN是基于SSL的VPN,其使用工业标准的SSL/TLS协议实现第2层和第3层的安全数据链路VPN。
最新版本是2.0.7,其优点包括:1、基于SSL协议,安全,并使用单一TCP或UDP端口即可实现;2、使用双向验证,服务器只需保存自己的证书和密钥;3、服务器只接受那些由主CA证书签名的客户端,并有撤回机制,而不需要重建整个PKI;4、可以实现基于Common Name的权限控制。
※注意,OpenVPN是基于SSL/TLS协议的,所以是不兼容IPSec和PPTP,在Windows也需要安装客户端二、网络架构 OpenVPN可以实现多点对单点,以及点对点的VPN功能。
今天我们先以一个简单的案例来说明,后续再详细描述更深入的配置。
架构如下说明:1、远程客户端通过Internet网络连接到公司网络(模拟成192.168.228.0网段),并建立10.8.0.0的VPN通道;2、公司内部网络为192.168.56.0网段,假设只有一台主机;3、目标是客户端和后台主机可以双向互通。
三、安装和配置 假设客户端和服务端都是Linux系统,如下:服务器平台: 红旗 DC Server 5.0 for x86客户端平台: 红旗 Desktop 5.0 for x861、服务端安装rpm -ivh lzo-1.08-4.redflag.i386.rpmrpm -ivh openvpn-2.0.7-2.redflag.i386.rpm1)示例配置文件在:引用/usr/share/doc/openvpn-2.0.7/2)拷贝创建CA证书的easy-rsa:cp -ra /usr/share/doc/openvpn-2.0.7/easy-rsa /etc/openvpn/3)拷贝示例配置文件cd /etc/openvpn/mkdir configcp -ra /usr/share/doc/openvpn-2.0.7/sample-config-files/server.conf config/ln -s config/server.conf ./※如果您今后希望Openvpn随系统自动启动,则它会自动寻找/etc/openvpn下的所有.conf配置文件,并运行。
所以,我这里先建立对应的链接了。
4)修改证书变量cd easy-rsavi vars根据您的实际情况,修改下面的变量:引用export KEY_COUNTRY=GZexport KEY_PROVINCE=GuangDongexport KEY_CITY=GuangZhouexport KEY_ORG="RFGZ"export KEY_EMAIL="linuxing@linuxfly.org"※这些变量在今后会用到,若修改,则必须重建所有的PKI。
5)初始化PKIsource varschmod +x clean-all./clean-all※小心,一旦运行clean-all,它会删除keys下的所有证书。
chmod +x build-ca./build-ca(Common Name可以自己定义,我这里用OpenVPN-CA)6)创建服务器的证书和密钥chmod +x build-key-server./build-key-server server(Common Name必须填写server,其余默认即可)7)创建客户端的证书和密钥chmod +x build-key./build-key client1./build-key client2(假设创建两个客户端,Common Name对应填写client1和client2,其作为今后识别客户端的标识)8)创建Diffie Hellman 参数Diffie Hellman 用于增强安全性,在OpenVPN是必须的:chmod +x build-dh./build-dh(时间比较常,若你修改了vars文件中的KEY_SIZE=1024,则时间会更长些)9)修改配置文件cd /etc/openvpnvi server.conf引用# 2006-08-11 由linuxing编写# #号和;号开头的都是注释# 设置监听IP,默认是监听所有IP;local a.b.c.dlocal 192.168.228.155# 设置监听端口,必须要对应的在防火墙里面打开port 1194# 设置用TCP还是UDP协议?;proto tcpproto udp# 设置创建tun的路由IP通道,还是创建tap的以太网通道# 路由IP容易控制,所以推荐使用它;但如果如IPX等必须# 使用第二层才能通过的通讯,则可以用tap方式,tap也# 就是以太网桥接;dev tapdev tun# Windows需要给网卡一个名称,这里设置,linux不需要;dev-node MyTap# 这里是重点,必须指定SSL/TLS root certificate (ca),# certificate(cert), and private key (key)# ca文件是服务端和客户端都必须使用的,但不需要ca.key# 服务端和客户端指定各自的.crt和.key# 请注意路径,可以使用以配置文件开始为根的相对路径,# 也可以使用绝对路径# 请小心存放.key密钥文件ca ./easy-rsa/keys/ca.crtcert ./easy-rsa/keys/server.crtkey ./easy-rsa/keys/server.key # This file should be kept secret# 指定Diffie hellman parameters.dh ./easy-rsa/keys/dh1024.pem# 配置VPN使用的网段,OpenVPN会自动提供基于该网段的DHCP# 服务,但不能和任何一方的局域网段重复,保证唯一server 10.8.0.0 255.255.255.0# 维持一个客户端和virtual IP的对应表,以方便客户端重新# 连接可以获得同样的IPifconfig-pool-persist ipp.txt# 配置为以太网桥模式,但需要使用系统的桥接功能# 这里不需要使用;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100# 为客户端创建对应的路由,以另其通达公司网内部服务器# 但记住,公司网内部服务器也需要有可用路由返回到客户端;push "route 192.168.20.0 255.255.255.0"push "route 192.168.56.0 255.255.255.0"# 为特定的客户端指定IP或指定路由,该路由通常是客户端后面的# 内网网段,而不是服务端连接的网段# ccd是/etc/openvpn下的目录,其中建有希望限制的客户端Common# Name为文件名的文件,并通过下面的命令写入固定IP地址# 例如Common Name为client1,则在/etc/openvpn/ccd/client1写有:# ifconfig-push 10.9.0.1 10.9.0.2;client-config-dir ccd;route 192.168.40.128 255.255.255.248# 为可以对不同的客户端设置防火墙等权限# 可以让其自动运行对应脚本,可参考man;learn-address ./script# 若客户端希望所有的流量都通过VPN传输,则可以使用该语句# 其会自动改变客户端的网关为VPN服务器,推荐关闭# 一旦设置,请小心服务端的DHCP设置问题;push "redirect-gateway"# 用OpenVPN的DHCP功能为客户端提供指定的DNS、WINS等;push "dhcp-option DNS 10.8.0.1";push "dhcp-option WINS 10.8.0.1"# 默认客户端之间是不能直接通讯的,除非把下面的语句注释掉client-to-client# 如果您希望有相同Common Name的客户端都可以登陆# 也可以注释下面的语句,推荐每个客户端都使用不用的Common Name# 常用于测试;duplicate-cn# 设置服务端检测的间隔和超时时间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# 使用lzo压缩的通讯,服务端和客户端都必须配置comp-lzo# 设置最大用户数;max-clients 100# 让OpenVPN以nobody用户和组来运行(安全);user 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-keypersist-tun# 输出短日志,每分钟刷新一次,以显示当前的客户端status /var/log/openvpn/openvpn-status.log# 缺省日志会记录在系统日志中,但也可以导向到其他地方# 建议调试的使用先不要设置,调试完成后再定义;log /var/log/openvpn/openvpn.log;log-append /var/log/openvpn/openvpn.log# 设置日志的级别## 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 verboseverb 3# Silence repeating messages. At most 20# sequential messages of the same message# category will be output to the log.;mute 20# 打开管理界面,可以定义监控的IP和端口management localhost 7505※几个需要注意的地方:a.定义tun为使用路由方式的VPNb.小心处理证书的路径,.key文件要保存好,特别是ca.key。
(ca.key不需要在OpenVPN中用到,可以另外保存)2、客户端安装 客户端的安装比较简单:1、服务端安装rpm -ivh lzo-1.08-4.redflag.i386.rpmrpm -ivh openvpn-2.0.7-2.redflag.i386.rpm1)示例配置文件在:引用/usr/share/doc/openvpn-2.0.7/2)从服务器拷贝客户端的证书到客户端首先在服务器上准备好需要的文件:cd /etc/openvpn/easy-rsa/keystar czvf client1.tar.gz ca.crt client1.crt client1.keyscp client1.tar.gz 192.168.228.201:/etc/openvpn然后客户端解压即可:cd /etc/openvpnmkdir -p easy-rsa/keystar xzvf client1.tar.gz3)拷贝示例配置文件mkdir configcp -ra /usr/share/doc/openvpn-2.0.7/sample-config-files/client.conf config/ln -s config/client.conf ./4)修改配置文件cd /etc/openvpnvi client.conf引用# 2006-08-11 由linuxing编写# #号和;号都是注释# 定义是一个客户端client# 定义使用路由IP模式,与服务端一致;dev tapdev tun# 定义Windows下使用的网卡名称,linux不需要;dev-node MyTap# 定义使用的协议,与服务端一致;proto tcpproto udp# 指定服务端地址和端口,可以用多行指定多台服务器# 实现负载均衡(从上往下尝试)remote 192.168.228.155 1194;remote my-server-2 1194# 若上面配置了多台服务器,让客户端随机连接;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 do not need to bind to# a specific local port number.nobind# 也是为了让Openvpn也nobody运行(安全)# 注意:Windows不能设置;user nobody;group nobody# Try to preserve some state across restarts.persist-keypersist-tun# 若客户端通过HTTP Proxy,在这里设置# 要使用Proxy,不能使用UDP为VPN的通讯协议;http-proxy-retry # retry on connection failures;http-proxy [proxy server] [proxy port #]# 无线网络有很多多余的头文件,设置忽略它;mute-replay-warnings# 重点,就是指定ca和客户端的证书ca ./easy-rsa/keys/ca.crtcert ./easy-rsa/keys/client1.crt# 如果服务端打开了PAM认证模块,客户端需要另其有效;auth-user-pass# 一些安全措施# 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# 使用lzo压缩,与服务端一致comp-lzo# Set log file verbosity.verb 3# Silence repeating messages;mute 203、启动服务1)先启动服务端cd /etc/openvpnopenvpn server.conf &2)然后启动客户端cd /etc/openvpnopenvpn client.conf &3)使用ifconfig,可以看到VPN通道已经建立:server端:4)测试server端:client端:※注意,每个虚拟tun网卡都是成对的,只有inet addr标识的才是用于VPN通讯。
并且必须在/30网段中,详见附录。
4、建立路由 其实到这里,OpenVPN部分的工作已经完成。
服务端和客户端的通讯已经正常。
但常见一个问题,就是客户端虽然可以访问到192.168.56.129,但却不能访问公司网内部的192.168.228.128服务器。
原因很简单,因为通讯是双方的,192.168.228.128没有到10.8.0.0网段的路由,有几种可能:a、VPN服务器与公司网的网关不是同一台机器,解决办法是在公司网网关上设置到10.8.0.0网段的路由,接口是VPN服务器网卡;b、192.168.56.128没有设置默认网关,解决办法是设置默认网关就是VPN服务器,或已经做了1配置的公司网网关;c、有防火墙屏蔽。
对于本示例中只有一台192.168.56.128,可以这样做:1)原路由表2)增加路由route add -net 10.8.0.0 netmask 255.255.255.0 gw 192.168.56.129 dev eth0路由表3)或增加默认网关route add default gw 192.168.56.129路由表※当然,实际应用中不应该出现这样手动配置路由的情况。
应该对公司网内部做好统一管理,以避免类似的问题。
四、附录1、示例配置文件可以从这里下载:服务端客户端2、为了兼容Windows的TAP驱动,所以每对tun都必须在/30子网中,如下列表:引用[ 1, 2] [ 5, 6] [ 9, 10] [ 13, 14] [ 17, 18][ 21, 22] [ 25, 26] [ 29, 30] [ 33, 34] [ 37, 38][ 41, 42] [ 45, 46] [ 49, 50] [ 53, 54] [ 57, 58][ 61, 62] [ 65, 66] [ 69, 70] [ 73, 74] [ 77, 78][ 81, 82] [ 85, 86] [ 89, 90] [ 93, 94] [ 97, 98][101,102] [105,106] [109,110] [113,114] [117,118][121,122] [125,126] [129,130] [133,134] [137,138][141,142] [145,146] [149,150] [153,154] [157,158][161,162] [165,166] [169,170] [173,174] [177,178][181,182] [185,186] [189,190] [193,194] [197,198][201,202] [205,206] [209,210] [213,214] [217,218][221,222] [225,226] [229,230] [233,234] [237,238][241,242] [245,246] [249,250] [253,254]3、OpenVPN安装后会自动加载到系统服务中,并启动。
可以手动关闭:chkconfig openvpn off4、如果已经打开防火墙,在使用OpenVPN的时候必须让其通过iptables -A INPUT -i tun+ -j ACCEPTiptables -A FORWARD -i tun+ -j ACCEPTiptables -A INPUT -i tap+ -j ACCEPTiptables -A FORWARD -i tap+ -j ACCEPT5、OpenVPN功能很强大,更多的配置请见后面的介绍。