[Linux安全运维] OpenVPN部署

OpenVPN部署

1. 前期准备

1.1 安装epel仓库源

[root@localhost ~]# yum -y install epel-release

2. OpenVPN服务端搭建

2.1 安装openvpn

[root@localhost ~]# yum -y install openvpn

2.2 安装easy-rsa

使用wegt命令获取压缩包:

[root@localhost ~]# wget https://gitcode.net/mirrors/OpenVPN/easy-rsa/-/archive/master/easy-rsa-master.tar.gz

解压缩:

[root@localhost ~]# mkdir openvpn
[root@localhost  openvpn]# tar -xzvf easy-rsa-master.tar.gz
[root@localhost  openvpn]# mv easy-rsa-master easy-rsa

在解压缩的使用遇见问题:

[root@localhost ~]# tar -xzvf easy-rsa-master.tar.gz

gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now

解决这个问题可以在github上下载最新的easy-rsa.zip压缩包再进行解压

下载网址: https://github.com/OpenVPN/easy-rsa

在这里插入图片描述

[root@localhost ~]# unzip easy-rsa-master.zip
[root@localhost ~]# mkdir openvpn/
[root@localhost ~]# mv easy-rsa-master openvpn/
[root@localhost ~]# cd openvpn/
[root@localhost openvpn]# mv easy-rsa-master easy-rsa

2.3 配置openvpn

创建/etc/openvpn/目录,将easy-rsa放进去

[root@localhost  ~]# mkdir -p /etc/openvpn/
[root@localhost  ~]# cd openvpn/
[root@localhost  openvpn]# cp -Rp easy-rsa /etc/openvpn/

配置easyrsa3/中的vars文件

[root@localhost ~]# cd /etc/openvpn/easy-rsa/easyrsa3/
[root@localhost  easyrsa3]# cp vars.example vars
[root@localhost  easy-rsa3]# vim vars

添加:


set_var EASYRSA_REQ_COUNTRY     "CN"
set_var EASYRSA_REQ_PROVINCE    "Shanghai"
set_var EASYRSA_REQ_CITY        "Shanghai"
set_var EASYRSA_REQ_ORG         "username"(自己选)
set_var EASYRSA_REQ_EMAIL       "1111@qq.com"(自己选)
set_var EASYRSA_REQ_OU          "My OpenVPN"

在这里插入图片描述

2.4 创建服务端证书等

初始化程序:

[root@localhost  ~]# cd /etc/openvpn/easy-rsa/easyrsa3/
[root@localhost  easyrsa3]# ./easyrsa init-pki

在这里插入图片描述

创建根证书:

[root@localhost  easyrsa3]# ./easyrsa build-ca

在这里插入图片描述

这里需要输入根证书密码,如果不想输入密码,可以在创建根证书的命令中加nopass,之后的创建命令也是一样,就不再过多赘述了。

例:

[root@localhost  easyrsa3]# ./easyrsa build-ca nopass

在创建根证书的时候,还需要输入一个Common Name用户名,我这里使用的是Dai

创建服务端证书:

[root@localhost  easyrsa3]# ./easyrsa gen-req server nopass
# 我这里就没有设置密码了,如果需要设置去除nopass,步骤和上面一样。

在这里插入图片描述

同样的,在创建服务端证书的时候也需要输入一个Common Name用户名,注意与根证书用户名不一样。

签约服务端证书:

[root@localhost  easyrsa3]# ./easyrsa sign server server

创建过程中需要提供创建根证书的密码

在这里插入图片描述

创建DH(Diffie-Hellman):

[root@localhost  easyrsa3]# ./easyrsa gen-dh

在这里插入图片描述

2.5 创建客户端证书等

2.5.1 创建client/目录,拷贝easy-rsa文件

[root@localhost  ~]# mkdir client
[root@localhost  ~]# cp /etc/openvpn/easy-rsa client -Rp
[root@localhost  ~]# cd client/easy-rsa/easyrsa3/

2.5.2 初始化程序:

[root@localhost  easyrsa3]# ./easyrsa init-pki

在这里插入图片描述

2.5.3 创建客户端证书:

[root@localhost  easyrsa3]# ./easyrsa gen-req client

这里的密码是客户端登录连接时候需要输入的密码,所以需要记录下来

在这里插入图片描述

我这里在二次输入密码的时候输入不一致所以报错了,正常情况下不会出现failure

2.5.4 将用户req导入

[root@localhost  easyrsa3]# cd /etc/openvpn/easy-rsa/easyrsa3/
[root@localhost easyrsa3]# ./easyrsa import-req /root/client/easy-rsa/easyrsa3/pki/reqs/client.req client_username

在这里插入图片描述

2.5.5 签约证书

[root@localhost  easyrsa3]# ./easyrsa sign client client_username

签约证书需要输入根证书的密码,与签约服务端证书时的过程一样。

在这里插入图片描述

2.6 整理相关证书文件

2.6.1 将服务端所需的必要文件放到/etc/openvpn/中

[root@localhost ~]# cp /etc/openvpn/easy-rsa/easyrsa3/pki/ca.crt /etc/openvpn/
[root@localhost ~]# cp /etc/openvpn/easy-rsa/easyrsa3/pki/private/server.key /etc/openvpn/
[root@localhost ~]# cp /etc/openvpn/easy-rsa/easyrsa3/pki/issued/server.crt /etc/openvpn/
[root@localhost ~]# cp /etc/openvpn/easy-rsa/easyrsa3/pki/dh.pem /etc/openvpn/

在这里插入图片描述

2.6.2 将客户端所需的必要文件放到/root/client/中

[root@localhost ~]# cp /etc/openvpn/easy-rsa/easyrsa3/pki/ca.crt /root/client/
[root@localhost issued]# cp /etc/openvpn/easy-rsa/easyrsa3/pki/issued/client_username.crt /root/client/
[root@localhost  ~]# cp /root/client/easy-rsa/easyrsa3/pki/private/client.key /root/client

在这里插入图片描述

2.7 修改配置文件

2.7.1 查看文件

[root@localhost  ~]# rpm -ql openvpn |grep server.conf

在这里插入图片描述

2.7.2 根据这个拷贝server.conf配置文件到/etc/openvpn/

[root@localhost issued]# cp /usr/share/doc/openvpn-2.4.12/sample/sample-config-files/server.conf /etc/openvpn/

2.7.3 修改配置文件

[root@localhost  ~]# vim /etc/openvpn/server.conf
local 0.0.0.0
port 1194
proto tcp
dev tun
ca /etc/openvpn/ca.crt
cert /etc/openvpn/server.crt
key /etc/openvpn/server.key  
dh /etc/openvpn/dh.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 8.8.8.8"
client-to-client
keepalive 10 120
cipher AES-256-GCM
comp-lzo
max-clients 100
user openvpn
group openvpn
persist-key
persist-tun
status /var/log/openvpn/openvpn-status.log
log         /var/log/openvpn/openvpn.log
verb 3

在这里插入图片描述

2.7.4 修改配置文件检查脚本

[root@localhost ~]# vim /etc/openvpn/checkpw.sh

#!/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/pw-file"
LOG_FILE="/var/log/openvpn/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 

2.7.5 编写密码文件

[root@localhost ~]# vim /etc/openvpn/pw-file
user1 passwd1
user2 passwd2

2.7.6 配置权限

在日志文件/var/log/中创建一个openvpn/

[root@localhost  ~]# mkdir /var/log/openvpn

分配权限:

[root@localhost  ~]# chown -R openvpn.openvpn /var/log/openvpn/
[root@localhost  ~]# chown -R openvpn.openvpn /etc/openvpn/*

2.7 iptables过滤

[root@localhost  ~]# iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
[root@localhost  ~]# iptables -A FORWARD -i tun0 -o eth0 -j ACCEPT
[root@localhost  ~]# iptables -A FORWARD -i eth0 -o tun0 -j ACCEPT

2.8 开启路由转发

[root@localhost  ~]# vim /etc/sysctl.conf 
net.ipv4.ip_forward = 1
[root@localhost  ~]# sysctl -p

2.9 开启openvpn服务

[root@localhost  ~]# openvpn /etc/openvpn/server.conf &
 
[root@localhost ~]# netstat -ntlp

在这里插入图片描述

3. 客户端连接

3.1 创建client.ovpn文件

client
dev tun
proto udp
remote 10.8.0.7 1194    ## openvpn服务端IP
resolv-retry infinite
nobind
## 指定ca、cert、key的路径
ca ca.crt
cert client.crt
key client.key
verb 3
persist-key
comp-lzo

在这里插入图片描述

3.2 连接openvpn

  • 11
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值