大漠知秋的加油站

  • 首页
大漠知秋的加油站
你就当我的烂笔头吧
  1. 首页
  2. Linux
  3. 正文

配置 VPN Server

2020年1月2日 707点热度 1人点赞 0条评论

环境

软件 版本
CentOS 7.6
OpenVPN 2.4.8
EasyRSA 3.0.6

  为什么做建立 VPN?

  举个例子(一下都是建立在没有公网 IP 的基础上),公司在全国各地都有分公司或者试验场地,在 A 地点无法访问 B 地点的内部网络,在 B 地点又无法访问 A 地点的内部网络。想做的是在任何地点都可以访问其他地点的内部网络,所以这里就用到了 VPN Server。

安装

  安装 yum 源

yum install -y epel-release
yum update -y

  安装依赖

yum install -y openssl-1.0.2k-19.el7 lzo-2.06-8.el7 pam-1.1.8-22.el7 openssl-devel-1.0.2k-19.el7 lzo-devel-2.06-8.el7 pam-devel-1.1.8-22.el7

  安装 OpenVPN

yum install -y easy-rsa-3.0.6-1.el7 openvpn-2.4.8-1.el7

  确定私有子网,Server 与 Client 的 VPN 通道子网,不要与已有环境的网络冲突即可。默认:10.8.0.0/16

  先清理:

rm -rf /etc/openvpn/easy-rsa
rm -rf /etc/openvpn/server/*

配置证书密钥

# 转移目录
cp -rf /usr/share/easy-rsa/3.0.6/ /etc/openvpn/easy-rsa
cd /etc/openvpn/easy-rsa

# 新建
cat << EOF >> vars
export KEY_COUNTRY="CN"
export KEY_PROVINCE="SH"
export KEY_CITY="MH"
export KEY_ORG="car2p"
export KEY_EMAIL="qsguo@car2p.com"
export KEY_CN=CN
export KEY_NAME=car2p
export KEY_OU=car2p
EOF

# 初始化 pki 目录
./easyrsa init-pki

# 创建 ca 证书,nopass:没有密码
echo "openvpn" | ./easyrsa build-ca nopass

# 创建服务端证书
openvpn | ./easyrsa build-server-full server nopass
# 创建客户端证书
#./easyrsa build-client-full client1 nopass

# 创建 Diffie-Hellman 密钥,会生成dh.pem文件(生成过程比较慢,在此期间不要去中断它),SSL 协商时 Diffie-Hellman 算法需要的 key
./easyrsa gen-dh

# 生成 ta.key 文件(防DDos攻击、UDP淹没等恶意攻击)
openvpn --genkey --secret ta.key

# 服务端证书 Copy 到 /etc/openvpn/server/ 目录
yes | cp -rp pki/ca.crt ta.key pki/dh.pem pki/issued/server.crt pki/private/server.key /etc/openvpn/server/

日志配置

# 日志存放目录
mkdir -p /var/log/openvpn/
# 配置权限
chown -R openvpn:openvpn /var/log/openvpn

针对客户端的用户配置

mkdir -p /etc/openvpn/server/user
# 获取密码检查脚本
wget https://pencil.file.lynchj.com/blog/checkpsw.sh -O /etc/openvpn/server/user/checkpsw.sh
chmod +x /etc/openvpn/server/user/checkpsw.sh

# 此文件用来管理客户端用户信息,一行一个,用户名和密码以空格分开
touch /etc/openvpn/server/user/psw-file && chmod 600 /etc/openvpn/server/user/psw-file
# 静态 IP 配置文件夹
mkdir /etc/openvpn/server/ccd

编辑 OpenVPN 服务端配置

wget https://pencil.file.lynchj.com/blog/openvpn-server.conf -O /etc/openvpn/server/server.conf

# 创建完配置文件后,需要做个配置文件的软连接,因为当前版本的 openvpn systemd 启动文件中读取的是 .service.conf 配置
cd /etc/openvpn/server/ && ln -sf server.conf .service.conf

# 更改为 openvpn 用户
chown -R openvpn:openvpn /etc/openvpn/server/

## 关闭影响服务、打开需要服务
# 防火墙
systemctl disable firewalld
systemctl stop firewalld
# SELinux
setenforce 0
sed -ri '/^[^#]*SELINUX=/s#=.+$#=disabled#' /etc/selinux/config
# 开启路由转发功能
echo "net.ipv4.ip_forward = 1" > /etc/sysctl.conf
sysctl -p

启动服务

systemctl start openvpn-server@server

配置 Client 的用户名和密码

  文件 etc/openvpn/server/user/psw-file,每一行代表了一个用户登录信息,格式为:用户名 + 空格 + 密码,编辑此文件之后无需重启 OpenVPN 服务。

配置 Client 静态 IP

  目录 /etc/openvpn/server/ccd,在其中以 Client 用户名作为文件名,新建文件,并在文件中配置静态 IP 信息,如下:

vim /etc/openvpn/server/ccd/client1
ifconfig-push 10.8.0.5 10.8.0.6

  ifconfig-push 中的每一对 IP 地址表示 虚拟客户端 和 服务器 的 IP 端点,它们必须从连续的 10.8.0.0/30 子网网段中获取,子网掩码为30位:255.255.255.252,也就是说每个子网有4个 IP,下面几个例子:

子网 主机 IP
11111111 11111111 11111111 000000 00 10.8.0.0
01 10.8.0.1
10 10.8.0.2
11 10.8.0.3
11111111 11111111 11111111 001110 00 10.8.0.56
01 10.8.0.57
10 10.8.0.58
11 10.8.0.59
11111111 11111111 11111111 111111 00 10.8.0.252
01 10.8.0.253
10 10.8.0.254
11 10.8.0.255

  每个子网都会损失掉两个 IP,全 0 和全 1。所以每个子网最多有两个 IP 可用。

  官方这样规定是便于与 Windows 客户端和 TAP-Windows 驱动兼容。明确地说,每个端点的 IP 地址对的最后8位字节必须取自下面的集合:

[  1,  2]   [  5,  6]   [  9, 10]   [ 13, 14]   [ 17, 18]
[ 21, 22]   [ 25, 26]   [ 29, 30]   [ 33, 34]   [ 37, 38]
[ 41, 42]   [ 45, 46]   [ 49, 50]   [ 53, 54]   [ 57, 58]
[ 61, 62]   [ 65, 66]   [ 69, 70]   [ 73, 74]   [ 77, 78]
[ 81, 82]   [ 85, 86]   [ 89, 90]   [ 93, 94]   [ 97, 98]
[101,102]   [105,106]   [109,110]   [113,114]   [117,118]
[121,122]   [125,126]   [129,130]   [133,134]   [137,138]
[141,142]   [145,146]   [149,150]   [153,154]   [157,158]
[161,162]   [165,166]   [169,170]   [173,174]   [177,178]
[181,182]   [185,186]   [189,190]   [193,194]   [197,198]
[201,202]   [205,206]   [209,210]   [213,214]   [217,218]
[221,222]   [225,226]   [229,230]   [233,234]   [237,238]
[241,242]   [245,246]   [249,250]   [253,254]
标签: Linux OpenVPN Server
最后更新:2023年8月21日

大漠知秋

唯黄昏而思烛明,唯覆雪始念日暖,唯放手方知情真,今困苦而怀峥嵘,今飘零而涌乡愁,今孑然而徒唏嘘,唏嘘成愁。

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

文章目录
  • 环境
  • 安装
    • 配置证书密钥
    • 日志配置
    • 针对客户端的用户配置
    • 编辑 OpenVPN 服务端配置
    • 启动服务
  • 配置 Client 的用户名和密码
  • 配置 Client 静态 IP

COPYRIGHT © 2023 大漠知秋的加油站. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

豫ICP备16029200号-2