Administrator
Published on 2024-11-17 / 51 Visits
0
0

openvpn交叉编译

首先从六个方面介绍一下openvpn

OpenVPN 是一个开源的 VPN(虚拟私人网络)解决方案,用于安全地连接网络。它使用标准的 VPN 协议(如 SSL/TLS)来创建加密的隧道,使数据能够在公共网络上安全传输。下面是 OpenVPN 的基本原理和工作流程:

  • 加密和认证

OpenVPN 使用 SSL/TLS 协议来提供加密和认证。SSL/TLS 是一种安全协议,最初用于保护 Web 流量,但 OpenVPN 将其扩展到 VPN 使用。

在连接建立过程中,OpenVPN 会使用公钥加密和对称加密技术来确保数据的安全性。它首先使用公钥加密来进行身份验证,然后在建立连接后,使用对称加密来加密数据流。

  • 隧道建立

当客户端和服务器建立连接时,OpenVPN 会创建一个虚拟的网络接口(TUN/TAP 设备)。这两个设备用于在客户端和服务器之间传输数据包。

TUN 设备用于处理点对点的 IP 数据包(第3层),而 TAP 设备用于处理以太网帧(第2层)。选择哪种设备取决于 VPN 配置和需求。

  • 配置和连接

OpenVPN 使用配置文件来指定如何建立和管理连接。配置文件定义了加密设置、认证方式、网络设置等。

客户端和服务器的配置文件会包括对方的 IP 地址或域名、证书、密钥文件等信息。

  • 身份验证

在连接建立时,OpenVPN 会使用证书和密钥对客户端和服务器进行身份验证。这样可以确保连接的安全性,防止未经授权的访问。

OpenVPN 支持多种认证方式,包括基于证书的认证和基于用户名/密码的认证。

  • 数据传输

一旦连接建立并经过认证,OpenVPN 就会在客户端和服务器之间创建一个加密的隧道。所有经过这个隧道的数据都会被加密和解密,从而保护数据不被第三方窃取或篡改。

数据包通过 TUN/TAP 设备在客户端和服务器之间传输,然后通过加密隧道进行保护。

  • 网络访问

通过 OpenVPN 建立的 VPN 连接可以使客户端访问服务器端网络上的资源。根据配置,客户端可以被分配到特定的虚拟 IP 地址,使其在服务器端网络上表现得像是一个本地客户端。

 

openvpn的应用场景之一

以下是一个系统架构图 左边部分是老年人的家,里面有很多的传感器设备和门口机设备等,其中Blood glucose monitor(血糖监测器)、Loud speaker(扬声器)以及Blood pressures reader(血压计)与家中的室内监控器相连也通过SIP/VPN的网络通信协议与下图右边部分的服务中心相连接。这个系统的主要目的是通过各种传感器和设备来监控老年人的健康状况和生活环境,确保他们的安全,并在紧急情况下提供及时的帮助。在这样一个场景里面,openvpn所起的作用就是提供了一个安全、可靠的数据传输通道,确保了家庭监控系统的数据安全和隐私保护,将服务中心和家中设备看作是一个局域网,家中的设备可以远程与服务中心的IP视频电话进行通信。

 

下面是一张VPN(虚拟私人网络)技术的网络通信过程

A 主机和 B 主机通过 VPN 连接进行通信,就像它们直接连接到同一个局域网上一样

  • 主机和应用层

A 主机和 B 主机上的应用程序(APP)需要进行通信。这些应用程序运行在 TCP/IP 协议栈的顶端,依赖于下层协议来完成数据的发送和接收。

  • VPN 连接建立

用户在 A 主机上启动一个 VPN 客户端,该客户端会与 B 主机所在网络的 VPN 服务器建立一个加密的通信隧道。

  • TCP/IP 协议栈

在 A 主机和 B 主机上,数据包都会通过各自的 TCP/IP 协议栈。TCP/IP 协议栈是互联网通信的基础,包括应用层、传输层、网络层、数据链路层等。

  • TUN/TAP 设备

VPN 连接通常使用 TUN/TAP 设备来在网络层和数据链路层之间创建虚拟网络接口。TUN 是一种网络层设备,可以处理 IP 数据包;TAP 是数据链路层设备,可以处理以太网帧。

在 A 主机上,VPN 客户端会创建一个 TUN 设备,将 IP 数据包发送到这个设备,然后 TUN 设备将这些数据包封装成可在 Internet 上传输的格式。

  • 数据封装

当 A 主机上的应用程序生成数据并发送到网络时,数据首先到达 TUN 设备。VPN 客户端将这些数据封装成 VPN 协议的数据包,通常是通过添加新的头部信息来实现。

  • 通过 Internet 传输

加密后的数据包通过 Internet 发送到 VPN 服务器,然后 VPN 服务器将数据包发送到 B 主机所在网络的 VPN 服务器或直接发送到 B 主机。

  • 解密和解封装

数据包到达 B 主机的 VPN 客户端后,会被解密并去除 VPN 协议头部,恢复成原始的 IP 数据包。

B 主机上的应用程序接收到数据后,通信过程完成。

 

以下图片同样解释了OpenVPN通信流程

  1. 启动OpenVPN: 用户启动OpenVPN客户端,准备建立VPN连接。

  2. 建立Socket连接: OpenVPN利用Socket通信机制与VPN服务器建立一个安全的连接。Socket是网络通信的基础,允许数据在应用程序之间传输。

  3. 数据写入: 当用户的数据需要发送时,OpenVPN将数据写入(write)到Socket,准备通过网络发送。

  4. 字符驱动处理: 数据通过字符设备驱动程序进一步处理。字符驱动通常处理字符数据流,这里可能涉及到将数据转换或封装成适合网络传输的格式。

  5. 数据封装: 在数据发送前,需要进行封装,这可能包括添加协议头、加密等,以确保数据在传输过程中的安全性和完整性。

  6. TCP/IP协议栈处理: 封装后的数据包通过TCP/IP协议栈进行处理。协议栈负责将数据包按照网络协议进行路由和传输。

  7. TUN/TAP虚拟网卡: 数据包到达虚拟网卡,TUN/TAP设备在这里充当虚拟网络接口,将虚拟网络流量与物理网络流量进行转换。

  8. 物理网卡传输: 虚拟网卡将处理好的数据包发送到物理网卡,物理网卡再将数据包发送到网络中,开始其在网络中的旅程。

  9. 接收端处理: 数据包到达接收端后,会经过相反的过程。首先由物理网卡接收,然后通过TUN/TAP虚拟网卡,再由TCP/IP协议栈进行处理。

  10. 数据提取: 在接收端,字符驱动读取(read)数据,进行必要的解码和解密,以恢复原始数据。

  11. 数据使用: 最终,接收到的数据被传递给应用程序,供用户使用。

成功搭建openvpn我用到了openssl1.1.1t版本,openvpn2.5.10版本,ubuntu20.04虚拟机,制作客户端和服务端证书的工具Easy-RSA,交叉编译工具mips-gcc472-glibc216-64bit.tar(本文以交叉编译到mips32架构的设备上为例),还有一个会思考的脑子

  • openssl1.1.1t

可以在ubuntu系统中通过wget https://www.openssl.org/source/old/1.1.1/openssl-1.1.1t.tar.gz命令下载到本地,然后使用tar -xzvf openssl-1.1.1t.tar.gz命令解压

  • openvpn2.5.10

可以在ubuntu系统中通过wget https://swupdate.openvpn.org/community/releases/openvpn-2.5.10.tar.gz命令下载到本地,然后使用tar -xzvf openvpn-2.5.10.tar.gz命令解压

  • Easy-RSA

可以在ubuntu系统中通过sudo apt install openvpn easy-rsa命令进行安装,默认安装在/usr/share/easy-rsa目录下

  • 交叉编译工具mips-gcc472-glibc216-64bit.tar(我是要把openvpn客户端最终要放在mips32架构的嵌入式设备上运行所以才用到这个,如果您只需将客户端安装在ubuntu系统则跳过该项)

在使用这个交叉编译工具之前,请先修改 ~/.bashrc文件(可以用这个命令sudo vim ~/.bashrc),按下键盘上的字母i进入编辑模式,在该文件最后一行增加这句话

export PATH=$PATH:/opt/mips-gcc472-glibc216-64bit/bin (冒号后的路径根据自己情况来定,我把交叉编译工具解压到opt目录下了),添加完按键盘Esc退出编辑模式然后输入(:wq)保存  然后在命令行中输入source ~/.bashrc来更新环境变量

 

接下来上配置代码 (博主本人经历几十次失败最终才定下来的能用的配置代码)

进入openssl源码目录,如下图,并打开终端输入su切换到root

输入配置命令 openssl交叉编译命令(我这里将openssl安装在/opt/openssl路径下,生成的是静态库)

./Configure no-asm no-async no-ssl2 no-ssl3 no-tls1_3 no-tls1_2 no-ec no-camellia no-seed no-ecdsa no-mdc2 no-chacha no-hw no-srp –prefix=/opt/openssl/openssl-1.1.1t linux-mips32 -static -static-libgcc -fPIC -DOPENSSL_NO_PAM –cross-compile-prefix=/opt/mips-gcc472-glibc216-64bit/bin/mips-linux-gnu-

openssl非交叉编译+去除静态编译配置命令(我这里将openssl安装在/opt/openssl路径下)

./config no-asm no-async no-ssl2 no-ssl3 no-tls1_3 no-tls1_2 no-ec no-camellia no-seed no-ecdsa no-mdc2 no-chacha no-hw no-srp –prefix=/opt/openssl/openssl-1.1.1t -fPIC -DOPENSSL_NO_PAM

openssl1.1.1t 交叉编译命令(生成的是动态共享库)

./Configure no-asm no-async no-ssl2 no-ssl3 no-tls1_3 no-tls1_2 no-ec no-camellia no-seed no-ecdsa no-mdc2 no-chacha no-hw no-srp \
–prefix=/opt/openssl/openssl-1.0.2r \
linux-mips32 -fPIC -DOPENSSL_NO_PAM \
–cross-compile-prefix=/opt/mips-gcc472-glibc216-64bit/bin/mips-linux-gnu- \
–openssldir=/opt/openssl/openssl-1.0.2r \
shared

 

配置成功后执行make CFLAGS=”-Os” LDFLAGS=”-s” -j$(nproc)make install即可

make CFLAGS=”-Os” LDFLAGS=”-s” -j$(nproc) 用这个命令编译出来的openssl占用空间会更小

CFLAGS: 这是编译器的标志,用于设置编译时的各种选项。-Os 选项告诉编译器优化生成的代码,以减小二进制文件的大小。-Os 优化级别会尝试减小程序的体积,同时保持代码的执行速度在一个合理的范围内。

LDFLAGS: 这是链接器的标志,用于设置链接时的各种选项。-s 选项告诉链接器剥离(strip)二进制文件中的符号信息

-j$(nproc): 这个选项用于指定 make 并行编译的线程数,其值为系统的核心数(nproc 命令的输出)。这样可以加快编译过程,因为它允许 make 同时在多个核心上编译不同的源文件。

 

进入openvpn源码目录,如下图,并打开终端输入su切换到root

输入配置命令(其中CC、LDFLAGS、CPPFLAGS需要根据自己情况进行修改,这个配置命令适合需要交叉编译的情况)

./configure –host=mips-linux-gnu \

CC=/opt/mips-gcc472-glibc216-64bit/bin/mips-linux-gnu-gcc \

LDFLAGS=”-L/opt/openssl/openssl-1.1.1t/lib -lssl -lcrypto -ldl” \

CPPFLAGS=”-I/opt/openssl/openssl-1.1.1t/include” \

–disable-silent-rules \

–disable-dependency-tracking \

–disable-lz4 \

–disable-lzo \

–disable-ofb-cfb \

–disable-plugins \

–disable-management \

–disable-fragment \

–disable-port-share \

–disable-debug \

–enable-small \

–disable-dco \

–disable-plugin-auth-pam \

–disable-plugin-down-root \

–disable-wolfssl-options-h \

–disable-libtool-lock \

–disable-unit-tests \

–with-crypto-library=openssl

 

openvpn非交叉编译配置命令(我关了很多功能,可以减小空间占用。请根据自己的情况来决定。更多信息可使用./configure –-help命令查看)

./configure –disable-silent-rules –disable-dependency-tracking –disable-lzo –disable-lz4 –disable-ofb-cfb –disable-plugins –disable-management –disable-fragment –disable-port-share –disable-debug –enable-small –disable-dco –disable-plugin-auth-pam –disable-plugin-down-root –disable-wolfssl-options-h –disable-libtool-lock –disable-unit-tests

 

配置完成后使用make和make install命令进行编译安装

openvpn编译完成之后默认在/usr/local/sbin路径下,可以使用ls-lh /usr/local/sbin/openvpn查看文件大小,可使用strip –strip-unneeded /usr/local/sbin/openvpn命令进行裁切(strip –strip-unneeded /usr/local/sbin/openvpn命令适合裁剪非交叉编译的openvpn,交叉编译的openvpn需要用命令/opt/mips-gcc472-glibc216-64bit/bin/mips-linux-gnu-strip –strip-all /usr/local/sbin/openvpn进行裁剪) 进一步缩小占用空间

使用Easy-RSA创建客户端和服务端的密钥和证书

Easy-RSA 是一种公钥基础设施 (PKI) 管理工具,用于在OpenVPN 服务器上生成证书请求。然后在 CA 服务器上验证和签名。在这里我们的 OpenVPN 服务器和CA服务器为同一个 Ubuntu 服务器

OpenVPN 的证书分为三部分:CA证书、Server端证书、Client端证书

 

Easy-RSA的配置文件是vars

作用:

定义证书参数:

vars 文件包含了生成证书时所用的各种默认值,例如证书的有效期、证书颁发机构(CA)的名称、组织信息等。这些参数会被 Easy-RSA 脚本用来自动填充证书请求和生成证书文件。

设置环境变量:

文件中定义的环境变量会影响 Easy-RSA 脚本的行为。例如,设置证书有效期、密钥长度、默认的国家和组织信息等。这些设置可以确保在生成证书时使用一致的配置。

简化证书生成过程:

通过预定义的变量和参数,vars 文件可以大大简化证书生成过程,避免在每次生成证书时都需要手动输入相同的信息。

Easy-RSA的使用方法(先使用cd命令进入easy-rsa的安装目录 一般为/usr/share/easy-rsa)

./easyrsa init-pki生成一个名为 pki 的子目录。这个目录将用来存储所有的证书、密钥和其他 PKI 相关文件

./easyrsa build-ca nopass ca.crt 包含 CA 的公钥,用于验证服务器和客户端证书的有效性。客户端会使用此证书来验证服务器的身份。

./easyrsa build-server-full server nopass 用来制作Server端证书

./easyrsa build-client-full client nopass 用来制作client端证书

./easyrsa gen-dh 用来生成迪菲·赫尔曼密钥

 

在 Easy-RSA 和 OpenVPN 等 VPN 配置中,dh.pem 文件包含了 Diffie-Hellman 参数,这些参数用于执行 Diffie-Hellman 密钥交换协议。dh.pem 文件通常包括一个长整数,代表了协议中的大质数和生成元(base)。这个文件的存在使得服务器能够与客户端进行安全的密钥交换,生成一个会话密钥来加密实际的数据传输。

在 OpenVPN 服务器配置中,dh.pem 文件用于配置服务器与客户端之间的加密通信。服务器会使用这个文件中的参数来生成会话密钥,客户端也会使用相同的参数来生成相同的密钥。

 

制作好的ca.crt和dh.pem在/usr/share/easy-rsa/pki目录下

服务器证书server.crt,client.crt在/usr/share/easy-rsa/pki/issued目录下

ca.key、server.key和client.key在/usr/share/easy-rsa/pki/private目录下

 

然后把刚刚创建好的CA证书、dh.pem、服务端证书/私钥、客户端证书/私钥复制过来,CA证书和dh.pem放在服务器上的/etc/openvpn目录下,服务端证书/私钥也放在服务器上的/etc/openvpn目录下,客户端证书/私钥放在ubuntu虚拟机的/nfs/client目录下,一会儿可以挂载到mips32架构的设备上(不知道什么是nfs的请看下文配置nfs)

 

编译和生成密钥与证书完成之后开始挂载nfs服务(如果您不想把openvpn映射到mips32架构设备上可跳过)

首先,在终端使用下列命令安装nfs

sudo apt-get install nfs-kernel-server

其次,在终端使用下列命令配置nfs服务

vim /etc/exports

在文件最后一行添加/home/zyw/nfs/ *(rw,sync,no_root_squash) 可以指定你想挂载的文件夹路径(博主这里是/home/zyw/nfs/)并保存退出

/home/zyw/nfs/:指示要共享的目录,需要自己创建

*:代表允许所有的网络段访问。

rw:指示具有可读写的权限。

sync:指示资料同步写入内存和硬盘。

no_root_squash:是 Ubuntu nfs 客户端分享目录使用者的权限。例如:如果客户端使用的是 root 用户,那么对于该共享目录而言,该客户端就具有 root 权限。

然后使用以下命令重新启动rpcbind服务和nfs服务

/etc/init.d/rpcbind restart

/etc/init.d/nfs-kernel-server restart

最后使用以下命令验证系统是否成功挂载nfs目录

showmount -e

博主这里显示出挂载的目录就算成功了

可以使用以下命令将nfs目录挂载到(博主这里是mips32架构设备)上 IP替换为设备的ip地址

busybox mount -t nfs -o nolock IP:/home/zyw/nfs/ /xxx/data/nfs/

 

挂载成功后把设备作为openvpn客户端去连接openvpn服务端,用以下命令运行设备上的openvpn客户端

/xxx/data/nfs/openvpn/openvpn –config /xxx/data/nfs/client/client.ovpn

client.ovpn文件需要放入ubuntu虚拟机的/nfs/client文件夹中

以下是我的客户端配置文件client.ovpn

route-nopull  # openvpn 客户端在连接时不从服务器端获取路由信息

 

route 10.8.0.0 255.255.255.0  # 不要与你的本地内网或其他内网环境中使用的 IP 地址网段冲突

 

client

 

dev tun

 

proto tcp

 

remote x.x.x.x 1194 (此处为注释 remote后面填的是openvpn服务端的ip和端口)

 

resolv-retry infinite

 

remote-cert-tls server

 

nobind

 

persist-key

 

persist-tun

 

ca /xxx/data/nfs/openvpn/ca.crt

 

cert /xxx/data/nfs/client/c112.crt

 

key /xxx/data/nfs/client/c112.key

 

data-ciphers AES-256-GCM:AES-128-GCM (此处为注释 客户端的加密算法要与服务端相同)

 

data-ciphers-fallback AES-256-CBC

 

verb 5  #日志级别

———————————————-分割线————————————————–

这里稍微对上述客户端配置做个解释

client:

这是 OpenVPN 配置文件的一个基本指令,用于指定这是一个客户端配置文件。它告诉 OpenVPN 该配置是用于客户端而不是服务器。

dev tun:

指定使用 TUN 设备。TUN 设备用于处理 IP 数据包,创建一个点对点的虚拟网络接口。TUN 设备将虚拟网络接口的 IP 数据包封装到加密隧道中,进行数据传输。

proto tcp:

指定 VPN 连接使用 TCP(传输控制协议)进行数据传输。TCP 是一种可靠的协议,它确保数据包的顺序和完整性,但可能会带来额外的延迟。如果使用 udp,则使用 UDP(用户数据报协议),它更快但不保证数据包的顺序和完整性。

remote x.x.x.x 1194:

指定 OpenVPN 服务器的 IP 地址和端口号。x.x.x.x 是服务器的 IP 地址,1194 是服务器监听的端口号。客户端将尝试连接到这个 IP 地址和端口以建立 VPN 连接。

resolv-retry infinite:

这个指令设置 DNS 解析的重试行为。infinite 表示客户端将无限期地重试解析远程服务器的地址,直到成功为止。这在网络不稳定或 DNS 服务器不可用时特别有用。

remote-cert-tls server:

指定客户端应该验证服务器的证书,并确保它是一个有效的服务器证书。这是一种额外的安全措施,确保客户端连接到合法的服务器,而不是伪装的服务器。server 参数告诉 OpenVPN 客户端检查服务器证书的类型。

nobind:

指定 OpenVPN 客户端在连接时不绑定到本地特定的端口。默认情况下,OpenVPN 客户端会绑定到一个端口以进行连接。nobind 参数允许客户端在连接时使用任何可用的端口,这样可以避免端口冲突问题。

persist-key:

指定 OpenVPN 在重新启动时保持当前的密钥文件。启用此选项后,OpenVPN 不会丢弃或重新加载密钥文件,这有助于在重新连接时保持相同的密钥状态,避免重新认证的过程。

persist-tun:

指定 OpenVPN 在重新启动时保持虚拟网络接口(TUN/TAP 设备)。启用此选项后,OpenVPN 会保留当前的网络接口配置,即使 VPN 连接中断或重新启动,也不会丢失接口的状态。这有助于减少因连接中断而导致的配置丢失问题。

 

博主这里把openvpn服务端通过上述编译安装的方式也安装在了一个公网服务器上

以下是服务端的配置文件server.conf

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 a.b.c.d

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 /etc/openvpn/ca.crt

cert /etc/openvpn/server.crt

key /etc/openvpn/server.key 

Diffie hellman parameters.

Generate your own with:

openssl dhparam -out dh2048.pem 2048

dh /etc/openvpn/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.255.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 /var/log/openvpn/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 10.8.0.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 192.168.40.128 255.255.255.248

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 8.8.8.8”

push “dhcp-option DNS 8.8.4.4”

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

data-ciphers AES-256-GCM:AES-128-GCM

data-ciphers-fallback 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 nobody

;group nogroup

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 /var/log/openvpn/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/openvpn.log

log-append /var/log/openvpn/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 5

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

;explicit-exit-notify 1

—————————————-分割线—————————————-

这里稍微对上述服务端配置做个解释

port 1194

描述: 指定 OpenVPN 监听的端口号。

默认: 1194(通常是 OpenVPN 的默认端口)。

用途: 用于配置 OpenVPN 服务器的监听端口。确保防火墙允许此端口的流量。

proto tcp

描述: 指定 OpenVPN 使用 TCP 协议。

默认: UDP(通常是 OpenVPN 的默认协议)。

用途: TCP 提供更可靠的连接,但可能有更高的延迟。使用 proto udp 可以提高性能但不保证可靠性。

dev tun

描述: 使用 TUN 设备创建一个虚拟的 IP 隧道。

默认: 未设置,OpenVPN 将使用默认设备。

用途: TUN 设备用于点对点的 IP 隧道;如果需要以太网级别的桥接,则使用 dev tap。

client-to-client

描述: 允许客户端之间直接通信。

默认: 未启用,客户端只能与服务器通信。

用途: 启用此选项后,所有连接到服务器的客户端将能够相互通信。

ca /etc/openvpn/ca.crt

描述: 指定 CA 根证书文件的位置。

用途: 用于验证客户端和服务器证书的有效性。

cert /etc/openvpn/server.crt

描述: 指定服务器证书文件的位置。

用途: 用于服务器身份验证。

key /etc/openvpn/server.key

描述: 指定服务器私钥文件的位置。

用途: 服务器用于加密和解密数据的私钥,必须保密。

dh /etc/openvpn/dh.pem

描述: 指定 Diffie-Hellman 参数文件的位置。

用途: 用于建立加密密钥交换的安全参数。

topology subnet

描述: 指定网络拓扑为子网模式。

用途: 用于设置 VPN 网络的 IP 地址分配方式。subnet 是推荐模式,net30 是旧的,现已不推荐。

server 10.8.0.0 255.255.255.0

描述: 配置 OpenVPN 服务器的虚拟 IP 地址池。

用途: 分配给客户端的 IP 地址范围。服务器将使用 10.8.0.1,其余地址分配给客户端。

ifconfig-pool-persist /var/log/openvpn/ipp.txt

描述: 保存客户端的虚拟 IP 地址与实际 IP 地址的映射。

用途: 保持客户端的 IP 地址一致性,即使在服务器重启后。

server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100

描述: 配置 OpenVPN 进行以太网桥接模式。

用途: 用于桥接 TAP 设备与以太网接口,以允许 VPN 客户端与 LAN 网络设备通信。

push “route 10.8.0.0 255.255.255.0”

描述: 向客户端推送路由信息。

用途: 告诉客户端将虚拟网络 10.8.0.0/24 的流量通过 VPN 发送。

client-config-dir ccd

描述: 指定客户端特定配置的目录。

用途: 允许为每个客户端配置特定的设置,例如固定 IP 地址或路由。

push “redirect-gateway def1 bypass-dhcp”

描述: 强制客户端将其默认网关通过 VPN 发送。

用途: 使所有流量(如 Web 浏览、DNS 查询)都通过 VPN 发送。需要服务器进行 NAT 或桥接。

push “dhcp-option DNS 8.8.8.8”

描述: 向客户端推送 DNS 服务器选项。

用途: 指定客户端使用的 DNS 服务器。8.8.8.8 和 8.8.4.4 是 Google 的公共 DNS 服务器。

client-to-client

描述: 允许客户端之间直接通信。

用途: 启用客户端间的互联。

duplicate-cn

描述: 允许多个客户端使用相同的证书。

用途: 主要用于测试环境,每个客户端应有唯一的证书用于生产环境。

keepalive 10 120

描述: 配置服务器和客户端之间的心跳检测。

用途: 发送 ping 消息以检测连接状态。如果 120 秒内没有收到 ping,连接将被认为已断开。

tls-auth ta.key 0

描述: 启用 HMAC 认证以增加额外的安全层。

用途: 防止 DoS 攻击和 UDP 端口洪水攻击。0 表示服务器端,客户端配置中应使用 1。

data-ciphers AES-256-GCM:AES-128-GCM

描述: 配置数据加密算法。

用途: 指定加密算法的优先顺序,AES-256-GCM 和 AES-128-GCM 是安全的加密算法。

compress lz4-v2

描述: 启用数据压缩(可选)。

用途: 减少通过 VPN 传输的数据量,但可能对某些版本的 OpenVPN 不兼容。

persist-key

描述: 在重新启动时保持密钥文件。

用途: 防止密钥文件丢失,确保重新连接时的密钥保持一致。

persist-tun

描述: 在重新启动时保持虚拟网络接口。

用途: 避免重新启动时丢失 TUN/TAP 设备配置。

status /var/log/openvpn/openvpn-status.log

描述: 指定状态文件的路径。

用途: 输出当前连接状态的日志文件。

log-append /var/log/openvpn/openvpn.log

描述: 指定日志文件的路径并选择追加模式。

用途: 记录 OpenVPN 的活动日志,以便查看和调试。

verb 5

描述: 设置日志详细级别。

用途: 详细的日志信息,适用于调试问题。5 是一个合理的级别,提供足够的详细信息而不会过于冗长。

mute 20

描述: 限制重复日志消息的输出数量。

用途: 防止日志文件中出现大量重复的相同消息。

explicit-exit-notify 1

描述: 在服务器重启时通知客户端。

用途: 当服务器重新启动时,客户端将收到通知以便重新连接。

 

附上两台设备(作为openvpn客户端)和家里电脑(作为openvpn客户端)连接成功的图片

 

在家里可以远程访问内网的mips32架构设备的管理界面

注意:如果需要在设备上使用openvpn,需要设备内核开启支持tun功能,不然就会报错(这里感谢李工帮我修改设备内核)

 

再次感谢苏经理和李工在项目中给予我的帮助!

参考文章:

https://blog.csdn.net/m0_56015193/article/details/134590283?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522172241329216800180677457%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=172241329216800180677457&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-134590283-null-null.142^v100^pc_search_result_base4&utm_term=openvpn%20ubuntu&spm=1018.2226.3001.4187

https://blog.csdn.net/iriczhao/article/details/126149918?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522172249397316800180620891%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=172249397316800180620891&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-126149918-null-null.142^v100^pc_search_result_base5&utm_term=ubuntu%20nfs&spm=1018.2226.3001.4187


Comment