需要一下rpm包及资源:
net-tools-2.0-0.25.20131004git.el7.x86_64.rpm
openvpn-2.4.8-1.el7.x86_64.rpm
pkcs11-helper-1.11-3.el7.x86_64.rpm
https://github.com/OpenVPN/easy-rsa/releases/download/v3.0.8/EasyRSA-3.0.8.tgz
安装openvpn及相关工具
yum localinstall openvpn-2.4.8-1.el7.x86_64.rpm pkcs11-helper-1.11-3.el7.x86_64.rpmyum localinstall net-tools-2.0-0.25.20131004git.el7.x86_64.rpm
服务端证书生成
为了简化证书生成流程这里使用easy-rsa工具包。
先创建一个工作目录用来存放生成证书中要用到的各种文件
mkdir /etc/openvpn/easy-rsa
准备证书生成相关文件
cp -r 解压后的EasyRSA-3.0.8.tgz /etc/openvpn/easy-rsa/
准备生成证书用的
CSR相关配置cat <<EOF > /etc/openvpn/easy-rsa/vars#公司信息,根据情况自定义set_var EASYRSA_REQ_COUNTRY "CHN"ANHUIset_var EASYRSA_REQ_PROVINCE "ANHUI"set_var EASYRSA_REQ_CITY "HEFEI"set_var EASYRSA_REQ_ORG "YSF"set_var EASYRSA_REQ_EMAIL "ysf@ysf.com"set_var EASYRSA_REQ_OU "ysf"#证书有效期set_var EASYRSA_CA_EXPIRE 3650set_var EASYRSA_CERT_EXPIRE 3650EOF
生成
CA证书 ```bash [root@root easy-rsa]# cd /etc/openvpn/easy-rsa/ [root@root easy-rsa]# ./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/pki ./easyrsa build-ca [root@root easy-rsa]# ./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:#设置一个密码,下面给证书签名时会用到,这里我设置为:888888 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.
Common Name (eg: your user, host, or server name) [Easy-RSA CA]:设置CN,直接回车使用默认:Easy-RSA 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
1. 生成服务端证书
[root@root easy-rsa]# ./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/pki/private/server.key.VWbGpsGSpM’
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]:设置CN,直接回车使用默认: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
1. 使用`CA`给服务端证书签名```bash[root@root easy-rsa]# ./easyrsa sign server serverNote: using Easy-RSA configuration from: ./varsUsing SSL: openssl OpenSSL 1.0.2k-fips 26 Jan 2017You are about to sign the following certificate.Please check over the details shown below for accuracy. Note that this requesthas not been cryptographically verified. Please be sure it came from a trustedsource 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 = serverType the word 'yes' to continue, or any other input to abort.Confirm request details: yes #输入yes确认Using configuration from /etc/openvpn/easy-rsa/pki/safessl-easyrsa.cnfEnter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key:#输入上边步骤4中生成CA时设置的密码Check that the request matches the signatureSignature okThe Subject's Distinguished Name is as followscommonName :ASN.1 12:'server'Certificate is to be certified until Apr 2 04:27:27 2030 GMT (3650 days)Write out database with 1 new entriesData Base UpdatedCertificate created at: /etc/openvpn/easy-rsa/pki/issued/server.crt
- 生成
DH证书 ```bash [root@root easy-rsa]# ./easyrsa gen-dh
Note: using Easy-RSA configuration from: ./vars
Using SSL: openssl OpenSSL 1.0.2k-fips 26 Jan 2017 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/easy-rsa/pki/dh.pem
1. 生成ta密钥```bashopenvpn --genkey --secret /etc/openvpn/ta.key
- 将server端证书和密钥都统一放到
/etc/openvpn/目录下,方便管理和配置。[root@root openvpn]# cp /etc/openvpn/easy-rsa/pki/ca.crt /etc/openvpn/[root@root openvpn]# cp /etc/openvpn/easy-rsa/pki/private/server.key /etc/openvpn/[root@root openvpn]# cp /etc/openvpn/easy-rsa/pki/issued/server.crt /etc/openvpn/[root@root openvpn]# cp /etc/openvpn/easy-rsa/pki/dh.pem /etc/openvpn/[root@root openvpn]# ls -ltotal 36-rw------- 1 root root 1172 Apr 4 12:52 ca.crtdrwxr-x--- 2 root openvpn 4096 Nov 1 20:11 client-rw------- 1 root root 424 Apr 4 12:52 dh.pemdrwxr-xr-x 4 root root 4096 Apr 4 12:27 easy-rsadrwxr-x--- 2 root openvpn 4096 Nov 1 20:11 server-rw------- 1 root root 4547 Apr 4 12:52 server.crt-rw------- 1 root root 1704 Apr 4 12:52 server.key-rw------- 1 root root 636 Apr 4 12:50 ta.key
客户端证书生成
生成证书 ``` [root@root easy-rsa]# ./easyrsa gen-req test nopass Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/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/pki/easy-rsa-13661.rmfNmd/tmp.mJ9AJb’
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) [test]: Keypair and certificate request completed. Your files are: req: /etc/openvpn/easy-rsa/pki/reqs/test.req key: /etc/openvpn/easy-rsa/pki/private/test.key
记住这个文件`/etc/openvpn/easy-rsa/pki/private/test.key`路径,下边会用到。<br />**签名**```bash# 签名[root@root easy-rsa]# ./easyrsa sign-req client testNote: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/varsUsing SSL: openssl OpenSSL 1.0.2k-fips 26 Jan 2017You are about to sign the following certificate.Please check over the details shown below for accuracy. Note that this requesthas not been cryptographically verified. Please be sure it came from a trustedsource 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 = testType the word 'yes' to continue, or any other input to abort.Confirm request details: yesUsing configuration from /etc/openvpn/easy-rsa/pki/easy-rsa-13784.sXpDIp/tmp.XXJ1dHEnter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key:Check that the request matches the signatureSignature okThe Subject's Distinguished Name is as followscommonName :ASN.1 12:'test'Certificate is to be certified until May 11 09:23:36 2030 GMT (3650 days)Write out database with 1 new entriesData Base UpdatedCertificate created at: /etc/openvpn/easy-rsa/pki/issued/test.crt
证书文件/etc/openvpn/easy-rsa/pki/issued/test.crt,下边会用到
配置
主配置文件:/etc/openvpn/server.conf
[root@root ~]# cat > /etc/openvpn/server.conf <<EOFlocal 0.0.0.0port 1194proto udpdev tun#user openvpn#group openvpnca ca.crtcert server.crtkey server.keydh dh.pem#客户端地址池server 192.168.255.0 255.255.255.0#内网网段push "route 172.16.0.0 255.240.0.0"ifconfig-pool-persist ipp.txt#心跳检测,10秒检测一次,2分钟内没有回应则视为断线keepalive 10 120#服务端值为0,客户端为1tls-auth ta.key 0cipher AES-256-CBC#传输数据压缩comp-lzopersist-keypersist-tunstatus openvpn-status.logverb 3EOF
_
端口放行
firewall-cmd --zone=public --add-port=1194/tcp --permanentfirewall-cmd --zone=public --add-port=1194/udp --permanentfirewall-cmd --reload
启动服务
systemctl start openvpn@serversystemctl enable openvpn@server
开启内核转发
echo 1 > /proc/sys/net/ipv4/conf/all/forwardingecho 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
客户端配置文件
创建客户端配置文件/etc/openvpn/client/test.conf
clientdev tunproto udpremote XX.XX.XX.XX 1194resolv-retry infinitenobindpersist-keypersist-tunremote-cert-tls servercomp-lzoverb 3#CA证书ca ca.crt#客户端证书cert test.crt#客户端密钥key test.key#ta密钥tls-auth ta.key 1cipher AES-256-CBC
将所需要的证书从server端下放到xnile.ovpn所在的当前目录。
启动客户服务
systemctl start openvpn-client@test.servicesystemctl enable openvpn-client@test.service
