大用户量下Openvpn部署方案(一)

来源:互联网 发布:博时沪深300基金知乎 编辑:程序博客网 时间:2024/05/15 13:10

前段时间公司有个项目需要数以千计的客户端都连入一个vpn下,并且有一部分需要获得固定的ip地址。

思路:简单的openvpn服务器部署起来并不难,但是大量的用户,如果采用简单的证书方式,批量生成客户证书不难,但是如此大量的证书如何分发给客户也是个问题。所以经过考虑采用用户名密码的验证方式,然后批量生成连续的用户名和密码,客户端分发可以给与客户端一个用户名密码段,由客户自行分发。

本文先介绍第一部分:配置基本的openvpn服务器

  1. 下载安装openvpn
    我用的是2.2.1版本的。openvpn官网天朝是访问不了的需翻墙。
    openvpn依赖于openssl库且使用lzo(lzo 和lzo-devel)进行压缩,安装前请执行yum install openssl* 和 yum install lzo*

    VPN ~]# tar zxvf openvpn-2.2.1.tar.gzVPN ~]# cd openvpn-2.2.1VPN ~]# ./configure --prefix=/usr/local/openvpn --with-lzo-headers=/usr/include --with-lzo-lib=/usr/lib         VPN ~]# makeVPN ~]# make install

    安装过程中若有报错,多是依赖的包未安装的问题,baidu/google之。

  2. 生成根证书、服务端证书以及客户端证书

    VPN ~]# cd /usr/local/src/openvpn-2.2.1/easy-rsa/2.0/注:从2.3.0版本开始,easy-rsa组件已经作为一个独立的项目,不再附带与openvpn的安装包里VPN ~]# vi vars                      #修改变量,亦可在稍后生成证书的过程中逐项手动更改    ...   export KEY_COUNTRY="US"   export KEY_PROVINCE="CA"   export KEY_CITY="SanFrancisco"   export KEY_ORG="Fort-Funston"   export KEY_EMAIL="me@myhost.mydomain"   export KEY_EMAIL=mail@host.domain   export KEY_CN=changeme   export KEY_NAME=changeme   export KEY_OU=changeme      export PKCS11_MODULE_PATH=changeme   export PKCS11_PIN=1234编辑好vars文件后,执行  VPN ~]# ./vars                                #使修改的变量值生效VPN ~]# ./clean-all                           #清除keys目录中所有文件 VPN ~]# ./build-ca                            #按提示执行完所有步骤VPN ~]# ./build-key-server server-key-name    #生成服务端证书及keyVPN ~]# ./build-key client                    #生成客户端证书及key,多个客户端可执                                          行多次此步骤生成多个client端文件VPN ~]# ./build-dh                            #生成传输进行密钥交换时用到的交换密钥协议所需 文件
  3. 在/etc下建立openvpn文件夹以便于集中管理openvpn配置文件以及证书和key文件

    VPN ~]# cd /usr/local/src/openvpn-2.2.1/easy-rsa/2.0
    VPN ~]# mkdir -p /etc/openvpn
    VPN ~]#cp -Rp keys /etc/openvpn
    VPN ~]#cd /usr/local/src/openvpn-2.2.1/sample-config-files
    VPN ~]#cp server.conf /etc/openvpn/
  4. 修改服务器端配置文件(主要解释一下我们会用到的,其余看注释基本应该可以明白)

    [root@51-DB 2.0]# grep -v "#" /etc/openvpn/server.conf | tr -s '\n';local a.b.c.d                                  #默认监听本地所有网卡,可修改port 1194;proto tcpproto udp dev tunca /etc/openvpn/keys/ca.crt                     #根证书及服务器端证书以及dh文件路径,用绝对路径cert /etc/openvpn/keys/mysqlvpn.crtdh /etc/openvpn/keys/dh1024.pemtopology subnet                                 #官方推荐的拓扑,但是不是默认的,默认的是net30                                                #可参考https://community.openvpn.net/openvpn/wiki/Topologyserver 10.8.0.0 255.255.0.0                     #分配给客户端的IP网段,此处默认是24位的掩码,我们根据客户端数量可以改为16位的掩码ifconfig-pool-persist /etc/openvpn/ipp.txt      #此文件用于记录每个客户端所获取到的ip尽量保证客户端每次都获取到同样的ip.如果通过第二篇所讲的为每个客户端指定IP,则不在此文件记录push "route 192.168.100.0 255.255.255.0"        #向客户端推送一条路由,可以让客户端访问到服务器后端的局域网的机器client-to-client                                #是否允许各客户机之间可见,;duplicate-cn                                   #是否设置多个客户端共用一套客户端证书keepalive 10 120comp-lzo                                        #启用lzo压缩max-clients 1000                                #控制最大客户端数量user nobody                                     #以nobody用户运行group nobodypersist-keypersist-tunstatus    /etc/openvpn/openvpn-status.log                  ;log         openvpn.loglog-append    /var/log/openvpn.log              #日志选项,将日志追加到指定的文件中verb 4                                          #此选项参数控制日志输出信息的详细程度;mute 20

    启动openvpn:
    /usr/local/openvpn/sbin/openvpn --daemon --config /etc/openvpn/server.conf

  5. 客户端设置
    关于客户端设置,之前走了些弯路吧,一直在寻找openvpn的客户端(最终寻得可用的客户端为“openvpn-2.0.9-gui-1.0.3-install.exe”和“openvpn-client.msi”)。但是,后来发现官方就提供了客户端,例如对应2.2.1版本的“openvpn-2.2.1-install.exe”,之前的过程有点舍近求远了。

    (openvpn-2.0.9-gui-1.0.3-install.exe和openvpn-2.2.1-install.exe安装后的目录结构和用法一致)

    客户端要安装openvpn gui工具(这个找起来还挺麻烦,还是翻墙去官网下载比较靠谱)用2.09版本的可以。

    安装客户端,将对应的文件(ca证书、client证书、client key)拷贝至客户端安装目录下的config目录,客户端可将安装目录下的sample-config文件夹下的client.ovpn拷贝至openv-gui安装目录下的config,修改将remote my-server 1194中的my-server改为自己服务器的ip地址即可。

    注意:win7环境“OpenVPN GUI”要以管理员运行,同时下客户端配置文件还需增加两条配置

    route-method   exeroute-delay    2

    这样才能正确的在机器中添加路由,否则会在Log文件中看到类似的信息:
    ROUTE: route addition failed using CreateIpForwardEntry: 至少有一个参数不正确。

    注:openvpn gui在没有将各种证书及key导入config文件夹之前右键点击是如下显示的
    image
    没有连接选项,正确添加根证书及用户key之后,会显示如下
    image
    到这里如果正常的话,就已经可以连接到openvpn服务器了,在本机可以ping通服务器的tun0及其他网口的ip。但通常仅这样是无法满足我们需求的,我们还需要从本机能连通服务端所在内网的其他机

  6. 服务器端iptables和route相关的配置
    引用一段官网的内容

    Once the VPN is operational in a point-to-point capacity between client and server, it may be desirable to expand the scope of the VPN so that clients can reach multiple machines on the server network, rather than only the server machine itself.

    For the purpose of this example, we will assume that the server-side LAN uses a subnet of 10.66.0.0/24 and the VPN IP address pool uses 10.8.0.0/24 as cited in the server directive in the OpenVPN server configuration file.

    First, you must advertise the 10.66.0.0/24 subnet to VPN clients as being accessible through the VPN. This can easily be done with the following server-side config file directive:

    push "route 10.66.0.0 255.255.255.0"

    Next, you must set up a route on the server-side LAN gateway to route the VPN client subnet (10.8.0.0/24) to the OpenVPN server (this is only necessary if the OpenVPN server and the LAN gateway are different machines).

    必须在服务器端的内网网关上将到10.8.0.0/24网段的路由指向到openvpn服务器,不然从服务器端内网其他机器根本找不到去往10.8.0.0/24网段的路由。这里又分两种情况,一种是服务端有内网网关设备的(按如上说法即可);一种是服务端内网没有网关设备,即服务器通过交换机相连,相互通讯靠广播的情况。我的就是这种情况。需要在想访问的server上增加到10.8.0.0/24的路由,如下
    route add -net 10.8.0.0/24 gw 192.168.1.211 #1.211为openvpn服务器的内网IP
    Make sure that you’ve enabled IP and TUN/TAP forwarding on the OpenVPN server machine.

    确定开启了转发功能,然后在openvpn服务器Iptables添加如下两条规则

    iptables -A FORWARD -i tun0 -s 10.8.0.0/24 -j ACCEPT    #简单说,允许数据从客户端到后端serveriptables -A FORWARD -i em2 -d 10.8.0.0/24 -j ACCEPT    #允许数据从后端server到客户端

    另外,如果想让客户端的出口ip变成vpn server的ip,需要 在server.conf配置文件里将下面行的注释放开
    push "redirect-gateway def1"
    然后在iptables添加如下两条规则

    filter表:-A FORWARD -s 10.8.0.1/24 -o em1 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT-A FORWARD -d 10.8.0.1/24 -i em1 -m state --state RELATED,ESTABLISHED -j ACCEPTnat表:-A POSTROUTING -s 10.8.0.1/24 ! -d 192.168.1.0/24 -o em1 -j MASQUERADE

至此,基本的基于证书认证的openvpn可说是实现了。下一篇将记录给客户端指定用户IP地址、批量生成用户名密码。

最后一点:在部署过程中不可能一帆风顺,有错误看日志:server端日志的存储位置在server.conf里可以指定,client端日志右击openvpn GUI软件可以查看。

再附上openvpn接收的几个信号,方便管理

Running on Linux/BSD/UnixOpenVPN accepts several signals:    SIGUSR1 -- Conditional restart, designed to restart without root privileges    SIGHUP -- Hard restart    SIGUSR2 -- Output connection statistics to log file or syslog    SIGTERM, SIGINT -- Exit
0 0
原创粉丝点击