searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

vpn 部署

2023-10-19 10:27:04
40
0

环境示意图

安装openvpn

1、环境
系统版本:CentOS 7.9
防火墙: 关闭状态
2、安装EPEL套件库,更新
yum -y install epel-release && yum -y update
3、安装 OpenVPN和证书工具
yum install -y openvpn easy-rsa
4、准备相关配置文件
cp /usr/share/doc/openvpn-2.4.12/sample/sample-config-files/server.conf /etc/openvpn/
cp -r /usr/share/easy-rsa/ /etc/openvpn/easy-rsa-server
cp /usr/share/doc/easy-rsa-3.0.8/vars.example /etc/openvpn/easy-rsa-server/3/vars
vim /etc/openvpn/easy-rsa-server/3/vars
#CA的证书有效期默为为10年,可以适当延长,比如:36500天
#set_var EASYRSA_CA_EXPIRE  3650
set_var EASYRSA_CA_EXPIRE   36500
#服务器证书默为为825天,可适当加长,比如:3650天
#set_var EASYRSA_CERT_EXPIRE    825
set_var EASYRSA_CERT_EXPIRE 3650

准备证书相关文件

初始化 PKI 和 CA 签发机构环境
初始化 PKI 生成 PKI 相关目录和文件
cd /etc/openvpn/easy-rsa-server/3
./easyrsa init-pki
创建 CA 机构
./easyrsa build-ca nopass
回车即可
创建服务端证书申请
./easyrsa gen-req server nopass
接受Common Name默认
签发服务端证书
 ./easyrsa sign server server
 #输入yes回车
创建 Diifie-Hellman 密钥
./easyrsa gen-dh
准备客户端证书环境
cp -r /usr/share/easy-rsa/ /etc/openvpn/easy-rsa-client
cp /usr/share/doc/easy-rsa-3.0.8/vars.example /etc/openvpn/easy-rsa-client/3/vars
cd /etc/openvpn/easy-rsa-client/3
./easyrsa init-pki
创建客户端证书申请
./easyrsa gen-req myclient nopass
# 创建client密码
接受默认回车
签发客户端证书
 cd /etc/openvpn/easy-rsa-server/3
 ./easyrsa import-req /etc/openvpn/easy-rsa-client/3/pki/reqs/myclient.req myclient
 ./easyrsa sign client myclient
 # 输入yes后回车
将 CA 和服务器证书相关文件复制到服务器相应的目录
mkdir /etc/openvpn/certs
cp /etc/openvpn/easy-rsa-server/3/pki/ca.crt /etc/openvpn/certs/
cp /etc/openvpn/easy-rsa-server/3/pki/issued/server.crt /etc/openvpn/certs/
cp /etc/openvpn/easy-rsa-server/3/pki/private/server.key /etc/openvpn/certs/
cp /etc/openvpn/easy-rsa-server/3/pki/dh.pem /etc/openvpn/certs/
将客户端私钥与证书相关文件复制到服务器相关的目录
mkdir /etc/openvpn/client/myclient
find /etc/openvpn/ \( -name "myclient.key" -o -name "myclient.crt" -o -name "ca.crt" \) -exec cp {} /etc/openvpn/client/myclient \;
准备用户认证的脚本
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 
abcdocker 123456
abc 123456
test test
#前面为用户名,后面为密码。 中间使用空格分开
准备 OpenVPN 服务器配置文件
vim /etc/openvpn/server.conf
port 1194    #端口
proto tcp     #协议,生产推荐使用TCP
dev tun
ca /etc/openvpn/certs/ca.crt    #ca证书文件
cert /etc/openvpn/certs/server.crt   #服务器证书文件
key /etc/openvpn/certs/server.key  #服务器私钥文件
dh /etc/openvpn/certs/dh.pem       #dh参数文件
server 192.168.0.0 255.255.255.0     #客户端连接后分配IP的地址池,服务器默认会占用第一个IP192.168.0.1将做为客户端的网关,注意:不能和VPN服务器内网网段有相同
#ifconfig-pool-persist ipp.txt      #为客户端分配固定IP,不需要配置,建议注释
push "route 172.16.0.0 255.255.255.0"  #推送路由信息到客户端,以允许客户端能够连接到服务器背后的其它私有子网
keepalive 10 120   #设置服务端检测的间隔和超时时间,默认为每10秒ping一次,如果 120秒没有回应则认为对方已经down
cipher AES-256-CBC   #加密算法
compress lz4-v2   #启用Openvpn2.4.X新版压缩算法
push "compress lz4-v2"  #推送客户端使用新版压缩算法
max-clients 100         #最大客户端数
#user openvpn            #运行openvpn服务的用户和组
#group openvpn
status /var/log/openvpn-status.log    #openVPN状态记录文件,每分钟会记录一次
log-append  /var/log/openvpn.log      #指定日志路径,重启openvpn后在之前的日志后面追加新的日志,生产环境建议使用
verb 3         #设置日志级别,0-9,级别越高记录的内容越详细,0 表示静默运行,只记录致命错误,4 表示合理的常规用法,5 和 6 可以帮助调试连接错误。9 表示极度冗余,输出非常详细的日志信息
mute 20         #相同类别的信息只有前20条会输出到日志文件中
script-security 3
auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env    #指定用户认证脚本
username-as-common-name
verify-client-cert none
准备 iptables 规则和内核参数
echo net.ipv4.ip_forward = 1 >>/etc/sysctl.conf
sysctl -p
echo "iptables -t nat -APOSTROUTING -s 192.168.0.0/24 -j MASQUERADE" >>/etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
/etc/rc.d/rc.local
启动 OpenVPN 服务
systemctl enable openvpn@server
systemctl start openvpn@server
准备 OpenVPN 客户端配置文件
生成客户端文件,文件后缀必须为.ovpn
vim /etc/openvpn/client/myclient/client.ovpn
client   #声明自己是个客户端
dev tun   #接口类型,必须和服务端保持一致
proto tcp    #协议类型,必须和服务端保持一致
remote 10.190.171.41 1194     #server端的ip和端口,可以写域名但是需要可以解析成IP,这里是虚拟机的eip
resolv-retry infinite      #如果是写的server端的域名,那么就始终解析,如果域名发生变化,会重新连接到新的域名对应的IP
nobind           #本机不绑定监听端口,客户端是随机打开端口连接到服务端的1194
ca ca.crt
cert myclient.crt
key myclient.key
remote-cert-tls server       #指定采用服务器证书校验方式
cipher AES-256-CBC
verb 3
compress lz4-v2
auth-user-pass 
将/etc/openvpn/client/myclient/ 下文件拷贝出来

OpenVPN 客户端使用

安装openvpn 客户端并导入client.ovpn 配置文件,输入client密码。
0条评论
0 / 1000
timeout
1文章数
0粉丝数
timeout
1 文章 | 0 粉丝
Ta的热门文章查看更多