谷动谷力

 找回密码
 立即注册
查看: 1766|回复: 4
打印 上一主题 下一主题
收起左侧

OpenVPN 开源服务搭建VPN整体架构解决方案

[复制链接]
跳转到指定楼层
楼主
发表于 2021-10-1 15:15:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
OpenVPN服务搭建

OpenVPN 为开源产品

1、VPN整体架构解决方案
┌───────────────────────────────────────────┐
│                                                                                      │
│                                     IDC 机房                                         │
│                                                                                      │
│ ┌──────────┐                                                             │
│ ││-- keys           │                                     ┌─────────┐  │
│ ││  │-- ca.crt     │                                     │   Lan Server 1   │  │
│ ││  │-- ca.key     │           ┌─────────┐  ↗│eth0 : 172.16.1.8 │  │  
│ ││  │-- dh1024.pem │  配置文件 │    VPN Server    │↙  └─────────┘  │
│ ││  │-- server.crt │ ←────│eth0 : 210.74.1.7 │                            │
│ ││  │-- server.csr │           │eth1 : 172.16.1.7 │↖                          │
│ ││  │-- server.key │           └─────────┘  ↘┌─────────┐  │
│ ││  │-- ta.key     │             ↗                      │   Lan Server 2   │  │
│ ││   `-- ca.key     │           ╱                        │eth0 : 172.16.1.9 │  │
│ │ `-- server.conf    │         ↗                          └─────────┘  │
│ └──────────┘       ╱                                                    │
│                              ↗                                                      │
└───────────────────────────────────────────┘
                             ↗
                           ╱   访问目的地址:172.16.1.0/24
   远程拨号目的地址:    ↗     所使用源地址:10.6.27.0/24
   IP :210.74.1.7     ╱               ┌────────────────────┐
                ┌──────┐        │ d:\program files\openvpn\config\tear   │
                │ 办公电脑   │软件安装│ ca.crt                                 │
                │Windows 系统│───→│ tear.crt                               │
                │            │配置文件│ tear.key                               │
                └──────┘        │ tear_vpn.ovpn                          │
                                        │ 路由 : push "route 172.16.16.1.0 ..."  │
                                        └────────────────────┘

2、企业或 IDC 机房网络互联场景逻辑图

┌───────────────────────────────────────────────────────────────────┐
│                                                                                                                                      │
│                                                           IPSec VPN Top                                                              │
│                                                                                                                                      │
│                                 北京 IDC 机房生产环境                           深圳 IDC 机房生产环境                                │
│ ┌─────────┐         ┌─────────┐                          ┌─────────┐         ┌─────────┐ │
│ │  LanA Server 1   │         │ A1 VPN Server    │                          │ B1 VPN Server    │         │  LanB Server 1   │ │
│ │eth0 : 172.16.1.17│ ←──→│eth0 : 10.0.0.7   │↖                      ↗│eth0 : 10.0.0.8   │←──→ │eth0 :192.168.1.18│ │
│ └─────────┘         │eth1 : 172.16.1.7 │  ╲                  ╱  │eth1 : 192.168.1.8│         └─────────┘ │  
│                      ↖        └─────────┘    ↘              ↙    └─────────┘        ↗                      │
│ ┌─────────┐ ╲    ↗         ↑                 ┌─────┐               ↑           ↖    ╱ ┌─────────┐ │
│ │  LanA Server 2   │─ ↘╱ ─────┤                 │ Internet │               ├────── ╲↙ ─│  LanB Server 2   │ │
│ │eth0 : 172.16.1.18│   ↗╲           ↓                 └─────┘               ↓             ╱↖   │eth0 :192.168.1.19│ │
│ └─────────┘ ╱    ↘┌─────────┐    ↗              ↖    ┌─────────┐↙    ╲ └─────────┘ │
│                      ↙        │ A2 VPN Server    │  ╱                  ╲  │ B2 VPN Server    │        ↘                      │
│ ┌─────────┐         │eth0 : 10.0.0.3   │↙                      ↘│eth0 : 10.0.0.2   │         ┌─────────┐ │
│ │  LanA Server N   │ ←──→│eth1 : 172.16.1.3 │                          │eth1 : 192.168.1.2│←──→ │  LanB Server N   │ │
│ │eth0 : 172.16.1.19│         └─────────┘                          └─────────┘         │eth0 :192.168.1.17│ │           
│ └─────────┘                                                                                        └─────────┘ │
│  说明:LANA Server 网关统一为 eth1:172.16.1.7                                         说明:LANB Server 网关统一为 eth1:192.168.1.8  │
│                                                                                                                                      │
└───────────────────────────────────────────────────────────────────┘
                           
以上是两个 IDC 机房的 IPSec VPN 的互联场景逻辑图,在实际生产环境中,可以有三个五个,甚至更多,也是没有问题的。
但是尽量不要形成环状的互联,要形成星型的互联。
以上每全 IDC 机房都有两台 VPN Server ,可以做高可用,也可以做负载均衡,这就是开源产品的好处,可以做扩展。、
当然部署三五个都没有问题。
而对于 OpenVPN 来说,不可能两端都是 Server 端,而是一个是 Client ,一个是 Server ,只不过 Client 端用的是 linux 版本的软件。
通过 Client 拨号访问 Server 服务器,因为 LanA Server 的网关都是 A1 VPN Server ,所以都可以访问到 Server 服务器后端的 LanB Server 。
而对于 IPsec VPN 来说的话,则两边部署的都是 Server 端,然后每个 Server 端的 内网 Server 都可以访问对端 Server 的内网 Server 。
对于 OpenVPN 或 IPSec VPN 都用到两个重要的命令 route 和 tcpdump 。
当前实验环境:
台式机 Client IP :10.0.0.100/24
OpenVPN Server OS :CentOS 6.5
               WAN IP eth1 :10.0.0.1/24
               LAN IP eth0 :192.168.1.212/24
OpenVPN Server 所在内网 web 服务器 IP 为 :192.168.1.207/24
                                  网关为 :192.168.1.1
注意:web 服务器的网关不是 VPN Server 。

3. 实验需求:
在远端通过 vpn 客户端拨号到 vpn server ,然后可以直接访问 vpnserver 所在局域网内的多个 server ,进行管理维护
VPN Server 查看系统部署情况 :
  1. [root@VPN-A ~]# cat /etc/redhat-release
  2. CentOS release 6.5 (Final)
  3. [root@VPN-A ~]# uname -r
  4. 2.6.32-431.el6.x86_64
  5. [root@VPN-A ~]# uname -m
  6. x86_64
复制代码

回复

使用道具 举报

沙发
 楼主| 发表于 2021-10-1 15:20:14 | 只看该作者
实验步骤
1、配置时间同步
[root@VPN-A ~]# /usr/sbin/ntpdate time.windows.com
注意:如果时间相差太大,会有很多乱七八糟的问题出现,甚至会出现连接不上的问题。
加入到定时任务:
[root@VPN-A ~]# echo '#time sync' >> /var/spool/cron/root
[root@VPN-A ~]# echo '*/5 * * * * /usr/sbin/ntpdate time.windows.com 2&> /dev/null' >> /var/spool/cron/root
[root@VPN-A ~]# crontab -l
#time sync
*/5 * * * * /usr/sbin/ntpdate time.windows.com 2&> /dev/null
2、创建存放工具目录
[root@VPN-A ~]# mkdir -p /home/tear/tools/openvpn
[root@VPN-A ~]# cd /home/tear/tools/openvpn
OpenVPN 的安装涉及到两个软件:lzo-2.06.tar.gz 和 openvpn-2.2.2.tar.gz 。
注意:因为这个版本比较稳定,这里选择使用这个版本。
因为 VPN 传输是经过公网的,所以需要一个压缩模块 lzo。
先把这两个软件上传到以上目录中。
[root@VPN-A openvpn]# ls
lzo-2.06.tar.gz  openvpn-2.2.2.tar.gz
[root@VPN-A openvpn]# tar -zxf lzo-2.06.tar.gz
[root@VPN-A openvpn]# cd lzo-2.06
[root@VPN-A lzo-2.06]# ./configure
[root@VPN-A lzo-2.06]# make && make install
[root@VPN-A lzo-2.06]# echo $?
0
[root@VPN-A lzo-2.06]# cd ..
开始安装 openvpn ,注意:openvpn 是依赖 ssl 和 tls 协议,需要安装相关软件包。
直接使用 yum 安装方便,如果不安装在编译安装 openvpn 的时候会出现问题。
[root@VPN-A openvpn]# yum -y install openssl*
[root@VPN-A openvpn]# tar -zxf openvpn-2.2.2.tar.gz
[root@VPN-A openvpn]# cd openvpn-2.2.2
[root@VPN-A openvpn-2.2.2]# ./configure --with-lzo-headers=/usr/local/include/ --with-lzo-lib=/usr/local/lib/
[root@VPN-A openvpn-2.2.2]# make && make install
[root@VPN-A openvpn-2.2.2]# echo $?
0
[root@VPN-A openvpn-2.2.2]# cd ..
[root@VPN-A openvpn]# which openvpn
/usr/local/sbin/openvpn                          <--出现此结果,就代表安装好了。
3、配置 openvpn server 建立 CA 证书
注意:需要到 openvpn 安装包的目录里去创建,因为这个版本的 openvpn 已经包含了这个工具,不需要再去别的地方弄。
[root@VPN-A openvpn]# cd openvpn-2.2.2/easy-rsa/2.0/
首先修改一下这个目录下的 vars 文件。
[root@VPN-A 2.0]# cp vars vars.tear.20160814              <--备份原文件
[root@VPN-A 2.0]# ls vars*
vars  vars.tear.20160814
编辑这个文件,然后切换到文件的结尾:
64 export KEY_COUNTRY="US"
65 export KEY_PROVINCE="CA"
66 export KEY_CITY="SanFrancisco"
67 export KEY_ORG="Fort-Funston"
68 export KEY_EMAIL="me@myhost.mydomain"
69 export KEY_EMAIL=mail@host.domain
70 export KEY_CN=changeme
71 export KEY_NAME=changeme
72 export KEY_OU=changeme
73 export PKCS11_MODULE_PATH=changeme
74 export PKCS11_PIN=1234
以上就是生成 CA 及服务端、客户端证书时需要提交的信息,先在这里修改,在生成时直接按“回车”即可方便操作。
修改为:
64 export KEY_COUNTRY="CN"                   <--国家
65 export KEY_PROVINCE="GD"                   <--省
66 export KEY_CITY="ShenZhen"                   <--市
67 export KEY_ORG="SYS"                          <--组织
68 export KEY_EMAIL="tear@sys.local"             <--邮箱
69 export KEY_EMAIL=tear@sys.local               <--邮箱
70 export KEY_CN=CN
71 export KEY_NAME=tear
72 export KEY_OU=tear
73 export PKCS11_MODULE_PATH=changeme            <--导入模块路径
74 export PKCS11_PIN=1234                        <--导入密码
修改后需要重新加载配置文件:
[root@VPN-A 2.0]# source vars
NOTE: If you run ./clean-all, I will be doing a rm -rf on /home/tear/tools/openvpn/openvpn-2.2.2/easy-rsa/2.0/keys
根据上面的提示,在搭建或者是重新创建证书目录时,可以使用当前目录下的 clean-all 命令,清除 keys 目录。
[root@VPN-A 2.0]# ./clean-all
使用当前目录下的 build-ca 命令新建证书:
[root@VPN-A 2.0]# ./build-ca
Generating a 1024 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) [CN]:                                   <--默认按“回车”即可
State or Province Name (full name) [GD]:                             <--默认按“回车”即可
Locality Name (eg, city) [ShenZhen]:                                 <--默认按“回车”即可
Organization Name (eg, company) [SYS]:                               <--默认按“回车”即可
Organizational Unit Name (eg, section) [tear]:                       <--默认按“回车”即可
Common Name (eg, your name or your server's hostname) [CN]: tear     <--这里输入 tear,可根据自己的需求设置
Name [tear]:                                                         <--默认按“回车”即可
Email Address [tear@sys.local]:                                      <--默认按“回车”即可
以上就是刚修改 vars 配置文件相关的内容,方便操作。
查看生成的 CA 证书:
[root@VPN-A 2.0]# ls keys/
ca.crt  ca.key  index.txt  serial

回复 支持 反对

使用道具 举报

板凳
 楼主| 发表于 2021-10-1 15:22:05 | 只看该作者
4、生成服务端的密钥文件
使用当前目录的 build-key-server 命令创建
[root@VPN-A 2.0]# ./build-key-server server      <--后面的是 server 是指定的文件名
注意:这个名字定义好后,以后都会需要调用到的。
[root@VPN-A 2.0]# ./build-key-server server
Generating a 1024 bit RSA private key
......++++++
...................................................++++++
writing new private key to 'server.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) [CN]:                                     <--默认按“回车”即可
State or Province Name (full name) [GD]:                               <--默认按“回车”即可
Locality Name (eg, city) [ShenZhen]:                                   <--默认按“回车”即可
Organization Name (eg, company) [SYS]:                                 <--默认按“回车”即可
Organizational Unit Name (eg, section) [tear]:                         <--默认按“回车”即可
Common Name (eg, your name or your server's hostname) [server]:        <--默认按“回车”即可
Name [tear]:                                                           <--默认按“回车”即可
Email Address [tear@sys.local]:                                        <--默认按“回车”即可
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456                                         <--要给一个密码,这个密码是在你发起证书请求的密码,这里
                                                                          设置为:123456,根据自己的要求更改。
An optional company name []:sys                                        <--一个可选的公司名
Using configuration from /home/tear/tools/openvpn/openvpn-2.2.2/easy-rsa/2.0/openssl-1.0.0.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName           RINTABLE:'CN'
stateOrProvinceName   RINTABLE:'GD'
localityName          RINTABLE:'ShenZhen'
organizationName      RINTABLE:'SYS'
organizationalUnitNameRINTABLE:'tear'
commonName            RINTABLE:'server'
name                  RINTABLE:'tear'
emailAddress          :IA5STRING:'tear@sys.local'
Certificate is to be certified until Aug 12 07:17:54 2026 GMT (3650 days)
Sign the certificate? [y/n]:y                                          <--输入 y

1 out of 1 certificate requests certified, commit? [y/n]y              <--输入 y
Write out database with 1 new entries
Data Base Updated                                                      <--有这个提示为成功
查看所生成的证书文件:
[root@VPN-A 2.0]# ls keys/
01.pem  ca.key     index.txt.attr  serial      server.crt  server.key
ca.crt  index.txt  index.txt.old   serial.old  server.csr
5、生成客户端证书和 key 文件
注意:客户端证书可以给不同的用户生成不同的证书和密钥文件,那样以后方便管理。
使用当前目录下的 build-key 命令
[root@VPN-A 2.0]# ./build-key tear       <--在工作当中,这个其实就是使用者的名字
Generating a 1024 bit RSA private key
.....++++++
.................................++++++
writing new private key to 'tear.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) [CN]:                                     <--默认按“回车”即可
State or Province Name (full name) [GD]:                               <--默认按“回车”即可
Locality Name (eg, city) [ShenZhen]:                                   <--默认按“回车”即可
Organization Name (eg, company) [SYS]:                                 <--默认按“回车”即可
Organizational Unit Name (eg, section) [tear]:                         <--默认按“回车”即可
Common Name (eg, your name or your server's hostname) [tear]:          <--默认按“回车”即可
Name [tear]:                                                           <--默认按“回车”即可
Email Address [tear@sys.local]:                                        <--默认按“回车”即可
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456                                         <--要给一个密码,这个密码是在你发起证书请求的密码,这里
                                                                          设置为:123456,根据自己的要求更改。
An optional company name []:tear                                       <--默认按“回车”即可
Using configuration from /home/tear/tools/openvpn/openvpn-2.2.2/easy-rsa/2.0/openssl-1.0.0.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName           RINTABLE:'CN'
stateOrProvinceName   RINTABLE:'GD'
localityName          RINTABLE:'ShenZhen'
organizationName      :PRINTABLE:'SYS'
organizationalUnitName:PRINTABLE:'tear'
commonName            :PRINTABLE:'tear'
name                  :PRINTABLE:'tear'
emailAddress          :IA5STRING:'tear@sys.local'
Certificate is to be certified until Aug 12 07:26:44 2026 GMT (3650 days)
Sign the certificate? [y/n]:y                                          <--输入 y

1 out of 1 certificate requests certified, commit? [y/n]y              <--输入 y
Write out database with 1 new entries
Data Base Updated
[root@VPN-A 2.0]# ls keys/
01.pem  ca.crt  index.txt       index.txt.attr.old  serial      server.crt  server.key  tear.csr
02.pem  ca.key  index.txt.attr  index.txt.old       serial.old  server.csr  tear.crt    tear.key
生成了 tear 用户的三个文件。
此时,就一共有两个证书及密钥分别是:公用的 CA 证书、服务端的证书、客户端的证书。
需要注意的是:如果使用 build-key 来创建用户的证书,那么这个用户只需要提供证书就可进行拨号连接,不需要密码。
下面创建一个需要使用证书和密码认证的用户证书及密钥文件。
使用当前目录下的 build-key-pass 命令:
[root@VPN-A 2.0]# ./build-key-pass tom
Generating a 1024 bit RSA private key
.........................................++++++
....++++++
writing new private key to 'tom.key'
Enter PEM pass phrase:                                   <--输入用户密码,当拨号时就会需要密码
Verifying - Enter PEM pass phrase:                       <--再次输入用户密码
-----
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) [GD]:                              <--默认按“回车”即可
Locality Name (eg, city) [ShenZhen]:                                  <--默认按“回车”即可
Organization Name (eg, company) [SYS]:                                <--默认按“回车”即可
Organizational Unit Name (eg, section) [tear]:                        <--默认按“回车”即可
Common Name (eg, your name or your server's hostname) [tom]:          <--默认按“回车”即可
Name [tear]:                                                          <--默认按“回车”即可
Email Address [tear@sys.local]:                                       <--默认按“回车”即可
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456                                        <--要给一个密码,这个密码是在你发起证书请求的密码,这里
                                                                         设置为:123456,根据自己的要求更改。
An optional company name []:sys                                       <--默认按“回车”即可
Using configuration from /home/tear/tools/openvpn/openvpn-2.2.2/easy-rsa/2.0/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:'GD'
localityName          :PRINTABLE:'ShenZhen'
organizationName      :PRINTABLE:'SYS'
organizationalUnitName:PRINTABLE:'tear'
commonName            :PRINTABLE:'tom'
name                  :PRINTABLE:'tear'
emailAddress          :IA5STRING:'tear@sys.local'
Certificate is to be certified until Aug 12 07:46:15 2026 GMT (3650 days)
Sign the certificate? [y/n]:y                                          <--输入 y

1 out of 1 certificate requests certified, commit? [y/n]y              <--输入 y
Write out database with 1 new entries
Data Base Updated
[root@VPN-A 2.0]# ls keys/
01.pem  ca.crt     index.txt.attr      serial      server.csr  tear.csr  tom.csr
02.pem  ca.key     index.txt.attr.old  serial.old  server.key  tear.key  tom.key
03.pem  index.txt  index.txt.old       server.crt  tear.crt    tom.crt

回复 支持 反对

使用道具 举报

地板
 楼主| 发表于 2021-10-1 15:23:26 | 只看该作者
6、生成 generate difflie hellman parameters  
提示:Diffie Hellman parameters must be generated for the the OpenVPN Server。
生成传输进行密钥交换时用到的交换密钥协议文件。
使用当前目录下的 build-dh 命令生成
[root@VPN-A 2.0]# ./build-dh
Generating DH parameters, 1024 bit long safe prime, generator 2
This is going to take a long time
.............................................+............+....................................................................................................................................................................................................................+..............................................................................................................+...................+...+.......+......................................................++*++*++*
这个步骤生成的是:
[root@VPN-A 2.0]# ls keys/dh1024.pem
keys/dh1024.pem
详解服务器及客户端的证书各文件用途
      ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
      ┃ Filename    ┃      Needed By         ┃        Purpose             ┃ Secret┃
      ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
      ┃ ca.crt      ┃server + all clients    ┃  Root CA certificate       ┃ NO    ┃
      ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
      ┃ ca.key      ┃key singing machine only┃  Root CA key               ┃ YES   ┃
      ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
      ┃ dh{n}.pem   ┃   server only          ┃  Diffie Hellman parameters ┃ NO    ┃
      ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
      ┃ server.crt  ┃   server only          ┃  Server Certificate        ┃ NO    ┃
      ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
      ┃ server.key  ┃   server only          ┃  Server key                ┃ YES   ┃
      ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
      ┃ tear.crt    ┃   tear only            ┃  tear  Certificate         ┃ NO    ┃
      ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
      ┃ tear.key    ┃   tear only            ┃  tear key                  ┃ YES   ┃
      ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
      ┃ tom.crt     ┃   tom only             ┃  tom  Certificate          ┃ NO    ┃
      ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
      ┃ tom.key     ┃   tom only             ┃  tom  key                  ┃ YES   ┃
      ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

为了防止恶意攻击(如 DOS、UDP port flooding),我们生成一个“HMAC firewall”
使用当前目录下的 openvpn 命令生成
[root@VPN-A 2.0]# openvpn --genkey --secret keys/ta.key
[root@VPN-A 2.0]# ls keys/ta.key
keys/ta.key
7、详解服务器端 VPN 重要命令
vars 脚本是用来创建环境变量,设置所需要的变量脚本
clean-all 脚本是创建生成 ca 证书及密钥文件所需要的文件及目录
build-ca 脚本生成 ca 证书(交互)
build-dh 脚本生成 Diffie-Hellman 文件(交互)
build-key-server 脚本生成服务端密钥(交互)
build-key 脚本生成客户端密钥(交互)
build-key-pass 脚本生成客户端带密码的密钥(交互)
build-dh 脚本生成 Diffie-Hellman 文件(交互)
pkitool 脚本直接使用 vars 的环境变量设置,直接生成证书(非交互)
8、详解服务器端 VPN server.conf 重要参数
1)拷贝 keys 及设置
把所有的 keys 和配置拷贝到 /etc/openvpn 目录下
[root@VPN-A 2.0]# mkdir -p /etc/openvpn
[root@VPN-A 2.0]# cd /home/tear/tools/openvpn/
[root@VPN-A openvpn]# cd openvpn-2.2.2/easy-rsa/2.0/
[root@VPN-A 2.0]# /bin/cp -ap keys /etc/openvpn/
[root@VPN-A 2.0]# cd /home/tear/tools/openvpn/openvpn-2.2.2/sample-config-files/
[root@VPN-A sample-config-files]# /bin/cp server.conf client.conf /etc/openvpn/
[root@VPN-A sample-config-files]# tree /etc/openvpn/
[root@VPN-A sample-config-files]# ls /etc/openvpn/
client.conf  keys  server.conf
服务端配置文件默认如下
[root@VPN-A sample-config-files]# grep -Ev ";|#|^$" server.conf
port 1194
proto udp
dev tun
ca ca.crt
cert server.crt
dh dh1024.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
keepalive 10 120
comp-lzo
persist-key
persist-tun
status openvpn-status.log
verb 3
企业生产环境 server.conf 配置案例:
配置参数:                           参数说明:
local 124.43.12.115                  哪一个本地地址要被 OpenVPN 进行监听,一般是外网 IP 地址
port 52115                           监听的端口,默认是 1194,这里为了安全起见,修改为 52115
proto upd                            指定监听的协议,当并发访问多时,推荐 tcp
dev tun                              vpn server 的模式采用路由模式,可选 tap 或 tun
ca ca.crt                            ca 证书,注意此文件和 server.conf 在一个目录下,否则要用绝对路径调用
cert server.crt                      server 端证书
key server.key                       server 端密钥
dh dh1024.pem                        密钥协议文件
server 10.8.0.0 255.255.255.0        这个是 vpn server 动态分配给 vpn client 的地址池,一般不需要更改,这个段
                                     不要和任何网络地址段重复。
ifconfig-pool-persist ipip.txt
push "route 10.0.0.0 255.255.255.0"  这个是 VPN Server 所在的内网网段,如果有多个可以写多个 push ,注意,此命令
                                     实际作用是在 vpn 客户端本地生成 vpn server 所在的内网网段通信,路由条目类似:
                                     10.0.0.0 255.255.255.0 10.8.0.9 10.8.0.10 1,如果想知道 VPN 到底在本地加了哪些
                                     路由,可以在拨号前,客户端上命令执行 route print 记录下所有路由条目,然后在拨号
                                     记录下所有路由条目,然后,用比较软件比较一下即可知道变化在哪。
                                     注意:可以有多条 push ,这是 openvpn 的特色。
client-to-clinet                     允许拨号的多个 vpn client 互相通信
duplicate-cn                         允许多个客户端使用同一个账号连接
keepalive 10 120                     每 10 秒 ping 一次,若是 120 秒未收到包,即定客户端断线
comp-lzo                             开启压缩功能
persist-key                          当 vpn 超时后,当重新启动 vpn 后,保持上一次使用的私钥,而不重新读取私钥
persist-tun                          通过 keepalive 检测 vpn 超时后,当重新启动 VPN 后,保持 tun 或 tap 设备自动
                                     连接状态
status openvpn-status.log            openvpn 日志状态信息
log /var/log/openvpn.log             日志文件
verb 3                               指定日志文件冗余
当前实验环境 server.conf 内容:
local 10.0.0.1
port 52115
proto tcp
dev tun
ca /etc/openvpn/keys/ca.crt
cert /etc/openvpn/keys/server.crt
key /etc/openvpn/keys/server.key
dh /etc/openvpn/keys/dh1024.pem
server 10.8.0.0 255.255.255.0
push "route 192.168.1.0 255.255.255.0"
ifconfig-pool-persist ipp.txt
keepalive 10 120
comp-lzo
persist-key
persist-tun
status openvpn-status.log
verb 3
client-to-client
duplicate-cn
log /var/log/openvpn.log
客户端默认配置文件:
[root@VPN-A openvpn]# egrep -v "#|^;|^$" client.conf
client
dev tun
proto udp
remote my-server-1 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cert client.crt
key client.key
ns-cert-type server
comp-lzo
verb 3
实验中客户端 client.conf 内容为:
client
dev tun
proto tcp                      <--与服务器使用同样的协议
remote 10.0.0.1 52115          <--服务器外网 IP 地址,端口
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cert tear.crt                  <--客户端证书
key tear.key                   <--客户端密钥
ns-cert-type server
comp-lzo
verb 3

9、调试服务端 vpn 服务启动环境
a)取消服务器上防火墙 iptables 对 openvpn (默认 1194,本例 52115)的拦截,以及允许服务进行转发
b)开启内核转发功能
[root@VPN-A openvpn]# vi /etc/sysconfig/iptables
在大概第 10 行左右添加以下内容:
-A INPUT -p tcp --dport 52115 -j ACCEPT
[root@VPN-A openvpn]# vi /etc/sysctl.conf
net.ipv4.ip_forward = 0
修改为:
net.ipv4.ip_forward = 1
[root@VPN-A openvpn]# sysctl -p       <--操作生效

回复 支持 反对

使用道具 举报

5#
 楼主| 发表于 2021-10-1 15:56:28 | 只看该作者
10、启动 VPN server 服务并检查

[root@VPN-A openvpn]# /usr/local/sbin/openvpn --config /etc/openvpn/server.conf &  <--在后台运行

[root@VPN-A openvpn]# pkill openvpn                                                <--停止 openvpn服务

注意:这里使用全路径来运行 openvpn ,是因为需要等下把它添加到 rc.local 文件里,作为开机自动启动。

[root@VPN-A openvpn]# netstat -lntup | grep vpn
tcp        0      0 10.0.0.1:52115              0.0.0.0:*                   LISTEN      21789/openvpn

用 netstat 命令查询服务是否启动。

添加到 rc.local 开启自动启动

[root@VPN-A openvpn]# echo '#startup openvpn service by tear' >> /etc/rc.local

[root@VPN-A openvpn]# echo '/usr/local/sbin/openvpn --config /etc/openvpn/server.conf &' >> /etc/rc.local

当 vpn 启动以后,本地会多一个虚拟网卡:

[root@VPN-A openvpn]# ifconfig

......

tun0      Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
          inet addr:10.8.0.1  P-t-P:10.8.0.2  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

注意:也可以把 oepnvpn 启动方式弄成 service openvpn start 等操作。

在 openvpn 安装目录中已经有相关现成的文件,直接拷贝到 /etc/init.d/ 目录下,并重命名为 openvpn ,需要注意的是:
配置文件的名字就只可是 server.conf ,则不可以手动指定配置文件。

[root@VPN-A openvpn]# cp /home/tear/tools/openvpn/openvpn-2.2.2/sample-scripts/openvpn.init /etc/init.d/openvpn

修改 /etc/init.d/openvpn 文件,在 148 行:

148         for c in `/bin/ls *.conf 2>/dev/null`; do

修改为:

148         for c in `/bin/ls server.conf 2>/dev/null`; do      

指定配置文件名,因为在 /etc/openvpn 目录下有两个 .conf 文件,所以在这里就需要指定。如果只有一个则不需要修改。

[root@VPN-A openvpn]# chmod 700 /etc/init.d/openvpn       <--设置权限

[root@VPN-A openvpn]# chkconfig openvpn on                <--即可以使用 chkconfig 设置开机自动启动

[root@VPN-A openvpn]# service openvpn restart

11、安装 windows 客户端配置 VPN 连接

windows 安装好客户端后(这里就默认安装到 C:\Program Files\OpenVPN\),

打开 C:\Program Files\OpenVPN\config 目录,把这目录下所有文件删除。

然后建立以使用者名字命令的文件夹,把服务器上的 ca.crt 、tear.crt、tear.key 都放在这个目录里。

并在这个目录下创建一个文件为 tear.ovpn ,注意:后缀名为 .ovpn 。

内容为:

client
dev tun
proto tcp
remote 10.0.0.1 52115
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cert tear.crt
key tear.key
ns-cert-type server
comp-lzo
verb 3

注意:这里以 tear 用户做为测试。

运行 OpenVPN GUI 程序。

点击右下角OpenVPN GUI,点击 content 。

成功连接。

注意:如果有多个用户的配置文件时,右下角则会有多个用户可以选择。

12、调试客户端可以访问 VPN Server 所在内网的服务器。

注意:按照以上操作,客户端是可以成功拨号访问到 VPN Server 服务器。

但是,因为 VPN Server 所在内网服务器的网关指的并不是 VPN Server 。如果指向的是 VPN Server
那么,客户端就可地正常的访问 VPN Server 。

此时,在客户端上操作 ping VPN Server 所在内网的服务器:

C:\Users\Administrator>ping 192.168.1.254

正在 Ping 192.168.1.254 具有 32 字节的数据:
请求超时。
请求超时。
请求超时。
请求超时。

192.168.1.254 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 0,丢失 = 4 (100% 丢失),

服务器上使用 tcpdump 抓包结果:

[root@OpenVPN-A keys]# tcpdump -nnn -s 10000 | grep -i icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 10000 bytes
13:28:10.240185 IP 10.8.0.6 > 192.168.1.135: ICMP echo request, id 1, seq 19, length 40
13:28:15.117122 IP 10.8.0.6 > 192.168.1.135: ICMP echo request, id 1, seq 20, length 40
13:28:20.124361 IP 10.8.0.6 > 192.168.1.135: ICMP echo request, id 1, seq 21, length 40
13:28:25.116514 IP 10.8.0.6 > 192.168.1.135: ICMP echo request, id 1, seq 22, length 40

会发现,数据包是有去无回的。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|深圳市光明谷科技有限公司|光明谷商城|Sunshine Silicon Corpporation ( 粤ICP备14060730号|Sitemap

GMT+8, 2024-4-19 14:18 , Processed in 0.095158 second(s), 40 queries .

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表