阿里ECS配置openvpn

以阿里云ECS服务器为例,介绍openvpn的配置过程
ECS服务器内网ip为 172.17.182.186

(1)、安装openvpn

安装依赖包
yum install -y lz4-devel lzo-devel pam-devel openssl-devel systemd-devel sqlite-devel kernel-headers

安装openvpn
从github上下载openvpn源代码包并解压后编译安装,最后建立软连接
# wget https://github.com/OpenVPN/openvpn/archive/v2.4.9.tar.gz
# mv v2.4.9.tar.gz openvpn-2.4.9.tar.gz
# tar xf openvpn-2.4.9.tar.gz
# cd openvpn-2.4.9/
# yum install autoconf automake libtool
# autoreconf -i -v -f
# ./configure --prefix=/usr/local/openvpn --enable-lzo --enable-lz4 --enable-crypto --enable-server --enable-plugins --enable-port-share --enable-iproute2 --enable-pf --enable-plugin-auth-pam --enable-pam-dlopen --enable-systemd
# make && make install
# ln -s /usr/local/openvpn/sbin/openvpn /usr/local/sbin/openvpn

配置文件修改
# vim /usr/local/openvpn/lib/systemd/system/openvpn-server@.service
### 找到 ExecStart 这行,改为如下
ExecStart=/usr/local/openvpn/sbin/openvpn --config server.conf

配置系统服务,并开机自启动
# cp -a /usr/local/openvpn/lib/systemd/system/openvpn-server@.service /usr/lib/systemd/system/openvpn.service
# systemctl enable openvpn.service

(2)、生成证书
easy-rsa下载与配置修改
# cd /usr/local/openvpn/
# wget https://github.com/OpenVPN/easy-rsa/archive/v3.0.7.tar.gz
# mv v3.0.7.tar.gz easy-rsa-3.0.7.tar.gz
# tar xf easy-rsa-3.0.7.tar.gz
根据easy-rsa-3.0.7/easyrsa3/vars.example文件生成全局配置文件vars
# cd easy-rsa-3.0.7/easyrsa3
# cp -a vars.example vars
修改vars文件,根据需要去掉注释,并修改对应值;或者直接在文件末尾追加如下信息:
# 国家
set_var EASYRSA_REQ_COUNTRY "CN"
# 省
set_var EASYRSA_REQ_PROVINCE "HeNan"
# 城市
set_var EASYRSA_REQ_CITY "ZhengZhou"
# 组织
set_var EASYRSA_REQ_ORG "guoquan"
# 邮箱
set_var EASYRSA_REQ_EMAIL "admin@guoquan.cn"
# 拥有者
set_var EASYRSA_REQ_OU "li"

# 长度
set_var EASYRSA_KEY_SIZE 2048
# 算法
set_var EASYRSA_ALGO rsa

# CA证书过期时间,单位天
set_var EASYRSA_CA_EXPIRE 3650
# 签发证书的有效期是多少天,单位天
set_var EASYRSA_CERT_EXPIRE 3650

生成服务端和客户端证书

初始化与创建CA根证书
# ./easyrsa init-pki
初始化,会在当前目录创建PKI目录,用于存储一些中间变量及最终生成的证书

# ./easyrsa build-ca
在这部分需要输入PEM密码 PEM pass phrase,输入两次,此密码必须记住,不然以后不能为证书签名。
还需要输入common name 通用名,如:openvpen,这个你自己随便设置个独一无二的。

生成服务端证书
# ./easyrsa build-server-full server nopass
为服务端生成证书对并在本地签名。nopass参数生成一个无密码的证书;在此过程中会让你确认ca密码

# ./easyrsa gen-dh
创建Diffie-Hellman,确保key穿越不安全网络的命令,时间会有点长,耐心等待

生成客户端证书
生成多个客户端证书

# ./easyrsa build-client-full client nopass # 无密码,实际应用中不推荐,客户端有密码可提高安全性
# ./easyrsa build-client-full zhangsan # 让你输入密码,后续VPN连接时会使用
# ./easyrsa build-client-full lisi # 让你输入密码,后续VPN连接时会使用
# ./easyrsa build-client-full wangwu # 让你输入密码,后续VPN连接时会使用
为客户端生成证书对并在本地签名。nopass参数生成一个无密码的证书;在此过程中都会让你确认ca密码

生成ta.key文件
为了提高安全性,生成ta.key
# openvpn --genkey --secret ta.key
ta.key的作用:
加强认证方式,防攻击。如果配置文件中启用此项(默认是启用的),就需要执行上述命令,并把ta.key放到/etc/openvpn/server目录。
配置文件中服务端第二个参数为0,同时客户端也要有此文件,且client.conf中此指令的第二个参数需要为1。
服务端有该配置,那么客户端也必须要有。

整理服务端证书
# mkdir -p /etc/openvpn/server/
# cp -a pki/ca.crt /etc/openvpn/server/
# cp -a pki/private/server.key /etc/openvpn/server/
# cp -a pki/issued/server.crt /etc/openvpn/server/
# cp -a pki/dh.pem /etc/openvpn/server/
# cp -a /usr/local/openvpn/easy-rsa-3.0.7/easyrsa3/ta.key /etc/openvpn/server/

(3)、创建服务端配置文件

# cat /etc/openvpn/server/server.conf
local 0.0.0.0
port 30011
proto udp
dev tun
ca /etc/openvpn/server/ca.crt
cert /etc/openvpn/server/server.crt
key /etc/openvpn/server/server.key
dh /etc/openvpn/server/dh.pem
topology subnet
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "route 172.17.176.0 255.255.240.0"
push "route 10.8.0.0 255.255.255.0"
client-to-client
;client-config-dir ccd
route 172.17.176.0 255.255.240.0
;duplicate-cn
keepalive 10 120
tls-auth /etc/openvpn/server/ta.key 0
cipher AES-256-CBC
compress lz4-v2
push "compress lz4-v2"
;comp-lzo
max-clients 1000
user nobody
group nobody
persist-key
persist-tun
status openvpn-status.log
log /var/log/openvpn.log
verb 3

启动openvpn服务并查看进程与端口
# systemctl start openvpn.service

# ps -ef | grep 'open'
root 903 3018 0 16:24 pts/1 00:00:00 grep --color=auto open
nobody 31689 1 0 16:00 ? 00:00:00 /usr/local/openvpn/sbin/openvpn --config server.conf

# netstat -tunlp |grep 31689
udp 0 0 0.0.0.0:30011 0.0.0.0:* 31689/openvpn

通过ifconfig命令,可以看到多出来一个tun0网卡,网卡ip为 10.8.0.1

(4)、路由转发、iptabels配置

开启openvpn服务器的路由转发功能
# grep 'net.ipv4.ip_forward = 1' /etc/sysctl.conf || echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
# sysctl -p

iptables配置
# iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
# iptables-save > /etc/sysconfig/iptables
# iptables -L -n -t nat
# service iptables save

查看openvpn服务器路由信息:
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.17.191.253 0.0.0.0 UG 0 0 0 eth0
10.8.0.0 0.0.0.0 255.255.255.0 U 0 0 0 tun0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
172.17.176.0 0.0.0.0 255.255.240.0 U 0 0 0 eth0

(5)、Windows客户端配置与访问

1、openvpn客户端安装 , 下载并安装openvpn-install-2.4.9-I601-Win10.rar ,安装完毕后会在「网络连接」中会多出一个网络适配器“TAP-Windows Adapter V9”。

2、客户端client用户配置文件,文件名 windows为client.ovpn,Linux为client.conf
在openvpn的安装路径的config目录下创建客vpn账户文件夹,其内容包括:
ca.crt 、 ta.key 、用户名.crt 、用户名.key、用户名.ovpn

ca.crt 、ta.key 文件可以从/etc/openvpn/server下载
用户名.crt 文件可以从/usr/local/openvpn/easy-rsa-3.0.7/easyrsa3/pki/issued 下载
用户名.key 文件可以从/usr/local/openvpn/easy-rsa-3.0.7/easyrsa3/pki/private下载

openvpn client 配置文件可参照 /root/openvpn-2.4.9/sample/sample-config-files/client.conf

例:
zhangsan.ovpn内容
client
dev tun
proto udp
remote 39.107.252.78 30011
resolv-retry infinite
nobind
;user nobody
;group nobody
persist-key
persist-tun
ca ca.crt
cert zhangsan.crt
key zhangsan.key
remote-cert-tls server
tls-auth ta.key 1
cipher AES-256-CBC
compress lz4-v2
verb 3
;mute 20

客户端测试:
测试效果: 能连接上openvpn, 能ping通openvpn服务器的内网ip 172.17.182.186 , 能ping同一个内网下的其他服务器ip 172.17.182.185。
阿里ECS配置openvpn
阿里ECS配置openvpn
阿里ECS配置openvpn

相关新闻

联系我们

全国服务热线

400-033-9553

电子邮件:admin@example.com
工作时间:09:00-17:00 周一至周五

在线客服
关注微信
关注微信
分享本页
返回顶部