目录
tls-auth ta.key 0 #访止DoS等攻击的安全增强配置,
VPN 介绍
一些组织需要实现跨地域的互相通信,比如:北京总公司和上海分公司之间进行网络互通,一般通过下面两 种方式实现
专用网:
VPN:VPN网关通过对数据包的加密 和数据包目标地址的转换实现远程访问。
安装vpn
yum install openvpn easy-rsa -y
#easy-rsa:是一个证书管理工具,通常与 OpenVPN 一起使用。它简化了证书和密钥的生成、管理和分发过程。通过 easy-rsa,你可以轻松地创建和管理用于加密通信的证书和密钥。
准备相关配置文件
阿里云创建3台,安装nginx 配置随机路由
[root@rocky8 ~]#tree /etc/openvpn/
/etc/openvpn/
├── client (客户端信息)
└── server (服务器信息)
#准备证书颁发相关文件 #用里面的工具创建证书的相关内容
cp -r /usr/share/easy-rsa/3/ /etc/openvpn/easy-rsa
#准备颁发证书相关变量的配置文件 #各种证书的有效信息 ca证书 服务器证书 客户端证书
cp /usr/share/doc/easy-rsa/vars.example /etc/openvpn/easy-rsa/vars
root@linux001 openvpn]# tree /etc/openvpn
/etc/openvpn
├── client
├── easy-rsa
│ ├── easyrsa
│ ├── openssl-easyrsa.cnf
│ ├── vars (读取文件证书有效期就是从这里来)
│ └── x509-types
│ ├── ca
│ ├── client
│ ├── code-signing
│ ├── COMMON
│ ├── kdc
│ ├── server
│ └── serverClient
└── server
#建议修改给CA和OpenVPN服务器颁发的证书的有效期,可适当加长
vim /etc/openvpn/easy-rsa/vars
#CA的证书默认有效期为10年,可以适当延长,比如:36500天
#set_var EASYRSA_CA_EXPIRE 3650
set_var EASYRSA_CA_EXPIRE 36500
#服务器证书默为为825天,可适当加长,比如:3650天
#set_var EASYRSA_CERT_EXPIRE 825
set_var EASYRSA_CERT_EXPIRE 3650
创建 CA 机构证书环境
始化数据,在当前目录下生成pki目录及相关文件
#[root@centos8 easy-rsa]#./easyrsa init-pki
#创建 CA 机构证书环境 私钥,自签名证书 nopass不设置密码
[root@CentOS easy-rsa]# ./easyrsa build-ca nopass
openssl x509 -in pki/ca.crt -noout -text 查看证书内容
sz pki/ca.crt 在win上查看
准备服务端证书环境
#创建服务器证书申请文件,其中server是文件前缀(文件名字) #gen-req生成证书申请
./easyrsa gen-req openvp-server nopass #(生成证书私钥生证书申请文件,不要密码)
[root@linux001 easy-rsa]# cat /etc/openvpn/easy-rsa/pki/dh.pem #一种秘钥交换算法
颁发服务端证书
#第一个server表示证书的类型,第二个server表示请求文件名的前缀
[root@CentOS easy-rsa]# ./easyrsa sign server openvp-server #yes
[root@CentOS easy-rsa]# tree
.
├── easyrsa
├── openssl-easyrsa.cnf
├── pki
│ ├── ca.crt #ca证书
│ ├── certs_by_serial
│ │ └── FB02464F06EF028C7A256857F6422C89.pem
│ ├── index.txt
│ ├── index.txt.attr
│ ├── index.txt.attr.old
│ ├── index.txt.old
│ ├── issued
│ │ └── openvp-server.crt #服务器证书
│ ├── openssl-easyrsa.cnf
│ ├── private
│ │ ├── ca.key
│ │ └── openvp-server.key
│ ├── renewed
│ │ ├── certs_by_serial
│ │ ├── private_by_serial
│ │ └── reqs_by_serial
│ ├── reqs
│ │ └── openvp-server.req
│ ├── revoked
│ │ ├── certs_by_serial
│ │ ├── private_by_serial
│ │ └── reqs_by_serial
│ ├── safessl-easyrsa.cnf
│ ├── serial
│ └── serial.old
├── vars
└── x509-types
├── ca
├── client
├── code-signing
├── COMMON
├── email
├── kdc
├── server
└── serverClient
14 directories, 25 files
查看颁发过的证书
[root@CentOS easy-rsa]# cat pki/index.txt
V 260312070219Z FB02464F06EF028C7A256857F6422C89 unknown /CN=openvp-server
创建 Diffie-Hellman 密钥
- 它是一种建立秘钥的方法,而不是加密方法,所以秘钥必须和其他一种加密算法结合使用。这种密钥交换技术的目的在于使两个用户安全地交换一个密钥,用此密钥做为对称密钥来加密后续的报文传输
#方法1
[root@centos8 easy-rsa]#./easyrsa gen-dh
发客户端证书
修改这里复用一个文件客户端证书不要太长,要考虑到离职率
vim /etc/openvpn/easy-rsa/vars
#set_var EASYRSA_CERT_EXPIRE 825
set_var EASYRSA_CERT_EXPIRE 100
申请舒宏的证书
./easyrsa gen-req shuhong nopass
颁发舒宏的证书
./easyrsa sign client shuhong
复制到空文件调的时候 好调
cp /etc/openvpn/easy-rsa/pki/ca.crt /etc/openvpn/server/ #根证书,随系统安装
cp /etc/openvpn/easy-rsa/pki/issued/openvp-server.crt /etc/openvpn/server/ #服务器 证书
cp /etc/openvpn/easy-rsa/pki/private/openvp-server.key /etc/openvpn/server #服务器的私钥
cp /etc/openvpn/easy-rsa/pki/dh.pem /etc/openvpn/server #加密
将客户端私钥与证书相关文件复制到服务器相关的目录
建议在cilent下个人简个人的文件,方便管理
cd /etc/openvpn/client
mkdir shuhong
[root@linux001 easy-rsa]# find -name "shuhong*"
./pki/private/shuhong.key
./pki/reqs/shuhong.req
./pki/issued/shuhong.crt
[root@linux001 easy-rsa]# find -name "shuhong*" -exec cp {} /etc/openvpn/client/shuhong/ \;
[root@linux001 client]# rm -f wangxiaochun/shuhong.req (证书申请文件没用)
也需要浏览器证书
cp server/ca.crt client/shuhong/
配置 OpenVPN 服务器并启动服务
#生成服务器配置文件
cp /usr/share/doc/openvpn/sample/sample-config-files/server.conf /etc/openvpn/
#vim /etc/openvpn/server.conf
查看是否是10.8.网段
修改服务器端配置文件
#vim /etc/openvpn/server.conf
port 1194
proto tcp
dev tun
ca /etc/openvpn/server/ca.crt
cert /etc/openvpn/server/openvp-server.crt
key /etc/openvpn/server/ server.key
dh /etc/openvpn/server/dh.pem
server 10.8.0.0 255.255.255.0
push "route 172.30.0.0 255.255.255.0"
keepalive 10 120
cipher AES-256-CBC
compress lz4-v2
push "compress lz4-v2"
max-clients 2048
user openvpn
group openvpn
status /var/log/openvpn/openvpn-status.log
log-append /var/log/openvpn/openvpn.log
verb 3
mute 20
准备服务器日志相关目录
#安装包时,自动创建相关用户和组openvpn
[root@centos8 ~]#getent passwd openvpn
[root@centos8 ~]#mkdir /var/log/openvpn 需要默认往这里写文件,但是里面没有这个文件
[root@centos8 ~]#chown openvpn.openvpn /var/log/openvpn 给其权限
[root@centos8 ~]#ll -d /var/log/openvpn
drwxr-xr-x 2 openvpn openvpn 6 Aug 3 23:07 /var/log/openvpn
启动 OpenVPN 服务
准备 OpenVPN 服务的service文件
自带文件起不来,有问题
root@linux001 ~]# cat /usr/lib/systemd/system/openvpn-server@.service [Unit]
Description=OpenVPN Robust And Highly Flexible Tunneling Application On %I
After=network.target
[Service]
Type=notify
PrivateTmp=true
ExecStart=/usr/sbin/openvpn --cd /etc/openvpn/ --config %i.conf
[Install]
WantedBy=multi-user.target#启动OpenVPN服务,注意service名称和文件名不一致
[root@centos8 openvpn]#systemctl daemon-reload
[root@centos8 openvpn]#systemctl enable --now openvpn@serve[root@linux001 ~]# systemctl status openvpn@server.service 看状态
[root@linux001 ~]# cat /var/log/openvpn/openvpn.log 建立连接,
生成客户端用户的配置文件
名字一直这样好修改,前面是为了容易识别
- mv wangxiaochun.key client.key
- mv wangxiaochun.crt client.crt
vim /etc/openvpn/client/wangxiaochun/client.ovpn
grep '^[[:alpha:]].*' /usr/share/doc/openvpn/sample/sample-config-files/client.conf > /etc/openvpn/client/wangxiaochun/client.ovpn
client #指明客户端
dev tun #指定和服务端一致的接口类型
proto udp #指定和服务端一致的协议类型
remote openvpn.shuhong.com 1194 #服务器端的ip或域名 FQDN及端口
resolv-retry infinite #指定服务器端FQDN而非IP时,当客户端重新连接后会重新解FQDN对应的IP
nobind #客户端不绑定监听端口,随机打开端口连接到服务端的端口
persist-key
persist-tun
ca ca.crt
cert client.crt
key client.key
remote-cert-tls server #使用服务器证书校验方式
tls-auth ta.key 1 #安全加强
cipher AES-256-CBC
verb 3
compress lz4-v2 #此项在OpenVPN2.4.X版本使用,需要和服务器端保持一致,如不指定,默认使用comp-lz压缩
配置部署 Windows 的 OpenVPN 客户端
官方客户端下载地址:
https://openvpn.net/community-downloads
安装好后在这里面放配置的文件
/etc/openvpn/client/shuhong 这个文件里面的直接拉过来放在目录下面
他是服务器不是路由器,不开启ip_forward就不会转发
vim /etc/sysctl.conf 下加上 net.ipv4.ip_forward = 1
route add default gw 172.30.0.1 把两个客户端网管指向0.1 这样如果不通的 需要加IP转换
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 ! -d 10.8.0.0/24 -j SNAT --to-source 172.30.0.1(ip转换)
echo 'iptables -t nat -A POSTROUTING -s 10.8.0.0/24 ! -d10.8.0.0/24 -j SNAT --to-source 172.30.0.1' >> /etc/rc.d/rc.local (永久)
- vim /etc/openvpn/server.conf 在这里面加一行duplicate-cn 就可以多人一起链
- systemctl status openvpn@server.service 启动
tls-auth ta.key 0 #访止DoS等攻击的安全增强配置,
服务器和每个客户端都需要拥有此密钥文件。第二个参数在服务器端为0,客户端为1
openvpn --genkey --secret /etc/openvpn/server/ta.key #生成配置文件
[root@centos8 ~]#vim /etc/openvpn/server.conf
tls-auth /etc/openvpn/server/ta.key 0 #客户端为1,服务器端为0 且讲起考到目录里去
systemctl restart openvpn@server.service #重启
修改客户端配置文件clent.ovpn,添加一行 tls-auth ta.key 1 #客户端为1,服务器端为0
文件加密,密钥加密
设置客户端的私钥密码增强安全性
- zip -e /root/mammam.zip *(文件加密)
创建新用户对应的有密码的私钥和证书申请
- ./easyrsa gen-req manman #在这里设置密码。步骤和上面一样
账户证书管理 吊销
root@linux001 ~]# cat /etc/openvpn/easy-rsa/pki/index.txt(查看颁发的证书)
cd /etc/openvpn/easy-rsa/
./easyrsa revoke wang (证书吊销)
#每次吊销证书后都需要更新证书吊销列表文件,并且需要重启OpenVPN服务
[root@centos8 easy-rsa]#./easyrsa gen-crl
将吊销列表文件发布
#第一次吊销证时需要编辑配置文件调用吊销证书的文件,后续吊销无需此步
[root@openvpn-server ~]# vim /etc/openvpn/server.conf
crl-verify /etc/openvpn/easy-rsa/pki/crl.pem
#每次吊销证书后,都需要重新启动才能生效
[root@centos8 ~]#systemctl restart openvpn@server.service
实现用户密码认证
基于证书验证的基础上再加上用户名密码验证可以实现更高的安全性
修改服务端配置
官方脚本下载
http://openvpn.se/files/other/checkpsw.sh
[root@centos8 ~]# vim /etc/openvpn/server.conf
# 添加三行,实现服务端支持密码认证方式 (需要给路径文件加执行权限)
script-security 3 # 允许使用自定义脚本
auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env #指定自定义脚本路径
username-as-common-name #开启用户密码验证chmod +x /etc/openvpn/checkpsw.sh #加执行权限
创建用户密码文件
# 创建用户和密码认证文件,每行是一个用户和密码 (两个账号公用一个证书)
[root@centos8 ~]# cat > /etc/openvpn/psw-file <<EOF
wang 123456
test 654321
EOF
[root@centos8 ~]# systemctl restart openvpn@server
修改客户端配置
修改客户端配置文件client.ovpn文件,增加下面一行,使其支持用户名/密码与服务器进行身份验证
vim /etc/openvpn/client/wangxiaochun/client.ovpn
#加下面一行,可以支持用户密码认证
auth-user-pass