OpenVPN在Centos上的使用

前段时间博主被迫又做了一次网站迁移,原本使用的狗云平台买的香港服务器,没想到这平台续费的时候有限制,必须要实名认证。太狗了,给我整吐了,当场换掉。

本网站如今已经迁移完成,狗云在香港的服务器还有半个月过期,正好拿来练手,试试OpenVPN的使用 博主之前做运维的时候,有负责过公司内网VPN,使用的WireGuard,一个基于UDP的VPN,配置比较简单

服务端搭建

一、安装配置easy-rsa证书配置

centos直接yum安装

yum -y install easy-rsa

下载有问题找不到包的这里博主都踩过坑了,下面三种方法绝对能解决 1、更新centos的yum源

cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache

2、安装扩展

yum install epel-release 

3、直接github下载tar

https://github.com/OpenVPN/easy-rsa
tar -xvf EasyRSA-3.1.7.tgz # 解压

创建文件夹,编写var参数 yum安装后的路径在/usr/share/easy-rsa/3.0.8

mkdir openvpn
mkdir easyrsa
cp vars.example vars
vi vars```
将下面6行写入文件
```shell
set_var EASYRSA_REQ_COUNTRY     "CN"
set_var EASYRSA_REQ_PROVINCE    "Beijing"
set_var EASYRSA_REQ_CITY        "Beijing"
set_var EASYRSA_REQ_ORG         "liangnianban"
set_var EASYRSA_REQ_EMAIL       "wangxueya123@gmail.com"
set_var EASYRSA_REQ_OU          "XueYa OpenVPN"
二、创建证书
[root@vm64791 EasyRSA-3.1.7]# ./easyrsa init-pki  #1、初始化
[root@vm64791 easy-rsa]# ./easyrsa build-ca    #2、创建根证书,会提示设置密码,用于ca对之后生成的server和client证书签名时使用,如果不想设置密码后面加上 nopass 其他提示内容直接回车即可
Enter New CA Key Passphrase:         #注意密码不能太短,我这边设置的是123456
Re-Enter New CA Key Passphrase: 
[root@vm64791 easy-rsa]# ./easyrsa gen-req server nopass    #3、创建server端证书和私钥文件,nopass表示不加密私钥文件,提示内容直接回车即可
[root@vm64791 easy-rsa]# ./easyrsa sign server server    #4、给server端证书签名,提示内容需要输入yes和创建ca根证书时候的密码
[root@vm64791 easy-rsa]# ./easyrsa gen-dh    #5、创建Diffie-Hellman文件,密钥交换时的Diffie-Hellman算法
[root@vm64791 easy-rsa]# ./easyrsa gen-req client nopass    #6、创建client端的证书和私钥文件,nopass表示不加密私钥文件,提示内容直接回车即可
[root@vm64791 easy-rsa]# ./easyrsa sign client client    #7、给client端证书签名,提示内容输入yes和创建ca根证书时候的密码
[root@vm64791 easy-rsa]# tree    #检查是否有ca根证书、客户端服务端证书、客户端服务端私钥,没有这个命令的可以手动查看,也可以yum -y install tree
.
├── easyrsa								#管理命令
├── gpl-2.0.txt
├── mktemp.txt
├── openssl-easyrsa.cnf
├── pki
│   ├── ca.crt							#ca根证书,服务端与客户端都需要用
│   ├── certs_by_serial
│   │   ├── 0B9A089DC70FD0C879844C04D5BBC3C0.pem
│   │   └── B8FCC94F2BA60C45A0BC21D462273BD2.pem
│   ├── dh.pem							#认证算法 服务端
│   ├── index.txt
│   ├── index.txt.attr
│   ├── index.txt.attr.old
│   ├── index.txt.old
│   ├── inline
│   ├── issued
│   │   ├── client.crt					#客户端证书
│   │   └── server.crt					#服务端证书
│   ├── openssl-easyrsa.cnf
│   ├── private
│   │   ├── ca.key
│   │   ├── client.key					#客户端私钥
│   │   └── server.key					#服务端私钥
三、安装openvpn并写入服务端配置文件
yum -y install openvpn
这里记录我的坎坷路程 yum能安装openvpn的请忽略:
本段最后找到了yum安装方法,请往下看
我服务器不知道什么情况,阿里的、官网的、以及http://elrepo.mirror.angkasa.id/elrepo/el7/$basearch/
的repo都找不到openvpn包

被迫手动下载源码编译安装:
https://github.com/openvpn/openvpn/releases 
或 wget https://swupdate.openvpn.org/community/releases/openvpn-2.5.3.tar.gz
---然后报错:configure: error: libnl-genl-3.0 package not found or too old. Is the development package and pkg-config (/usr/bin/pkg-config) installed? Must be version 3.4.0 or newer for DCO

yum安装的话版本太低了,仓库也没找到高版本。。。接着下
https://github.com/thom311/libnl/releases

#再下载源码安装完依赖,步骤如下:
tar -zxvf libnl-3.9.0.tar.gz
cd libnl-3.9.0
./configure --prefix=/usr/local/libnl3  --sysconfdir=/etc --disable-static
make && make install

# 定义库位置
echo /usr/local/libnl3/lib > /etc/ld.so.conf.d/libnl3.conf
ldconfig
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/usr/local/libnl3/lib/pkgconfig"

# 查看版本
pkg-config --modversion libnl-genl-3.0
# 显示3.9.0表示安装成功
#开始configure
 ./configure --with-crypto-library=openssl --enable-systemd --enable-lz4 --enable-pkcs11 --prefix=/usr/local/openvpn
上述命令将在/usr/local/openvpn目录下安装OpenVPN,并使用OpenSSL作为加密库。

--然后又报错:configure: error: libcap-ng package not found. Is the development package and pkg-config (/usr/bin/pkg-config) installed?
yum install libcap-ng-devel  #安装解决
--然后又报错:configure: error: No compatible LZ4 compression library found. Consider --disable-lz4
yum install lz4-devel lz4 #安装解决
--然后又报错:Package requirements (libsystemd-daemon) were not met:
No package 'libsystemd-daemon' found
yum -y install systemd-devel #安装解决
--然后走流程报错:PKCS11 enabled but libpkcs11-helper is missing
yum install -y pkcs11-helper pkcs11-helper-devel #安装解决

之后终于安装成功了!!!
最后做一层连接
ln -s /usr/local/openvpn/sbin/openvpn /usr/sbin/openvpn
大功告成!!强烈建议不要编译安装,太麻烦了!!!

但是这时,我也找到了yum下载的方法。。。。下面三步即可

wget https://repo.huaweicloud.com/epel/epel-release-latest-7.noarch.rpm
rpm -ivh epel-release-latest-7.noarch.rpm
yum update
写入服务器配置文件
port 1194                                    #端口
proto udp                                    #协议
dev tun                                      #采用路由隧道模式
ca /opt/easy-rsa/pki/ca.crt                  #ca证书的位置
cert /opt/easy-rsa/pki/issued/server.crt     #服务端公钥的位置
key /opt/easy-rsa/pki/private/server.key     #服务端私钥的位置
dh /opt/easy-rsa/pki/dh.pem                  #证书校验算法  
server 10.0.0.0 255.255.255.0                #给客户端分配的地址池
push "route 0.0.0.0 0.0.0.0"        #允许客户端访问的内网网段
ifconfig-pool-persist ipp.txt                #地址池记录文件位置,未来让openvpn客户端固定ip地址使用的
keepalive 10 120                             #存活时间,10秒ping一次,120秒如果未收到响应则视为短线
max-clients 100                              #最多允许100个客户端连接
status openvpn-status.log                    #日志位置,记录openvpn状态
log /var/log/openvpn.log                     #openvpn日志记录位置
verb 3                                       #openvpn版本
client-to-client                             #允许客户端与客户端之间通信
persist-key                                  #通过keepalive检测超时后,重新启动VPN,不重新读取
persist-tun                                  #检测超时后,重新启动VPN,一直保持tun是linkup的,否则网络会先linkdown然后再linkup
duplicate-cn                                 #客户端密钥(证书和私钥)是否可以重复
comp-lzo                                     #启动lzo数据压缩格式
push "redirect-gateway def1 bypass-dhcp"	#自动推送客户端上的网关及DHCP,此项开启了流量转发,有这项才能使用服务器代理上网
push "dhcp-option DNS 114.114.114.114"		#OpenVPN的DHCP功能为客户端提供指定的 DNS、WINS 等
client-to-client							#允许客户端与客户端相连接,默认情况下客户端只能与服务器相连接

开启内核转发功能

echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
systemctl restart network

启动openvpn服务并加入开机自启

systemctl enable openvpn@server.service    #设置启动文件
systemctl start openvpn@server.service        #启动openvpn服务

客户端搭建(Linux)

yum -y install openvpn
cat /etc/openvpn/client.conf
client                  #指定当前VPN是客户端
dev tun                 #使用tun隧道传输协议
proto udp               #使用udp协议传输数据
remote liangnianban.com 1194   #openvpn服务器IP地址端口号
resolv-retry infinite   #断线自动重新连接,在网络不稳定的情况下非常有用
nobind                  #不绑定本地特定的端口号
ca ca.crt               #指定CA证书的文件路径
cert client.crt         #指定当前客户端的证书文件路径
key client.key          #指定当前客户端的私钥文件路径
verb 3                  #指定日志文件的记录详细级别,可选0-9,等级越高日志内容越详细
persist-key     #通过keepalive检测超时后,重新启动VPN,不重新读取keys,保留第一次使用的keys
persist-tun     #检测超时后,重新启动VPN,一直保持tun是linkup的。否则网络会先linkdown然后再linkup

我们需要把rsa生成的客户端的公钥、私钥以及ca证书放在客户端服务器配置文件同级目录下 然后启动测试

systemctl start openvpn@client
systemctl enable openvpn@client

客户端搭建(Windows)

  • 先安装windows客户端,默认安装即可,安装完成后会提示你装在了哪个位置,config文件夹在哪
  • 将ca根证书、client.key、client.crt放入config目录(非密码登录)
  • 然后连接即可
client
dev tun
proto udp
remote 10.0.0.7 1194        #注意此处更改为openvpn服务端代码
resolv-retry infinite
nobind
ca ca.crt
cert client.crt
key client.key
verb 3
persist-key
comp-lzo
redirect-gateway def1 #开启全局代理

四、用户名密码认证方式

服务端

server.conf里面添加配置

script-security 3                                   #允许使用自定义脚本
auth-user-pass-verify /etc/openvpn/check.sh via-env #指定认证脚本
username-as-common-name                             #用户密码登陆方式验证

编写脚本文件

cat /etc/openvpn/check.sh           #
#!/bin/bash
PASSFILE="/etc/openvpn/openvpnfile"   #密码文件 用户名 密码明文
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

添加执行权限

chmod +x /etc/openvpn/check.sh

创建用户名密码,空格为分隔符

cat /etc/openvpn/openvpnfile
liangnianban xueya

重启服务

客户端

client.conf最下面加上auth-user-pass,之后重启连接测试!!

windows客户端连接