服务器私网 yum + openvpn+mysql认证安装

安装openvpn 和easy-rsa 

yum install -y openvpn easy-rsa

配置 easy-rsa

cd /etc/openvpn/easy-rsa/3
# 查找 vars.example 文件并拷贝
find / -type f -name "vars.example"
cp -v /usr/share/doc/easy-rsa-3.0.8/vars.example vars

生成 CA 证书

cd /etc/openvpn/easy-rsa/3

./easyrsa init-pki

# nopass指CA 不使⽤密码 直接回车即可
./easyrsa build-ca nopass

生成配置服务端证书


./easyrsa gen-req server nopass
# 交互处回车

./easyrsa sign server server
#输入yes

# ⽣成 Diffie-Hellman ⽂件

./easyrsa gen-dh

#整理文件方便后期维护
cp -v pki/dh.pem pki/ca.crt pki/issued/server.crt pki/private/server.key /etc/openvpn/server

此处代码可忽略 因采用账号密码的方式则不需要生成客户端证书(需要客户端证书自取)

./esayrsa gen-req client nopass
#交互处回车

./easyrsa sign-req client client
#交互处yes client 可更换名称

创建 TLS 认证密钥(可选)

openvpn --genkey --secret /etc/openvpn/ta.key

openvpn服务运行配置文件

cat  /etc/openvpn/server/server.conf
# 不存在则新增文件如下内容

local 0.0.0.0
port 16800
# 可更换为udp
proto tcp
dev tun
ca /etc/openvpn/server/ca.crt
cert /etc/openvpn/server/server.crt
key /etc/openvpn/server/server.key # This file should be kept secret
dh /etc/openvpn/server/dh.pem
server 10.168.0.0 255.255.0.0 # openvpn client 分配的 IP 地址范围
ifconfig-pool-persist ipp.txt
# 如没有内网服务器 需要通过vpn 访问则注释下面一行
push "route 192.27.0.0 255.255.0.0" # 需要通过 vpn 可访问的内⽹⽹段
keepalive 10 120
cipher AES-256-CBC
max-clients 300
user root
group root
persist-key
persist-tun
status openvpn-status.log
log openvpn.log
log-append openvpn.log
verb 3
client-cert-not-required
verify-client-cert none
username-as-common-name
# 连接vpn 客户端想互通需要下面一行
client-to-client
# 以下暂不使用mysql 数据库账号密码认证可注释下面一行
plugin /etc/openvpn/openvpn-plugin-auth-pam.so openvpn_mysql

禁用 Centos7 默认的 firewalld

systemctl stop firewalld
systemctl mask firewalld

禁用 SELinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled' /etc/selinux/config
# 或直接修改文件(/etc/selinux/config)中的属性值

#立即生效
sysctl -p && setenforce 0

设置防火墙  iptables

# 16800 为配置文件的服务端口
iptables -A INPUT -p tcp --dport 16800 -j ACCEPT
# 10.168.0.0/16 配置文件中配置ip池 代表 openvpn client 可分配地址
$iptables -t nat -A POSTROUTING -s 10.168.0.0/16 -o eth0 -j MASQUERADE 
$iptables-save > /etc/sysconfig/iptables

百科: 0/16 的意思

如果使用mysql 则注释配置文件中的配置后启动openvpn使用生成的客户端证书即可连接

安装mysql 采用 mariadb 

yum install mariadb-server

systemctl enable mariadb

systemctl start mariadb

mysql_secure_installation

# 登录数据库 根据提示重置密码删除多余的数据库等
mysql -u root -p 

# 进入mysql 创建数据库
CREATE DATABASE IF NOT EXISTS openvpn DEFAULT CHARSET utf8;
# 创建表结构
create table vpnuser(username varchar(16)not null,password char(41)not null,email varchar(50)null,active tinyint not null default 1,created_at TIMESTAMP NULL,updated_at TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,remark text,primary key(username));

# 创建openvpn 独有mysql 账号,记得修改执行命令中的密码
grant all on openvpn.* to openvpn@'localhost' identified by '密码记得修改';

# 插入一个mysql 执行用户
 insert into vpnuser (username,password,created_at) values ('登录用户名',password('登录用户密码'),CURRENT_TIMESTAMP);

安装pam_mysql包

rpm -ivh http://repo.iotti.biz/CentOS/7/x86_64/pam_mysql-0.8.1-0.22.el7.lux.x86_64.rpm

# pam 认证文件

cat > /etc/pam.d/openvpn_mysql << EOF
auth sufficient pam_mysql.so user=openvpn passwd=该账号数据库连接密码 host=localhost db=openvpn table=vpnuser usercolumn=username passwdcolumn=password [where=vpnuser.active=1] sqllog=0 crypt=2
account required pam_mysql.so user=openvpn passwd=该账号数据库连接密码 host=localhost db=openvpn table=vpnuser usercolumn=username passwdcolumn=password [where=vpnuser.active=1] sqllog=0 crypt=2
EOF

使用cyrus-sasl工具测试pam认证

yum install cyrus-sasl

systemctl start saslauthd

testsaslauthd -u 数据库登录账号 -p 数据库登录密码 -s openvpn_mysql

注: 配置文件中的一行开启

#使用Mysql认证
plugin /usr/lib64/openvpn/plugins/openvpn-plugin-auth-pam.so "openvpn_mysql"

还需安装openvpn-plugin-auth-pam.so 插件

#下载源码包 可注意当前你所在目录,免得随意乱放
linux根目录运行 wget http://swupdate.openvpn.org/community/releases/openvpn-2.4.12.tar.gz
# 解压
tar xf openvpn-2.4.12.tar.gz
cd openvpn-2.4.12/
# 安装并进行编译 
yum -y install openssl openssl-devel lzo-devel pam-devel
./configure
make
# 拷贝对应文件至openvpn
cp src/plugins/auth-pam/.libs/openvpn-plugin-auth-pam.so  /etc/openvpn/

# 启动openvpn 如果你全部执行咯;我也不会说什么

systemctl start openvpn-server@server.service
systemctl restart openvpn-server@server.service
systemctl status openvpn-server@server.service

生成客户端ovpn 文件 查看先前生成的ca 证书


# 新建文件client.ovpn 文件名随意

client
# 16800 全文统一修改,此外openvpn 统一服务端口
remote 公网ip 16800
# 根据服务端 tcp 还是udp配置
proto tcp
dev tun
route-delay 2
route-method exe
script-security 3
key-direction 1
verb 3
auth-user-pass
<ca>
-----BEGIN CERTIFICATE-----
# 删掉注释替换为 服务端前期生成的ca相对于区块内容 cat  /etc/openvpn/server/ca.crt
-----END CERTIFICATE-----
</ca>

大功告成 使用openvpn客户端加载文件 连接输入数据库录入的 账号,密码即可有

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值