Ocserv安装(账号+密码+OTP认证方式)

简单介绍

ocserv 是目前比较常用的vpn软件。这里完整记录下从零开始搭建过程。

名称 说明
操作系统 Centos 7 所用到的基础image

源码安装ocserv

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#依赖的安装
yum install epel-release
yum install -y pam-devel lz4-devel libseccomp-devel readline-devel mysql/libnl3-devel krb5-devel radcli-devel libcurl-devel cjose-devel mysql/jansson-devel protobuf-c-devel libtalloc-devel http-parser-devel mysql/protobuf-c gperf nuttcp lcov uid_wrapper pam_wrapper nss_wrapper mysql/socket_wrapper gssntlmssp haproxy iputils freeradius gawk mysql/gnutls-utils iproute yajl tcpdump gnutls-devel libev-devel

#下载安装认证模块
wget https://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/l/liboath-devel-2.6.2-1.el7.x86_64.rpm
rpm -ivh liboath-devel-2.6.2-1.el7.x86_64.rpm
wget https://download-ib01.fedoraproject.org/pub/epel/7/SRPMS/Packages/o/oath-toolkit-2.6.2-1.el7.src.rpm
rpm -ivh oath-toolkit-2.6.2-1.el7.src.rpm

#下载源代码
git clone https://gitlab.com/openconnect/ocserv.git

#编译安装及配置OTP模块
cd ocserv/
autoreconf -fvi
./configure --prefix=/usr/local/ocserv --with-local-talloc --with-local-http-parser --with-local-protobuf-c --with-openssl --with-pam --with-geoip --with-otp
make && make install

#内核参数调优
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
echo "net.core.default_qdisc = fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control = bbr" >> /etc/sysctl.conf
sysctl -p

配置账号(账号+密码+OTP认证方式)

上述步骤执行完基本的ocserv 已经安装好, 其实如果不编译 直接执行 apt 安装也行

1
2
3
4
5
6
7
8
9
10
#创建账户
ocpasswd -c /etc/ocserv/ocpasswd user
#生成一个16字节的随机字符串作为用户的密钥
echo "HOTP/T30 user - $(head -c 16 /dev/urandom |xxd -c 256 -ps)" >>/etc/ocserv/ocserv.otp
#查看key
cat ocserv.otp
#生成totp
oathtool --totp -v xxx
#通过上面的Base32 secret生成otp二维码
qrencode -t ANSIUTF8 "otpauth://totp/user@ocserv?secret=xxx"

配置服务

  1. 修改配置文件

    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
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    # 下载个网上现成的,具体优化参考官方的 sample 也行,这里就不优化了
    wget -N --no-check-certificate -P "/etc/ocserv" "https://files.zorz.cc/ocserv.conf"


    auth = "plain[passwd=/etc/ocserv/ocpasswd]"
    # listen-host = [IP|HOSTNAME]
    tcp-port = 443
    #udp-port = 443 #最好注释掉(0)
    run-as-user = nobody
    run-as-group = daemon
    socket-file = /var/run/ocserv-socket
    server-cert = /etc/letsencrypt/live/xxx.com/fullchain.pem (1
    server-key = /etc/letsencrypt/live/xxx.com/privkey.pem (2#刚才写的
    ca-cert = /etc/ocserv/ssl/ca-cert.pem
    isolate-workers = true
    banner = "Hello"
    max-clients = 16
    max-same-clients = 2
    server-stats-reset-time = 604800
    keepalive = 32400
    dpd = 30
    mobile-dpd = 90
    switch-to-tcp-timeout = 25
    try-mtu-discovery = true
    tls-priorities = "NORMAL:%SERVER_PRECEDENCE:%COMPAT:-VERS-SSL3.0"
    auth-timeout = 240
    min-reauth-time = 300
    max-ban-score = 80
    ban-reset-time = 1200
    cookie-timeout = 300
    deny-roaming = false
    rekey-time = 172800
    rekey-method = ssl
    use-occtl = true
    pid-file = /var/run/ocserv.pid
    device = vpns
    predictable-ips = true
    default-domain = xxx.com #(3)

    ipv4-network = 192.168.169.0 #(4)
    ipv4-netmask = 255.255.255.0
    # An alternative way of specifying the network:
    #ipv4-network = 192.168.1.0/24
    # The IPv6 subnet that leases will be given from.
    #ipv6-network = fda9:4efe:7e3b:03ea::/48
    # Specify the size of the network to provide to clients. It is
    # generally recommended to provide clients with a /64 network in
    # IPv6, but any subnet may be specified. To provide clients only
    # with a single IP use the prefix 128.
    #ipv6-subnet-prefix = 128
    #ipv6-subnet-prefix = 64
    tunnel-all-dns = true
    dns = 8.8.8.8
    dns = 8.8.4.4
    ping-leases = false
    # route = 10.10.10.0/255.255.255.0
    # route = 192.168.0.0/255.255.0.0
    # route = fef4:db8:1000:1001::/64
    # route = default
    # no-route = 192.168.5.0/255.255.255.0
    cisco-client-compat = true
    dtls-legacy = true



    # 需要改的我已经标注好了
  2. 启动

1
/usr/local/ocserv/sbin/ocserv --foreground --pid-file /run/ocserv.pid --config /usr/local/ocserv/ocserv.conf
  1. 查看日志
1
journalctl -xe -u ocserv.service

yum安装ocserv

为了教程更简便,这里我直接用EPEL源安装Ocserv,可以省去麻烦的编译过程。首先安装EPEL源:

1
yum -y install epel-release

然后就可以直接YUM安装Ocserv了:

1
yum -y install ocserv

新建一个目录,用来存放SSL证书相关文件,然后进入到这个目录内:

1
2
mkdir ssl
cd ssl

新建一个证书模板:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
vi ca.tmpl

写入:

cn = "LALA"
organization = "LALA.IM"

serial = 1
expiration_days = 9999
ca
signing_key
cert_signing_key
crl_signing_key

注:LALA和LALA.IM可以根据自己的需要更改,都是自签证书,随便写也没关系。

然后生成私钥和CA证书:

1
2
certtool --generate-privkey --outfile ca-key.pem
certtool --generate-self-signed --load-privkey ca-key.pem --template ca.tmpl --outfile ca-cert.pem

接着来生成服务器证书,还是老样子新建一个证书模板:

1
2
3
4
5
6
7
8
9
10
11
12
vi server.tmpl

写入:

cn = "你的服务器IP"
organization = "LALA.IM"
expiration_days = 9999
signing_key
encryption_key
tls_www_server

注:cn后面的值改成你的服务器公网IP。

然后生成私钥和证书:

1
2
certtool --generate-privkey --outfile server-key.pem
certtool --generate-certificate --load-privkey server-key.pem --load-ca-certificate ca-cert.pem --load-ca-privkey ca-key.pem --template server.tmpl --outfile server-cert.pem

然后我们把证书文件用移动到Ocserv默认的目录下:

1
2
3
cp server-cert.pem /etc/pki/ocserv/public/
cp server-key.pem /etc/pki/ocserv/private/
cp ca-cert.pem /etc/pki/ocserv/cacerts/

现在编辑ocserv的配置文件(需要改动的地方很多,如果vi不好用就自己用SFTP把这个文件下载到本地用专业的编辑器编辑):

1
vi /etc/ocserv/ocserv.conf

让我们一起来看看需要改哪些地方:

  1. auth也就是验证方式要改为:
1
auth = "plain[passwd=/etc/ocserv/ocpasswd,otp=/etc/ocserv/ocserv.otp]"
  1. 默认的监听端口为443,如果你的服务器上跑着HTTPS的WEB站点,那么443端口肯定是被占用了的,所以如果有需求的话,可以更改下面的值:
1
2
3
\# TCP and UDP port number
tcp-port = 443
udp-port = 443

如图所示:

# When the server has a dynamic DNS address (that may change),  # should set that to true to ask the client to resolve again  # reconnects.  *listen-host-is-dyndns -  - true  # TCP and UDP  tcp-;nzrt = 443  udp-port = 443  rt number

  1. Anyconnect有一个设置连接欢迎信息的功能,也就是你在连接的时候会弹出一个提示框,提示框的内容就可以自行设置,如有需要可以更改下面的值:
1
2
\# A banner to be displayed on clients
banner = "Welcome LALA.IM"

如图所示:

# A banner to be displayed on  clients  banner -  "Welcome L ALA. 1M"'

  1. Anyconnect可以限制最大允许连接的设备数量,如有需要可以更改下面这两个值:

    1
    2
    max-clients = 16
    max-same-clients = 2

如图所示:

Unset  134  136  138  # L Imit the number of  *max-clients -  1ß24  max-clients =  # Limit the number of  clients.  identical  or  set  to  zero for  unlimited .  clients  to zero  (i.e.,  users connecting  # multiple times). IJ  n set  or set  max- same- clients  for unlimited.

  1. 更改服务器证书以及私钥的路径为我们刚才移动的路径:
1
2
server-cert = /etc/pki/ocserv/public/server-cert.pem
server-key = /etc/pki/ocserv/private/server-key.pem

如图所示:

186  188  19E  There may be multiple server- cert and server-key directives,  but each key should correspond to the preceding certificate.  The certificate files will be reloaded when changed allowing for in-place  certificate renewal (they are checked and  a SIGHIJP signal t  o mal n server WI  force  reloaded periodically  reload).  server- cert  server- key  / etc/ /ocserv/ public/ server- cert . pem  / etc/ /ocserv/ pri vat e/ server- key. pem

  1. 更改CA证书的路径为我们刚才移动的路径:
1
ca-cert = /etc/pki/ocserv/cacerts/ca-cert.pem

如图所示:

226  228  # The Certificate Authority that will be used to verify  # client certificates (public keys) if certificate authentication  # is set.  etc/ /ocserv/cacerts/ca- cert . pem  ca-cert = /

  1. 取消如下几个参数的注释(去掉#号就是去掉注释):

    1
    2
    ipv4-network
    ipv4-netmask

如图所示:

422  423  424  425  426  427  428  429  ASE  431  192.168.1.a/24  432  The pool of addresses that leases will be given fnm. If the leases  are given via Radius, or via the explicit-ip? per-user config option then  these network values should contain a network w  ith at least a single  address that will  remain under the full contnl of ocserv (that is  to  i pv4  i pv4  i pv4  be able to assi n  the local part of the tun device address).  - network  - netmask  - network  192.168.1 a  255.255.255.a  Ying the network

  1. 去掉如下参数的注释以及设置DNS服务器地址:

    1
    2
    3
    tunnel-all-dns = true
    dns = 8.8.8.8
    dns = 8.8.4.4

如图所示:

# Whether to tunnel all DNS queries via the VPN.  nute is set.  # when a default  This  is the default  tunnel-all-dns  true  # The advertized DNS server.  # multiple servers.  use multiple lines for  dns  dns  8.8.8.8  8.8.4.4  451

确定你已经修改好上面的内容,然后保存即可。

现在来创建一个VPN用户:

ocpasswd -c /etc/ocserv/ocpasswd lala

盲输两遍密码即可。如果不想让这个用户继续使用了,可以执行下面的命令删除指定的用户:

ocpasswd -c /etc/ocserv/ocpasswd -d lala

现在我们开启机器的IPV4转发功能:

echo 1 > /proc/sys/net/ipv4/ip_forward

然后启动CentOS7的Firewalld防火墙:

systemctl start firewalld.service

放行Anyconnect的端口(我这里之前设置的是默认的443端口,如果你修改了端口,那么这里也要对应):

1
2
firewall-cmd --permanent --zone=public --add-port=443/tcp
firewall-cmd --permanent --zone=public --add-port=443/udp

设置转发:

1
2
firewall-cmd --permanent --add-masquerade
firewall-cmd --permanent --direct --passthrough ipv4 -t nat -A POSTROUTING -o eth0 -j MASQUERADE

注:eth0是你的公网网卡名字,每个机器的名字可能都不一样,自己用ifconfig命令查一下就行了。

重加载,让新的配置生效:

firewall-cmd --reload

现在就可以尝试运行一下Ocserv了:

ocserv -f -d 1

如果一切正常,回显的内容大致如下图所示:

ocsem,• -f -d I  note: setting ' plain' as primary authentication method  note: setting 'file' as supplemental config option  listening (TCP) on ø.ø.ø.ø:443...  listening (TCP) on C:  listening (UDP) on ø.ø.ø.ø:443...  listening (UDP) on C:  ocser•u'C1593): main: initialized ocsem,• ø.12.I  ocser•u'C1594): sec-mod: reading supplemental config from files  ocsetwC1594): sec-mod: sec-mod initialized (socket: /var/lib/ocsetw/ocset".'. sock. ba2befa6)

确定正常后按键盘组合键Ctrl+C退出运行,现在我们就可以直接用systemctl来管理Ocserv的进程。

设置Ocserv开机启动:

systemctl enable ocserv

启动Ocserv:

systemctl start ocserv

配置日志

ExecStart=/bin/sh -c '/usr/sbin/ocserv --pid-file /var/run/ocserv.pid --config /etc/ocserv/ocserv.conf -f -d 5 >> /var/log/agent.log 2>&1'


Ocserv安装(账号+密码+OTP认证方式)
https://johnnysxy.github.io/2023/04/24/Ocserv安装(账号+密码+OTP认证方式)/
作者
Johnny Song
发布于
2023年4月24日
许可协议