[root@openvpn easy-rsa]# cd /opt/easy-rsa/[root@openvpn easy-rsa]# /opt/easy-rsa/easyrsa init-pki
Note: using Easy-RSA configuration from:/opt/easy-rsa/vars
init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is:/opt/easy-rsa/pki
复制代码
2.5.创建根证书
根证书用于ca对之后生成的server和client证书签名时使用。(输入两次密码,直接回车)
代码语言:javascript
复制
[root@openvpn easy-rsa]# /opt/easy-rsa/easyrsa build-ca
Note: using Easy-RSA configuration from:/opt/easy-rsa/vars
Using SSL: openssl OpenSSL 1.0.2k-fips 26 Jan 2017
Enter New CA Key Passphrase:
Re-Enter New CA Key Passphrase:
Generating RSAprivate 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.-----
Common Name(eg: your user, host, or server name)[Easy-RSACA]:CA creation complete and you may now import and sign cert requests.
Your newCA certificate file for publishing is at:/opt/easy-rsa/pki/ca.crt
复制代码
2.6.创建server端证书和私钥文件
nopass表示不加密私钥文件,生成过程中直接回车默认
代码语言:javascript
复制
[root@openvpn easy-rsa]# /opt/easy-rsa/easyrsa gen-req server nopass
Note: using Easy-RSA configuration from:/opt/easy-rsa/vars
Using SSL: openssl OpenSSL 1.0.2k-fips 26 Jan 2017
Generating a 2048 bit RSAprivate key
...........................+++........................................................................+++
writing newprivate key to '/opt/easy-rsa/pki/easy-rsa-1326.TIfM4D/tmp.rxSnIM'-----
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]:
Keypair and certificate request completed. Your files are:req:/opt/easy-rsa/pki/reqs/server.req
key:/opt/easy-rsa/pki/private/server.key
复制代码
2.7.给server证书签名(输入yes,输入密码)
代码语言:javascript
复制
[root@openvpn easy-rsa]# /opt/easy-rsa/easyrsa sign server server
Note: using Easy-RSA configuration from:/opt/easy-rsa/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 for825days:
subject=
commonName = server
Type the word 'yes' to continue, or any other input to abort.
Confirm request details: yes
Using configuration from /opt/easy-rsa/pki/easy-rsa-1397.ds5qpo/tmp.lX0IFN
Enter pass phrase for/opt/easy-rsa/pki/private/ca.key:
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName:ASN.112:'server'
Certificate is to be certified until Jun 314:02:462023GMT(825 days)
Write out database with1newentries
Data Base Updated
Certificate created at:/opt/easy-rsa/pki/issued/server.crt
复制代码
2.8.创建Diffie-Hellman文件,秘钥交换时的Diffie-Hellman算法
代码语言:javascript
复制
/opt/easy-rsa/easyrsa gen-dh
复制代码
2.9.创建client端证书和私钥文件
nopass表示不加密私钥文件,生成过程中直接回车默认
代码语言:javascript
复制
[root@openvpn easy-rsa]# /opt/easy-rsa/easyrsa gen-req client nopass
Note: using Easy-RSA configuration from:/opt/easy-rsa/vars
Using SSL: openssl OpenSSL 1.0.2k-fips 26 Jan 2017
Generating a 2048 bit RSAprivate key
..................................................................+++...................................................................................................................+++
writing newprivate key to '/opt/easy-rsa/pki/easy-rsa-1761.HYs4Xv/tmp.z02JuI'-----
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:/opt/easy-rsa/pki/reqs/client.req
key:/opt/easy-rsa/pki/private/client.key
复制代码
2.10. 给client端证书签名(输入yes,输入密码)
代码语言:javascript
复制
[root@openvpn easy-rsa]# /opt/easy-rsa/easyrsa sign client client
Note: using Easy-RSA configuration from:/opt/easy-rsa/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 client certificate for825days:
subject=
commonName = client
Type the word 'yes' to continue, or any other input to abort.
Confirm request details: yes
Using configuration from /opt/easy-rsa/pki/easy-rsa-1828.VwQHeF/tmp.eYqBSS
Enter pass phrase for/opt/easy-rsa/pki/private/ca.key:
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName:ASN.112:'client'
Certificate is to be certified until Jun 314:09:372023GMT(825 days)
Write out database with1newentries
Data Base Updated
Certificate created at:/opt/easy-rsa/pki/issued/client.crt
复制代码
四、OpenVPN服务端部署
1.安装openvpn软件
代码语言:javascript
复制
yum -y install openvpn
复制代码
2.修改配置文件
自行创建配置文件/etc/openvpn/server.conf,并加入如下配置
代码语言:javascript
复制
port 1194 #端口
proto udp #协议
dev tun #采用路由隧道模式tun
ca ca.crt #ca证书文件位置
cert server.crt #服务端公钥名称
key server.key #服务端私钥名称
dh dh.pem #交换证书
server 10.8.0.0255.255.255.0 #给客户端分配地址池,注意:不能和VPN服务器内网网段有相同
push "route 192.168.31.1 255.255.255.0" #允许客户端访问内网192.168.31.1网段
ifconfig-pool-persist ipp.txt #地址池记录文件位置
keepalive 10120 #存活时间,10秒ping一次,120 如未收到响应则视为断线
max-clients 100 #最多允许100个客户端连接
status openvpn-status.log #日志记录位置
verb 3 #openvpn版本
client-to-client #客户端与客户端之间支持通信
log /var/log/openvpn.log #openvpn日志记录位置
persist-key #通过keepalive检测超时后,重新启动VPN,不重新读取keys,保留第一次使用的keys。
persist-tun #检测超时后,重新启动VPN,一直保持tun是linkup的。否则网络会先linkdown然后再linkup
duplicate-cn
复制代码
3. 拷贝证书到openvpn主配置文件目录下
代码语言:javascript
复制
cp -a /opt/easy-rsa/pki/ca.crt /etc/openvpn/
cp -a /opt/easy-rsa/pki/issued/server.crt /etc/openvpn/
cp -a /opt/easy-rsa/pki/private/server.key /etc/openvpn/
cp -a /opt/easy-rsa/pki/dh.pem /etc/openvpn/
复制代码