ocserv-centos7部署

一键部署脚本:https://www.iaodun.com/faq/technical/3853.html

一 ocserv 已经在 epel 仓库中提供了,所以可以直接通过 yum 安装

yum install epel-release

yum install ocserv

二 创建ssl证书目录并放入证书(我证书名称ssl.pem/ssl.key)

mkdir -p /etc/ocserv/ssl
cd /etc/ocserv/ssl

三 默认情况下,配置文件位于 /etc/ocserv/ocserv.conf,以下的配置字段需要重点关注

cd /etc/ocserv
mv /etc/ocserv/ocserv.conf /etc/ocserv/ocserv.conf_bak


cat << EOF > /etc/ocserv/ocserv.conf
#auth = "pam[gid-min=1000]"
auth = "plain[passwd=/etc/ocserv/ocpasswd]"
socket-file = /var/run/ocserv-socket
pid-file = /var/run/ocserv.pid
use-occtl = true
occtl-socket-file = /var/run/occtl.socket

#指定替代的登录方式,这里使用证书登录作为第二种登录方式
#enable-auth = "certificate"

#证书路径
server-cert = /etc/ocserv/ssl/ssl.pem
server-key = /etc/ocserv/ssl/ssl.key

#记录anyconnect连接断开的日志
connect-script = /etc/ocserv/connect-script
disconnect-script = /etc/ocserv/connect-script

#ca路径
#ca-cert = /etc/ocserv/ca-cert.pem

#从证书中提取用户名的方式,这里提取的是证书中的 CN 字段作为用户名
#cert-user-oid = 2.5.4.3

#最大用户数量
max-clients = 100

#同一个用户最多同时登陆数
max-same-clients = 1

#tcp和udp端口
tcp-port = 443
udp-port = 443

#运行用户和组
run-as-user = ocserv
run-as-group = ocserv

#虚拟设备名称
device = vpns

# 不分配固定ip
#predictable-ips = false
# mtu = 1369

#分配给VPN客户端的IP段
ipv4-network = 10.11.18.0/24

#DNS
#dns = 8.8.8.8
#dns = 8.8.4.4
tunnel-all-dns = false

#select-group = operation_cs
#select-group = coldwallet

banner = "欢迎进入xxxx内部网络"
default-select-group = staff
auto-select-group = false

#组
config-per-user = /etc/ocserv/config-per-user/
config-per-group = /etc/ocserv/config-per-group/
default-group-config = /etc/ocserv/config-per-group/test

#注释掉route的字段,这样表示所有流量都通过 VPN 发送
#no-route = 10.197.0.121/32
restrict-user-to-routes = true
compression = true
no-compress-limit = 256
# The time (in seconds) that a client is allowed to stay connected

session-timeout = 5400

# score达到80就禁用ip, 错误密码10, kdcp 1, conncetion 1
max-ban-score = 1000

# ban重置时间
ban-reset-time = 1200

# 一次认证最长允许时间
auth-timeout = 60

# 失败后, 不能重新登录的时间
min-reauth-time = 1

# 允许的无流量时间
idle-timeout = 1800
mobile-idle-timeout = 1800

# cookie有效期
cookie-timeout = 300
persistent-cookies = true
deny-roaming = false
rekey-time = 28800
rekey-method = ssl
isolate-workers = true

# 服务器状态重置时间86400
server-stats-reset-time = 604800
keepalive = 28800
dpd = 90
mobile-dpd = 1800
tls-priorities = "NORMAL:%SERVER_PRECEDENCE:%COMPAT:-VERS-SSL3.0"
cisco-client-compat = true
EOF

3.1 新建日志目录

参考文档:https://github.com/chendong12/ocserv/blob/master/README.md

新建 connect-script 文件

touch /etc/ocserv/connect-script
chmod +x /etc/ocserv/connect-script

/etc/ocserv/connect-script 文件内容如下

#!/bin/bash
 
export LOGFILE=/etc/ocserv/login.log
 
#echo $USERNAME : $REASON : $DEVICE
case "$REASON" in
  connect)
echo `date` $USERNAME "connected" >> $LOGFILE
echo `date` $REASON $USERNAME $DEVICE $IP_LOCAL $IP_REMOTE $IP_REAL >> $LOGFILE
    ;;
  disconnect)
echo `date` $USERNAME "disconnected" >> $LOGFILE
    ;;
esac
exit 0

四 设置转发和防火墙开启端口443,设置转发

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

systemctl start firewalld.service

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

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

4.1 注意这里-o的eth0为当前网卡名称

firewall-cmd --permanent --add-masquerade

firewall-cmd --permanent --direct --passthrough ipv4 -t nat -A POSTROUTING -o eth0 -j MASQUERADE

4.2 使配置生效

firewall-cmd --reload

4.3 确定没有问题之后,就可以启动 ocserv 了

systemctl enable ocserv && systemctl start ocserv
netstat -tnlp |grep 443

五 设置用户 ocpasswd是存储用户密码信息

touch /etc/ocserv/ocpasswd

ocpasswd -c /etc/ocserv/ocpasswd test

5.1 扩展-删除和禁用-解除禁用用户

ocpasswd -c /etc/ocserv/ocpasswd -l user1 #禁用用户

ocpasswd -c /etc/ocserv/ocpasswd -u user1 #解锁被禁用的用户

ocpasswd -c /etc/ocserv/ocpasswd -d user1 #删除用户

六 occtl常用命令

occtl 是 ocserv 的管理控制工具,可以查看在线用户、ocserv 的状态等

6.1 查看在线用户

occtl show users

6.2查看 ocserv 的状态

occtl show status

`