openvpn搭建
技术博客 http://idea.coderyj.com/
1.环境
华为云服务器 操作系统 centos7
2.安装部署
- 1.安装 openvpn 和 easy-rsa(该包用来制作 ca 证书)
# 安装 epel 源
yum install epel-release -y
# 安装
yum install openvpn easy-rsa
- 2、配置 /etc/openvpn/easy-rsa 目录
mkdir -p /etc/openvpn/easy-rsa
cp -r /usr/share/easy-rsa/3.0.7/* /etc/openvpn/easy-rsa/
# 拷贝 证书配置文件
cp /usr/share/doc/easy-rsa-3.0.7/vars.example /etc/openvpn/easy-rsa/vars
# 拷贝 openvpn 模版配置文件
cp /usr/share/doc/openvpn-2.4.8/sample/sample-config-files/server.conf /etc/openvpn/
- 3、编辑 vars
vim /etc/openvpn/easy-rsa/vars
# 修改内容
set_var EASYRSA_REQ_COUNTRY "CN"
set_var EASYRSA_REQ_PROVINCE "GuangDong"
set_var EASYRSA_REQ_CITY "Guangzhou"
set_var EASYRSA_REQ_ORG "coderyj"
set_var EASYRSA_REQ_EMAIL "997745354@qq.com"
set_var EASYRSA_REQ_OU "IT"
三、创建 根、服务器端 证书 和 key
- 1、初始化
cd /etc/openvpn/easy-rsa/
# 初始化,成功后出现 pki 目录
./easyrsa init-pki
- 2、创建 根 证书
./easyrsa build-ca
# 输入密码,这里输入 123456
Enter New CA Key Passphrase:
………
# 输入名称,这里输入 vpn-ca
Common Name (eg: your user, host, or server name) [Easy-RSA CA]:vpn-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
# ca.crt 证书文件
# private 目录里面放的是所有密钥文件,ca.key 后面的 server.key 也会在这里
- 3、创建 服务器端 证书
./easyrsa build-server-full server nopass
# 输入前面定义的 ca 密码 123456
Enter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key:123456
....
Signature ok
The Subject's Distinguished Name is as follows
commonName :ASN.1 12:'server'
Certificate is to be certified until Jan 15 03:05:13 2025 GMT (825 days)
Write out database with 1 new entries
Data Base Updated
- 4、创建Diffie-Hellman,确保key穿越不安全网络的命令
./easyrsa gen-dh
- 5、生成 ta 密钥文件
openvpn --genkey --secret ta.key
四、创建 客户端 证书 和 key
- 在 ccd下面创建客户端证书和路由器证书
/etc/openvpn/ccd
cd ccd
touch client01
- 客户端证书配置
# 客户端的路由表 代表可以访问哪个网段开头的地址
push "route 172.0.0.0 255.0.0.0"
- 路由器证书配置
10.8.0.101
指定了ip地址
ifconfig-push 10.8.0.101 255.255.0.0
#iroute 172.16.100.0 255.255.255.0
iroute 172.0.0.0 255.0.0.0
每个客户端要创建一个证书 证书名字要不能重复
client01
证书的名称
client01 client02 client03 client04 是客户端证书
client100 client101 client102 是路由器证书
创建客户端证书命令
cd easy-rsa/
./easyrsa build-client-full client01 nopass
输入服务端的证书密码 123456 即可生成
生成好的证书在/etc/openvpn/easy-rsa/pki/issued
目录下
密钥在/etc/openvpn/easy-rsa/pki/private
目录下
ca在/etc/openvpn/easy-rsa/pki/
目录下
五 配置服务器
- 1、拷贝文件
# 拷贝根与服务器端的相关证书
cp pki/ca.crt /etc/openvpn/server
cp pki/private/server.key /etc/openvpn/server
cp pki/issued/server.crt /etc/openvpn/server
cp pki/dh.pem /etc/openvpn/server
cp /etc/openvpn/easy-rsa/ta.key /etc/openvpn/server
- 2 修改 server.conf 配置文件
local 0.0.0.0
port 1194
# TCP or UDP server?
;proto tcp
proto udp
dev tun
ca ca.crt
cert server.crt
key server.key # This file should be kept secret
dh dh.pem
topology subnet
server 10.8.0.0 255.255.0.0
ifconfig-pool-persist ipp.txt
client-config-dir ccd
route 172.0.0.0 255.0.0.0
client-to-client
keepalive 10 120
cipher AES-256-CBC
;comp-lzo
max-clients 100
persist-key
persist-tun
status openvpn-status.log
log /var/log/openvpn.log
verb 3
explicit-exit-notify 1
- 3、说明
# 监听地址
local 0.0.0.0
# 监听端口
port 1194
# 监听协议
proto tcp
# 采用路由隧道模式
dev tun
# ca证书路径
ca /etc/openvpn/ca.crt
# 服务器证书
cert /etc/openvpn/server.crt
# This file should be kept secret 服务器秘钥
key /etc/openvpn/server.key
# 密钥交换协议文件
dh /etc/openvpn/dh.pem
# VPN服务端为自己和客户端分配IP的地址池,服务端自己获取网段的第一个地址(这里为10.8.0.1),后为客户端分配其他的可用地址。以后客户端就可以和10.8.0.1进行通信。注意:该网段地址池不要和已有网段冲突或重复
server 10.8.0.0 255.255.255.0
#使用一个文件记录已分配虚拟IP的客户端和虚拟IP的对应关系,以后openvpn重启时,将可以按照此文件继续为对应的客户端分配此前相同的IP。也就是自动续借IP的意思
ifconfig-pool-persist ipp.txt
# 客户端所有流量都通过 openvpn 转发,类似于代理
push "redirect-gateway def1 bypass-dhcp"
# 注意:客户端配置,当服务端开启上面一条流量转发后,客户端需要配置如下2条,客户端只有访问192.168.0.0/24的地址才会走vpn隧道,也就是会使用vpn服务端的IP地址,访问其他地址还是会走本地网络
route-nopull
route 172.16.0.0 255.255.0.0 vpn_gateway
# 将服务器端的内网地址推给客户端,添加到客户端的路由表中,可以添加多条
push "route 172.16.0.0 255.255.0.0"
# 服务端推送客户端dhcp分配dns
push "dhcp-option DNS 8.8.8.8"
# 客户端之间互相通信
client-to-client
# 存活时间,10秒ping一次,120 如未收到响应则视为断线
keepalive 10 120
# openvpn 2.4版本的vpn才能设置此选项。表示服务端启用lz4的压缩功能,传输数据给客户端时会压缩数据包。Push后在客户端也配置启用lz4的压缩功能,向服务端发数据时也会压缩。如果是2.4版本以下的老版本,则使用用comp-lzo指令
compress lz4-v2
push "compress lz4-v2"
# 启用lzo数据压缩格式。此指令用于低于2.4版本的老版本。且如果服务端配置了该指令,客户端也必须要配置
comp-lzo
# 最多允许 100 客户端连接
max-clients 100
# 用户
user openvpn
# 用户组
group openvpn
# 参数 0 可以省略,如果不省略,那么客户端配置相应的参数该配成 1;如果省略,那么客户端不需要 tls-auth 配置
tls-auth /etc/openvpn/server/ta.key 0
- 4.最终我的服务器配置文件
#################################################
# Sample OpenVPN 2.0 config file for #
# multi-client server. #
# #
# This file is for the server side #
# of a many-clients <-> one-server #
# OpenVPN configuration. #
# #
# OpenVPN also supports #
# single-machine <-> single-machine #
# configurations (See the Examples page #
# on the web site for more info). #
# #
# This config should work on Windows #
# or Linux/BSD systems. Remember on #
# Windows to quote pathnames and use #
# double backslashes, e.g.: #
# "C:\\Program Files\\OpenVPN\\config\\foo.key" #
# #
# Comments are preceded with '#' or ';' #
#################################################
# Which local IP address should OpenVPN
# listen on? (optional)
local 0.0.0.0
# Which TCP/UDP port should OpenVPN listen on?
# If you want to run multiple OpenVPN instances
# on the same machine, use a different port
# number for each one. You will need to
# open up this port on your firewall.
port 1194
# TCP or UDP server?
;proto tcp
proto udp
# "dev tun" will create a routed IP tunnel,
# "dev tap" will create an ethernet tunnel.
# Use "dev tap0" if you are ethernet bridging
# and have precreated a tap0 virtual interface
# and bridged it with your ethernet interface.
# If you want to control access policies
# over the VPN, you must create firewall
# rules for the the TUN/TAP interface.
# On non-Windows systems, you can give
# an explicit unit number, such as tun0.
# On Windows, use "dev-node" for this.
# 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 or higher,
# you may need to selectively disable the
# Windows firewall for the TAP adapter.
# Non-Windows systems usually don't need this.
;dev-node MyTap
# SSL/TLS root certificate (ca), certificate
# (cert), and private key (key). Each client
# and the server must have their own cert and
# key file. The server and all clients will
# use the same ca file.
#
# See the "easy-rsa" directory for a series
# of scripts for generating RSA certificates
# and private keys. Remember to use
# a unique Common Name for the server
# and each of the client certificates.
#
# Any X509 key management system can be used.
# OpenVPN can also use a PKCS #12 formatted key file
# (see "pkcs12" directive in man page).
ca ca.crt
cert server.crt
key server.key # This file should be kept secret
# Diffie hellman parameters.
# Generate your own with:
# openssl dhparam -out dh2048.pem 2048
dh dh.pem
# Network topology
# Should be subnet (addressing via IP)
# unless Windows clients v2.0.9 and lower have to
# be supported (then net30, i.e. a /30 per client)
# Defaults to net30 (not recommended)
topology subnet
# Configure server mode and supply a VPN subnet
# for OpenVPN to draw client addresses from.
# The server will take 10.8.0.1 for itself,
# the rest will be made available to clients.
# Each client will be able to reach the server
# on 10.8.0.1. Comment this line out if you are
# ethernet bridging. See the man page for more info.
server 10.8.0.0 255.255.0.0
# Maintain a record of client <-> virtual IP address
# associations in this file. If OpenVPN goes down or
# is restarted, reconnecting clients can be assigned
# the same virtual IP address from the pool that was
# previously assigned.
ifconfig-pool-persist ipp.txt
# Configure server mode for ethernet bridging.
# You must first use your OS's bridging capability
# to bridge the TAP interface with the ethernet
# NIC interface. Then you must manually set the
# IP/netmask on the bridge interface, here we
# assume 10.8.0.4/255.255.255.0. Finally we
# must set aside an IP range in this subnet
# (start=10.8.0.50 end=10.8.0.100) to allocate
# to connecting clients. Leave this line commented
# out unless you are ethernet bridging.
;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100
# Configure server mode for ethernet bridging
# using a DHCP-proxy, where clients talk
# to the OpenVPN server-side DHCP server
# to receive their IP address allocation
# and DNS server addresses. You must first use
# your OS's bridging capability to bridge the TAP
# interface with the ethernet NIC interface.
# Note: this mode only works on clients (such as
# Windows), where the client-side TAP adapter is
# bound to a DHCP client.
;server-bridge
# Push routes to the client to allow it
# to reach other private subnets behind
# the server. Remember that these
# private subnets will also need
# to know to route the OpenVPN client
# address pool (10.8.0.0/255.255.255.0)
# back to the OpenVPN server.
;push "route 192.168.10.0 255.255.255.0"
;push "route 192.168.20.0 255.255.255.0"
# To assign specific IP addresses to specific
# clients or if a connecting client has a private
# subnet behind it that should also have VPN access,
# use the subdirectory "ccd" for client-specific
# configuration files (see man page for more info).
# EXAMPLE: Suppose the client
# having the certificate common name "Thelonious"
# also has a small subnet behind his connecting
# machine, such as 192.168.40.128/255.255.255.248.
# First, uncomment out these lines:
client-config-dir ccd
route 172.0.0.0 255.0.0.0
;push "route 172.16.100.0 255.255.255.0"
# Then create a file ccd/Thelonious with this line:
# iroute 192.168.40.128 255.255.255.248
# This will allow Thelonious' private subnet to
# access the VPN. This example will only work
# if you are routing, not bridging, i.e. you are
# using "dev tun" and "server" directives.
# EXAMPLE: Suppose you want to give
# Thelonious a fixed VPN IP address of 10.9.0.1.
# First uncomment out these lines:
;client-config-dir ccd
;route 10.9.0.0 255.255.255.252
# Then add this line to ccd/Thelonious:
# ifconfig-push 10.9.0.1 10.9.0.2
# Suppose that you want to enable different
# firewall access policies for different groups
# of clients. There are two methods:
# (1) Run multiple OpenVPN daemons, one for each
# group, and firewall the TUN/TAP interface
# for each group/daemon appropriately.
# (2) (Advanced) Create a script to dynamically
# modify the firewall in response to access
# from different clients. See man
# page for more info on learn-address script.
;learn-address ./script
# If enabled, this directive will configure
# all clients to redirect their default
# network gateway through the VPN, causing
# all IP traffic such as web browsing and
# and DNS lookups to go through the VPN
# (The OpenVPN server machine may need to NAT
# or bridge the TUN/TAP interface to the internet
# in order for this to work properly).
;push "redirect-gateway def1 bypass-dhcp"
# Certain Windows-specific network settings
# can be pushed to clients, such as DNS
# or WINS server addresses. CAVEAT:
# http://openvpn.net/faq.html#dhcpcaveats
# The addresses below refer to the public
# DNS servers provided by opendns.com.
;push "dhcp-option DNS 208.67.222.222"
;push "dhcp-option DNS 8.8.8.8"
# Uncomment this directive to allow different
# clients to be able to "see" each other.
# By default, clients will only see the server.
# To force clients to only see the server, you
# will also need to appropriately firewall the
# server's TUN/TAP interface.
client-to-client
# Uncomment this directive if multiple clients
# might connect with the same certificate/key
# files or common names. This is recommended
# only for testing purposes. For production use,
# each client should have its own certificate/key
# pair.
#
# IF YOU HAVE NOT GENERATED INDIVIDUAL
# CERTIFICATE/KEY PAIRS FOR EACH CLIENT,
# EACH HAVING ITS OWN UNIQUE "COMMON NAME",
# UNCOMMENT THIS LINE OUT.
;duplicate-cn
# The keepalive directive causes ping-like
# messages to be sent back and forth over
# the link so that each side knows when
# the other side has gone down.
# Ping every 10 seconds, assume that remote
# peer is down if no ping received during
# a 120 second time period.
keepalive 10 120
# For extra security beyond that provided
# by SSL/TLS, create an "HMAC firewall"
# to help block DoS attacks and UDP port flooding.
#
# Generate with:
# openvpn --genkey --secret ta.key
#
# The server and each client must have
# a copy of this key.
# The second parameter should be '0'
# on the server and '1' on the clients.
;tls-auth ta.key 0 # This file is secret
# Select a cryptographic cipher.
# This config item must be copied to
# the client config file as well.
# 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
# Enable compression on the VPN link and push the
# option to the client (v2.4+ only, for earlier
# versions see below)
;compress lz4-v2
;push "compress lz4-v2"
# For compression compatible with older clients use comp-lzo
# If you enable it here, you must also
# enable it in the client config file.
;comp-lzo
# The maximum number of concurrently connected
# clients we want to allow.
max-clients 100
# It's a good idea to reduce the OpenVPN
# daemon's privileges after initialization.
#
# You can uncomment this out on
# non-Windows systems.
;user openvpn
;group openvpn
# The persist options will try to avoid
# accessing certain resources on restart
# that may no longer be accessible because
# of the privilege downgrade.
persist-key
persist-tun
# Output a short status file showing
# current connections, truncated
# and rewritten every minute.
status openvpn-status.log
# By default, log messages will go to the syslog (or
# on Windows, if running as a service, they will go to
# the "\Program Files\OpenVPN\log" directory).
# Use log or log-append to override this default.
# "log" will truncate the log file on OpenVPN startup,
# while "log-append" will append to it. Use one
# or the other (but not both).
log /var/log/openvpn.log
;log-append openvpn.log
# Set the appropriate level of log
# file verbosity.
#
# 0 is silent, except for fatal errors
# 4 is reasonable for general usage
# 5 and 6 can help to debug connection problems
# 9 is extremely verbose
verb 3
# Silence repeating messages. At most 20
# sequential messages of the same message
# category will be output to the log.
;mute 20
# Notify the client that when the server restarts so it
# can automatically reconnect.
explicit-exit-notify 1
- 5、创建日志目录
mkdir -p /var/log/openvpn/
六、设置 路由
# 如果不设置,连上 VPN 后将无法正常上网
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j MASQUERADE
service iptables save
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
# 生效
sysctl -p
七、启动
systemctl start openvpn@server.service
或 /usr/sbin/openvpn --cd /etc/openvpn/ --config server.conf&
停止
killall openvpn
# 查看
netstat -lntp | grep 1194
tcp 0 0 0.0.0.0:1194 0.0.0.0:* LISTEN 1470/openvpn
八、客户端连接
- 下载地址
2.5.8
版本- 官方地址 https://openvpn.net/community-downloads/
- github地址 https://github.com/OpenVPN/openvpn
- 2、在安装目录 config下 创建 client.ovpn 文件
client
dev tun
proto tcp
remote 192.168.1.71 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cert tomma.crt
key tomma.key
remote-cert-tls server
tls-auth ta.key 1
cipher AES-256-GCM
compress lz4-v2
verb 3
auth-nocache
- 3、拷贝服务器客户端上的相关文件
生成好的证书在
/etc/openvpn/easy-rsa/pki/issued
目录下
密钥在/etc/openvpn/easy-rsa/pki/private
目录下
ca在/etc/openvpn/easy-rsa/pki/
目录下
- 4.安装openvpn
自行安装
- 5.打开文件目录退出程序找到config目录
把刚刚拷贝的证书文件可以client.ovpn放进来 证书名字更改一下
- 6.打开openvpn进行连接
有ip地址证明成功
九 配置路由器 以四信路由器为例
- 配置证书
- 设置路由器ip 设置 自动对时
- 查看到路由器 openvpn有地址就代表成功了
- 访问路由器 在浏览器输入
10.8.0.100
能出来页面代表成功
十配置摄像头 将摄像头与路由器链接 配置摄像头ip 网关等
- 在浏览器访问 输入地址
172.16.100.115
出现摄像机管理页面代表成功
- 至此openvpn搭建完成 可以访问路由器和摄像头了