当前位置:   article > 正文

centos7搭建openvpn服务端_cp -a /usr/share/doc/easy-rsa-3.0.8/vars.example .

cp -a /usr/share/doc/easy-rsa-3.0.8/vars.example ./vars

适用场景

  • 个人电脑和公司办公网络互通

  • 不同部门,分公司的网络互通

主机环境

主机角色公网ip,内网ip,网关,子网系统信息
server(linux)公网地址,172.19.233.160,172.19.239.253,255.255.255.0centos7.9
client(linux)192.168.202.128,192.168.202.2,255.255.255.0ubuntu22.04
client(linux)192.168.202.129,192.168.202.2,255.255.255.0centos7.9

安装openvpn服务端(centos7.9)

repo源不能直接安装,需要下载rpm包

  1. #安装vim和wget以及下载epel的rpm包
  2. yum install -y vim wget
  3. mkdir /tmp/openvpn
  4. cd /tmp/openvpn
  5. wget https://repo.huaweicloud.com/epel/epel-release-latest-7.noarch.rpm
  6. rpm -ivh epel-release-latest-7.noarch.rpm
  7. yum update
  8. #安装easy-rsa以及生成server和client的证书
  9. yum install -y openssl lzo pam easy-rsa
  10. mkdir /root/easy-rsa
  11. cd /root/easy-rsa/
  12. cp -a /usr/share/easy-rsa/3.0.8/* ./
  13. cp -a /usr/share/doc/easy-rsa-3.0.8/vars.example ./vars
  14. grep -v '^#' vars | grep -v ^$
  15. if [ -z "$EASYRSA_CALLER" ]; then
  16. echo "You appear to be sourcing an Easy-RSA 'vars' file." >&2
  17. echo "This is no longer necessary and is disallowed. See the section called" >&2
  18. echo "'How to use this file' near the top comments for more details." >&2
  19. return 1
  20. fi
  21. set_var EASYRSA_DN "cn_only"
  22. set_var EASYRSA_REQ_COUNTRY "CN"
  23. set_var EASYRSA_REQ_PROVINCE "SiChuan"
  24. set_var EASYRSA_REQ_CITY "ChengDu"
  25. set_var EASYRSA_REQ_ORG "aaaaaa"
  26. set_var EASYRSA_REQ_EMAIL "admin.openvpn@aaaaaa.cn"
  27. set_var EASYRSA_REQ_OU "aaa"
  28. set_var EASYRSA_NS_SUPPORT "yes"
  29. [root@iZf8z3xsyghofo5njspgavZ easy-rsa]# ./easyrsa init-pki
  30. Note: using Easy-RSA configuration from: /root/easy-rsa/vars
  31. init-pki complete; you may now create a CA or requests.
  32. Your newly created PKI dir is: /root/easy-rsa/pki
  33. ##生成ca根证书
  34. [root@iZf8z3xsyghofo5njspgavZ easy-rsa]# ./easyrsa build-ca
  35. Note: using Easy-RSA configuration from: /root/easy-rsa/vars
  36. Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017
  37. Enter New CA Key Passphrase:     #输入密码
  38. Re-Enter New CA Key Passphrase:  #再次输入密码
  39. Generating RSA private key, 2048 bit long modulus
  40. .+++
  41. ......+++
  42. e is 65537 (0x10001)
  43. You are about to be asked to enter information that will be incorporated
  44. into your certificate request.
  45. What you are about to enter is what is called a Distinguished Name or a DN.
  46. There are quite a few fields but you can leave some blank
  47. For some fields there will be a default value,
  48. If you enter '.', the field will be left blank.
  49. -----
  50. Common Name (eg: your user, host, or server name) [Easy-RSA CA]:  #直接回车或输入主机名
  51. CA creation complete and you may now import and sign cert requests.
  52. Your new CA certificate file for publishing is at:
  53. /root/easy-rsa/pki/ca.crt
  54. ##生成服务端的证书和私钥
  55. [root@iZf8z3xsyghofo5njspgavZ easy-rsa]# ./easyrsa build-server-full server nopass
  56. Note: using Easy-RSA configuration from: /root/easy-rsa/vars
  57. Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017
  58. Generating a 2048 bit RSA private key
  59. ...................................................................+++
  60. .....+++
  61. writing new private key to '/root/easy-rsa/pki/easy-rsa-27540.IzadnR/tmp.COc2PW'
  62. -----
  63. Using configuration from /root/easy-rsa/pki/easy-rsa-27540.IzadnR/tmp.KLkuTm
  64. Enter pass phrase for /root/easy-rsa/pki/private/ca.key:
  65. Check that the request matches the signature
  66. Signature ok
  67. The Subject's Distinguished Name is as follows
  68. commonName           :ASN.1 12:'server'
  69. Certificate is to be certified until Mar 13 12:26:56 2026 GMT (825 days)
  70. Write out database with 1 new entries
  71. Data Base Updated
  72. ##生成Diffie-Hellman算法需要的密钥文件
  73. [root@iZf8z3xsyghofo5njspgavZ easy-rsa]# ./easyrsa gen-dh
  74. Note: using Easy-RSA configuration from: /root/easy-rsa/vars
  75. Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017
  76. Generating DH parameters, 2048 bit long safe prime, generator 2
  77. This is going to take a long time
  78. ................................+.......................................................................+..................................................................................................................................................................................................+.......................+..........+........+...................+.......................++*++*
  79. DH parameters of size 2048 created at /root/easy-rsa/pki/dh.pem
  80. ##生成客户端的证书和私钥保存到本地,并传递到客户端
  81. ##在服务端生成客户端的证书和私钥,每多一个客户端就要多生成一份
  82. [root@iZf8z3xsyghofo5njspgavZ easy-rsa]# ./easyrsa build-client-full client nopass
  83. Note: using Easy-RSA configuration from: /root/easy-rsa/vars
  84. Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017
  85. Generating a 2048 bit RSA private key
  86. .....+++
  87. ...........................................................................................................................................+++
  88. writing new private key to '/root/easy-rsa/pki/easy-rsa-27647.Iycjw0/tmp.20jrOv'
  89. -----
  90. Using configuration from /root/easy-rsa/pki/easy-rsa-27647.Iycjw0/tmp.KaU3pL
  91. Enter pass phrase for /root/easy-rsa/pki/private/ca.key:
  92. Check that the request matches the signature
  93. Signature ok
  94. The Subject's Distinguished Name is as follows
  95. commonName           :ASN.1 12:'client'
  96. Certificate is to be certified until Mar 13 12:36:10 2026 GMT (825 days)
  97. Write out database with 1 new entries
  98. Data Base Updated
  99. #安装openvpn
  100. yum install -y openvpn
  101. #拷贝证书到/etc/openvpn/server/下面
  102. ls /etc/openvpn/server/
  103. [root@iZf8z3xsyghofo5njspgavZ easy-rsa]# cd /etc/openvpn/server/
  104. [root@iZf8z3xsyghofo5njspgavZ server]# ls
  105. [root@iZf8z3xsyghofo5njspgavZ server]# cp /root/easy-rsa/pki/ca.crt .
  106. [root@iZf8z3xsyghofo5njspgavZ server]# cp /root/easy-rsa/pki/issued/server.crt .
  107. [root@iZf8z3xsyghofo5njspgavZ server]# cp /root/easy-rsa/pki/private/server.key .
  108. [root@iZf8z3xsyghofo5njspgavZ server]# cp /root/easy-rsa/pki/dh.pem .
  109. [root@iZf8z3xsyghofo5njspgavZ server]# vim /etc/openvpn/server/server.conf
  110. port 1194                               #端口,默认1194,这里可以修改为其他的
  111. proto udp                               #协议,可客户端一致,云上一定要打开此协议的端口
  112. dev tun                                 #采用路由隧道模式tun
  113. ca ca.crt                               #ca证书文件位置
  114. cert server.crt                         #服务端公钥名称
  115. key server.key                          #服务端私钥名称
  116. dh dh.pem                               #交换证书
  117. server 10.8.0.0 255.255.255.0           #给客户端分配地址池,注意:不能和VPN服务器内网网段有相同
  118. push "route 172.32.45.0 255.255.255.0"   #允许客户端访问内网网段,这个可以通过查看服务端的route -n获取
  119. ifconfig-pool-persist ipp.txt           #地址池记录文件位置
  120. keepalive 10 120                        #存活时间,10秒ping一次,120 如未收到响应则视为断线
  121. max-clients 10                         #最多允许10个客户端连接
  122. status openvpn-status.log               #日志记录位置
  123. verb 3                                  #openvpn版本
  124. client-to-client                        #客户端与客户端之间支持通信
  125. log /var/log/openvpn.log                #openvpn日志记录位置
  126. persist-key     #通过keepalive检测超时后,重新启动VPN,不重新读取keys,保留第一次使用的keys。
  127. persist-tun     #检测超时后,重新启动VPN,一直保持tun是linkup的。否则网络会先linkdown然后再linkup
  128. duplicate-cn
  129. comp-lzo  #使用lzo压缩的通讯,服务端和客户端都必须配置
  130. [root@iZf8z3xsyghofo5njspgavZ server]# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
  131. [root@iZf8z3xsyghofo5njspgavZ server]# systemctl restart network
  132. ##暂时未确定是否必须配置
  133. #临时关闭selinux
  134. setenforce 0
  135. #配置文件永久关闭 修改/etc/selinux/config 文件
  136. SELINUX=disabled
  137. iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
  138. iptables -I INPUT -p udp --dport 1194 -j ACCEPT ##端口和协议和server.conf一致
  139. #保存规则并重启
  140. service iptables save
  141. systemctl restart iptables
  142. #启动服务
  143. [root@testc openvpn]# systemctl -f enable openvpn@server.service
  144. Created symlink from /etc/systemd/system/multi-user.target.wants/openvpn@server.service to /usr/lib/systemd/system/openvpn@.service.
  145. #用systemctl启动暂时有问题,先手动启动看看
  146. cd /etc/openvpn/server/
  147. openvpn server.conf   #新打开一个命令行,然后查看/var/log/openvpn.log日志
  148. systemctl start openvpn@server.service
  149. systemctl status openvpn@server.service

注意事项

        云上部署openvpn服务端,一定要放开对应协议的端口即实例所在安全组设置。

安装openvpn客户端(ubuntu22.04)

  1. #安装openvpn软件和route命令
  2. apt update
  3. apt install openvpn vim net-tools
  4. #传递密钥文件 ca.crt client.key client.crt
  5. ls /etc/openvpn/client/
  6. #编写配置文件
  7. vim /etc/openvpn/client/client.conf
  8. client
  9. dev tun
  10. proto udp ##和服务端的协议需要一致
  11. remote 公网ip地址 1194 #公网的ip以及端口
  12. resolv-retry infinite
  13. nobind
  14. ca ca.crt
  15. cert client.crt
  16. key client.key
  17. verb 3
  18. persist-key
  19. comp-lzo ##服务端客户端都要加
  20. #生成service文件设置开机自启或手动启动步骤
  21. cd /etc/openvpn/client
  22. openvpn client.conf
  23. systemctl -f enable openvpn@client.service
  24. systemctl start openvpn@client

测试是否成功

        可以在客户端直接查看是否在服务打开后查看ip a s看看是否新增了一个tun0

        可以查看route -n看看是否有新增一条路由指向服务端的内部路由信息

        也可直接ssh连接服务端所指向的内网某个主机,看看是否可以连接

参考文档

openVN安装搭建步骤,实现内网穿透_openvpn搭建-CSDN博客

OpenVPN搭建(1) - 知乎

【运维知识进阶篇】手把手教你搭建OpenVPN(保姆级教程)_openvpn客户端-CSDN博客

CentOS搭建OpenVPN - 知乎



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/481192
推荐阅读
相关标签
  

闽ICP备14008679号