本配置主要用于打洞,进行SSH或其它低数据量传输,如果数据量延迟要求较高请个别配置
首先生成所需密钥文件
准备easy-rsa环境
sudo apt install easy-rsa
mkdir openvpn_config
cd openvpn_config
cp -r /usr/share/easy-rsa/
cd easy-rsa
修改配置文件
cp vars.example vars
vim vars
set_var EASYRSA_REQ_COUNTRY "CN"
set_var EASYRSA_REQ_PROVINCE "Guang Dong"
set_var EASYRSA_REQ_CITY "Shen Zhen"
set_var EASYRSA_REQ_ORG "xxxx"
set_var EASYRSA_REQ_EMAIL "xxxxx.com"
set_var EASYRSA_REQ_OU "OpenSVN"
#提高加密等级
set_var EASYRSA_KEY_SIZE 4096
#修改时间这里改大,如果需要高等级改小
set_var EASYRSA_CA_EXPIRE 365000
#生效时间这里改大,如果需要高等级改小
set_var EASYRSA_CERT_EXPIRE 3650
生成所需密钥
./easyrsa init-pki #1、初始化,在当前目录创建PKI目录,用于存储整数
./easyrsa build-ca #2、创建根证书,会提示设置密码,用于ca对之后生成的server和client证书签名时使用,其他提示内容直接回车即可
Enter New CA Key Passphrase: #注意密码不能太短,我这边设置的是123456
Re-Enter New CA Key Passphrase:
./easyrsa gen-req server nopass #3、创建server端证书和私钥文件,nopass表示不加密私钥文件,提示内容直接回车即可
./easyrsa sign server server #4、给server端证书签名,提示内容需要输入yes和创建ca根证书时候的密码
./easyrsa gen-dh #5、创建Diffie-Hellman文件,密钥交换时的Diffie-Hellman算法
./easyrsa gen-req client nopass #6、创建client端的证书和私钥文件,nopass表示不加密私钥文件,提示内容直接回车即可
./easyrsa sign client client #7、给client端证书前面,提示内容输入yes和创建ca根证书时候的密码
tree #检查是否有ca根证书、客户端服务端证书、客户端服务端私钥
.
├── easyrsa #管理命令
├── openssl-easyrsa.cnf
├── pki
│ ├── ca.crt #ca根证书,服务端与客户端都需要用
│ ├── certs_by_serial
│ │ ├── 633C217979C7B5F1D0A9ECA971006F96.pem
│ │ └── 857F9B2E3F6C3D35934672212343B42D.pem
│ ├── dh.pem #认证算法 服务端
│ ├── index.txt
│ ├── index.txt.attr
│ ├── index.txt.attr.old
│ ├── index.txt.old
│ ├── issued
│ │ ├── client.crt #客户端证书
│ │ └── server.crt #服务端证书
│ ├── openssl-easyrsa.cnf
│ ├── private
│ │ ├── ca.key
│ │ ├── client.key #客户端私钥
│ │ └── server.key #服务端私钥
......
服务器端配置
sudo apt install openvpn
j将ca.crt,server.crt,server.key,dh.pem传输至服务器/etc/openvpn/server
生成ta.key
cd /etc/openvpn/server
OpenVPN --genkey --secret ./ta.key
ta.key用于增强连接安全性,需要复制到从机
cd /etc/openvpn
vim server.conf
port xxxx
proto tcp
dev tun
ca /etc/openvpn/server/ca.crt
cert /etc/openvpn/server/server.crt
key /etc/openvpn/server/server.key
dh /etc/openvpn/server/dh.pem
server 10.110.0.0 255.255.255.0 #根据需求调整
ifconfig-pool-persist ./server/ipp.txt 记录调整从机ip文件
push "redirect-gateway def1 bypass-dhcp"
push "redirect-gateway ipv6"
client-to-client #允许从机互相访问
keepalive 10 120
tls-crypt ./server/ta.key
cipher AES-256-GCM
auth SHA256
user nobody #这里可能会引发错误注意看日志
group nogroup #这里可能会引发错误注意看日志
persist-key
persist-tun
status openvpn-status.log
log-append openvpn.log #调试时打开,否则日志会增大的很厉害
verb 3
explicit-exit-notify 0
启动服务器测试
systemctl start openvpn@server
easy-rsa]# systemctl enable openvpn@server
ip a
如果有问题查看openvpn.log查看具体问题
配置从机
采用脚本化+模板方式配置,先编辑模板文件
mkdir make_client_file
cd make_client_file
vim base.conf
##############################################
# Sample client-side OpenVPN 2.0 config file #
# for connecting to multi-client server. #
# #
# This configuration can be used by multiple #
# clients, however each client should have #
# its own cert and key files. #
# #
# On Windows, you might want to rename this #
# file so it has a .ovpn extension #
##############################################
# Specify that we are a client and that we
# will be pulling certain config file directives
# from the server.
client
# Use the same setting as you are using on
# the server.
# On most systems, the VPN will not function
# unless you partially or fully disable
# the firewall for the TUN/TAP interface.
;dev tap
dev tun
# Windows needs the TAP-Win32 adapter name
# from the Network Connections panel
# if you have more than one. On XP SP2,
# you may need to disable the firewall
# for the TAP adapter.
;dev-node MyTap
# Are we connecting to a TCP or
# UDP server? Use the same setting as
# on the server.
proto tcp
;proto udp
# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
remote xx.xx.xx.xx xxxxx #公网ip及端口
;remote my-server-2 1194
# Choose a random host from the remote
# list for load-balancing. Otherwise
# try hosts in the order specified.
;remote-random
# Keep trying indefinitely to resolve the
# host name of the OpenVPN server. Very useful
# on machines which are not permanently connected
# to the internet such as laptops.
resolv-retry infinite
# Most clients don't need to bind to
# a specific local port number.
nobind
# Downgrade privileges after initialization (non-Windows only)
user nobody
group nogroup
# Try to preserve some state across restarts.
persist-key
persist-tun
# If you are connecting through an
# HTTP proxy to reach the actual OpenVPN
# server, put the proxy server/IP and
# port number here. See the man page
# if your proxy server requires
# authentication.
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]
# Wireless networks often produce a lot
# of duplicate packets. Set this flag
# to silence duplicate packet warnings.
;mute-replay-warnings
# SSL/TLS parms.
# See the server config file for more
# description. It's best to use
# a separate .crt/.key file pair
# for each client. A single ca
# file can be used for all clients.
;ca ca.crt
;cert client.crt
;key client.key
# Verify server certificate by checking that the
# certicate has the correct key usage set.
# This is an important precaution to protect against
# a potential attack discussed here:
# http://openvpn.net/howto.html#mitm
#
# To use this feature, you will need to generate
# your server certificates with the keyUsage set to
# digitalSignature, keyEncipherment
# and the extendedKeyUsage to
# serverAuth
# EasyRSA can do this for you.
remote-cert-tls server
# If a tls-auth key is used on the server
# then every client must also have the key.
;tls-auth ta.key 1
# Select a cryptographic cipher.
# If the cipher option is used on the server
# then you must also specify it here.
# Note that v2.4 client/server will automatically
# negotiate AES-256-GCM in TLS mode.
# See also the ncp-cipher option in the manpage
;cipher AES-256-CBC
cipher AES-256-GCM
auth SHA256
key-direction 1
;redirect-gateway def1
route-nopull
route 10.110.0.0 255.255.0.0 vpn_gateway #和服务器端一样
# Enable compression on the VPN link.
# Don't enable this unless it is also
# enabled in the server config file.
#comp-lzo
47.115.60.163
# Set log file verbosity.
verb 3
# Silence repeating messages
;mute 20
构建所需文件
mkdir key #存储相关key文件
mkdir files #生成的配置文件
vim make_config.sh
#!/bin/bash
# First argument: Client identifier
KEY_DIR=./keys
OUTPUT_DIR=./files
BASE_CONFIG=./base.conf
cat ${BASE_CONFIG} \
<(echo -e '<ca>') \
${KEY_DIR}/ca.crt \
<(echo -e '</ca>\n<cert>') \
${KEY_DIR}/$1.crt \
<(echo -e '</cert>\n<key>') \
${KEY_DIR}/$1.key \
<(echo -e '</key>\n<tls-crypt>') \
${KEY_DIR}/ta.key \
<(echo -e '</tls-crypt>') \
> ${OUTPUT_DIR}/$1.ovpn\
chmod +x make_config.sh
j将ca.crt,client.crt,client.key,dh.pem,ta.key放置到key文件夹下
./make_config.sh client
在/file下生成client.ovpn文件
注意此文件中包含所有的key文件,不要泄露
然后启动从机
sudo openvpn --config client.ovpn
ip a #查看ip是否联通tunx