1. 安装openvpn
apt install openvpn
  1. 安装证书 如果已有证书 这步省略

参考文献 CSDN博主「疯飙的蜗牛」
在github 上,下载最新的easy-rsa
https://github.com/OpenVPN/easy-rsa 下载包
上传/etc/openvpn目录 解压unzip easy-rsa-master.zip
⑴配置自己的环境

[root@localhost  test]# cd /etc/openvpn/easy-rsa-master/easyrsa3
[root@localhost  easyrsa3]# cp vars.example vars
[root@localhost  easy-rsa3]# vim vars (将下面6行写入文件)
 
set_var EASYRSA_REQ_COUNTRY     "CN"
set_var EASYRSA_REQ_PROVINCE    "Beijing"
set_var EASYRSA_REQ_CITY        "Beijing"
set_var EASYRSA_REQ_ORG         "username"
set_var EASYRSA_REQ_EMAIL       "username@163.com"
set_var EASYRSA_REQ_OU          "My OpenVPN"

⑵创建跟证书

cd /etc/openvpn/easy-rsa-master/easyrsa3
 ./easyrsa build-ca

微信图片_20231204202607.png
注意:在上述部分需要输入PEM密码 PEM pass phrase,输入两次,此密码必须记住,不然以后不能为证书签名。还需要输入common name 通用名,这个你自己随便设置个独一无二的。
⑶创建服务器端证书

./easyrsa gen-req server nopass

该过程中需要输入common name,随意但是不要跟之前的根证书的一样
⑷签约服务端证书

./easyrsa sign server server

该命令中.需要你确认生成,要输入yes,还需要你提供我们当时创建CA时候的密码。如果你忘记了密码,那你就重头开始再来一次吧
⑸创建Diffie-Hellman,确保key穿越不安全网络的命令
./easyrsa gen-dh
⑹把服务器端必要文件放到etc/openvpn/ 目录下

cp /etc/openvpn/easy-rsa-master/easyrsa3/pki/ca.crt /etc/openvpn/
cp /etc/openvpn/easy-rsa-master/easyrsa3/pki/private/server.key /etc/openvpn/
cp /etc/openvpn/easy-rsa-master/easyrsa3/pki/issued/server.crt /etc/openvpn/
cp /etc/openvpn/easy-rsa-master/easyrsa3/pki/dh.pem /etc/openvpn/

因为是设置用户和密码登录 就不操作客户端证书了

  1. 配置服务配置

vim /etc/openvpn/server.conf

local 0.0.0.0
port 19744
# TCP or UDP server?
;proto tcp
proto udp
;dev tap
dev tun
ca /etc/openvpn/ca.crt
cert /etc/openvpn/server.crt
key /etc/openvpn/server.key  # This file should be kept secret
dh /etc/openvpn/dh.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
;push "redirect-gateway def1 bypass-dhcp"
;push "dhcp-option DNS 8.8.8.8"
client-to-client
keepalive 10 120
cipher AES-256-CBC
;comp-lzo
max-clients 100
;user openvpn
;group openvpn
persist-key
persist-tun
;status /var/log/openvpn/openvpn-status.log
;log         /var/log/openvpn/openvpn.log
verb 3
script-security 3
auth-user-pass-verify /etc/openvpn/checkpw.sh via-env    #指定用户认证脚本
username-as-common-name
verify-client-cert none
  1. 客户端配置

client
proto udp
remote ip地址 19744
dev tun
nobind
persist-key
persist-tun
cipher AES-256-CBC
verb 3
;comp-lzo
auth-user-pass


<ca>
/etc/openvpn/ca.crt 复制出来
</ca>

@. 给每个客户固定Ip
server.conf里加入 client-config-dir ccd
在/etc/openvpn/ccd/ 里面创建指定的用户名文档
vim /etc/openvpn/ccd/name
ifconfig-push 10.8.0.5 10.8.0.6
每对 ifconfig-push 地址代表虚拟客户端和服务器 IP 端点。它们必须取自连续的 /30 子网才能与 Windows 客户端和 TAP-Windows 驱动程序兼容。具体来说,每个端点对的 IP 地址中的最后一个八位字节必须从这个集合中获取:
[ 1, 2] [ 5, 6] [ 9, 10] [ 13, 14] [ 17, 18]
[ 21, 22] [ 25, 26] [ 29, 30] [ 33, 34] [ 37, 38]
[ 41, 42] [ 45, 46] [ 49, 50] [ 53, 54] [ 57, 58]
[ 61, 62] [ 65, 66] [ 69, 70] [ 73, 74] [ 77, 78]
[ 81, 82] [ 85, 86] [ 89, 90] [ 93, 94] [ 97, 98]
[101,102] [105,106] [109,110] [113,114] [117,118]
[121,122] [125,126] [129,130] [133,134] [137,138]
[141,142] [145,146] [149,150] [153,154] [157,158]
[161,162] [165,166] [169,170] [173,174] [177,178]
[181,182] [185,186] [189,190] [193,194] [197,198]
[201,202] [205,206] [209,210] [213,214] [217,218]
[221,222] [225,226] [229,230] [233,234] [237,238]
[241,242] [245,246] [249,250] [253,254]
比如 10.8.0.7 就不行,上面集合里面没有 就会出错

发表评论