liukuan.net

liukuan.net,发现,记录,分享

186

Centos6安装openvpn使用easy-rsa3.0证书认证


安装openvpn

1
2
<code>yum install -y epel-release
yum install -y openvpn easy-rsa openssl openssl-devel lzo lzo-devel pam pam-devel automake pkgconfig</code>

上述命令执行完成后,会有一个/etc/openvpn的目录,通常我们把配置文件都放在这个目录下。

1.查看openvpn版本:

1
2
3
<code>[root@openvpn ~]# openvpn --version |head -n1
OpenVPN 2.4.6 x86_64-redhat-linux-gnu [Fedora EPEL patched] [SSL (OpenSSL)] [LZO] [LZ4] [EPOLL] [PKCS11] [MH/PKTINFO] [AEAD] built on Apr 26 2018
</code>

2.复制服务端配置文件到配置文件目录

1
2
<code>[root@openvpn ~]# cp /usr/share/doc/openvpn-2.4.6/sample/sample-config-files/server.conf /etc/openvpn/
</code>

服务端证书

1
2
3
4
<code>ls /usr/share/easy-rsa/3.0.3/     #easy-rsa3的版本只有如下几个文件、目录,比2版本少了很多
easyrsa  openssl-1.0.cnf  x509-types
mkdir /etc/openvpn/easy-rsa
cp -r /usr/share/easy-rsa/3.0.3/* /etc/openvpn/easy-rsa/</code>
  • ca证书制作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<code>cd /etc/openvpn/easy-rsa
[root@localhost easy-rsa]# cp /usr/share/doc/easy-rsa-3.0.3/vars.example ./vars
[root@localhost easy-rsa]# vim vars        #修改证书的相关配置,根据需要自定义,也可以忽略不设置
 
set_var EASYRSA_REQ_COUNTRY     &quot;US&quot;          #国家
set_var EASYRSA_REQ_PROVINCE    &quot;California&quot;    #省
set_var EASYRSA_REQ_CITY        &quot;Shanghai&quot;       #城市
set_var EASYRSA_REQ_ORG &quot;Copyleft Certificate Co&quot;    #组织
set_var EASYRSA_REQ_EMAIL       &quot;test@example.net&quot;     #邮箱
set_var EASYRSA_REQ_OU          &quot;My Organizational Unit&quot;    #公司、组织
 
[root@localhost easy-rsa]# ./easyrsa init-pki          #初始化pki,生成目录文件结构
[root@localhost easy-rsa]# ./easyrsa build-ca            #创建ca证书
 
Note: using Easy-RSA configuration from: ./vars            #使用vars文件里面配置的信息
Generating a 2048 bit RSA private key
.................+++
........................................................................................+++
writing new private key to '/etc/openvpn/easy-rsa/pki/private/ca.key.Lg8IKADc4Q'
Enter PEM pass phrase:                  #设置ca密码(我此处是写的123456)
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.
-----
Common Name (eg: your user, host, or server name) [Easy-RSA CA]:          #直接回车,就是默认的CA作为名字
 
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/pki/ca.crt        #ca证书存放路径
</code>
  • 服务端证书server.crt

1).制作证书

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<code>[root@localhost easy-rsa]# ./easyrsa gen-req server nopass   #nopass设置免证书密码,如果要设置密码可以取消此参数选项
Note: using Easy-RSA configuration from: ./vars       #使用vars文件里面配置的信息
Generating a 2048 bit RSA private key
.....................................+++
................................................................................................+++
writing new private key to '/etc/openvpn/easy-rsa/pki/private/server.key.yuG9HRsSlU'
-----
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.
-----
Common Name (eg: your user, host, or server name) [server]:    #直接回车,默认名字为server
 
Keypair and certificate request completed. Your files are:
req: /etc/openvpn/easy-rsa/pki/reqs/server.req
key: /etc/openvpn/easy-rsa/pki/private/server.key              #密钥key的路径
</code>

2).证书签名、签约

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<code>[root@localhost easy-rsa]# ./easyrsa sign server server            #第二个server是只上面服务端证书的CN名字,我们用的默认server,根据实际证书名自行定义
 
Note: using Easy-RSA configuration from: ./vars
 
 
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 3650 days:
 
subject=
    commonName                = server
 
 
Type the word 'yes' to continue, or any other input to abort.
  Confirm request details: yes
Using configuration from ./openssl-1.0.cnf
Enter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key:            #输入上面ca证书生成时的密码(123456)
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :PRINTABLE:'server'
Certificate is to be certified until May 22 03:23:38 2028 GMT (3650 days)
 
Write out database with 1 new entries
Data Base Updated
 
Certificate created at: /etc/openvpn/easy-rsa/pki/issued/server.crt          #服务端证书路径
</code>
  • dh证书
1
2
3
4
5
6
7
8
<code>[root@localhost easy-rsa]# ./easyrsa gen-dh     #创建Diffie-Hellman,时间有点长
Note: using Easy-RSA configuration from: ./vars
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
............................................................+...........................................................+.......................................................................................................+...........+..........................................................................................................................................................................................................................................................................+............................................................................................................+....................................................................................................+................................................................+.....................................................................................................................................................+............................................+............+......................................................................................+......................................................................+...........................+................................................................+...........................................................................................................++*++*
 
DH parameters of size 2048 created at /etc/openvpn/pki/dh.pem      #dh证书路径
</code>
  • ta密钥
1
2
3
<code>cd /etc/openvpn
openvpn --genkey --secret ta.key
</code>

客户端证书

为了便于区别,我们把客户端使用的证书存放在新的路径。/etc/openvpn/client

  • 创建客户端证书
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<code>[root@localhost client]# mkdir -p /etc/openvpn/client
[root@localhost client]# cd /etc/openvpn/client
[root@localhost client]# cp -r /usr/share/easy-rsa/3.0.3/* /etc/openvpn/client
[root@localhost client]# cp /usr/share/doc/easy-rsa-3.0.3/vars.example ./vars
[root@localhost client]# ./easyrsa init-pki
[root@localhost client]# ./easyrsa gen-req client nopass   #client为证书名,可自定义,nopass同样设置免密
Generating a 2048 bit RSA private key
.....................................................+++
.................................+++
writing new private key to '/etc/openvpn/client/pki/private/client.key.0rbEXauafe'
-----
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.
-----
Common Name (eg: your user, host, or server name) [client]:
 
Keypair and certificate request completed. Your files are:
req: /etc/openvpn/client/pki/reqs/client.req
key: /etc/openvpn/client/pki/private/client.key               #key路径
</code>
  • 对客户端证书签名、签约
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<code>#切换到服务端easy-rsa目录下:
cd /etc/openvpn/easy-rsa
#导入req
./easyrsa import-req /etc/openvpn/client/pki/reqs/client.req client
./easyrsa sign client client        #签名,第一个client是固定的参数表示客户端,第二个client指上面导入的客户端证书名
./easyrsa sign client client
Note: using Easy-RSA configuration from: ./vars
 
 
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 client certificate for 3650 days:
 
subject=
    commonName                = client
 
 
Type the word 'yes' to continue, or any other input to abort.
  Confirm request details: yes                                    #输入'yes'
Using configuration from ./openssl-1.0.cnf
Enter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key:   #输入ca密码(123456)
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :PRINTABLE:'client'
Certificate is to be certified until Apr 13 14:37:17 2028 GMT (3650 days)
 
Write out database with 1 new entries
Data Base Updated
 
Certificate created at: /etc/openvpn/easy-rsa/pki/issued/client.crt     #最终客户端证书路径
</code>

修改配置文件

服务器端证书和密钥统一放到和server.conf一个目录下,便于配置

1
2
3
4
5
<code>cp /etc/openvpn/easy-rsa/pki/ca.crt /etc/openvpn/
cp /etc/openvpn/easy-rsa/pki/private/server.key /etc/openvpn/
cp /etc/openvpn/easy-rsa/pki/issued/server.crt /etc/openvpn/
cp /etc/openvpn/easy-rsa/pki/dh.pem /etc/openvpn/
</code>

1.修改openvpn服务端配置文件server.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<code>cat /etc/openvpn/server.conf
local 0.0.0.0
port 1194           #指定端口
proto tcp            #指定协议
dev tun             #采用路由隧道模式
ca ca.crt            #ca证书位置,相对路径,表示ca.crt和server.conf要在同一目录
cert server.crt      #服务端证书
key server.key       #服务端key
dh dh.pem             #dh密钥
server 10.8.0.0 255.255.255.0        #给客户端分配的地址池
ifconfig-pool-persist ipp.txt
push &quot;redirect-gateway def1 bypass-dhcp&quot;     #客户端网关使用openvpn服务器网关
push &quot;dhcp-option DNS 8.8.8.8&quot;               #指定dns
push &quot;dhcp-option DNS 114.114.114.114&quot;
keepalive 10 120              #心跳检测,10秒检测一次,2分钟内没有回应则视为断线
tls-auth ta.key 0             #服务端值为0,客户端为1
cipher AES-256-CBC
comp-lzo            #传输数据压缩
persist-key
persist-tun
status openvpn-status.log
verb 3
</code>

2.设置客户端使用的配置文件(在用户客户端使用)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<code>
yum install -y openvpn  #linux客户端安装
 
cp /usr/share/doc/openvpn-2.4.6/sample/sample-config-files/client.conf /etc/openvpn/client.ovpn
cat /etc/openvpn/client.ovpn
client
dev tun
proto tcp             #和server端一致
remote 123.xxx.xxx.xxx 1194   #指定服务端IP和端口
resolv-retry infinite
nobind
persist-key
persist-tun
remote-cert-tls server
ca ca.crt          #ca证书
cert client.crt      #客户端证书
key client.key      #客户端密钥
tls-auth ta.key 1    #ta密钥
cipher AES-256-CBC
comp-lzo         #传输内容压缩
verb 3         #日志级别
</code>

3.客户端所需证书(下载保存到客户端和客户端配置文件同一目录下)

1
/etc/openvpn/easy-rsa/pki/issued/client.crt
#在服务端证书生成目录下
1
/etc/openvpn/client/pki/private/client.key
#上面的客户端生成目录下
1
/etc/openvpn/easy-rsa/pki/ca.crt
#ca证书
1
/etc/openvpn/ta.key

服务启动

服务端:

1
2
<code>server openvpn start
</code>

windows客户端:
双击客户端图标运行(比较简单,就不叙述了)

Linux客户端:

1
2
<code>openvpn --daemon --cd /etc/openvpn --config client.ovpn --log-append /var/log/openvpn.log   #放后台执行  
</code>

防火墙设置

开启内核转发

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<code>[root@localhost]# sed -i 's#net.ipv4.ip_forward = 0#net.ipv4.ip_forward = 1#' /etc/sysctl.conf
[root@localhost]# sysctl -p
 
[root@localhost]# cat /etc/sysconfig/iptables               
# Generated by iptables-save v1.4.7 on Tue Dec 26 16:39:36 2017
*filter
:INPUT ACCEPT [1707:292253]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1615:1130954]
-A FORWARD -i tun+ -j ACCEPT 
COMMIT
# Completed on Tue Dec 26 16:39:36 2017
# Generated by iptables-save v1.4.7 on Tue Dec 26 16:39:36 2017
*nat
:PREROUTING ACCEPT [28:3113]
:POSTROUTING ACCEPT [16:960]
:OUTPUT ACCEPT [22:1365]
-A POSTROUTING -o eth0 -j MASQUERADE 
COMMIT
 
#iptables -t nat -L -n
[root@localhost]# iptables -t nat -L -n
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
 
Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
MASQUERADE  all  --  0.0.0.0/0            0.0.0.0/0           
 
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination </code>
原文地址 : https://liukuan.net/php/124.html
本站遵循 : 知识共享署名-非商业性使用-相同方式共享 3.0 版权协议
版权声明 : 原创文章转载时,请务必以超链接形式标明 文章原始出处
作者:admin | 分类:php | 标签: Null
此文章共有条评论, 人参与 |Powerd By Angboo