目录
  1. 1. 安装OpenVPN
  2. 2. 生成证书
    1. 2.1. 安装easy-rsa
    2. 2.2. 配置PKI 与 生成服务端证书
    3. 2.3. 生成及签发客户端证书
    4. 2.4. 证书整理
  3. 3. OpenVPN服务端配置
    1. 3.1. 服务端配置文件
    2. 3.2. 内核参数及iptables配置
  4. 4. 客户端配置
  5. 5. User/Pass认证方式
    1. 5.1. 服务端配置
    2. 5.2. 客户端配置

安装OpenVPN

 这里使用yum安装,先安装依赖。需要编译安装可到这里下载源码包

1
2
yum install -y openssl openssl-devel lzo lzo-devel pam pam-devel automake pkgconfig
yum -y install openvpn

生成证书

安装easy-rsa

 OpenVPN2.3(此处用2.3.7)后的版本需要独立下载easy-rsa用于生成证书,目前easy-rsa最新版本为3.x(此处用3.x版本),网上教程多为2.x版本。

1
2
3
4
cd /etc/openvpn
wget -c -O easy-rsa.zip https://github.com/OpenVPN/easy-rsa/archive/master.zip
unzip easy-rsa.zip
mv easy-rsa-master easy-rsa

配置PKI 与 生成服务端证书

 根据自身情况配置PKI——public key infrastructure(公钥基础设施)相关信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cd /etc/openvpn/easy-rsa/easyrsa3/
cp vars.example vars
export set_var EASYRSA_REQ_COUNTRY "CN"
export set_var EASYRSA_REQ_PROVINCE "GuangDong"
export set_var EASYRSA_REQ_CITY "XXXXX"
export set_var EASYRSA_REQ_ORG "Company"
set_var EASYRSA_REQ_EMAIL "XXXXX@gmail.com"
export set_var EASYRSA_REQ_OU "YunWeiBu"
#easy-rsa3已经不支持nsCertType设置,若需要则设置EASYRSA_NS_SUPPORT
export set_var EASYRSA_NS_SUPPORT "yes"
./easyrsa init-pki #初始化PKI
./easyrsa build-ca #创建CA,输入PEM密码(签发证书使用),其余一律回车
./easyrsa gen-req server nopass #创建服务端证书。 设置一下common name后其余一律回车
./easyrsa sign server server #签发服务端证书。输入yes,输入创建CA的PEM密码
./easyrsa gen-dh #创建Diffie-Hellman,增强OpenVPN安全性,确保key穿越不安全网络的命令

生成及签发客户端证书

 生成客户端所需的证书。如果是使用证书认证方式则需此步(生成客户端证书),假若是使用User/Passwd认证则无需此步骤。

1
2
3
4
5
mkdir -p /home/openvpn_client && cd /home/openvpn_client
cp -a /etc/openvpn/easy-rsa/easyrsa3/ /home/openvpn_client/easyrsa3_client && cd /home/openvpn_client/easyrsa3_client/
mv pki/ "/tmp/pki_`date "+%F_%H:%M:%S"`"
./easyrsa init-pki #初始化PKI
./easyrsa gen-req mogl #为用户生成证书,输入密码,此后改key则使用该密码,其余默认回车

 签发客户端证书

1
2
3
cd /etc/openvpn/easy-rsa/easyrsa3/
./easyrsa import-req /home/openvpn_client/easyrsa3_client/pki/reqs/mogl.req mogl #导入用户的req
./easyrsa sign client mogl #用户签约,输入yes,输入服务端PEM密码

 最终服务端的easyrsa3pki目录如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@openvpn easyrsa3]# cd /etc/openvpn/easy-rsa/easyrsa3
[root@openvpn easyrsa3]# tree pki
pki
├── ca.crt
├── certs_by_serial
│   ├── 01.pem
│   └── 02.pem
├── dh.pem
├── index.txt
├── index.txt.attr
├── index.txt.attr.old
├── index.txt.old
├── issued
│   ├── mogl.crt
│   └── server.crt
├── private
│   ├── ca.key
│   └── server.key
├── reqs
│   ├── mogl.req
│   └── server.req
├── serial
└── serial.old
4 directories, 16 files

证书整理

 将服务端和客户端证书集中,方便管理以及查找

1
2
3
4
5
6
7
8
9
10
11
mkdir -p /etc/openvpn/keys
#服务端证书
cp /etc/openvpn/easy-rsa/easyrsa3/pki/ca.crt /etc/openvpn/keys
cp /etc/openvpn/easy-rsa/easyrsa3/pki/private/ca.key /etc/openvpn/keys
cp /etc/openvpn/easy-rsa/easyrsa3/pki/private/server.key /etc/openvpn/keys
cp /etc/openvpn/easy-rsa/easyrsa3/pki/issued/server.crt /etc/openvpn/keys
cp /etc/openvpn/easy-rsa/easyrsa3/pki/dh.pem /etc/openvpn/keys/dh2048.pem

#客户端证书
cp /etc/openvpn/easy-rsa/easyrsa3/pki/issued/mogl.crt /etc/openvpn/keys
cp /home/openvpn_client/easyrsa3_client/pki/private/mogl.key /etc/openvpn/keys

OpenVPN服务端配置

服务端配置文件

 对配置文件/etc/openvpn/server.conf进行修改,local可不配置,默认监听0.0.0.0

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
#指定OpenVPN监听的ip
;local xxx.xxx.xxx.xxx
#指定OpenVPN监听的端口
port 1194
#指定OpenVPN使用的协议(TCP/UDP)
proto udp
#指定通信隧道类型(tun/tap),tap链路层协议,tun网络层点对点协议
dev tun
#指定ca cert key dh文件路径
ca /etc/openvpn/keys/ca.crt
cert /etc/openvpn/keys/server.crt
key /etc/openvpn/keys/server.key
dh /etc/openvpn/keys/dh2048.pem
#指定client分配的IP段
server 192.168.10.0 255.255.255.0
#指定client使用的ip地址
ifconfig-pool-persist /etc/openvpn/ipp.txt
#指定client默认网关为VPN,既使所有流量都走VPN隧道
push "redirect-gateway def1 bypass-dhcp"
#指定client的DHCP选项(DNS)
push "dhcp-option DNS 223.5.5.5"
#每10秒检查存活,连续120秒无响应则认为连接丢失并重新启动VPN和重连
keepalive 10 120
#对数据进行压缩(server和client都设置才有效)
comp-lzo
#指定最大client数目
max-clients 100
#设定连接保持密钥功能。在由于keepalive检测超时后而重新启动VPN的情况,不重新读取keys,而保留第一次使用的keys
persist-key
#设定连接保持在线功能。由于keepalive检测超时后而重新启动VPN,一直保持tun/tap是linkup,否则连接会先linkdown然后linkup
persist-tun
#日志相关设置
status /var/log/openvpn-status.log
log /var/log/openvpn.log
#指定日志记录级别(0-9)
verb 3
#指定脚本安全级别,当前客户端设置需要要将此值设置大于3,否则客户端连接时会报错
script-security 3

内核参数及iptables配置

 允许本机ip转发

1
2
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
echo 1 > /proc/sys/net/ipv4/ip_forward

 iptables允许连接OpenVPN,注意自己添加开机启动。网卡设备eth0根据自身服务器上为准

1
2
3
iptables -A INPUT -p udp --dport 1194 -j ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o eth0 -j MASQUERADE
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

客户端配置

 下载Windows客户端,安装程序点击这里
ca.crtmogl.crtmogl.keyclient.ovpn四个文件放到OpenVPN安装目录下的config目录,client.ovpn文件内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
client
dev tun
proto udp
remote 服务器主机IP 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cert mogl.crt
key mogl.key
#ns-cert-type server #假若客户端报错'VERIFY nsCertType ERROR: require nsCertType=SERVER',则将此行注释
comp-lzo
verb 3

User/Pass认证方式

服务端配置

 以上配置客户端是通过证书的验证方式,OpenVPN可以配置成通过User/Pass认证方式
 在保持配置文件/etc/openvpn/server.conf不变的基础上添加如下内容:

1
2
3
4
#User/Pass认证方式
auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env
client-cert-not-required
username-as-common-name

 添加client-cert-not-required则代表只使用用户名密码方式验证登录,如果不加,则代表需要证书和用户名密码双重验证登录!
/etc/openvpn/checkpsw.sh内容如下所示,也可点击这里进行下载:

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
#!/bin/sh
###########################################################
# checkpsw.sh (C) 2004 Mathias Sundman <mathias@openvpn.se>
#
# This script will authenticate OpenVPN users against
# a plain text file. The passfile should simply contain
# one row per user with the username first followed by
# one or more space(s) or tab(s) and then the password.

PASSFILE="/etc/openvpn/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

 默认密码文件在/etc/openvpn/psw-file,用户名和密码用空格隔开,同时确保openvpn启动用户可读取该文件

1
2
3
cat /etc/openvpn/psw-file
test mogl1002
chmod 600 /etc/openvpn/psw-file

 服务端配置完后需要重启OpenVPN

1
/etc/init.d/openvpn restart

客户端配置

 客户端client.ovpn只需要ca.crt即可,将cert mogl.crtkey mogl.key注释,新增auth-user-pass

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
client
auth-user-pass
dev tun
proto udp
remote 服务器主机IP 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
;cert mogl.crt
;key mogl.key
#ns-cert-type server #假若客户端报错'VERIFY nsCertType ERROR: require nsCertType=SERVER',则将此行注释
comp-lzo
verb 3

 Linux下的客户端,ca.crt如常放在/etc/openvpn/目录下

1
2
yum install openvpn
openvpn --daemon --cd /etc/openvpn --config client.ovpn

tips1,假若不想每次登录都输入用户名/密码,则可以修改auth-user-pass参数,将用户名/密码明文存到文件中让OpenVPN客户端读取(安全性会降低)

1
2
3
4
auth-user-pass pwd.txt
#在config目录下新建文件pwd.txt明文保存用户名/密码
username
password

tips2, OpenVPN若结合了Google动态验证码,除了修改auth-user-pass参数外,每次还需要请求Google身份验证获取动态验证码写入到用户/密码文件pwd.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 安装Google验证码工具
# sudo apt-get install oathtool

# 动态修改pwd.txt,拨号脚本
. /etc/profile
set -ue
BASE_DIR="$(dirname $(readlink -f $0))"
cd $BASE_DIR

TOKEN="xxxxxxxxxxxxxxxxxxx"

GOOGLE_AUTH_NUM="`oathtool --totp -b $TOKEN`"
PASSWORD_FILE="pwd.txt"

# OpenVPN密码为`固定密码(xxxxxxxxx) + Google动态验证码`
sed -ri "s/(xxxxxxxxx)(.*)/\1$GOOGLE_AUTH_NUM/" $PASSWORD_FILE
sleep 1
sudo openvpn --daemon --cd /etc/openvpn --config /etc/openvpn/client.ovpn && sleep 5 && route -n

tips3,windows中若想OpenVPN客户端自动连接,则可按以下步骤设置:

  1. 创建openvpn-gui.exe快捷方式
  2. 右键——>属性——>高级——以管理员身份运行
  3. 右键——>属性——>目标,添加--connect client.ovpn,例如——“C:\Program Files\OpenVPN\bin\openvpn-gui.exe” –connect client.ovpn

Powered: Hexo, Theme: Nadya remastered from NadyMain