注意事项:
1、回环路由记得写--网关或核心交换到vpn网段路由( 或者使用iptables做一个nat,把所有虚拟ip转换为固定本地网卡ip:)
iptables -t nat -A POSTROUTING -s 10.250.250.0/24 -o eth0 -j MASQUERADE
2、配置文件中推送路由网段(推送的网段尽量不要和本地冲突,子网要注意别太大)3、默认证书两年过期,重复执行生成证书从---#创建server1证书和签名到cp pki/crl.pem /etc/openvpn/server/结束,重启下服务,就可以了----检测命令openssl x509 -in server.crt -noout -dates
1、服务端部署
1、安装epel库和openvpn软件
yum -y install epel-release && yum -y install openvpn easy-rsa
#开启路由转发
echo "net.ipv4.ip_forward = 1" >>/etc/sysctl.conf
sysctl -p
2、创建OpenVPN相关的密钥--创建CA密钥,server端、client端密钥,DH和CRL PEM, TLS认证钥匙ta.key
cp -rf /usr/share/easy-rsa/3.0/* /etc/openvpn/easy-rsa/
cp -p /usr/share/doc/easy-rsa-3.0.6/vars.example /etc/openvpn/easy-rsa/vars
cd /etc/openvpn/easy-rsa/
#创建ca证书
./easyrsa init-pki
./easyrsa build-ca nopass
#创建server1证书和签名
./easyrsa gen-req server1 nopass #Enter
./easyrsa sign-req server server1 #需要输入yes
#创建client证书和签名
./easyrsa gen-req client1 nopass #Enter
./easyrsa sign-req client client1 #需要输入yes
#创建dh、tls、crl(证书撤销秘钥)
./easyrsa gen-dh
openvpn --genkey --secret /etc/openvpn/easy-rsa/ta.key
./easyrsa gen-crl
#将秘钥拷贝至相应目录
cp -p pki/ca.crt /etc/openvpn/server/
cp -p pki/issued/server1.crt /etc/openvpn/server/
cp -p pki/private/server1.key /etc/openvpn/server/
cp -p ta.key /etc/openvpn/server/
cp -p pki/ca.crt /etc/openvpn/client/
cp -p pki/issued/client1.crt /etc/openvpn/client/
cp -p pki/private/client1.key /etc/openvpn/client/
cp -p ta.key /etc/openvpn/client/
cp pki/dh.pem /etc/openvpn/server/
cp pki/crl.pem /etc/openvpn/server/
cp /usr/share/doc/openvpn-2.4.11/sample/sample-config-files/server.conf /etc/openvpn/
3、编辑配置文件 /etc/openvpn/server.conf(注意在路由设备中加一条到虚拟网段的路由,下一跳地址为物理机ip)
cp /usr/share/doc/openvpn-2.4.11/sample/sample-config-files/server.conf /etc/openvpn/ #拷贝实例修改或直接拷贝以下内容
vim /etc/openvpn/server.conf
local 0.0.0.0 #监听地址
port 1194 #监听端口
proto tcp #监听协议
dev tun #采用路由隧道模式
ca /etc/openvpn/server/ca.crt #ca证书路径
cert /etc/openvpn/server/server1.crt #服务器证书
key /etc/openvpn/server/server1.key #服务器秘钥
dh /etc/openvpn/server/dh.pem #密钥交换协议文件
#####注意在设备中加一条到10.100.100.0虚拟网段的路由!!!!
server 10.100.100.0 255.255.255.0 ####给客户端分配地址池,注意:不能和VPN服务器内网网段有相同
#####注意在设备中加一条到10.100.100.0虚拟网段的路由!!!!
ifconfig-pool-persist ipp.txt
#push "redirect-gateway def1 bypass-dhcp" #推送默认路由(所有流量走vpn)
push "route 192.168.0.0 255.255.255.0" #推送客户端常用路由(根据自己内网网段来写或修改客户端配置添加路由)
push "dhcp-option DNS 192.168.5.251" #dhcp分配dns
client-to-client #客户端之间互相通信
keepalive 10 120 #存活时间,10秒ping一次,120 如未收到响应则视为断线
comp-lzo #传输数据压缩
max-clients 100 #最多允许 100 客户端连接
user openvpn #用户
group openvpn #用户组
cipher AES-256-CBC #加密方式--客户端必须一致
persist-key
persist-tun
status /var/log/openvpn-status.log
log /var/log/openvpn.log
verb 3
#以下参数为用户认证使用
script-security 3
auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env #指定用户认证脚本
username-as-common-name
verify-client-cert none
4、编写用户认证脚本文件 (脚本是由openvpn官网提供)
vim /etc/openvpn/checkpsw.sh
#!/bin/sh
###########################################################
# checkpsw.sh (C) 2004 Mathias Sundman
# This script will authenticate OpenVPN users against
# a plain text file. The passfile should simply contain
# one row per user with the username first followed by
# one or more space(s) or tab(s) and then the password.
PASSFILE="/etc/openvpn/psw-file"
LOG_FILE="/etc/openvpn/openvpn-password.log"
TIME_STAMP=`date "+%Y-%m-%d %T"`
###########################################################
if [ ! -r "${PASSFILE}" ]; then
echo "${TIME_STAMP}: Could not open password file \"${PASSFILE}\" for reading." >>${LOG_FILE}
exit 1
fi
CORRECT_PASSWORD=`awk '!/^;/&&!/^#/&&$1=="'${username}'"{print $2;exit}' ${PASSFILE}`
if [ "${CORRECT_PASSWORD}" = "" ]; then
echo "${TIME_STAMP}: User does not exist: username=\"${username}\", password=\"${password}\"." >> ${LOG_FILE}
exit 1
fi
if [ "${password}" = "${CORRECT_PASSWORD}" ]; then
echo "${TIME_STAMP}: Successful authentication: username=\"${username}\"." >> ${LOG_FILE}
exit 0
fi
echo "${TIME_STAMP}: Incorrect password: username=\"${username}\", password=\"${password}\"." >> ${LOG_FILE}
exit 1
#给脚本添加执行权限
chmod 755 /etc/openvpn/checkpsw.sh
#编辑密码文件
vim /etc/openvpn/psw-file
tom 123456
Jerry 654321
#前面为用户名,后面为密码。 中间使用空格分开
5、执行启动服务(端口使用1194 公网出口记得放行)
openvpn /etc/openvpn/server.conf
2、客户端登录
1、用户方式登录
下载安装openvpn gui
微云连接:https://share.weiyun.com/PD3PzL7h
配置文件在服务端/usr/share/doc/openvpn-2.4.11/sample/sample-config-files/下,名称为clent.conf
将服务端的ca.crt 一同下载,和配置文件一个目录(或配置client.conf中加入ca参数)
cd /usr/share/doc/openvpn-2.4.11/sample/sample-config-files/
sz client.conf
cd /etc/openvpn/client
sz ca.crt
修改配置文件client.conf(内容如下-公网ip记得修改);修改文件名为client.ovpn,然后将包含ca.crt和client.ovpn文件的目录拷贝至openVPN-gui的config目录中或者选导入配置文件;
client
dev tun
proto tcp
remote 公网ipxxx.xxx.xxx.xxx 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cipher AES-256-CBC
comp-lzo
verb 3
auth-user-pass #使用用户名密码登录openvpn服务器
auth-nocache
##---------以下为将ca.crt证书写入到客户端文件的方式(直接给一个client.ovpn文件即可)------------##
client
dev tun
proto tcp
remote 公网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g8NipnabU20aQvIIwfPFmzBoMw7FHnutEy/rxf3Vt3Moo60V+BNdQ/
kj/2Isi+hxa46jsJ0ogpQYttlMpR/nBlX8WYzkbVsXnBxt5oee7ZJHyB/C6JhE0Y
x+FzN62BgVW7
-----END CERTIFICATE-----
</ca>
cipher AES-256-CBC
comp-lzo
verb 3
auth-user-pass #使用用户名密码登录openvpn服务器
auth-nocache
然后连接2、客户端使用证书登录
用于证书认证的客户端
cd /usr/share/doc/openvpn-2.4.11/sample/sample-config-files/
sz client.conf
cd /etc/openvpn/client
sz ca.crt client1.crt client1.key
修改配置文件client.conf(内容如下-公网ip记得修改);修改文件名为client.ovpn,然后将包含ca.crt client1.crt client1.key和client.ovpn文件的目录拷贝至openVPN-gui的config目录中或者选导入配置文件;
client
dev tun
proto tcp
remote 公网ipxxx.xxx.xxx.xxx 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cert client1.crt
key client1.key
remote-cert-tls server
cipher AES-256-CBC
comp-lzo
verb 3
直接不需要输入账号密码就可以登录了
参考文章:
open-vpn部署
https://www.jianshu.com/p/0ce6560b2279
常见错误
https://me.jinchuang.org/archives/573.html
https://blog.csdn.net/weixin_34256074/article/details/92584557
https://me.jinchuang.org/archives/573.html
——————
pptp-vpn
https://me.jinchuang.org/archives/517.html
1、安装epel源和pptpd服务
[root@pptp-vpn ~] yum install epel-release -y
[root@pptp-vpn ~] yum install ppp ppp-devel pptpd -y
2、修改相关配置