OpenVPN完全指南原创

OpenVPN完全指南原创

openvpn是什么?

OpenVPN是一个功能齐全的SSL VPN,它使用行业标准的SSL/TLS协议实现了OSI模型第2层(数据链路层)或第3层(网络层)的安全网络扩展。OpenVPN支持基于证书、智能卡以及用户名/密码等多种形式的灵活的客户端认证方法,并可以通过应用于VPN虚拟接口的防火墙规则为指定用户或用户组设置访问控制策略。

openvpn 配置方案

  • tap网络桥接
  • udp通讯协议
  • 版本 2.4.0-3
  • 基于证书认证策略

openvpn服务器配置

注:以下操作部分需要root账户权限,如不是以root账户登录系统,请自行使用sudo提权。

  • 修改并更新APT源

vim /etc/apt/sources.list

添加:

deb http://ftp.debian.org/debian jessie-backports main

更新软件列表:

apt-get update

这里推荐将debian8.6的163源改为阿里云的源:

deb http://mirrors.aliyun.com/debian/ jessie main non-free contrib
deb http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ jessie main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib
  • 安装openvpn
apt-get -t jessie-backports install openvpn
  • 检查openvpn版本

安装apt-showversions

apt-get install apt-show-versions

检查版本是否为2.4.0-3~bpo8+1

apt-show-versions openvpn

openvpn服务器配置(debian8.6)

  • 使用easy-rsa 2.2.2-1生成服务端证书
  • 生成主CA证书和密钥
    默认目录为 /usr/share/easy-rsa/
cd /usr/share/easy-rsa/
. ./vars
./clean-all
./build-ca

最后一个命令(build-ca)将通过调用交互的openssl命令来创建CA证书和密钥。

root@debian:/usr/share/easy-rsa# ./build-ca
Generating a 2048 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) [US]:CN
State or Province Name (full name) [CA]:Hn
Locality Name (eg, city) [SanFrancisco]:Changsha
Organization Name (eg, company) [Fort-Funston]:farseer
Organizational Unit Name (eg, section) [MyOrganizationalUnit]:farseer
Common Name (eg, your name or your server's hostname) [Fort-Funston CA]:VDS
Name [EasyRSA]:farseer
Email Address [me@myhost.mydomain]:farseer@farseer.cc
  • 生成服务端证书、密钥
./build-key-server server

在这一步骤中,大多数参数都可以使用默认值。当询问Common Name时,请输入服务器名。此外,有两个询问操作必须正面响应:"Sign the certificate? [y/n]"和"1 out of 1 certificate requests certified, commit? [y/n]".。

root@debian:/usr/share/easy-rsa# 
./build-key-server vds
Generating a 2048 bit RSA private key
.............................................
.............................................
..............................+++
.....+++
writing new private key to 'vds.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) [US]:CN
State or Province Name (full name) [CA]:Hn
Locality Name (eg, city) [SanFrancisco]:Changsha
Organization Name (eg, company) [Fort-Funston]:farseer
Organizational Unit Name (eg, section) [MyOrganizationalUnit]:farseer
Common Name (eg, your name or your server's hostname) [vds]:
Name [EasyRSA]:farseer
Email Address [me@myhost.mydomain]:farseer@farseer.cc

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:farseer2017
An optional company name []:farseer
Using configuration from /usr/share/easy-rsa/openssl-1.0.0.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName           :PRINTABLE:'CN'
stateOrProvinceName   :PRINTABLE:'Hn'
localityName          :PRINTABLE:'Changsha'
organizationName      :PRINTABLE:'farseer'
organizationalUnitName:PRINTABLE:'farseer'
commonName            :PRINTABLE:'vds'
name                  :PRINTABLE:'farseer'
emailAddress          :IA5STRING:'farseer@farseer.cc'
Certificate is to be certified until Mar 19 12:26:21 2027 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
  • 为客户端生成证书
./build-key client_name

请为每个客户端生成唯一的client_name,命名规则如下:

- 类型 前缀 举例
员工 staff staff_wlf
生产环境服务器 p p_s0001
开发环境服务器 d d_s0001
测试环境服务器 t t_s0001
  • 生成迪菲·赫尔曼参数
./build-dh 2048
  • 关键文件解释
文件名 谁需要 作用 是否需要保密
ca.crt 服务器、所有客户端 根CA证书 NO
ca.key 密钥签名机 根CA密钥 YES
dh2048.pem 服务器、所有客户端 迪菲·赫尔曼加密 NO
server.crt 服务器 服务器证书 NO
server.key 服务器 服务器密钥 YES
client_name.crt client_name客户端 client_name证书 NO
client_name.key client_name客户端 client_name密钥 YES
  • 创建服务端配置文件
    进入openvpn默认配置文件目录:cd /etc/openvpn
    创建配置文件 'vim server.conf'
  • 配置文件详解
# OpenVPN监听端口
# 在同一台计算机上运行多个OpenVPN实例,必须用不同的端口号区分
# 另,需要在防火墙上开放这些端口
port 1194
#使用UDP协议通讯,客户端需要与服务端保持一致
proto udp
# 指定OpenVPN创建的通信隧道类型
# "dev tun"将会创建一个路由IP隧道
# "dev tap"将会创建一个以太网隧道
dev tap
# 设置SSL/TLS根证书(ca)、证书(cert)和私钥(key)
# 每个客户端和服务器端都需要它们各自的证书和私钥文件
# 服务器端和所有的客户端都将使用相同的CA证书文件
ca keys/farseer/ca.crt
cert keys/farseer/server.crt
key keys/farseer/server.key
# 指定迪菲·赫尔曼参数文件位置
dh keys/farseer/dh2048.pem
# 设置服务器端模式,并提供一个VPN子网,以便于从中为客户端分配IP地址
server 172.26.0.0 255.255.0.0
# 指定用于记录客户端和虚拟IP地址的关联关系的文件。
# 当重启OpenVPN时,再次连接的客户端将分配到与上一次分配相同的虚拟IP地址
ifconfig-pool-persist ipp.txt
# 固定IP地址配置地址
client-config-dir /etc/openvpn/servers/farseer/ccd
# 将dns服务器地址推送到客户端
push "dhcp-option DNS 172.26.0.1"
# 允许不同的客户端之间相互"可见"(允许客户端之间互相访问)
client-to-client
# keepalive指令将导致类似于ping命令的消息被来回发送,以便于服务器端和客户端知道对方何时被关闭
# 每10秒钟ping一次,如果120秒内都没有收到对方的回复,则表示远程连接已经关闭
keepalive 10 120
# 出于SSL/TLS之外更多的安全考虑,创建一个"HMAC 防火墙"可以帮助抵御DoS攻击和UDP端口淹没攻击
# 你可以使用以下命令来生成:
# openvpn --genkey --secret ta.key
# 服务器和每个客户端都需要拥有该密钥的一个拷贝
# 第二个参数在服务器端应该为'0',在客户端应该为'1'
tls-auth servers/farseer/ta.key 0
# 选择一个密码加密算法。
# 该配置项也必须复制到每个客户端配置文件中
cipher AES-128-CBC
# 在VPN连接上启用压缩
comp-lzo
# 持久化选项可以尽量避免访问那些在重启之后由于用户权限降低而无法访问的某些资源
persist-key
persist-tun
# 输出一个简短的状态文件,用于显示当前的连接状态,该文件每分钟都会清空并重写一次
status servers/farseer/logs/openvpn-status.log
# 默认情况下,日志消息将写入syslog(在Windows系统中,如果以服务方式运行,日志消息将写入OpenVPN安装目录的log文件夹中)
# 你可以使用log或者log-append来改变这种默认情况
# "log"方式在每次启动时都会清空之前的日志文件
# "log-append"这是在之前的日志内容后进行追加
log-append servers/farseer/logs/openvpn.log
# 为日志文件设置适当的冗余级别(0~9)。冗余级别越高,输出的信息越详细。
# 0 表示静默运行,只记录致命错误。
# 4 表示合理的常规用法。
# 5 和 6 可以帮助调试连接错误。
# 9 表示极度冗余,输出非常详细的日志信息。
verb 2
# 重复信息的沉默度。
# 相同类别的信息只有前20条会输出到日志文件中。
mute 20
# 吊销证书的记录文件位置
crl-verify keys/farseer/crl.pem

openvpn服务器启动及开机自启配置

  • 启动openvpn
    方式一:
cd /etc/openvpn<br></br>openvpn server_name.con<br></br>

方式二:

service openvpn start
  • 检查开机自启

安装sysv-rc-conf

apt-get install sysv-rc-conf
sysv-rc-conf

58d1511e36e4a_58d1511e.png

客户端配置文件详解

#本配置文件在linux下使用.conf后缀,windows下使用ovpn后缀
#指定这是一个客户端,我们将从服务器获取某些配置文件指令
client
# 指定连接的服务器是采用TCP还是UDP协议
# 这里需要使用与服务器端相同的设置
proto udp
#设置虚拟网卡制式tun or tap
#在大多数系统中,除非你部分禁用或者完全禁用了TUN/TAP接口的防火墙,否则VPN将不起作用
dev tap
#服务器根CA证书
ca ca.crt
#迪菲-赫尔曼加密文件
dh dh2048.pem
#客户端证书及密钥
cert staff_XXX.crt
key staff_XXX.key
#指定服务器的主机名(或IP)以及端口号
remote 106.14.26.36 1194
#服务器端使用了tls-auth密钥,每个客户端也都应该有该密钥
tls-auth ta.key 1
#通讯加密方式,与服务器保持一致
cipher AES-128-CBC
#在初始化完毕后,降低OpenVPN的权限(该指令仅限于非Windows系统中使用)
user nobody
group nogroup
# 设置日志文件冗余级别(0~9)
# 0 表示静默运行,只记录致命错误
# 4 表示合理的常规用法
# 5 和 6 可以帮助调试连接错误
# 9 表示极度冗余,输出非常详细的日志信息
verb 2
# 忽略过多的重复信息
# 相同类别的信息只有前20条会输出到日志文件中
mute 20
#重连设置
keepalive 10 120
# 在VPN连接中启用压缩。
# 该指令的启用/禁用应该与服务器端保持一致
comp-lzo
#持久化选项可以尽量避免访问在重启时由于用户权限降低而无法访问的某些资源。
persist-key
persist-tun
# 启用该指令,与服务器连接中断后将自动重新连接
#这在网络不稳定的情况下(例如:笔记本电脑无线网络)非常有用
resolv-retry infinite
# 大多数客户端不需要绑定本机特定的端口号
nobind

Openvpn *.tgz 文件简介

文件名是否必须说明
ca.crt根CA证书
staff-\*.crt客户端证书
staff-\*.key客户端密钥
ta.key连接加密,防御DOS、UDP淹没攻击
dh2048.pem迪菲-赫尔曼密钥交换通讯加密
staff-\*.ovpnopenvpn配置文件(windows)
staff-\*.confopenvpn配置文件(linux)

Openvpn-Linux客户端安装与配置

安装步骤请参考 [openvpn服务器安装]

Openvpn-Mac-OS客户端安装与配置

  • 客户端下载地址 : Tunnelblick
  • 获取openvpn证书及配置文件: 联系openvpn负责人
  • 配置文件加载

...未完续待...

Openvpn-windows客户端安装与配置

  • 客户端下载地址:OpenVPN
  • 获取openvpn证书及配置文件: 联系openvpn负责人
  • 配置文件加载
  • 前往C:\Program Files\OpenVPN\config将配置文件压缩包(*.tgz)里的下列文件拷入其中
  • ca.crt
  • ta.key
  • dh2048.pem
  • staff_*.ovpn
  • staff_*.crt
  • staff_*.key
  • 双击桌面图标 「OpenVPN GUI」 按提示操作即可运行。(请留意连接成功后所提示分配到的ip地址是否与管理员提供的一致,如不一致请与管理员联系)
汪淼

汪淼

china