OpenVpn搭建

柳三千

文章最后更新时间:2023年03月15日已超过551天没有更新。

环境要求:

OpenVpn服务器需要一个公网IP,供给客户端访问!

软件下载地址:

点击下载>>

easy-rsa-old-master.zip(win7)

easy-rsa-old-master.zip(win10)

一、制作OpenVpn证书(证书制作工具easy-rsa-old-master.zip)

1、配置vars文件

[root@hk-web openvpn]# unzip easy-rsa-old-master.zip
[root@hk-web 2.0]# pwd
/root/openvpn/easy-rsa-old-master/easy-rsa/2.0

[root@hk-web 2.0]# vim vars

图片.png

[root@hk-web 2.0]# source vars 
NOTE: If you run ./clean-all, I will be doing a rm -rf on /root/openvpn/easy-rsa-old-master/easy-rsa/2.0/keys
[root@hk-web 2.0]# ./clean-all
#多出keys目录

图片.png


2、生成根证书和秘钥 ca.crt、ca.key

[root@hk-web 2.0]# ./build-ca

图片.png


生成根证书、根秘钥

图片.png


3、制作服务器根证书、根秘钥 server.crt、server.key

[root@hk-web 2.0]# ./build-key-server server

图片.png

[root@hk-web 2.0]# ll keys/
total 56
-rw-r--r-- 1 root root 8196 Sep 27 21:36 01.pem
-rw-r--r-- 1 root root 2415 Sep 27 21:33 ca.crt
-rw------- 1 root root 3272 Sep 27 21:33 ca.key
-rw-r--r-- 1 root root  132 Sep 27 21:36 index.txt
-rw-r--r-- 1 root root   21 Sep 27 21:36 index.txt.attr
-rw-r--r-- 1 root root    0 Sep 27 21:29 index.txt.old
-rw-r--r-- 1 root root    3 Sep 27 21:36 serial
-rw-r--r-- 1 root root    3 Sep 27 21:29 serial.old
-rw-r--r-- 1 root root 8196 Sep 27 21:36 server.crt
-rw-r--r-- 1 root root 1769 Sep 27 21:36 server.csr
-rw------- 1 root root 3272 Sep 27 21:36 server.key


4、制作客户端证书和秘钥 client.crt client.key

[root@hk-web 2.0]# ./build-key client

图片.png

[root@hk-web 2.0]# ll keys/
total 88
-rw-r--r-- 1 root root 8196 Sep 27 21:36 01.pem
-rw-r--r-- 1 root root 8078 Sep 27 21:39 02.pem
-rw-r--r-- 1 root root 2415 Sep 27 21:33 ca.crt
-rw------- 1 root root 3272 Sep 27 21:33 ca.key
-rw-r--r-- 1 root root 8078 Sep 27 21:39 client.crt
-rw-r--r-- 1 root root 1769 Sep 27 21:39 client.csr
-rw------- 1 root root 3272 Sep 27 21:39 client.key
-rw-r--r-- 1 root root  264 Sep 27 21:39 index.txt
-rw-r--r-- 1 root root   21 Sep 27 21:39 index.txt.attr
-rw-r--r-- 1 root root   21 Sep 27 21:36 index.txt.attr.old
-rw-r--r-- 1 root root  132 Sep 27 21:36 index.txt.old
-rw-r--r-- 1 root root    3 Sep 27 21:39 serial
-rw-r--r-- 1 root root    3 Sep 27 21:36 serial.old
-rw-r--r-- 1 root root 8196 Sep 27 21:36 server.crt
-rw-r--r-- 1 root root 1769 Sep 27 21:36 server.csr
-rw------- 1 root root 3272 Sep 27 21:36 server.key


5、生成秘钥交换文件

[root@hk-web 2.0]# ./build-dh  dh2048.pem
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
...................................................................+................................+......+..........
[root@hk-web 2.0]# ll keys/
total 92
-rw-r--r-- 1 root root 8196 Sep 27 21:36 01.pem
-rw-r--r-- 1 root root 8078 Sep 27 21:39 02.pem
-rw-r--r-- 1 root root 2415 Sep 27 21:33 ca.crt
-rw------- 1 root root 3272 Sep 27 21:33 ca.key
-rw-r--r-- 1 root root 8078 Sep 27 21:39 client.crt
-rw-r--r-- 1 root root 1769 Sep 27 21:39 client.csr
-rw------- 1 root root 3272 Sep 27 21:39 client.key
-rw-r--r-- 1 root root  424 Sep 27 21:43 dh2048.pem
-rw-r--r-- 1 root root  264 Sep 27 21:39 index.txt
-rw-r--r-- 1 root root   21 Sep 27 21:39 index.txt.attr
-rw-r--r-- 1 root root   21 Sep 27 21:36 index.txt.attr.old
-rw-r--r-- 1 root root  132 Sep 27 21:36 index.txt.old
-rw-r--r-- 1 root root    3 Sep 27 21:39 serial
-rw-r--r-- 1 root root    3 Sep 27 21:36 serial.old
-rw-r--r-- 1 root root 8196 Sep 27 21:36 server.crt
-rw-r--r-- 1 root root 1769 Sep 27 21:36 server.csr
-rw------- 1 root root 3272 Sep 27 21:36 server.key


二、配置openvpn服务端

1、安装OpenVpn

[root@hk-web 2.0]# yum -y install openvpn


2、配置OpenVpn

[root@hk-web 2.0]# cd /etc/openvpn/
[root@hk-web openvpn]# ll
total 8
drwxr-x--- 2 root openvpn 4096 Mar 18  2022 client
drwxr-x--- 2 root openvpn 4096 Mar 18  2022 server
[root@hk-web openvpn]# mkdir keys
[root@hk-web openvpn]# ll
total 12
drwxr-x--- 2 root openvpn 4096 Mar 18  2022 client
drwxr-xr-x 2 root root    4096 Sep 27 21:48 keys
drwxr-x--- 2 root openvpn 4096 Mar 18  2022 server
[root@hk-web openvpn]# cd /root/openvpn/easy-rsa-old-master/easy-rsa/2.0/keys/
[root@hk-web keys]# cp -a {ca.crt,server.crt,server.key,dh2048.pem} /etc/openvpn/keys/
[root@hk-web keys]# cd /etc/openvpn/keys/
[root@hk-web keys]# ll
total 24
-rw-r--r-- 1 root root 2415 Sep 27 21:33 ca.crt
-rw-r--r-- 1 root root  424 Sep 27 21:43 dh2048.pem
-rw-r--r-- 1 root root 8196 Sep 27 21:36 server.crt
-rw------- 1 root root 3272 Sep 27 21:36 server.key
[root@hk-web keys]# cp -a /usr/share/doc/openvpn-2.4.12/sample/sample-config-files/server.conf /etc/openvpn/


 修改配置文件

#修改成刚刚拷贝过来的文件路径

ca keys/ca.crt
cert keys/server.crt
key keys/server.key  # This file should be kept secret
dh keys/dh2048.pem

tls-auth keys/ta.key 0 #拒绝服务攻击的文件

#修改网段

server 192.168.88.0 255.255.255.0
push "route 192.168.88.0 255.255.255.0"
push "route 192.168.0.0 255.255.255.0" #真实内网的网段地址
cipher AES-256-GCM #改为GCM


3、启用路由转发功能

[root@hk-web openvpn]# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
[root@hk-web openvpn]# sysctl -p
vm.swappiness = 0
net.core.somaxconn = 1024
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.ip_forward = 1


4、建立ta.key文件(拒绝服务攻击证书文件)

[root@hk-web keys]# openvpn --genkey --secret ta.key
[root@hk-web keys]# ls
ca.crt  dh2048.pem  server.crt  server.key  ta.key


5、启动openvpn

[root@hk-web openvpn]# openvpn --daemon --config server.conf
[root@hk-web openvpn]# netstat -pltun | grep 1194
udp        0      0 0.0.0.0:1194            0.0.0.0:*                           22994/openvpn


三、配置OpenVpn客户端

1、客户端的配置文件

[root@hk-web ~]# cd /usr/share/doc/openvpn-2.4.12/sample/sample-config-files/
[root@hk-web sample-config-files]# ls
client.conf      office.up                roadwarrior-server.conf  tls-office.conf
firewall.sh      openvpn-shutdown.sh      server.conf              xinetd-client-config
home.up          openvpn-startup.sh       static-home.conf         xinetd-server-config
loopback-client  README                   static-office.conf
loopback-server  roadwarrior-client.conf  tls-home.conf
[root@hk-web sample-config-files]# cp -a client.conf /root/
[root@hk-web sample-config-files]# cd ~
[root@hk-web ~]# mkdir client
[root@hk-web ~]# mv client.conf client
[root@hk-web ~]# cd client/
vim client.conf
cipher AES-256-GCM
remote 159.138.129.221 1194


2、生成客户端的证书文件

[root@hk-web client]# cd /root/openvpn/easy-rsa-old-master/easy-rsa/2.0/
[root@hk-web 2.0]# ls
build-ca        build-key-pkcs12  inherit-inter      openssl-1.0.0.cnf  whichopensslcnf
build-dh        build-key-server  keys               pkitool
build-inter     build-req         list-crl           revoke-full
build-key       build-req-pass    openssl-0.9.6.cnf  sign-req
build-key-pass  clean-all         openssl-0.9.8.cnf  vars
[root@hk-web 2.0]# cd keys/
[root@hk-web keys]# cp -a client.key /root/client/
[root@hk-web keys]# cp -a client.crt /root/client/
[root@hk-web keys]# cp -a ca.crt /root/client/
[root@hk-web keys]# cd /etc/openvpn/keys/
[root@hk-web keys]# ls
ca.crt  dh2048.pem  server.crt  server.key  ta.key
[root@hk-web keys]# cp -a ta.key /root/client/
[root@hk-web keys]# cd /root/client/
[root@hk-web client]# ls
ca.crt  client.conf  client.crt  client.key  ta.key
[root@hk-web client]# mv client.conf client.ovpn #在windows端生效
[root@hk-web client]# ll
total 24
-rw-r--r-- 1 root root 2415 Sep 27 21:33 ca.crt
-rw-r--r-- 1 root root 8078 Sep 27 21:39 client.crt
-rw------- 1 root root 3272 Sep 27 21:39 client.key
-rw-r--r-- 1 root root 3613 Sep 27 22:26 client.ovpn
-rw------- 1 root root  636 Sep 27 22:15 ta.key


3、打包客户端目录

[root@hk-web client]# cd ../
[root@hk-web ~]# zip client.zip client/  adding: client/ (stored 0%)
[root@hk-web ~]# ll
total 95356
-rw-r--r-- 1 root root    35335 Sep  3 12:56 btmp
drwxr-xr-x 2 root root     4096 Sep 27 22:31 client
-rw-r--r-- 1 root root      164 Sep 27 22:33 client.zip
-rw-r--r-- 1 root root      546 Sep  3 12:56 error_login,log
drwxr-xr-x 3 root root     4096 Sep 27 21:25 openvpn
-rwxr-xr-x 1 root root     1518 Aug 25 17:08 syslisten.sh
-rw-r--r-- 1 root root    22416 Aug 27 09:01 sys.log
-rw-r--r-- 1 root root        0 Aug 30 17:05 text
-rw-r--r-- 1 root root        3 Aug 27 09:01 tmp
-rwxr-xr-x 1 root root      673 Aug 23 16:44 unlogin.sh
-rw-r--r-- 1 root root 97548489 Sep 26 15:21 zabbix.sql


四、windows客户端测试

1、解压导入(客户端文件 拷贝到windows操作)

图片.png


2、安装openvpn客户端(win10一直点下一步操作就行,win7会报数字签名错误,需要开机F按8启动,关闭数字签名)

    win7客户端安装:

图片.png

图片.png

图片.png

图片.png


以管理身份运行

图片.png


警报提示:

图片.png


3、修改正确的文件路径

右键右下角图标,选择选项->高级,修改路径

图片.png


导入配置、证书等文件到config目录下

图片.png

重启按F8,禁用驱动程序签名强制


连接测试

图片.png

图片.png

192.168.0.0/24是vpn服务器所在的局域网

192.168.89.0/24是vpn分配的地址

172.16.20.0/22是本机所在的局域网

本机通过vpn分配的89.0网段地址与192.168.0.0局域网的通信

图片.png


关闭vpn再测试,网络不通

图片.png

文章版权声明:除非注明,否则均为柳三千运维录原创文章,转载或复制请以超链接形式并注明出处。

目录[+]

取消
微信二维码
微信二维码
支付宝二维码