打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
OpenVPN 安装配置资料总结和整理

一、关于 VPN

VPN(Virtual Private Network),虚拟私有网络,又名虚拟专用网络,是一种常用于大中型企业或团体之间的私有网络通信方法。VPN 透过公用的网络架构(例如:互联网)来传送内联网的网络信息,利用加密隧道协议(Tunneling Protocol)来达到保密、认证、准确等私有信息的安全要求。如果使用得法,这种技术可用不安全的网络(例如:互联网)来传送可靠、安全的信息,但需要注意的是,信息的加密与否是可以控制的,没有加密的虚拟私有网络信息依然有被窃取的危险。1

1.1 具体实现

VPN 有多种实现方式,比较常见的有 PPTPL2TPIPSec VPNSSL VPN 这几种。PPTPL2TP 都是基于 PPP 协议的虚拟隧道实现,L2TP 可以认为是 PPTP 的升级版。由于设计上的缺陷,使得 PPTPL2TP 无法正常通过 NAT 设备,并且数据传输存在安全隐患,一般是结合 IPSec 来实现完整的安全传输保障。IPSec 也可以完全独立实现 VPN,但因为配置繁琐而为人诟病。SSL VPN 是近年兴起的一种新的 VPN 实现,构建于完全开放的 SSL/TLS 协议,但由于缺乏统一的标准,不同的厂商实现不同,有种 SSL Web VPN,相比于其他 VPN 实现,其优点在于客户端只需有支持 SSL 的网络浏览器即可,无需额外的客户端。当然,大部分 SSL VPN 需要客户端支持,比如本文的主角 OpenVPN。2

二、服务器安装和配置

2.1 基本安装

OpenVPN 服务器可以架设在多种操作系统平台上(Linux、Windows NT 系列、OpenBSD、FreeBSD、NetBSD、Mac OS X、Solaris)。

OpenVPN 可以使用 TUN 或者 TAP 接口建立隧道,配置略有不同。TUN 接口创建的是三层路由隧道,建立方便;TAP 是二层网卡桥接隧道,即创建一个以太网桥接,相对复杂。TUN 接口下所有的客户端处于一个完全独立的子网内,与 VPN 服务器所在的子网没有关系;TAP 接口的好处相较之下则相当明显,客户端可以获得 VPN 服务器所处子网的 IP(即,忽略物理上的区别,可以完全将客户端看做是于 VPN 服务器处于同一子网的另一台机器)。

我们选定 OpenVPN 子网的网段为 10.8.0.0/24。

2.1.1 Linux

Linux 下可以选则 TUN 或 TAP 作为接口,但均需要内核支持。TUN接口下,如果需要访问 Internet,还需要配置 IP 转发,以及建立 DNS 服务。

2.1.1.1 内核支持

首先需要检查内核是否支持 TUN/TAP 设备驱动,运行 modinfo tun,如果输出 ERROR: modinfo: could not find module tun 则说明内核不支持 TUN/TAP 设备,若输出为如下类似则成功。

filename:       /lib/modules/2.6.34-ARCH/kernel/drivers/net/tun.koalias:          char-major-10-200license:        GPLauthor:         (C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>description:    Universal TUN/TAP device driverdepends:vermagic:       2.6.34-ARCH SMP preempt mod_unload 686

确认内核支持 TUN/TAP 模块之后,运行 lsmod tun 无输出则说明 TUN 模块没有加载,运行 modprobe tun 加载之(也可以运行 ls /dev/net/tun 通过检查 /dev/net/tun 文件是否存在来判断 TUN 模块是否加载)。

确认加载内核 TUN/TAP 模块之后,安装 OpenVPN 程序,不同的 Linux 发行版有其相应的包管理工具可以完成,或者也可以下载源码编译安装,缺点是依赖关系处理比较麻烦(openssl、lzo)。

2.1.1.2 TUN 接口3

如果使用 TUN 接口,不需要额外的操作,配置好 OpenVPN 服务器模式即可。鉴于目前国内的网络现状,大部分朋友购买了国外的 VPS 架设 VPN 并不仅仅是为了建立一个虚拟专用网络,最重要的是需要有 Internet 访问。因此,我们还需要额外的一些步骤来使的我们可以通过 VPN 访问 Internet。

首先,我们需要开启系统的 IP 转发功能,运行命令 sysctl -w net.ipv4.ip_forward=1(也可以编辑 vim /etc/sysctl.conf 文件,修改配置为 net.ipv4.ip_forward = 1),可以使用 sysctl -a | grep forward 来检查 IP 转发功能是否打开,输出结果应类似如下,所有含有 forward 字段的值应该为 1。

net.ipv4.conf.all.forwarding = 1net.ipv4.conf.all.mc_forwarding = 0net.ipv4.conf.default.forwarding = 1net.ipv4.conf.default.mc_forwarding = 0net.ipv4.conf.lo.forwarding = 1net.ipv4.conf.lo.mc_forwarding = 0net.ipv4.conf.eth0.forwarding = 1net.ipv4.conf.eth0.mc_forwarding = 0

接着,使用 iptables 建立路由转发规则,

Bash:

# DEVICE 应该为 OpenVPN 监听的本地地址 LOCAL_IP_ADDRESS 所属的网卡,一般为 eth0(在 OpenVZ 的 VPS,一般为 venet0)。iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o DEVICE -j SNAT --to-source LOCAL_IP_ADDRESS

或者,使用如下命令,不需要配置关注本地 IP 地址,

Bash:

# DEVICE 应该为 OpenVPN 监听的本地地址所属的网卡,一般为 eth0(在 OpenVZ 的 VPS,一般为 venet0)。iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o DEVICE -j MASQUERADE

最后,使用 /etc/init.d/iptables save 保存当前规则。

提示:Linux 下可以使用 ifconfig -a 查看本地的网络信息。

2.1.1.3 TAP 接口

如果使用 TAP 接口,需要首先建立相应桥接界面。在建立桥接界面之前,先确保系统有 bridge-tuils 安装。

(假设 tap0 为 tap 设备,br0 为桥接设备,eth0 为当前以太网卡)。

  • 桥接界面的建立:

    Bash:

    openvpn --mktun --dev tap0 #为 tap 设备建立路由隧道brctl addbr br0 #建立桥接设备brctl addif br0 eth0 #添加 eth 设备到网桥brctl addif br0 tap0 #添加 tap 设备到网桥ifconfig tap0 0.0.0.0 promisc up #设置 tap 设备为混杂模式ifconfig eth0 0.0.0.0 promisc up #设置 eth 设备为混杂模式ifconfig br0 ETH_IP netmask ETH_NETMASK broadcast ETH_BROADCAST #为 br0 设置原来以太网卡的网络参数
  • 桥接界面的移除:

    Bash:

    ifconfig br0 downbrctl delbr br0openvpn --rmtun --dev tap0

每次系统重新启动,都需要重新建立相关设备,最好以 shell 脚本形式开机自动建立。另外,如果有多个 tap 设备,也可以考虑用 shell 脚本来简化这一工作。4

2.1.1.4 域名解析

为了避免出现应为 DNS 污染而导致域名解析失败的问题,除了在 OpenVPN 服务端配置文件添加 “push dhcp-option DNS nameserver” 指令外,还可以考虑在服务器上建立自己的域名服务。这需要安装 bind 软件包,并运行 /etc/init.d/named start,同时,我们同样需要在 OpenVPN 服务端配置文件添加 “push dhcp-option DNS 10.8.0.1″ 指令。

2.1.2 Windows

OpenVPN 在 Windows 下的安装比较简单,有安装程序可以下载。在 Windows 下不存在像 Linux 那么多的隧道接口选择,只能使用 tap 桥接方式。另外,Windows 2000 及之前的版本均不支持 tap 设备,所以只能在 Windows XP 及其后续版本上安装和配置 OpenVPN 服务器,但不影响客户端的使用。

Windows 下的 tap 设备有两种方式实现互联网访问,一种是以太网卡桥接,另外一种是以太网卡共享 tap 设备上网。

  1. 网卡桥接5
    1. 控制面板 -> 网络连接
    2. 重命名 OpenVPN 安装时已经建立的 tap 虚拟网卡为 OpenVPN-Tap(可以访问 OpenVPN 的开始菜单项,使用 OpenVPN 提供的工具建立或删除 tap 设备)
    3. 同时选择以太网卡设备和 OpenVPN-Tap 设备,选择右键菜单“桥接”命令,等待命令完成,产生一个新的桥接设备
    4. 重命名新的桥接设备为 OpenVPN-Bridge,选择右键菜单“属性”命令,为其配置原来以太网卡的网络参数
  2. 共享上网

    选择右键菜单“属性”命令,高级选项卡,启用 “Internet 共享连接”特性并选择需要共享的设备为 OpenVPN 建立的 tap 设备。

2.2 认证方式

OpenVPN 支持多种认证方式,常用的有基于共享密钥的证书认证和用户名密码认证。

2.2.1 证书认证6
2.2.1.1 创建 CA 证书以及服务器证书
  1. 设置基本信息

    首先,需要设置 CA 证书所需要的一些基本信息,这些信息会被包含在生成的证书文件中。您需要编辑 OpenVPN 安装路径下的 easy-rsa 目录里的 vars(Linux 下) 或 vars.bat.sample(Windows 下) 文件,根据实际情况修改如下字段的值。

    KEY_ORG="Fort-Funston" # 组织KEY_CITY="SanFrancisco" # 城市KEY_COUNTRY="US" # 国家KEY_PROVINCE="CA" # 省份KEY_EMAIL="me@myhost.mydomain" # 邮件地址
  2. 创建 CA 证书

    接着,开始生成相应的 ca 证书、服务器证书以及 dh1024.pem 文件。在生成 CA 和客户端证书的过程中,基本保持默认的设置即可, Common Name 可以设置为当前的主机名或者域名,也可以是 OpenVPN-CA。

    • Linux:

      Bash:

      cd /usr/share/openvpn/easy-rsasource varschmod +x clean-all./clean-allchmod +x build-ca./build-cachmod +x build-dh./build-dh
    • Windows:

      Bash:

      cd "C:\Program Files\OpenVPN\easy-rsa"init-configvarsclean-allbuild-cabuild-dh

    命令完成后,我们会得到 ca.crt、ca.key、dh1024.pem 等文件。其中,ca.key 的安全非常重要,OpenVPN 并不需要这个文件,所以可以存放在其他比较安全的地方,否则,OpenVPN 的通信将不再安全。

  3. 创建服务器证书

    Common Name 必须被设置为 server。

    • Linux:

      Bash:

      chmod +x build-key-server./build-key-server server
    • Windows:

      Bash:

      build-key-server server

    命令完成后,我们会得到 server.crt、server.csr、server.key 等文件。

  4. 小结

    我们需要把所有生成的这些文件(ca.crt、ca.key、dh1024.pem、server.crt、server.csr、 server.key)放置到 OpenVPN 的配置目录中,或者修改配置文件中相应路径,使 OpenVPN 能够找到它们。ca.key 可以不放在配置文件目录中而单独存放在安全的地方,OpenVPN 程序运行并不需要它。

2.2.1.2 添加客户端证书

接下来,我们需要为客户端颁发证书,这样他们就能通过认证而使用 OpenVPN。为客户端生成证书时,我们只需要使用相同的 vas 配置即可。切记,不要再使用 clean-all 命令,否则,所有之前生成的 CA 证书和服务器证书都会随这 keys 目录一并删除。一般来说,每个客户端其基本信息均不相同,所以在生成客户端证书时应该根据实际情况填写相应的证书属性,除了 Common Name 必须唯一之外其他的属性可以相同。

  • Linux:

    Bash:

    cd /usr/share/openvpn/easy-rsasource varschmod +x build-key./build-key client
  • Windows:

    Bash:

    cd "C:\Program Files\OpenVPN\easy-rsa"varsbuild-key client

命令完成后,会生成 cleint.crt、client.csr、client.key 等文件,我们需要将它们连同 CA 证书 ca.crt 一起交给客户端。

2.2.1.3 吊销客户端证书7

当我们需要取消某个客户端的 OpenVPN 使用权限时,我们只需要吊销该客户端的证书即可。

切记,"Common Name" 是 OpenVPN 用来区别不同客户端的关键,所以在建立证书时,必须使用有意义其唯一的字串。
  • Linux:

    Bash:

    cd /usr/share/openvpn/easy-rsasource varschmod +x revoke-full./revoke-full client
  • Windows:

    Bash:

    cd "C:\Program Files\OpenVPN\easy-rsa"varsrevoke-full client

这条命令执行完成之后, 会在 keys 目录下面, 生成一个 crl.pem 文件,这个文件中包含了吊销证书的名单。成功注销某个证书之后,可以打开 keys/index.txt 文件,可以看到被注销的证书前面,已标记为R.

最后,我们需要修改服务端的配置文件,使被吊销的证书失效。

在服务端的配置文件 server.conf 中,加入这样一行:

crl-verify crl.pem

如果 server.conf 文件和 crl.pem 没有在同一目录下面,则 crl.pem 应该写绝对路径,例如:

crl-verify /usr/share/openvpn/easy-rsa/keys/crl.pem
2.2.1.4 小结

除此之外,还会生成 01.pem 02.pem 之类的文件以及 index.txt,它记录了当前 RSA 工具所生成的证书的历史信息。

2.2.2 用户名/密码8

OpenVPN 也支持使用用户名/密码进行认证。虽然证书认证相对安全,但为每个客户端生成证书比较麻烦,同时客户端的配置和很不方便,不符合用户的使用习惯,相比之 下,用户名/密码的方式更符合目前用户的使用习惯,并且可以将客户端和 CA 证书绑定打包,省去客户端用户的配置麻烦,确实有不小吸引力。OpenVPN 支持这些额外的认证方式的关键在于配置文件中 auth-user-pass-verifyclient-cert-not-requiredusername-as-common-nameauth-user-pass这些指令以及 OpenVPN 的 plugin 机制。

2.2.2.1 文本文件9

首先,下载 http://www.openvpn.se/files/other/checkpsw.sh,然后修改服务端配置文件,添加如下指令:

auth-user-pass-verify /etc/openvpn/scripts/checkpsw.sh via-envclient-cert-not-requiredusername-as-common-name

接着,建立 /etc/openvpn/psw-file 文件,作为用户名密码的储存文件,每行一个 OpenVPN 账户(首先是用户名,然后是空格或者 TAB,最后是明文的密码)。

最后,在客户端配置文件中添加一行 auth-user-pass,并删除和客户端证书相关的指令。

这样,虽然可以实现用户名和密码的访问,并且存储数据到文本文件,但所用 checkpsw.sh 毕竟是一个样例,很多安全性的问题并没有考虑,比如密码明文存贮等。如果熟悉 shell、perl、python 任何一种脚本语言的话,完全可以完善相关认证存储的细节,增强安全性。

2.2.2.2 PAM_MYSQL10

OpenVPN 服务器需要安装 pam 程序和 pam_mysql 库。

  1. 建立数据库

    保存以下代码为 openvpn.mysql 文件

    SQL:

    # 创建 openvpn 用户,对 openvpn 这个 database 有所有操作权限,密码为 openvpn# 若 active 不为 1,无权使用 VPNCREATE DATABASE openvpn;GRANT ALL ON vpn.* TO openvpn@localhost IDENTIFIED BY 'openvpn';FLUSH PRIVILEGES;USE openvpn;CREATE TABLE user (    name char(20) NOT NULL,    password char(128) default NULL,    active int(10) NOT NULL DEFAULT 1,    PRIMARY KEY (name));

    接着,运行命令 mysql -uroot -pPASSWORD < openvpn.mysql 完成 MySQL 相关配置。

    最后,使用命令 mysql -uroot -pPASSWORD 登录 MySQL 数据库,运行 INSERT INTO user (name,password) VALUES (‘test’,password(‘test’)) 增加一个 OpenVPN 用户,用户名为 test,密码 test,用于测试。

  2. 配置 pam_mysql 模块

    创建/etc/pam.d/openvpn文件,文件内容如下:

    auth sufficient pam_mysql.so user=openvpn passwd=openvpn host=localhost db=openvpn table=user usercolumn=name passwdcolumn=password where=active=1 sqllog=0 crypt=2account required pam_mysql.so user=openvpn passwd=openvpn host=localhost db=openvpn table=user usercolumn=name passwdcolumn=password where=active=1 sqllog=0 crypt=2

    关于 crypt 参数的意义如下:

    crypt(0) -- Used to decide to use MySQL's PASSWORD() function or crypt()0 = No encryption. Passwords in database in plaintext. NOT recommended!1 = Use crypt2 = Use MySQL PASSWORD() function3 = Use MySQL PASSWORD() function with MD5
  3. 编辑 OpenVPN 服务端配置文件,添加如下内容
    plugin ./openvpn-auth-pam.so openvpn # 说明使用的插件,openvpn 为插件的参数,使用 pam 的 servicesnameclient-cert-not-required # 不请求客户的 CA 证书,使用 User/Pass 验证username-as-common-name # 使用客户提供的 UserName 作为 Common Name
  4. 配置成功,进行测试
2.2.2.3 LDAP

LDAP 认证实际上也有二种,一种用 openvpn-auth-ldap 即直接通过 LDAP 验证,另一种与 pam-mysql 认证相似,使用 pam-ldap ,通过 PAM ,然后再找 LDAP 验证。

关于这部分,以后再补充,目前没有实际经验。

2.3 配置文件

由于使用的网络接口不同,服务器端的配置文件也不尽相同,首先给出一份比较详细的配置文件说明。11

#################################################        ################################################ Sample OpenVPN 2.0 config file for            #        # OpenVPN 2.0 多客户端服务器配置文件样例# multi-client server.                          #        ##                                               #        ## This file is for the server side              #        # 这个文件是 多客户端 <--> 单服务器 的 OpenVPN# of a many-clients <-> one-server              #        # 服务器配置文件。# OpenVPN configuration.                        #        ##                                               #        ## OpenVPN also supports                         #        # OpenVPN 也支持 单客户端 <--> 单服务器 的配置(# single-machine <-> single-machine             #        # 查看 OpenVPN 网站上的配置文件样例页面获取更多# configurations (See the Examples page         #        # 信息)。# on the web site for more info).               #        ##                                               #        ## This config should work on Windows            #        # 这个配置文件可工作于 Windows 或者 Linux/BSD 系# or Linux/BSD systems.  Remember on            #        # 统之上。注意,Windows 系统上需要用引号包裹路径# Windows to quote pathnames and use            #        # 名并且使用双反斜杠。比如:# double backslashes, e.g.:                     #        ## "C:\\Program Files\\OpenVPN\\config\\foo.key" #        # "C:\\Program Files\\OpenVPN\\config\\foo.key"#                                               #        ## Comments are preceded with '#' or ';'         #        #  注释使用 '#' 或者 ';' 开头。#################################################        ################################################ Which TCP/UDP port should OpenVPN listen on?           # OpenVPN 应该监听哪个 TCP/UDP 端口?# If you want to run multiple OpenVPN instances          # 如果需要在同一机器上运行# on the same machine, use a different port              # 多个 OpenVPN 进程,为每# number for each one.  You will need to                 # 个进程使用不同的端口。# open up this port on your firewall.                    # 你需要在防火墙上打开这些端口。port 1194# "dev tun" will create a routed IP tunnel,              # "dev tun" 将会建立一个路由 IP 隧道,# "dev tap" will create an ethernet tunnel.              # "dev tap" 将会建立一个以太网卡隧道。# Use "dev tap0" if you are ethernet bridging            # 如果已有一个预先建立的 tap0 虚拟网卡,且# and have precreated a tap0 virtual interface           # 已桥接到以太网卡,请使用 "dev tap0"。# and bridged it with your ethernet interface.           ## If you want to control access policies                 # 如果你想控制 VPN 的访问策略,你需要# over the VPN, you must create firewall                 # 为 TUN/TAP 接口建立相应的防火墙规则。# rules for the the TUN/TAP interface.                   ## On non-Windows systems, you can give                   # 在非 Windows 系统上,你可以附加# an explicit unit number, such as tun0.                 # 一个单一的号码,比如 tun0。# On Windows, use "dev-node" for this.                   # 在 Windows 系统上,使用 "dev-node"。# On most systems, the VPN will not function             # 在大多数系统上,VPN 将不会工作# unless you partially or fully disable                  # 直到你完全或者部分地为 TUN/TAP# the firewall for the TUN/TAP interface.                # 接口的关闭了防火墙。;dev tapdev tun# Windows needs the TAP-Win32 adapter name               # 在 Windows 上如果存在多个 TAP 适配器# from the Network Connections panel if you              # ,需要指明所用网络连接控制面板上的# have more than one.  On XP SP2 or higher,              # TAP-Win32 适配器名。# you may need to selectively disable the                # 在 Windows XP SP2 或更高系统上,需要为该# Windows firewall for the TAP adapter.                  # TAP 适配器选择性的关闭 Windows 防火墙。# Non-Windows systems usually don't need this.           # 非 Windows 系统通常不需要此设置。;dev-node MyTap# TCP or UDP server?                                     # TCP 还是 UDP 服务器?;proto tcpproto udp# Which local IP address should OpenVPN                  # OpenVPN 应该监听哪个本地 IP 地址?# listen on? (optional)                                  # (可选);local a.b.c.d# SSL/TLS root certificate (ca), certificate             # SSL/TLS 根证书 (ca), 证书 (cert)# (cert), and private key (key).  Each client            # 和私钥 (key) 每个客户端和服务器必# and the server must have their own cert and            # 须有他们自己的 cert 和 key。服务器# key file.  The server and all clients will             # 和所有的客户端使用相同的 ca 文件。# use the same ca file.                                  ##                                                        ## See the "easy-rsa" directory for a series              # 查看 "easy-rsa" 目录可以获得一系# of scripts for generating RSA certificates             # 列的用来生成 RSA 证书和私钥的脚本。# and private keys.  Remember to use                     # 切记为服务器和每个客户端生成唯一# a unique Common Name for the server                    # 的 Common Name。# and each of the client certificates.                   ##                                                        ## Any X509 key management system can be used.            # 可以使用任何 X509 私钥管理系统。# OpenVPN can also use a PKCS #12 formatted key file     # OpenVPN 同样可以使用一个 PKCS #12 格式的私钥文件# (see "pkcs12" directive in man page).                  # (查看 MAN 也的 "pkcs12" 配置段)。ca ca.crtkey server.key # This file should be kept secret         # 这个文件需要保密cert server.crt# Diffie hellman parameters.                             # Diffie hellman 参数。# Generate your own with:                                # 使用如下命令生成:#   openssl dhparam -out dh1024.pem 1024                 #   openssl dhparam -out dh1024.pem 1024# Substitute 2048 for 1024 if you are using              # 如果你使用 2048 bit 的私钥,用 2048# 2048 bit keys.                                         # 替换 1024dh dh1024.pem# Configure server mode and supply a VPN subnet          # 配置服务器模式并且为 OpenVPN 提供一个# for OpenVPN to draw client addresses from.             # VPN 子网来从中获取客户端的地址。# The server will take 10.8.0.1 for itself,              # 服务器自己使用 10.8.0.1,其余则分配给# the rest will be made available to clients.            # 客户端。# Each client will be able to reach the server           # 每个客户端都可以用 10.8.0.1 访问服务器。# on 10.8.0.1. Comment this line out if you are          # 如果你使用以太网卡桥接,注释此行。查看 MAN# ethernet bridging. See the man page for more info.     # 手册页获取更多信息。server 10.8.0.0 255.255.255.0# Maintain a record of client <-> virtual IP address     # 维护一个客户端 <--> 虚拟 IP 地址之间关联# 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     # 如果 OpenVPN 关闭或者重启,重新连接的客户# previously assigned.                                   # 端能够从地址池获取与以前一样的虚拟 IP。ifconfig-pool-persist ipp.txt# Configure server mode for ethernet bridging.           # 配置服务器的以太网卡桥接模式。# You must first use your OS's bridging capability       # 你必须首先使用你的系统的桥接特性为 TAP 接口# to bridge the TAP interface with the ethernet          # 建立起到以太网卡 NIC 接口的桥接界面。# NIC interface.  Then you must manually set the         # 接着,你必须手动为桥接界面配置 IP/netmask,# IP/netmask on the bridge interface, here we            # 这里假定为 10.8.0.4/255.255.255.0。# assume 10.8.0.4/255.255.255.0.  Finally we             # 最后,我们必须在这个子网中为连接的# must set aside an IP range in this subnet              # 客户端设置一个可分配的 IP 段# (start=10.8.0.50 end=10.8.0.100) to allocate           # (开始 = 10.8.0.50 结束 = 10.8.0.100)# 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                         # OpenVPN 客户端的地址池的路# to know to route the OpenVPN client                    # 由(10.8.0.0/255.255.255.0)# address pool (10.8.0.0/255.255.255.0)                  # 来回访 OpenVPN# back to the OpenVPN server.                            # 服务器。;push "route 192.168.10.0 255.255.255.0";push "route 192.168.20.0 255.255.255.0"# To assign specific IP addresses to specific            # 要为特定的客户端分配特定的 IP 地址# clients or if a connecting client has a private        # 或者如果一个连接中的客户端其后的私# subnet behind it that should also have VPN access,     # 有子网也要有 VPN 访问权,可使用子目# use the subdirectory "ccd" for client-specific         # 录 ccd 来存放客户端的特别配置文件# configuration files (see man page for more info).      # (查看 MAN 也来获取更多信息)。# EXAMPLE: Suppose the client                            # 示例:# having the certificate common name "Thelonious"        # 假设客户端的认证名称为 "Thelonious",# also has a small subnet behind his connecting          # 并且有一个小的子网位于他的连接机器之后,# machine, such as 192.168.40.128/255.255.255.248.       # 比如 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:      # 接着,建立一个文件 ccd/Thelonious,添加这行:#   iroute 192.168.40.128 255.255.255.248                #   iroute 192.168.40.128 255.255.255.248# This will allow Thelonious' private subnet to          # 这会允许 Thelonious' 的私有子网访问VPN。# access the VPN.  This example will only work           # 这个示例仅仅在使用路由隧道而不是网卡桥接时起# if you are routing, not bridging, i.e. you are         # 作用。# using "dev tun" and "server" directives.               # 比如,你使用的是 "dev tun" 和 "server" 指令。# EXAMPLE: Suppose you want to give                      # 示例:假如你想给 Thelonious 一个固定的 VPN# Thelonious a fixed VPN IP address of 10.9.0.1.         # IP 地址 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:                  # 接着,添加如下行到 ccd/THelonious:#   ifconfig-push 10.9.0.1 10.9.0.2                      #   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         # (1) 运行多个OpenVPN守护进程, 每个对应#     group, and firewall the TUN/TAP interface          # 不同的组,对每个组的 TUN/TAP 接口应用#     for each group/daemon appropriately.               # 防火墙。# (2) (Advanced) Create a script to dynamically          # (2) (高级)创建一个动态脚本使防火墙对#     modify the firewall in response to access          # 接入的不同客户端执行不同的策略。查看#     from different clients.  See man                   # MAN 手册页获取更多关于 learn-address#     page for more info on learn-address script.        # 脚本的信息。;learn-address ./script# If enabled, this directive will configure              # 如果开启,这个指令会配置所有的客户端将# all clients to redirect their default                  # 它们的默认网关指向 VPN,导致所有的 IP# network gateway through the VPN, causing               # 流量比如网络浏览和 DNS 查询等全部通过# all IP traffic such as web browsing and                # VPN。# and DNS lookups to go through the VPN                  ## (The OpenVPN server machine may need to NAT            # (OpenVPN 服务器需要 NAT 它的 TUN/TAP# the TUN/TAP interface to the internet in               # 接口到互联网以使其正常工作)# order for this to work properly).                      ## CAVEAT: May break client's network config if           # 警告:如果客户端的本地 DHCP 服务器数据# client's local DHCP server packets get routed          # 通过隧道路由,可能破坏客户端的网络配置。# through the tunnel.  Solution: make sure               ## client's local DHCP server is reachable via            # 解决:确保客户端的本地 DNCP 服务器# a more specific route than the default route           # 可以通过一个更特殊的路由而不是默认# of 0.0.0.0/0.0.0.0.                                    # 的路由 0.0.0.0/0.0.0.0 来访问。;push "redirect-gateway"# Certain Windows-specific network settings              # 确定的 Windows 专用网络配置可以被推送到客# can be pushed to clients, such as DNS                  # 户端,比如 DNS 或者 WINS 服务器地址。# or WINS server addresses.  CAVEAT:                     # 警告:# http://openvpn.net/faq.html#dhcpcaveats                # http://openvpn.net/faq.html#dhcpcaveats;push "dhcp-option DNS 10.8.0.1";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 appropriate  firewall the            # 个适当的防火墙来保护服务器的 TUN/TAP# server's TUN/TAP interface.                            # 接口。client-to-client# Uncomment this directive if multiple clients           # 如果多个客户端以相同的证书/私钥或者# might connect with the same certificate/key            # Common Name 连接,取消对这个指令的# 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",              # “Common Name”时,取消注释# UNCOMMENT THIS LINE OUT.                               # 该行。;duplicate-cn# The keepalive directive causes ping-like               # keepalive 指令可以使类似 ping 的# 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                # 每 10 秒 ping 一次,假如远端在# a 120 second time period.                              # 120 秒内没有回应则认为连接丢失。keepalive 10 120# For extra security beyond that provided                # 如果需要除 SSL/TLS 提供之外的额外安全,# by SSL/TLS, create an "HMAC firewall"                  # 建立一个 “HMAC 防火墙” 来帮助屏蔽 DoS# to help block DoS attacks and UDP port flooding.       # 攻击和 UDP 端口洪水。#                                                        ## Generate with:                                         # 用如下命令生成:#   openvpn --genkey --secret ta.key                     #   openvpn --genkey --secret ta.key#                                                        ## The server and each client must have                   # 服务器和每个客户端必须有一# a copy of this key.                                    # 份该密钥的拷贝。# The second parameter should be '0'                     # 第二个参数服务器应该为 0,# on the server and '1' on the clients.                  # 而客户端应该为 1。;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.                    # 对 VPN 连接启用数据压缩。如果你在# 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                 # 初始化后降级 OpenVPN 进程的权限# daemon's privileges after initialization.              # 是个不错的主意。#                                                        ## You can uncomment this out on                          # 你可以在非 Windows 系统上取消该行# non-Windows systems.                                   # 注释。user nobodygroup nobody# The persist options will try to avoid                  # 这个 persisit 选项可以避免在重启# accessing certain resources on restart                 # 时访问那些由于权限降级而不再可用# that may no longer be accessible because               # 的资源。# of the privilege downgrade.persist-keypersist-tun# Output a short status file showing                     # 输出简短的文件来显示当前的连接状态# current connections, truncated                         # ,每一分钟重写或删节一次。# and rewritten every minute.                            #status openvpn-status.log# By default, log messages will go to the syslog (or     # 默认的,日志信息会输出到 syslog (或者在# on Windows, if running as a service, they will go to   # Windows 上,如果运行一个服务,它们会输出# the "\Program Files\OpenVPN\log" directory).           # 到 "\Program Files\OpenVPN\log" 目录)。# Use log or log-append to override this default.        # 使用 log 或者 log-append 来覆写这个默认设置。# "log" will truncate the log file on OpenVPN startup,   # "log" 会在每次 OpenVPN 启动时删节日志文件,# while "log-append" will append to it.  Use one         # 而 "log-append" 则会附加在其后。使用其一即可。;log /var/log/openvpn.log;log-append openvpn.log# Set the appropriate level of log                       # 设置适当的日志文件详细级别。# file verbosity.                                        ##                                                        ## 0 is silent, except for fatal errors                   # 0 是静默的,严重错误除外# 4 is reasonable for general usage                      # 4 是合理的,通常使用即可# 5 and 6 can help to debug connection problems          # 5 和 6 可以帮助调试连接问题# 9 is extremely verbose                                 # 9 是极端详细的verb 3# Silence repeating messages.  At most 20                # 静默重复消息。相同消息# sequential messages of the same message                # 策略的 20 个连续消息# category will be output to the log.                    # 会被输出到日志。;mute 20

关于服务器配置文件说明,还有以下版本可以参考:

  1. OpenVPN服务器端配置文件祥解
  2. Linux下OpenVPN的建设详细教程
  3. 使用OpenVPN搭建Linux下的VPN服务

最新 OpenVPN 2.1.1 版本服务器配置文件与以上版本的差异如下,可以看出,新的版本已经部分的解决了推送客户端配置参数可能导致的破坏客户端网络配置的问题。

diff server.conf server-2.1.1.conf,server.conf 为 OpenVPN Howto 上的配置文件样例,server-2.1.1.conf 为 OpenVPN 最新版本提供的配置文件样例。

116a117,128> # Configure server mode for ethernet bridging                # 为使用 DHCP-proxy 的以太网卡桥接配置服务> # using a DHCP-proxy, where clients talk                     # 器模式,客户端可以同服务器端的 DHCP 服务> # to the OpenVPN server-side DHCP server                     # 器交流获取 IP 地址和 DNS 服务器的地址。> # to receive their IP address allocation                     #> # and DNS server addresses.  You must first use              # 你必须首先使用系统的桥接功能来桥接 TAP 设> # your OS's bridging capability to bridge the TAP            # 备到以太网卡 NIC 设备上。> # interface with the ethernet NIC interface.                 #> # Note: this mode only works on clients (such as             # 注意:这个模式仅仅工作与那些客户端(比如> # Windows), where the client-side TAP adapter is             # Winodws)的 TAP 适配器被连接到一个 DHCP> # bound to a DHCP client.                                    # 客户端的情况。> ;server-bridge>173,181c185,187< # the TUN/TAP interface to the internet in                   # TUN/TAP 界面到互联网以使其正常工作。< # order for this to work properly).                          #< # CAVEAT: May break client's network config if               # 警告:如果客户端的本地 DHCP 服务器数据< # client's local DHCP server packets get routed              # 包经过隧道路由,可能会波坏客户端的的网< # through the tunnel.  Solution: make sure                   # 络配置。< # client's local DHCP server is reachable via                # 解决:确保客户端的本地 DHCP 服务器通过< # a more specific route than the default route               # 一个更特殊的路由而不是默认的 0.0.0.0/0.0.0.0< # of 0.0.0.0/0.0.0.0.                                        # 是可达的。< ;push "redirect-gateway"---> # or bridge the TUN/TAP interface to the internet            # 或者桥接 TUN/TAP 设备到互联网以使其正常> # in order for this to work properly).                       # 工作。> ;push "redirect-gateway def1 bypass-dhcp"187,188c193,196< ;push "dhcp-option DNS 10.8.0.1"< ;push "dhcp-option WINS 10.8.0.1"---> # The addresses below refer to the public                    # 下面的地址指向由 opendns.com 提供的> # DNS servers provided by opendns.com.                       # 公共 DNS 服务器。> ;push "dhcp-option DNS 208.67.222.222"> ;push "dhcp-option DNS 208.67.220.220"

服务器采用不同的网络接口,其配置文件也不尽相同,下面给出不同环境下的配置文件区别:

  1. Linux 采用 tun 接口

    Bash:

    dev tun # 还可以使用具体的 tun 设备的名字,如 tun0port 1194proto udp # 如果需要经过 HTTP proxy 代理 VPN 连接,则必须使用 TCP 协议local OPENVPN_SERVER_IPserver 10.8.0.0 255.255.255.0
  2. Linux 采用 tap 桥接

    Bash:

    dev tap # 还可以使用具体的 tap 设备的名字,如 tap0port 1194proto udp #如果需要经过 HTTP proxy 代理 VPN 连接,则必须使用 TCP 协议local OPENVPN_SERVER_IPserver 10.8.0.0 255.255.255.0
  3. Windows 采用 tap 共享上网

    Bash:

    dev tap;dev-node OpenVPN-Tap # Windows 系统下如果有多个 TAP 设备,必须配置这个字段,提供所用 tap 设备的名字port 1194proto udp #如果需要经过 HTTP proxy 代理 VPN 连接,则必须使用 TCP 协议local OPENVPN_SERVER_IPserver 10.8.0.0 255.255.255.0
  4. Windows 采用 tap 桥接上网

    Bash:

    dev tap;dev-node OpenVPN-Tap # Windows 系统下如果有多个 TAP 设备,必须配置这个字段,提供所用 tap 设备的名字port 1194proto udp #如果需要经过 HTTP proxy 代理 VPN 连接,则必须使用 TCP 协议local OPENVPN_SERVER_IP;serve-bridge # 如果服务器直接通过 DHCP 获得地址,那么这里直接使用 server-bridge 即可server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100 # 服务器所在子网可供提供给客户端的 IP 地址范围

服务器配置文件的其余部分基本不变,只是注意日志文件路径在 Linux 和 Winodws 下的区别。

# 安全认证参数 #dh dh1024.pemca ca.crtkey server.key  # 需要注意这个文件的安全cert server.crt# 网络配置参数 #push "dhcp-option DNS 208.67.222.222"push "dhcp-option DNS 208.67.220.220"push "redirect-gateway def1 bypass-dhcp"# 其他配置参数 #comp-lzopersist-keypersist-tunkeepalive 10 120# 日志配置参数 #verb 3status        /var/log/openvpn/status.log # Windows 下应该为 "C:\\Program Files\\OPENVPN\\log\\status.log"log-append    /var/log/openvpn/openvpn.log # Windows 下应该我 "C:\\Program Files\\OPENVPN\\log\\openvpn.log"

三、客户端的安装和配置

3.1 基本安装

OpenVPN 是点对点的隧道协议实现,在网络中,服务端和客户端的地位是对等的,软件自身不分服务端还是客户端,关键取决于配置文件。如果配置文件是客户端的配置文 件,则以客户端模式运行,反之亦然。因此,不论什么系统平台,安装好 OpenVPN 之后,均可以使其已客户端的模式运行,只需要准备好 ca.crt、client.crt、client.key 等证书和密钥以及相应的客户端配置文件 client.conf(client.ovpn # Winodws下),并将配置文件放在相应的配置文件目录内,并确保配置文件中相应证书密钥的路径正确(注意区分 Unix-like 系统和 Windows 系统在路径上的区别),即可启动 OpenVPN 作为客户端。

下面简要介绍下相应平台其他的 OpenVPN 客户端实现,这些实现的共同特点是拥有比较直观的图形配置界面。

3.1.1 Mac

Apple 的 Mac 电脑上可以使用 Tunnelblick 作为 OpenVPN 的客户端。安装完成后,放入当前客户端的 ca.crt,client.crt,client.key 到配置文件夹(\Users\你登陆的用户名\Library\Application Support\Tunnelblick\Configurations)。首次运行需要编辑配置文件,作相应的修改后,点击右上角状态栏隧道图标即可连 接。12

3.1.2 Linux

Linux 的 Gnome 桌面下的 NetworkManager 网络管理套件提供了一个 OpenVPN 的插件,以直观方便的界面提供对 OpenVPN 管理配置,一般的发行版都有提供这个软件包,请使用相应的包管理工具进行安装。

3.1.3 Windows13

Winodws 下可以使用 OpenVPN GUI for Windows 作为客户端,在 http://openvpn.se/ 下载并安装 OpenVPN GUI for Windows 后,放入当前客户端的 ca.crt,client.crt,client.key 到其安装目录下的 config 文件夹,并编辑或生成配置文件 client.ovpn,做相应的修改,点击状态栏图标连接即可。14需要注意的是,OpenVPN GUI for Windows 是对应于相应的 OpenVPN 版本的,http://openvpn.se/ 目前只提供对 2.0.9 的支持,如果你要连接的 OpenVPN 服务器使用的是其后的版本(比如 2010-8-15 发布的 2.1.2),可能有些新的配置参数无法支持(比如 dhcp-bypass 参数),会出现连接失败或者连接后无法访问网络的问题。不过,OpenVPN 在 2.1.2 的 Windows 安装程序中已经集成了 OpenVPN GUI for Windows ,直接在 http://openvpn.net/ 下载即可。

3.2 配置文件15

##############################################                 ############################################ Sample client-side OpenVPN 2.0 config file #                 # OpenVPN 2.0 连接多客户端服务器的客户端配置# 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  #                 # 在 Windows 上,你可能需要重命名这个文件使它# file so it has a .ovpn extension           #                 # 以 .ovpn 后缀。##############################################                 ############################################ 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                   # 在大多数系统上,VPN 将不会工# unless you partially or fully disable                        # 作,直到你完全或者部分地为# the firewall for the TUN/TAP interface.                      # TUN/TAP 接口的关闭了防火墙。;dev tapdev tun# Windows needs the TAP-Win32 adapter name                     # 如果你有不止一个 TAP-Win32# from the Network Connections panel                           # 适配器, Windwos 需要指明它在# if you have more than one.  On XP SP2,                       # 网络连接控制面板中的名字。# you may need to disable the firewall                         # 在 XP SP2 上,你需要为 TAP 适# for the TAP adapter.                                         # 配器关闭防火墙。;dev-node MyTap# Are we connecting to a TCP or                                # 我们使用的是一个 TCP 或者# UDP server?  Use the same setting as                         # UCP 服务器?使用和服务器相# on the server.                                               # 同的设置。;proto tcpproto udp# The hostname/IP and port of the server.                      # 服务器的主机名或者 IP 以及端口。# You can have multiple remote entries                         # 你可以有多个远端入口来平衡服务# to load balance between the servers.                         # 器之间的负载。remote my-server-1 1194;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                      # 不停的尝试解析 OpenVPN 服务器的主# 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) # 初始化后降级权限(仅支持非 Windows 系统);user nobody;group nobody# Try to preserve some state across restarts.                  # 尝试保持一些状态通过重新启动。persist-keypersist-tun# If you are connecting through an                             # 如果你是通过一个 HTTP 代理来连接# HTTP proxy to reach the actual OpenVPN                       # 实际的 OpenVPN 服务器,在这里添# server, put the proxy server/IP and                          # 加代理服务器的 IP 和端口号。# port number here.  See the man page                          ## if your proxy server requires                                # 如果你需要验证,请查看 MAN 手册# 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.                                               # SSL/TLS 参数。# See the server config file for more                          # 查看服务器的配置文件获取更多的描述。# description.  It's best to use                               # 最好为每个客户端使用独立的 .crt/.key# a separate .crt/.key file pair                               # 文件对。# for each client.  A single ca                                # 一个单独的 ca 文件可以用于所有的# file can be used for all clients.                            # 客户端。ca ca.crtkey client.keycert client.crt# Verify server certificate by checking                        # 通过检查证书有无值为 "server" 的# that the certicate has the nsCertType                        # nsCertType  字段来确认服务器证书。# field set to "server".  This is an                           # 这是一个非常重要的预防措施来防止在# important precaution to protect against                      # http://openvpn.net/howto.html#mitm# a potential attack discussed here:                           # 讨论过的潜在的攻击。#  http://openvpn.net/howto.html#mitm                          ##                                                              # 要使用这个特性,你必须通过设定# To use this feature, you will need to generate               # nsCertType 字段为 "server" 来# your server certificates with the nsCertType                 # 生成你服务器的证书。# field set to "server".  The build-key-server                 # 可以使用 easy-rsa 目录下的# script in the easy-rsa folder will do this.                  # build-key-server 脚本来完成。;ns-cert-type server# If a tls-auth key is used on the server                      # 如果服务器使用 tls-auth 密钥,请选择# 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.                          # 对 VPN 连接启用压缩。# 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

关于客户端配置文件,还有以下版本可以参考:

  1. Linux下OpenVPN的建设详细教程
  2. 使用OpenVPN搭建Linux下的VPN服务

下面是一份与前面的服务器配置文件相对应的客户端配置文件,客户端的配置文件比较简单,只需要根据服务器的配置调整 dev、proto 等参数即可。

clientdev tun # 根据服务器配置决定,如果服务器采用 tap 设备,则为 tap;dev-node OpenVPN-Tap # Windows 系统下如果有多个 TAP 设备,必须配置这个字段,提供所用 TAP 设备的名字proto udp # 与服务器保持一致remote OPENVPN_SERVER_IP 1194ca ca.crtkey client.keycert client.crtcomp-lzopersist-keypersist-tun

四、安全相关

如果需要除 SSL/TLS 提供之外的额外安全,建立一个 “HMAC 防火墙” 来帮助屏蔽 DoS 攻击和 UDP 端口洪水,使用 openvpn --genkey --secret keys/ta.key 来生成 ta.key 文件,服务器和每个客户端必须有一份该密钥的拷贝。

同时,修改服务器配置文件,添加 tls-auth ta.key 0;修改客户端配置文件,添加 tls-auth ta.key 1。注意,第二个参数服务端和客户端应该不同,比如服务器若为 0,则客户端应该为 1。

四、流量控制

若果我们需要队 OpenVPN 客户端的的流量控制,我们可以使用 TC 这个程序。

这是一个示例,假如 OpenVPN 客户端通过 OpenVPN GUI 软件连接到服务器,然后通过 NAT 连接到外网。其中,eth0 为服务器以太网卡接口 (66.77.88.99),tap0 为服务器虚拟网卡接口 (10.8.0.1),VPN 客户端 IP 为10.8.0.2。

  • 启动NAT

    Bash:

    iptables -t nat -A PREROUTING -d 66.77.88.99 -j DNAT --to 10.8.0.2iptables -t nat -A POSTROUTING -s 10.8.0.2 -o eth0 -j SNAT --to-source 66.77.88.99
  • 限制VPN客户下载速度为 256Kbit/s

    Bash:

    tc qdisc  add dev tap0 root handle 1: htb default 10tc class  add dev tap0 parent 1: classid 1:1 htb rate 256kbit  burst 10ktc filter add dev tap0 parent 1: protocol ip prio 16 u32 match ip dst 10.8.0.2 flowid 1:1
  • 限制VPN客户上载速度为 128Kbit/s

    Bash:

    tc qdisc  add dev eth0 root handle 2: htb default 10tc class  add dev eth0 parent 2: classid 2:1 htb rate 128kbit burst 10ktc filter add dev eth0 parent 2: protocol ip prio 16 handle 1 fw flowid 2:1iptables -t mangle -A PREROUTING -i tap0 -s 10.8.0.2 -j MARK --set-mark 1iptables -t mangle -A PREROUTING -i tap0 -s 10.8.0.2 -j RETURN

五、插件机制

OpenVPN 提供跟进一步的插件机制来满足不同用户的需求。通过插件机制,可以在程序逻辑的一些关键点调用脚本或者插件来完成不同的任务。关于这部分,目前我了解也不是很多,下面是两篇比较深刻的相关的源代码分析,可能会有所帮助。

  1. OpenVPN 中虚拟 IP 地址的分配
  2. OpenVPN 中虚拟 IP 地址的自定义分配

六、参考资料

  1. OpenVPN
  2. OpenVPN How to
  3. OpenVPN 常见问题解答(FAQ)
  4. 深入 OpenVPN 的配置
  5. 使用 OpenVPN 搭建 Linux 下的 VPN 服务
  6. 使用 OpenVPN 构建安全 VPN [OpenVPN + CA]
  7. OpenVZ 安装 Openvpn 配置 VPN -Burstnet vps CentOS
  8. Linux 下 OpenVPN 安装和 Windows OpenVPN GUI 安装笔记
  9. Windows 下利用 OpenVPN 搭建 VPN 服务器
  10. Windows 下使用 CA 验证的 OpenVPN Server 的配置方法
  11. 中国科学技术大学网络OpenVPN系统

  1. 参考中文维基百科VPN?
  2. 参考 PPTPL2TPIPSec VPNSSL VPN 的進化(遠端網路接取服務)PPTPL2TP和IPsec的区别及优缺点?
  3. 参考 OpenVZ VPS 安装配置 OpenVPN?
  4. 参考 Ethernet Bridging 提供的脚本,或者 Implementing OpenVPN 这篇文章中更加易用的脚本 ?
  5. 参考 OpenVPN Bridging with Windows HOWTO 提供的视频和配置教程、OpenVPN-桥方式安装利用 OpenVPN 建立桥接 VPN?
  6. 参考 RSA Key Management ?
  7. 参考 如何在 Linux OpenVPN 服务器吊销 (revoke) 客户端证书?
  8. 参考 OpenVPN 的验证方式,一个对于 OpenVPN 其他认证方式的总结,以及OpenVPN How to — Using alternative authentication methods。 ?
  9. 查看 OpenVPN 邮件列表的 [Openvpn-Users] clear text file authentication script 主题,虽然可以把用户名/密码保存到文件的 OpenVPN 程序–[下载]也提供了一种方案,但是建立在修改源代码的基础上的,不推荐。 ?
  10. OpenVPN 使用 User/Pass 验证登录OpenVPN 以用户名、密码方式登录OpenVPN 在 Windows 下使用 User/Pass 验证?
  11. 翻译自 OpenVPN HOWTO — Sample OpenVPN 2.0 configuration files — sample-config-files/server.conf?
  12. 详细安装配置过程参见 Mac 上用 OpenVPN 教程?
  13. [原]Windows 作为 OpenVPN 的客户端[原]OpenVPN 在 Win7 下的使用Using OpenVPN on Windows Vista?
  14. 参考小辉的 Linux 下 OpenVPN 安装和 Windows OpenVPN GUI 安装笔记 一文的“OpenVPN GUI For Windows 客户端安装过程”部分 ?
  15. 翻译自 OpenVPN HOWTO — Sample OpenVPN 2.0 configuration files — sample-config-files/client.conf?
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
centos 7部署openvpn easy-rsa 3.0部署方法
Openvpn 加载了 tun 模块,仍然提示Cannot open TUN/TAP dev /dev/net/tun
在家远程控制(远程桌面)到公司的电脑
CentOS平台OpenVPN(UDP53端口)架设 | 小米の网志
Windows NT/2000 Server Logon using Fineid Smart Cards
SSTP Remote Access Step-by-Step Guide: Deployment
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服