https://blog.phpkoo.com/posts/490.html
一、环境版本
1、CentOS 7.4
2、OpenVPN 2.4.6
3、easy-rsa 3.0
二、安装
yum install epel-release
yum install -y openssl openssl-devel lzo lzo-devel pam pam-devel automake pkgconfig makecache
yum install -y openvpn
yum install -y easy-rsa
三、添加openvpn配置用户
#启动openvpn的用户
groupadd openvpn
useradd -g openvpn -M -s /sbin/nologin openvpn
四、创建配置文件
mkdir /etc/openvpn/
cp -r /usr/share/easy-rsa/ /etc/openvpn/
cp -r /usr/share/doc/easy-rsa-3.0.3/vars.example /etc/openvpn/easy-rsa/3.0/vars
cp -r /usr/share/doc/openvpn-2.4.6/sample/sample-config-files/server.conf /etc/openvpn/server.conf
五、修改easy-rsa密钥生成配置(修改第45、65、76、84-89、97、105、113、117、134、139、171、180、192行)
vim /etc/openvpn/easy-rsa/3.0/vars
set_var EASYRSA "$PWD"
set_var EASYRSA_PKI "$EASYRSA/pki"
set_var EASYRSA_DN "cn_only"
set_var EASYRSA_REQ_COUNTRY "CN"
set_var EASYRSA_REQ_PROVINCE "BEIJING"
set_var EASYRSA_REQ_CITY "BEIJING"
set_var EASYRSA_REQ_ORG "OpenVPN CERTIFICATE AUTHORITY"
set_var EASYRSA_REQ_EMAIL "110@qq.com"
set_var EASYRSA_REQ_OU "OpenVPN EASY CA"
set_var EASYRSA_KEY_SIZE 2048
set_var EASYRSA_ALGO rsa
set_var EASYRSA_CA_EXPIRE 7000
set_var EASYRSA_CERT_EXPIRE 3650
set_var EASYRSA_NS_SUPPORT "no"
set_var EASYRSA_NS_COMMENT "OpenVPN CERTIFICATE AUTHORITY"
set_var EASYRSA_EXT_DIR "$EASYRSA/x509-types"
set_var EASYRSA_SSL_CONF "$EASYRSA/openssl-1.0.cnf"
set_var EASYRSA_DIGEST "sha256"
六、生成ca证书
cd /etc/openvpn/easy-rsa/3.0
./easyrsa init-pki
./easyrsa build-ca
#设置ca密码(输入两次):
#生成Diffie Hellman key exchange文件
./easyrsa gen-dh
#生成tls-auth key文件
cd /etc/openvpn
openvpn --genkey --secret ta.key
#使用gen-req来生成req
cd /etc/openvpn/easy-rsa/3.0
./easyrsa gen-req wwwserver
#签发服务端证书
./easyrsa sign-req server wwwserver
#生成客户端用户
./easyrsa build-client-full www001
七、修改openvpn服务端配置
vim /etc/openvpn/server.conf
port 1194
proto udp
dev tun
ca /etc/openvpn/easy-rsa/3.0/pki/ca.crt
cert /etc/openvpn/easy-rsa/3.0/pki/issued/wwwserver.crt
key /etc/openvpn/easy-rsa/3.0/pki/private/wwwserver.key
dh /etc/openvpn/easy-rsa/3.0.3/pki/dh.pem
tls-auth /etc/openvpn/ta.key 0
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 223.5.5.5"
push "dhcp-option DNS 114.114.114.114"
keepalive 10 120
cipher AES-256-CBC
comp-lzo
max-clients 50
user openvpn
group openvpn
persist-key
persist-tun
status openvpn-status.log
log-append openvpn.log
verb 3
mute 20
八、服务器防火墙和路由配置
在iptables中添加一条路由转发规则, 并保存:
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j MASQUERADE
iptables-save
同时, 我们必须在sysctl中启用ip forward.
打开文件vi /etc/sysctl.conf, 并添加以下内容:
net.ipv4.ip_forward = 1
然后重启网络:
systemctl restart network.service
九、启动服务器
systemctl start openvpn@server
#启动时输入服务端证书密码:
十、客户端配置(下载客户端证书)
安装OpenVPN GUI
依屏幕指示安装openvpn gui即可。
三、配置OpenVPN GUI
安装结束后, 进入安装文件夹下的 config 目录,建立一个client.ovpn文件,内容如下
mkdir -p /etc/openvpn/client
cp -r /etc/openvpn/easy-rsa/3.0/pki/issued/www001.crt /etc/openvpn/client/
cp -r /etc/openvpn/easy-rsa/3.0/pki/private/www001.key /etc/openvpn/client/
cp -r /etc/openvpn/easy-rsa/3.0/pki/ca.crt /etc/openvpn/client/
cp -r /etc/openvpn/ta.key /etc/openvpn/client/
十一、修改客户端ovpn文件
client
dev tun
proto udp
remote xxx.xxx.xxx.xxx 1194 #改成外网IP
resolv-retry infinite
nobind
persist-key
persist-tun
comp-lzo
ca ca.crt
cert www001.crt
key www001.key
remote-cert-tls server
tls-auth ta.key 1
cipher AES-256-CBC
keepalive 10 120
verb 5
ps
1、如果是阿里云的专用网络,需要配置放行1194端口udp
2、如生成证书时输错密码(删除以下文件即可)
rm -rf /etc/openvpn/easy-rsa/3.0/pki/reqs/www001.req
rm -rf /etc/openvpn/easy-rsa/3.0/pki/private/www001.key
3、撤销证书
cd /etc/openvpn/easy-rsa/3.0
./easyrsa revoke www001
4、解决服务器每次都要输入Enter PEM pass phrase
openssl rsa -in server.key -out server.key.unsecure
服务器改用这个server.key.unsecure就不会每次提示了
二.客户端安装
1.windows客户端安装
1.1在windows上面安装openvpn-install-2.3.3-I002-x86_64.exe软件
1.2 进入服务端操作如下命令
cd /etc/openvpn/easy-rsa/2.0/keys
tar zcvf liuzhuang.tar.gz ca.crt liuzhuang.* #liuzhuang为客户端证书
1.3进入windows 解压liuzhuang.tar.gz到C:\Program Files\Open×××\config文件夹下
1.4复制C:\Program Files\Open×××\sample-config\client.ovpn到C:\Program Files\Open×××\config文件夹下,并做如下修改 #(修改存在下面的配置文件)
dev tun
proto tcp
remote 192.168.3.200 1194
ca ca.crt
cert liuzhuang.crt
key liuzhuang.key
1.5启动客户端
右键点击以管理员身份运行这软件,然后双击下面连接就OK。
2.linux客户端安装
======================================================
//基础库
yum install -y openssl openssl-devel lzo lzo-devel pam pam-devel automake pkgconfig
//openvpn
cd /root/soft
wget -c
tar zxvf openvpn-2.3.0.tar.gz
cd openvpn-2.3.0
./configure
make && make install
//配置
mkdir -p /etc/openvpn/keys
cp sample/sample-config-files/client.conf /etc/openvpn/
//创建日志目录
mkdir -p /var/log/openvpn/
//进入×××服务端操作如下命令
cd /etc/openvpn/easy-rsa/2.0/keys
tar zcvf liuzhuang.tar.gz ca.crt ca.key liuzhuang.* #liuzhuang为客户端证书
//证书拷贝
把liuzhuang.tar.gz证书解压到vpn客户端/etc/openvpn/keys/目录
//修改项
vi /etc/openvpn/client.conf
#服务端地址端口
remote 192.168.3.200 1194 #(此IP假设为公网IP)
#服务器端颁发证书秘钥
#SSL/TLS root crt
ca /etc/openvpn/keys/ca.crt
#客户端.crt
cert /etc/openvpn/keys/liuzhuang.crt
#服务端.key
key /etc/openvpn/keys/liuzhuang.key
连接
==============================================
openvpn --daemon --config /etc/openvpn/keys/client.conf