本教程不描述如何FQ

一、OpenVPN服务端部署

$ yum -y install net-tools lzo lzo-devel openssl-devel pam-devel gcc gcc-c++ make unzip
$ wget https://files01.tchspt.com/temp/openvpn-2.4.6.tar.gz
$ tar xvf openvpn-2.4.6.tar.gz -C /usr/local/src
$ cd /usr/local/src/openvpn-2.4.6/
$ ./configure --prefix=/usr/local/openvpn
$ make && make install
​
$ mkdir /etc/openvpn
$ cp -r /usr/local/src/openvpn-2.4.6/sample /etc/openvpn/
$ cp /etc/openvpn/sample/sample-config-files/server.conf /etc/openvpn
​
$ cd
$ wget https://github.com/OpenVPN/easy-rsa/archive/master.zip
$ unzip master.zip
$ mv easy-rsa-master easy-rsa
$ cp -r easy-rsa /etc/openvpn/
$ cd /etc/openvpn/easy-rsa/easyrsa3/
$ cp vars.example  vars
$ chmod +x vars
#   cn_only  - use just a CN value
#   org      - use the "traditional" Country/Province/City/Org/OU/email/CN format
    set_var EASYRSA_DN              "org"
    set_var EASYRSA_REQ_COUNTRY     "CN"
    set_var EASYRSA_REQ_PROVINCE    "Zhejiang"
    set_var EASYRSA_REQ_CITY        "Hangzhou"
    set_var EASYRSA_REQ_ORG         "Qygame Certificate"
    set_var EASYRSA_REQ_EMAIL       "m17608418893@163.com"
    set_var EASYRSA_REQ_OU          "My OpenVPN"
    ···
    set_var EASYRSA_NS_SUPPORT      "yes"

1.初始化,在当前目录创建PKI目录

用于存储一些中间变量以及最终生成的证书

$ cd /etc/openvpn/easy-rsa/easyrsa3/
$ ./easyrsa init-pki
​
Note: using Easy-RSA configuration from: ./vars
​
init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /etc/openvpn/easy-rsa/easyrsa3/pki

2.创建根证书

用于ca对之后生成的server和client证书签名时使用

$ ./easyrsa build-ca
Note: using Easy-RSA configuration from: ./vars
​
Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017
​
Enter New CA Key Passphrase:                # 设定密码,后面签约证书时需要
Re-Enter New CA Key Passphrase:             # 确认密码
Generating RSA private key, 2048 bit long modulus
..........+++
........................................................+++
e is 65537 (0x10001)
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) [Zhejiang]:                   #默认回车
Locality Name (eg, city) [Hangzhou]:                            #默认回车
Organization Name (eg, company) [Qygame Certificate]:             #默认回车
Organizational Unit Name (eg, section) [My OpenVPN]:              #默认回车
Common Name (eg: your user, host, or server name) [Easy-RSA CA]:   #默认回车
Email Address [m17608418893@163.com]:                           #默认回车
​
CA creation complete and you may now import and sign cert requests.
Your new CA certificate file for publishing is at:
/etc/openvpn/easy-rsa/easyrsa3/pki/ca.crt

3.创建server端证书和private key

nopass表示不加密的private key

$ ./easyrsa gen-req server nopass
Note: using Easy-RSA configuration from: ./vars
​
Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017
Generating a 2048 bit RSA private key
.....................+++
..+++
writing new private key to '/etc/openvpn/easy-rsa/easyrsa3/pki/private/server.key.gXRjSM1RHn'
-----
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) [Zhejiang]:                    #默认回车
Locality Name (eg, city) [Hangzhou]:                             #默认回车
Organization Name (eg, company) [Qygame Certificate]:              #默认回车
Organizational Unit Name (eg, section) [My OpenVPN]:               #默认回车
Common Name (eg: your user, host, or server name) [Easy-RSA CA]:    #默认回车
Email Address [m17608418893@163.com]:                             #默认回车
​
Keypair and certificate request completed. Your files are:
req: /etc/openvpn/easy-rsa/easyrsa3/pki/reqs/server.req
key: /etc/openvpn/easy-rsa/easyrsa3/pki/private/server.key

  

4.给server端证书做签名

$ ./easyrsa sign server server
Note: using Easy-RSA configuration from: ./vars
​
Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017
​
You are about to sign the following certificate.
Please check over the details shown below for accuracy. Note that this request
has not been cryptographically verified. Please be sure it came from a trusted
source or that you have verified the request checksum with the sender.
​
Request subject, to be signed as a server certificate for 1080 days:
​
subject=
    countryName               = CN
    stateOrProvinceName       = Zhejiang
    localityName              = Hangzhou
    organizationName          = Qygame Certificate
    organizationalUnitName    = My OpenVPN
    commonName                = server
    emailAddress              = m17608418893@163.com
​
​
Type the word 'yes' to continue, or any other input to abort.
  Confirm request details: yes
Using configuration from /etc/openvpn/easy-rsa/easyrsa3/pki/safessl-easyrsa.cnf
Enter pass phrase for /etc/openvpn/easy-rsa/easyrsa3/pki/private/ca.key:
                                                               #输入创建CA证书时的密码
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName           :PRINTABLE:'CN'
stateOrProvinceName   :ASN.1 12:'Zhejiang'
localityName          :ASN.1 12:'Hangzhou'
organizationName      :ASN.1 12:'Qygame Certificate'
organizationalUnitName:ASN.1 12:'My OpenVPN'
commonName            :ASN.1 12:'server'
emailAddress          :IA5STRING:'m17608418893@163.com'
Certificate is to be certified until Jan 29 06:02:34 2022 GMT (1080 days)
​
Write out database with 1 new entries
Data Base Updated
​
Certificate created at: /etc/openvpn/easy-rsa/easyrsa3/pki/issued/server.crt

  

5.创建Diffie-Hellman,确保key穿越不安全网络的命令

$ ./easyrsa gen-dh

  

6.创建客户端证书

$ mkdir /root/openvpn_users
$ cp  -r /etc/openvpn/easy-rsa/easyrsa3/ /root/openvpn_users
$ cd /root/openvpn_users/easyrsa3/
$ rm -rf pki
​
# 初始化
$ ./easyrsa init-pki
​
Note: using Easy-RSA configuration from: ./vars
​
init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /root/openvpn_users/easy-rsa/easyrsa3/pki
​
# 创建客户端key以及生成证书(生成的是自己输入的密码,client为自定义名字)
$ ./easyrsa gen-req client nopass
​
# 切换到Server证书目录下,将client.req导入,然后签约证书
$ cd /etc/openvpn/easy-rsa/easyrsa3/
$ ./easyrsa import-req /root/openvpn_users/easyrsa3/pki/reqs/client.req client1
​
# 用户签约
$ ./easyrsa sign client client1
​
# 查看生成的文件
$ ls /etc/openvpn/easy-rsa/easyrsa3/pki/reqs/
client1.req  server.req
$ ls /etc/openvpn/easy-rsa/easyrsa3/pki/ca.crt
/etc/openvpn/easy-rsa/easyrsa3/pki/ca.crt
$ ls /etc/openvpn/easy-rsa/easyrsa3/pki/private/
ca.key  server.key
$ ls /etc/openvpn/easy-rsa/easyrsa3/pki/issued/
server.crt  client1.crt
$ ls /etc/openvpn/easy-rsa/easyrsa3/pki/dh.pem
/etc/openvpn/easy-rsa/easyrsa3/pki/dh.pem
$ ls /root/openvpn_users/easy-rsa/easyrsa3/pki/private/
client.key
$ ls /root/openvpn_users/easy-rsa/easyrsa3/pki/reqs/
client.req
​
# 拷贝服务器证书到/etc/openvpn目录
$ cd /etc/openvpn/easy-rsa/easyrsa3/
$ cp pki/ca.crt /etc/openvpn/
$ cp pki/private/server.key  /etc/openvpn
$ cp pki/issued/server.crt /etc/openvpn/
$ cp pki/dh.pem /etc/openvpn/
​
# Client证书(集中在一个文件夹,给VPN用户使用)
$ mkdir /root/users/client1 -p
$ cd /root/users/client1/
$ cp /etc/openvpn/ca.crt .
$ cp /etc/openvpn/easy-rsa/easyrsa3/pki/issued/client1.crt .
$ cp /root/openvpn_users/easyrsa3/pki/private/client.key .
$ ls
ca.crt  vpn.crt  vpn.key

  

7.生成ta.key

$ vim /etc/profile
export PATH=/usr/local/openvpn/sbin/:$PATH
$ source /etc/profile
$ /usr/local/openvpn/sbin/openvpn --genkey --secret ta.key
$ cp ta.key /etc/openvpn/
$ cp ta.key /root/users/client1/

  

8.修改Server端配置文件

$ vim /etc/openvpn/server.conf
     # 申明本机使用的IP地址,也可以不说明
     local 192.168.1.142
     # 申明使用的端口,默认1194
     port 1194
     # 申明使用的协议,默认使用UDP,如果使用HTTP proxy,必须使用TCP协议,如果采用了tcp,需要注释最后的--explicit-exit-notify     can only be used with --proto udp
     proto tcp
     # 申明使用的设备可选tap和tun,tap是二层设备,支持链路层协议。
     dev tun
     # 指定ca证书的路径
     ca /etc/openvpn/ca.crt
     # 指定server.crt路径
     cert /etc/openvpn/server.crt
     # 指定server.key路径
     key /etc/openvpn/server.key
     # 指定dh.pem路径
     dh /etc/openvpn/dh.pem
     # 为VPN客户端指定分配的网络地址(自己根据规划分配)
     server 10.10.10.0 255.255.255.0
     # 定义客户端和虚拟ip地址之间的关系。特别是在openvpn重启时,再次连接的客户端将依然被分配和断开之前的IP地址
     ifconfig-pool-persist ipp.txt
     # 向客户端push网关,在进行FQ时会使用到
     ;push "redirect-gateway def1 bypass-dhcp"
     # 向客户端push DNS
     push "dhcp-option DNS 114.114.114.114"
     # 让客户端彼此可以互相访问
     client-to-client
     # 定义openvpn一个证书在同一时刻是否允许多个客户端接入,默认没有启用
     duplicate-cn
     # 记录日志,每次重新启动openvpn后删除原有的log信息。也可以自定义log的位置。默认是在/etc/openvpn/目录下
     log openvpn.log
     # 此处客户端配置文件中该参数需要改为 tls-auth sec.key 1
     tls-auth   /etc/openvpn/ta.key 0
     comp-lzo
     auth md5
      cipher AES-256-CBC
      max-clients 100
      keepalive 10 120
      persist-key
      persist-tun
      status openvpn-status.log
      verb 3

9.启动VPN

$ openvpn --config /etc/openvpn/server.conf &

  

二、客户端配置(Windows)

1.下载客户端

下载客户端Windows

2.安装客户端
1.双击下载包开始安装,Next

2.点击I Agree

3.勾选上 EasyRSA 2 Certificate Management Scripts

4.Install

默认安装位置不要改变

5.等待安装

6.安装完成,Next

7.完成

3.下载证书文件

将之前存放在/root/users/client1中的四个文件下载到客户端,并存放在C:\Program Files\OpenVPN\config目录下

4.创建client.ovpn

client
dev tun
proto udp
remote 10.0.8.28 1194  #主要这里修改成自己server ip  端口
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt               #这里需要证书
cert vpn.crt
key  vpn.key
auth md5
cipher AES-256-CBC
tls-auth ta.key 1
comp-lzo
verb 3

  

5.连接OpenVPN

点击桌面的OpnVPN GUI,桌面的右下角出现一个带锁的小电脑图标

右键带锁的电脑

选择Connect

等待如上桌面自动消失,再次观察刚才带锁的电脑,绿了则表示成功

============================================2019.4.6修改===============================================

连接图标显示为绿色,但是内网仍然ping不通,查看日志,报错内容如下:

Sat Apr 06 07:38:58 2019 ERROR: Windows route add command failed [adaptive]: returned error code 1
Sat Apr 06 07:38:58 2019 C:\WINDOWS\system32\route.exe ADD 172.16.1.0 MASK 255.255.255.0 172.16.1.5
Sat Apr 06 07:38:58 2019 ROUTE: route addition failed using CreateIpForwardEntry: 拒绝访问。   [status=5 if_index=12]
Sat Apr 06 07:38:58 2019 Route addition via IPAPI failed [adaptive]
Sat Apr 06 07:38:58 2019 Route addition fallback to route.exe
Sat Apr 06 07:38:58 2019 env_block: add PATH=C:\WINDOWS\System32;C:\WINDOWS;C:\WINDOWS\System32\Wbem
Sat Apr 06 07:38:58 2019 ERROR: Windows route add command failed [adaptive]: returned error code 1
Sat Apr 06 07:38:58 2019 Initialization Sequence Completed
Sat Apr 06 07:38:58 2019 MANAGEMENT: >STATE:1554507538,CONNECTED,SUCCESS,172.16.1.6,1.1.1.1,4396,192.168.0.8,59294

  解析:

此问题原因是在windows主机上没有权限去创建路由表,所以在运行时,我们需要指定以管理员身份去运行OpenVPN GUI.

6.简单测试

Win+R 输入cmd,进行如下测试

结果如上测试正常

<-----------------------------2019.4.9补充------------------------------------>

三、Linux客户端

$ yum -y install epel-release
$ yum -y install openvpn

  安装完成之后,将之前的客户端配置文件上传到/etc/openvpn/client目录下

$ pwd
/etc/openvpn/client
$ ls
ca.crt  client.crt  client.key  client.ovpn  ta.key

  运行

$ openvpn --daemon --config /etc/openvpn/client/client.ovpn --log-append /var/log/openvpn.log

  

四、报错

1.编译报错-1

$ yum -y install lz4-devel

  

2.编译报错-2

$ yum -y install net-tools
3.编译报错-3

$ yum -y install lzo-devel
4.运行报错-1

在运行服务端时,出现Could not determine IPv4/IPv6 protocol. Using AF_INET

可以在server.conf文件中指定proto时指定为tcp4/udp4,强制使用IPv4连接

5.运行报错-2

开启了tls-auth时,报错,检查你的client的秘钥文件

6.连接报错-3

成功连接上OpenVPN之后,ping内网发现不通

1.修改配置文件

$ vim /etc/openvpn/server.conf
# 向客户端推送的路由信息,假如客户端的IP地址为10.8.0.2,要访问10.0.0.0网段的话
push "route 10.0.0.0 255.0.0.0"     #VPN内网网段

2.修改iptables(附图的172.16.1.0/24应该为10.10.10.0/24,这是后面截得图,只做一个效果参考)

$ iptables -t nat -A POSTROUTING -s 10.10.10.0/24 -o eth0 -j MASQUERADE
$ iptables -nL -t nat

3.再次测试

------------------------------------------->2019年4月2日  更改   <------------------------------------------------

按照上述方式可以通过openvpn的秘钥进行连接,但是如果一旦仅仅是获取了秘钥就可以进行连接,显然这是非常不安全的,所以需要,开启双因素认证,有秘钥的同时需要输入正确的账号密码才可以连接,这也可以从一定程度上保证如果有同事离职,及时他保留了秘钥文件,但是没有账号密码的话,依然无法登陆

1.修改openvpn配置文件
auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env
username-as-common-name
script-security 3
2.检查账户密码脚本
$ vim /etc/openvpn/checkpsw.sh#!/bin/sh
###########################################################
# checkpsw.sh (C) 2004 Mathias Sundman <mathias@openvpn.se>
#
# This script will authenticate OpenVPN users against
# a plain text file. The passfile should simply contain
# one row per user with the username first followed by
# one or more space(s) or tab(s) and then the password.
PASSFILE="/etc/openvpn/psw-file"
LOG_FILE="/var/log/openvpn-password.log"
TIME_STAMP=`date "+%Y-%m-%d %T"`
###########################################################
if [ ! -r "${PASSFILE}" ]; then
  echo "${TIME_STAMP}: Could not open password file \"${PASSFILE}\" for reading." >> ${LOG_FILE}
  exit 1
fi
CORRECT_PASSWORD=`awk '!/^;/&&!/^#/&&$1=="'${username}'"{print $2;exit}' ${PASSFILE}`
if [ "${CORRECT_PASSWORD}" = "" ]; then
  echo "${TIME_STAMP}: User does not exist: username=\"${username}\", password=\"${password}\"." >> ${LOG_FILE}
  exit 1
fi
if [ "${password}" = "${CORRECT_PASSWORD}" ]; then
  echo "${TIME_STAMP}: Successful authentication: username=\"${username}\"." >> ${LOG_FILE}
  exit 0
fi
echo "${TIME_STAMP}: Incorrect password: username=\"${username}\", password=\"${password}\"." >> ${LOG_FILE}
exit 1

给执行权限

$ chmod +x /etc/openvpn/checkpsw.sh
3.创建用户和密码认证文件

账户和密码空格隔开

$ vim /etc/openvpn/psw-file
test  123456
4.客户端.ovpn文件配置

在客户端的.ovpn文件中添加

auth-user-pass

PS:欢迎各位指正,如有疑惑也可以留言,如果软件包下不到也可以留下邮箱,私发给你。

OpenVPN部署,实现访问云服务器的内网的更多相关文章

  1. 如何把php项目部署到阿里云服务器window server2012__含公网ip访问时jquery/ajax失效解决办法

    记一次蛋疼的折腾. 弄了一晚上最后发觉是360浏览器的问题,换个浏览器就好了.垃圾360用什么IE7文档模式.导致界面和功能失效. 建议大家测试的时候用firefox或者chrome. 项目部署到服务 ...

  2. 将nodejs代码部署到阿里云服务器

    概述 最近在做一个小项目,其中用nodejs做了个数据转发的接口,之后需要将这部分代码部署到服务器上面,并使用Nginx做反向代理.期间使用搜索引擎大量查阅了其他同鞋的经验,不过写的大多很笼统,因此踩 ...

  3. javaWeb项目部署到阿里云服务器步骤 完整版

    记录web项目部署到阿里云服务器步骤 (使用 web项目.阿里云服务器.Xftp.Xshell),敬请参考和指正 1.将要部署的项目打包成WAR文件格式,可以在MyEclipse.Eclipse都可以 ...

  4. 2018.11.7 关于将Web项目部署到阿里云服务器-5个步骤搞定

    将Eclipse导出的War包部署到阿里云服务器上,提供给移动端实时的访问 1. 先登录阿里云网站注册账号,选择服务器类型(我用的是 云服务器ECS), 如果你还是在读大学生可享受优惠价,最低好像是9 ...

  5. 将html代码部署到阿里云服务器,并进行域名解析,以及在部署过程中遇到的问题和解决方法

    本博客主要是说一下,,如何将html代码部署到阿里云服务器,并进行域名解析,以及在部署过程中遇到的问题和解决方法. 1.先在阿里云上购买一台阿里云服务器(ECS云服务器): 2.远程连接上该服务器,在 ...

  6. Java Web项目部署到阿里云服务器(ECS)

    本篇随笔只是记录博主第一次将自己的Java项目部署到阿里云服务器的大致过程,具体细节还请参考别的博文. 一.项目介绍 我做的项目是利用maven项目构建工具进行搭建基于SSM框架的代码共享管理系统,主 ...

  7. 【hexo博客搭建】将搭建好的hexo博客部署到阿里云服务器上面(下)

    一.部署到阿里云服务器 既然博客也已经成功在本地部署,然后主题也成功安装,接下来就可以部署到服务器上面了,如果你也想要魔改matery主题,可以去各种博客上面找一找大佬的教程,或者联系我,也可以让你少 ...

  8. javaWeb项目部署到阿里云服务器步骤

    记录web项目部署到阿里云服务器步骤 (使用 web项目.阿里云服务器.Xftp.Xshell),敬请参考和指正 1.将要部署的项目打包成WAR文件格式,可以在MyEclipse.Eclipse都可以 ...

  9. docker 部署公司阿里云服务器 (一)

    持续更新... 背景环境: 阿里云ecs服务器 centos7.4          公网地址:xx.xx.xx.xx      内网地址:172.16.77.4 阿里云RDS 阿里云 Redis 第 ...

随机推荐

  1. oracle储存过程学习笔记

    转载至: https://www.2cto.com/database/201610/559389.htm 1.什么是oracle存储过程 存储过程和函数也是一种PL/SQL块,是存入数据库的PL/SQ ...

  2. python基础之 正则表达式,re模块

    1.正则表达式 正则表达式:是字符串的规则,只是检测字符串是否符合条件的规则而已 1.检测某一段字符串是否符合规则 2.将符合规则的匹配出来re模块:是用来操作正则表达式的 2.正则表达式组成 字符组 ...

  3. [py]pandas数据统计学习

    pandas.core.base.DataError: No numeric types to aggregate错误规避 我没有去解决这个问题, 而用填充0规避了这个问题 统计 聚合 d = [ { ...

  4. MySQL数据库常用命令和概念 (1)

    一.数据库的创建: 1.创建一个名称为mydb1的数据库 create database mydb1; 2.创建一个使用utf8字符集的mydb2数据库. create database mydb2 ...

  5. adobe air for ios 应用完成appstore评论

    1,跳转到App Store:NSString *str = [NSString stringWithFormat:@"http://itunes.apple.com/us/app/id%d ...

  6. #WEB安全基础 : HTTP协议 | 0x11 HTTP的分块传输模块

    HTTP通信中,请求的编码实体资源没全部传输完成之前,浏览器无法显示页面,所以传输大容器数据时,把数据分块,能让浏览器逐步显示页面,这就叫分块传输模块 请看分块传输的流程图 每一块都会用十六进制来标记 ...

  7. 快速学习C语言途径,让你少走弯路

    1.标准C语言能干什么? 坦白讲,在今天软件已经发展了半个多世纪,单纯的C语言什么都干不了.标准C语言库只提供了一些通用的逻辑运算方法以及字符串处理,当然字符串在C语言看来也是一种操作内存的方法,所以 ...

  8. NOIP2018总结

    细细想来,学习OI也有4年多的时间了,今年已经是第二次参加noip提高组了,有必要写点什么了 NOIP2018 记得在天刚蒙蒙亮的时候走进70中,这是第四次了,但紧张只增不减,在刺骨的寒风下身体微微发 ...

  9. Oracle数据库的一些重要概念

    一.oracle数据库的结构 完整的Oracle数据库通常由两部分组成:Oracle数据库和数据库实例. 1) 数据库是一系列物理文件的集合(数据文件,控制文件,联机日志,参数文件等): 2) Ora ...

  10. 2018-2019-2 《网络对抗技术》Exp2 后门原理与应用 20165215

    目录 实验内容 基础问题回答 常用后门工具 Netcat windows 获取 linux 的shell linux 获取 winsdows 的shell 使用nc传输数据 使用nc传文件 Socat ...