文章介绍:VyOS 配置 OpenVPN 服务端模式,iKuai 软路由 OpenVPN 客户端拨号连接,实现简单组网,采用账号密码加证书认证方式实现。

一、VyOS

1.1、VyOS镜像

官方下载 博主共享盘

1.2、VyOS安装

【Vyos-开源篇-1】- VMware安装VyOS虚拟机

1.3、VyOS基础

【VyOS-开源篇-32】- 家庭软路由 VyOS 完整配置

二、VyOS配置OpenVPN

2.1、生成ca证书

run generate pki ca install ca

yydy_2024-11-25_14-34-20

2.2、生成服务端证书和密钥

run generate pki certificate sign ca install server-cert-key

yydy_2024-11-25_14-43-02

yydy_2024-11-25_14-43-53

2.3、生成客户端证书和密钥

run generate pki certificate sign ca install client-cert-key

yydy_2024-11-25_14-47-18

2.4、创建checkpsw.sh

sudo nano /config/auth/checkpsw.sh
#!/bin/sh
###########################################################
# checkpsw.sh (C) 2004 Mathias Sundman <[email protected]>
#
# 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="/config/auth/openvpn_user_passwd.txt"
LOG_FILE="/config/auth/openvpn.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

2.5、赋权checkpsw.sh

sudo chmod +x /config/auth/checkpsw.sh

2.6、创建密码本

密码本格式是,一行一个用户,中间用空格隔开密码

sudo nano /config/auth/openvpn_user_passwd.txt

yydy_2024-11-25_18-41-30

2.7、赋权密码本

sudo chmod 664 /config/auth/openvpn_user_passwd.txt

2.8、创建拨号日志文件

sudo touch /config/auth/openvpn.log

2.9、OpenVPN接口

set interfaces openvpn vtun11996 description 'OpenVPN-Server'
set interfaces openvpn vtun11996 local-host '123.123.123.123'
set interfaces openvpn vtun11996 local-port '11996'
set interfaces openvpn vtun11996 mode 'server'
set interfaces openvpn vtun11996 openvpn-option '--script-security 3'
set interfaces openvpn vtun11996 openvpn-option '--persist-key'
set interfaces openvpn vtun11996 openvpn-option '--duplicate-cn'
set interfaces openvpn vtun11996 openvpn-option '--auth-user-pass-verify /config/auth/checkpsw.sh via-env'
set interfaces openvpn vtun11996 openvpn-option '--username-as-common-name'
set interfaces openvpn vtun11996 openvpn-option '--cipher AES-256-GCM'
set interfaces openvpn vtun11996 openvpn-option '--tun-mtu 1500'
set interfaces openvpn vtun11996 openvpn-option '--mssfix 1300'
set interfaces openvpn vtun11996 persistent-tunnel
set interfaces openvpn vtun11996 protocol 'udp'
set interfaces openvpn vtun11996 server client ikuai ip '100.64.168.2'
set interfaces openvpn vtun11996 server client ikuai push-route '192.168.65.0/24'
set interfaces openvpn vtun11996 server client-ip-pool start '100.64.168.2'
set interfaces openvpn vtun11996 server client-ip-pool stop '100.64.168.254'
set interfaces openvpn vtun11996 server client-ip-pool subnet-mask '255.255.255.0'
set interfaces openvpn vtun11996 server max-connections '250'
set interfaces openvpn vtun11996 server subnet '100.64.168.0/24'
set interfaces openvpn vtun11996 server topology 'subnet'
set interfaces openvpn vtun11996 tls ca-certificate 'ca'
set interfaces openvpn vtun11996 tls certificate 'server-cert-key'
set interfaces openvpn vtun11996 use-lzo-compression
  • set interfaces openvpn vtun11996 description 'OpenVPN-Server':为接口添加描述,标识其为OpenVPN服务端
  • set interfaces openvpn vtun11996 local-host '123.123.123.123':绑定服务端监听的本地IP地址
  • set interfaces openvpn vtun11996 local-port '11996':设置服务端监听的UDP端口
  • set interfaces openvpn vtun11996 mode 'server':定义接口工作模式为服务端
  • set interfaces openvpn vtun11996 openvpn-option '--script-security 3':设置脚本安全级别为3,允许执行带外部调用的脚本
  • set interfaces openvpn vtun11996 openvpn-option '--persist-key':启用密钥持久化,重连时无需重新读取私钥
  • set interfaces openvpn vtun11996 openvpn-option '--duplicate-cn':允许相同通用名称的客户端同时连接
  • set interfaces openvpn vtun11996 openvpn-option '--auth-user-pass-verify /config/auth/checkpsw.sh via-env':通过指定脚本验证客户端账号密码,信息通过环境变量传递
  • set interfaces openvpn vtun11996 openvpn-option '--username-as-common-name':将客户端用户名作为TLS认证的通用名称
  • set interfaces openvpn vtun11996 openvpn-option '--cipher AES-256-GCM':指定数据通道加密算法为AES-256-GCM
  • set interfaces openvpn vtun11996 openvpn-option '--tun-mtu 1500':设置TUN设备的MTU为1500字节
  • set interfaces openvpn vtun11996 openvpn-option '--mssfix 1300':限制TCP最大分段大小为1300字节,避免分片
  • set interfaces openvpn vtun11996 persistent-tunnel:启用隧道持久化,空闲时不主动断开连接
  • set interfaces openvpn vtun11996 protocol 'udp':使用UDP协议传输VPN数据
  • set interfaces openvpn vtun11996 server client ikuai ip '100.64.168.2':为标识为ikuai的客户端分配固定IP
  • set interfaces openvpn vtun11996 server client ikuai push-route '192.168.65.0/24':向ikuai客户端推送192.168.65.0/24网段的路由
  • set interfaces openvpn vtun11996 server client-ip-pool start '100.64.168.2':定义客户端动态IP池的起始地址
  • set interfaces openvpn vtun11996 server client-ip-pool stop '100.64.168.254':定义客户端动态IP池的结束地址
  • set interfaces openvpn vtun11996 server client-ip-pool subnet-mask '255.255.255.0':指定IP池的子网掩码
  • set interfaces openvpn vtun11996 server max-connections '250':限制最大并发客户端连接数为250
  • set interfaces openvpn vtun11996 server subnet '100.64.168.0/24':定义VPN客户端所属的统一子网
  • set interfaces openvpn vtun11996 server topology 'subnet':配置VPN网络拓扑为子网模式,客户端共处同一子网
  • set interfaces openvpn vtun11996 tls ca-certificate 'ca':指定用于TLS认证的根证书
  • set interfaces openvpn vtun11996 tls certificate 'server-cert-key':指定服务端的TLS证书和私钥
  • set interfaces openvpn vtun11996 use-lzo-compression:启用LZO数据压缩,优化传输效率

三、iKuai配置OpenVPN

3.1、CA证书获取

因为是vyos作为服务端,所以要先从vyos上获取ca证书。

show configuration commands |grep 'ca certificate'

yydy_2025-09-14_10-26-15

3.2、客户端证书获取

因为是vyos作为服务端,所以要先从vyos上获取刚刚创建的客户端证书。

show conf com |grep client-cert-key

yydy_2025-09-14_10-30-30

3.3、iKuai填写CA证书模板

-----BEGIN CERTIFICATE-----
这里填刚刚从vyos里面show出来的内容,只需要单引号里面的内容。
-----END CERTIFICATE-----

3.4、iKuai填写客户端证书模板

-----BEGIN CERTIFICATE-----
这里填刚刚从vyos里面show出来的内容,只需要单引号里面的内容。
-----END CERTIFICATE-----

3.5、iKuai填写客户端私钥模板

-----BEGIN RSA PRIVATE KEY-----
这里填刚刚从vyos里面show出来的内容,只需要单引号里面的内容。
-----END RSA PRIVATE KEY-----

3.6、iKuai完整配置

yydy_2025-09-14_10-23-49

四、连接状态查询

4.1、VyOS查看

show openvpn server

yydy_2025-09-14_10-42-15

4.2、iKuai查看

yydy_2025-09-14_10-43-31

4.3、iKuai路由表查看

可以通过VyOS的OpenVPN服务端分配固定IP地址给客户端使用,其次可以通过VyOS服务端控制传递那些路由给指定客户端,可以理解为是一个CCD功能。

yydy_2025-09-14_10-45-02

4.4、爱快traceroute测试

yydy_2025-09-14_10-47-26