最近翻阅了N多中英文文档,搭建了一个 OpenVPN + Freeradius + Mysql + Daloradius 的认证、限流、限速 VPN。网上的教程要么过时要么不全,我就在这里记录下搭建的全过程。

配置 OpenVPN Server

服务端安装

1
# apt install openvpn easy-rsa

公钥基础设施设置

创建证书目录

1
2
# mkdir /etc/openvpn/easy-rsa/
# cp -r /usr/share/easy-rsa/* /etc/openvpn/easy-rsa/  

接下来,编辑 /etc/openvpn/easy-rsa/vars 调整到适合您的环境

1
2
3
4
5
6
7
8
export KEY_COUNTRY="US"
export KEY_PROVINCE="NC"
export KEY_CITY="Winston-Salem"
export KEY_ORG="Example Company"
export KEY_EMAIL="steve@example.com"
export KEY_CN=MyVPN
export KEY_NAME=MyVPN
export KEY_OU=MyVPN

生成 CA 证书

1
2
3
4
# cd /etc/openvpn/easy-rsa/
# source vars
# ./clean-all
# ./build-ca

生成服务端证书和密钥

1
# ./build-key-server myvpn  //待会生成的证书和密钥名称

为 OpenVPN 服务器生成 Diffie Hellman 参数

1
# ./build-dh  

由于我们将采用用户名 + 密码的方式来进行认证,可以不生成客户端证书和密钥。
拷贝所需的证书和密钥到 /etc/openvpn/

1
2
# cd keys/
# cp myvpn.crt myvpn.key ca.crt dh2048.pem /etc/openvpn/

服务端简单配置

拷贝服务端配置文件模板

1
2
# cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/
# gzip -d /etc/openvpn/server.conf.gz

编辑 /etc/openvpn/server.conf ,更改配置如下

 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
port 1194
dev tun
ca ca.crt
cert myvpn.crt
key myvpn.key
dh dh2048.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
duplicate-cn
keepalive 10 120
max-clients 500
persist-key
persist-tun
status openvpn-status.log
verb 3
sndbuf 393216
rcvbuf 393216
push "sndbuf 393216"  //缓存区优化,下同
push "rcvbuf 393216"
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS xxx.xxx.xxx.xxx"
plugin /usr/lib/openvpn/radiusplugin.so  //radius认证插件 /etc/openvpn/radiusplugin.cnf  //radius认证配置文件
plugin /usr/lib/openvpn/bwlimitplugin.so  //限速插件 /etc/openvpn/bwlimitplugin.cnf  //限速插件配置文件
client-cert-not-required
username-as-common-name
script-security 3 system
reneg-sec 60  //重协商时间间隔

编辑 /etc/sysctl.conf,取消注释并更改以下项目

1
net.ipv4.ip_forward = 1

重新载入一下内核参数

1
# sysctl -p

设置 iptables

  • 清空规则

    1
    2
    
    # iptables -F
    # iptables -X
    
  • 之后这一步非常重要,配置允许ssh端口通过

    1
    
    # iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    
  • 配置默认过滤策略

    1
    2
    3
    
    # iptables -P INPUT DROP
    # iptables -P FORWARD DROP
    # iptables -P OUTPUT ACCEPT
    
  • 接下来允许openvpn的端口连接

    1
    
    # iptables -A INPUT -p tcp --dport 1194 -j ACCEPT
    
  • 允许虚拟网段的连接

    1
    
    # iptables -A INPUT -s 10.8.0.0/24 -j ACCEPT
    
  • 配置NAT转发

    1
    
    # iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
    
  • 保持已经建立的连接

    1
    
    # iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    
  • 保存iptables规则

    1
    
    # iptables-save > /etc/iptables-rules
    
  • 编辑/etc/network/interfaces文件,在最后插入下面内容

    1
    2
    
    pre-up iptables-restore < /etc/iptables-rules
    pre-up ip6tables-restore < /etc/ip6tables-rules
    

客户端简单配置

复制客户端配置文件模板

1
# cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf /etc/openvpn/

修改配置文件:

1
# vim /etc/openvpn/client.conf

以我使用的配置文件为例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
client
dev tun
proto tcp
resolv-retry infinite
nobind
persist-key
persist-tun
mute-replay-warnings
remote-cert-tls server
verb 3
mute 20
cipher AES-128-CBC
auth-user-pass  //设置为用户名加密码验证

<ca>
    //插入CA证书
<ca>