UseOpenVPN

OpenVPN使用手册

使用账号密码认证来配置,可以轻松实现多人登录使用VPN。OpenVPN所有的通信都基于一个单一的IP端口。OpenVPN 是一个开源的应用程序,它允许您通过公共互联网创建一个安全的专用网络。OpenVPN 实现一个虚拟专用网(VPN)来创建一个安全连接。OpenVPN 使用 OpenSSL 库提供加密,它提供了几种身份验证机制,如基于证书的、预共享密钥和用户名/密码身份验证。

1. 软件版本

  • Centos - 7.9
  • easy-rsa - 3.0.8
  • OpenVPN - 2.4.7

2. 安装

本文使用yum来安装openvpn,openvpn及其依赖的一些包在epel源上,首先先安装epel源。

1
2
3
4
5
6
7
8
9
#更新yum
yum update -y
#安装epel源
yum install -y epel-release
#安装依赖包
yum install -y openssl lzo pam openssl-devel lzo-devel pam-devel
yum install -y easy-rsa
#安装openvpn
yum install -y openvpn

3. 配置

已经安装好了openvpn了,下面我们对openvpn进行配置。

3.1 连接方式

使用路由方式连接,确定私有子网:

Server 与 Client 的VPN通道子网,不要与已有环境的网络冲突即可。

默认:10.8.0.0/16

3.2 配置证书密钥

我们通过yum方式安装的 easy-rsa 版本是3.0.8,直接从安装路径copy一份工具出来。这里用默认的 easy-rsa 3.0.8 来配置生成证书密钥。

1
2
3
4
5
6
7
8
9
10
#复制easy-rsa工具
cp -rf /usr/share/easy-rsa/3.0.8 /etc/openvpn/server/easy-rsa
cd /etc/openvpn/server/easy-rsa
#生成证书密钥
./easyrsa init-pki
./easyrsa build-ca nopass
./easyrsa build-server-full server nopass
#下面这步可能要几分钟
./easyrsa gen-dh
openvpn --genkey --secret server.key #TLS-auth密钥

3.3 配置Server端

3.3.1 创建使用的目录

1
2
3
4
5
6
# 日志存放目录
mkdir -p /var/log/openvpn/
# 用户管理目录
mkdir -p /etc/openvpn/server/user
# 配置权限
chown -R openvpn:openvpn /var/log/openvpn

3.3.2 创建Server配置文件

1
2
3
#编辑server.conf文件
yum install -y vim
vim /etc/openvpn/server/server.conf

服务器openvpn基本配置,写入以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 配置权限
port 1194 #监听端口号
proto tcp #服务端用的协议,可以为tcp/udp
dev tun #指定创建的通信隧道类型,可选tun或tap
user openvpn
group openvpn

#配置证书信息
ca /etc/openvpn/server/easy-rsa/pki/ca.crt #CA 根证书路径
cert /etc/openvpn/server/easy-rsa/ta.key #open VPN 服务器证书路径
key /etc/openvpn/server/easy-rsa/server.key #open VPN 服务器密钥路径
dh /etc/openvpn/server/easy-rsa/pki/dh.pem #Diffie-Hellman 算法密钥文件路径
tls-auth /etc/openvpn/server/easy-rsa/ta.key 0
# 开启TLS-auth,使用防御攻击。服务器端的第二个参数值为0,客户端的为

#配置账号密码的认证方式
auth-user-pass-verify /etc/openvpn/server/user/checkpsw.sh via-env
script-security 3
verify-client-cert none
username-as-common-name
client-to-client #允许客户端与客户端相连接,默认情况下客户端只能与服务器相连接
duplicate-cn

#配置网络信息
server 10.8.0.0 255.255.255.0
# 该网段为 open VPN 虚拟网卡网段,不要和内网网段冲突即可。open VPN 默认为 10.8.0.0/24
push "dhcp-option DNS 223.5.5.5"
push "dhcp-option DNS 114.114.114.114"
push "route 192.168.1.0 255.255.255.0" #修改网段为内网网段
push "route 192.168.2.0 255.255.255.0" #同上

compress lzo #使用lzo压缩的通讯,服务端和客户端都必须配置
cipher AES-256-GCM #加密认证算法
keepalive 10 120 #每10秒ping一次,连接超时时间设为120秒
persist-key #重启时仍保留一些状态
persist-tun
verb 3 #指定日志文件的记录详细级别,可选0-9,等级越高日志内容越详细

log /var/log/openvpn/server.log #指定 log 文件位置
log-append /var/log/openvpn/server.log
status /var/log/openvpn/status.log

3.3.3 创建用户密码文件

格式是用户 密码以空格分割即可,这里将用户名设置为mytest,密码设置为mytestpass

1
2
3
echo 'mytest mytestpass' >> /etc/openvpn/server/user/psw-file
chmod 600 /etc/openvpn/server/user/psw-file
chown openvpn:openvpn /etc/openvpn/server/user/psw-file

3.3.4 创建密码检查脚本

新建shell文件

1
vim /etc/openvpn/server/user/checkpsw.sh

在shell文件中写入以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/sh
PASSFILE="/etc/openvpn/server/user/psw-file"
LOG_FILE="/var/log/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

赋予可执行的权限

1
2
chmod 700 /etc/openvpn/server/user/checkpsw.sh
chown openvpn:openvpn /etc/openvpn/server/user/checkpsw.sh

3.3.5 防火墙配置

  • 防火墙

禁用centos7默认的firewalld,使用iptables防火墙管理软件:

1
2
systemctl stop firewalld
systemctl mask firewalld
  • 禁用SELinux
1
2
3
4
5
#临时生效
setenforce 0

#永久关闭,配置后需要重启服务器生效
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
  • iptables

    启用iptables

    1
    2
    3
    4
    yum install iptables-services
    systemctl enable iptables
    systemctl start iptables
    iptables -F # 清理所有防火墙规则

    添加防火墙规则

    将openvpn的网络流量转发到内网(172.31.0.0)

    1
    2
    3
    4
    # 如下网段记得与server.conf 当中定义的网段保持一致
    iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
    iptables -L -t nat
    iptables-save > /etc/sysconfig/iptables # iptables 规则持久化保存
  • Linux服务器启用地址转发

    1
    vi /etc.sysctl.conf
    1
    2
    3
    4
    #在最后一行添加以下内容
    net.ipv4.ip_forward = 1
    #执行生效
    sysctl -p

===============================以下是firewalld配置可不看=========================

  • firewalld

    1
    2
    3
    4
    5
    6
    7
    8
    yum install -y firewall-cmd config(替换为下一句)
    yum install -y firewalld firewall-config
    firewall-cmd --permanent --add-masquerade
    firewall-cmd --permanent --add-service=openvpn
    # 或者添加自定义端口
    firewall-cmd --permanent --add-port=1194/tcp
    firewall-cmd --permanent --direct --passthrough ipv4 -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
    firewall-cmd --reload

注:

如果执行失败需确认防火墙是否开启。

1、查看防火墙状态:

1
systemctl status firewalld

img

inactive表示防火墙为关闭状态。

2、开启防火墙

1
systemctl start firewalld

img

启动后无任何提示,再次查看防火墙状态,可以看到变成active,成功启动。

3、关闭防火墙

1
systemctl stop firewalld

===============================以上是firewalld配置可不看=========================

3.3.6 启动服务

1
nohup openvpn /etc/openvpn/server/server.conf >/dev/null 2>&1 &

启动之后,服务端有一个tun0的网卡,如下

1
2
3
4
5
6
7
8
tun0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST>  mtu 1500
inet 10.8.0.1 netmask 255.255.255.255 destination 10.8.0.2
inet6 fe80::10da:ceb2:8a1c:4b4f prefixlen 64 scopeid 0x20<link>
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 100 (UNSPEC)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3 bytes 144 (144.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

开机自启动部分待设定

3.4 配置客户端

客户端我们同样使用2.4.7的版本,下载连接:

https://www.techspot.com/downloads/5182-openvpn.html

因为我们前面配置的是账号密码认证,所以我们只需要下载ca.crt、ta.key文件即可,从server上将生成的ca.crtta.key下载到客户端的安装目录 config下。

ca.crt文件路径:/etc/openvpn/server/easy-rsa/pki/ca.crt

ta.key文件路径:/etc/openvpn/server/easy-rsa/ta.key

在config目录下新建一个文件 client.ovpn,文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
client
proto tcp
dev tun
auth-user-pass
remote your.domain.com 1194
#1194为虚机开放的端口号,与前文中的配置要统一
#your.domain.com替换为Linux虚机的域名地址或者是该虚机的公有地址
ca ca.crt
tls-auth ta.key 1
remote-cert-tls server
cipher AES-256-GCM
auth-nocache
persist-tun
persist-key
compress lzo
verb 3
mute 10

image-20220225142525549

右键openvpn客户端图标的选项,需确认OpenVPN的设置如下:

image-20220225143238563

保存退出之后,我们启动openvpn的客户端,然后输入账号密码即可登录。

image-20220225142731834

telnet测试内网连通性

1
2
telnet 172.31.27.207 22
#telnet 内网ip 22端口
1
2
init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /etc/openvpn/server/easy-rsa/pki
1
2
/etc/openvpn/server/easy-rsa/pki/ca.crt
123456
1
123456