一、证书制作
1、复制密钥生成文件EasyRSA-2.2.2到服务器目录
2、解压文件并给与777权限
[root@localhost ~]# unzip EasyRSA-2.2.2.zip
[root@localhost ~]# chmod -R 777 EasyRSA-2.2.2
3、修改vars配置文件
[root@localhost EasyRSA-2.2.2]# vim vars
[root@localhost EasyRSA-2.2.2]# grep -Ev '^#|^$' vars
export EASY_RSA="`pwd`"
export OPENSSL="openssl"
export PKCS11TOOL="pkcs11-tool"
export GREP="grep"
export KEY_CONFIG=`$EASY_RSA/whichopensslcnf $EASY_RSA`
export KEY_DIR="$EASY_RSA/keys"
echo NOTE: If you run ./clean-all, I will be doing a rm -rf on $KEY_DIR
export PKCS11_MODULE_PATH="dummy"
export PKCS11_PIN="dummy"
export KEY_SIZE=2048
export CA_EXPIRE=3650
export KEY_EXPIRE=3650
export KEY_COUNTRY="cn" #国家
export KEY_PROVINCE="henan" #省
export KEY_CITY="zhengzhou" #城市
export KEY_ORG="xinan" #组织
export KEY_EMAIL="3037800336@qq.com" #邮箱
export KEY_OU="xinan" #公司、组织
export KEY_NAME="EasyRSA"
[root@localhost EasyRSA-2.2.2]# source vars
NOTE: If you run ./clean-all, I will be doing a rm -rf on /root/EasyRSA-2.2.2/keys
#刷新环境并更新配置,以后的密钥文件都放在keys目录下
[root@localhost EasyRSA-2.2.2]# ./clean-all
[root@localhost EasyRSA-2.2.2]# ls
build-ca build-key-pass build-req keys openssl-1.0.0.cnf vars
build-dh build-key-pkcs12 build-req-pass list-crl pkitool whichopensslcnf
build-inter build-key-server clean-all openssl-0.9.6.cnf revoke-full
build-key build-ner inherit-inter openssl-0.9.8.cnf sign-req
4、生成根证书和密钥
ca.crt ca.key
[root@localhost EasyRSA-2.2.2]# ./build-ca
Generating a 2048 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) [henan]:
Locality Name (eg, city) [zhengzhou]:
Organization Name (eg, company) [xinan]:
Organizational Unit Name (eg, section) [xinan]:
Common Name (eg, your name or your server's hostname) [xinan CA]:
Name [EasyRSA]:
Email Address [3037800336@qq.com]:
[root@localhost EasyRSA-2.2.2]# ls keys
ca.crt ca.key index.txt serial
5、生成服务端证书和密钥
server.crt server.key
[root@localhost EasyRSA-2.2.2]# ./build-key-server server
Generating a 2048 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) [henan]:
Locality Name (eg, city) [zhengzhou]:
Organization Name (eg, company) [xinan]:
Organizational Unit Name (eg, section) [xinan]:
Common Name (eg, your name or your server's hostname) [server]:
Name [EasyRSA]:
Email Address [3037800336@qq.com]:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /root/EasyRSA-2.2.2/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:'henan'
localityName :PRINTABLE:'zhengzhou'
organizationName :PRINTABLE:'xinan'
organizationalUnitName:PRINTABLE:'xinan'
commonName :PRINTABLE:'server'
name :PRINTABLE:'EasyRSA'
emailAddress :IA5STRING:'3037800336@qq.com'
Certificate is to be certified until Jul 5 00:52:45 2031 GMT (3650 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
[root@localhost EasyRSA-2.2.2]# ls keys
01.pem ca.crt ca.key index.txt index.txt.attr index.txt.old serial serial.old server.crt server.csr server.key
6、生成客户端证书和密钥
client.key client.crt
[root@localhost EasyRSA-2.2.2]# ./build-key client
Generating a 2048 bit RSA private key
..........................+++
...................................+++
writing new private key to 'client.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) [henan]:
Locality Name (eg, city) [zhengzhou]:
Organization Name (eg, company) [xinan]:
Organizational Unit Name (eg, section) [xinan]:
Common Name (eg, your name or your server's hostname) [client]:
Name [EasyRSA]:
Email Address [3037800336@qq.com]:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /root/EasyRSA-2.2.2/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:'henan'
localityName :PRINTABLE:'zhengzhou'
organizationName :PRINTABLE:'xinan'
organizationalUnitName:PRINTABLE:'xinan'
commonName :PRINTABLE:'client'
name :PRINTABLE:'EasyRSA'
emailAddress :IA5STRING:'3037800336@qq.com'
Certificate is to be certified until Jul 5 00:55:35 2031 GMT (3650 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
[root@localhost EasyRSA-2.2.2]# ls keys/
01.pem ca.crt client.crt client.key index.txt.attr index.txt.old serial.old server.csr
02.pem ca.key client.csr index.txt index.txt.attr.old serial server.crt server.key
7、生成密钥交换文件
dh2048.pem
[root@localhost EasyRSA-2.2.2]# ./build-dh
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
............................................................+.............................................+................................+...................................................................+..........................+..................................................................................................+..................................................................................................................+.....+..+........+.+.......
[root@localhost EasyRSA-2.2.2]# ls keys
01.pem ca.crt client.crt client.key index.txt index.txt.attr.old serial server.crt server.key
02.pem ca.key client.csr dh2048.pem index.txt.attr index.txt.old serial.old server.csr
二、配置OpenVPN服务端
1、安装OpenVPN并修改配置文件
[root@localhost EasyRSA-2.2.2]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# curl -o epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 664 100 664 0 0 10639 0 --:--:-- --:--:-- --:--:-- 10709
[root@localhost yum.repos.d]# ls
CentOS-Base.repo CentOS-fasttrack.repo CentOS-Vault.repo epel.repo
CentOS-CR.repo CentOS-Media.repo CentOS-x86_64-kernel.repo
CentOS-Debuginfo.repo CentOS-Sources.repo docker-ce.repo
[root@localhost yum.repos.d]# yum clean all && yum makecache
[root@localhost yum.repos.d]# yum -y install openvpn
#配置openvpn 配置文件在/etc/openvpn
[root@localhost yum.repos.d]# cd /etc/openvpn/
[root@localhost openvpn]# mkdir keys
[root@localhost keys]# cd /root/EasyRSA-2.2.2/keys
#复制证书文件
[root@localhost keys]# cp {server.crt,server.key,ca.crt,dh2048.pem} /etc/openvpn/keys
[root@localhost keys]# cd /etc/openvpn/keys
[root@localhost keys]# ls
ca.crt dh2048.pem server.crt server.key
#复制配置文件
[root@localhost keys]# cp /usr/share/doc/openvpn-2.4.11/sample/sample-config-files/server.conf ../
[root@localhost keys]# vim ../server.conf
[root@localhost keys]# grep -Ev '^#|^$' ../server.conf
;local a.b.c.d
port 1194
;proto tcp
proto udp
;dev tap
dev tun
;dev-node MyTap
ca keys/ca.crt
cert keys/server.crt
key keys/server.key # This file should be kept secret
dh keys/dh2048.pem
;topology subnet
server 10.0.1.0 255.255.255.0 #客户端进来以后分配的地址网段
ifconfig-pool-persist ipp.txt
;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100
;server-bridge
;push "route 192.168.10.0 255.255.255.0"
;push "route 192.168.20.0 255.255.255.0"
push "route 10.0.1.0 255.255.255.0" #添加路由规则要跟上面的分配网段一致
push "route 192.168.0.0 255.255.255.0" #添加内网路由规则
;client-config-dir ccd
;route 192.168.40.128 255.255.255.248
;client-config-dir ccd
;route 10.9.0.0 255.255.255.252
;learn-address ./script
;push "redirect-gateway def1 bypass-dhcp"
;push "dhcp-option DNS 208.67.222.222"
;push "dhcp-option DNS 208.67.220.220"
;client-to-client
;duplicate-cn
keepalive 10 120
tls-auth keys/ta.key 0 #拒绝服务攻击文件
cipher AES-256-GCM #加密模式
;compress lz4-v2
;push "compress lz4-v2"
;comp-lzo
;max-clients 100
;user nobody
;group nobody
persist-key
persist-tun
status openvpn-status.log
;log openvpn.log
;log-append openvpn.log
verb 3
;mute 20
explicit-exit-notify 1
#启用路由转发
[root@localhost keys]# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
[root@localhost keys]# sysctl -p
net.ipv4.ip_forward = 1
2、建立ta.key文件(拒绝服务攻击证书文件)
[root@localhost keys]# openvpn --genkey --secret ta.key
[root@localhost keys]# ls
ca.crt dh2048.pem server.crt server.key ta.key
3、启动openvpn服务
[root@localhost openvpn]# cd /etc/openvpn/
[root@localhost openvpn]# openvpn --daemon --config server.conf
[root@localhost openvpn]# netstat -tlnup | grep 1194
udp 0 0 0.0.0.0:1194 0.0.0.0:* 20157/openvpn