使用docker快速搭建openvpn

打印 上一主题 下一主题

主题 672|帖子 672|积分 2016

1 概述

本文用来教大家怎样快速使用docker搭建openvpn,话不多说直接进入正题。
2 摆设

2.1 情况需求

需要有外网IP。
2.2 步骤。

  1. docker pull kylemanna/openvpn:2.4
  2. #生成配置文件
  3. #fu服务器公网 公网IP
  4. docker run -v /opt/apps/openvpn:/etc/openvpn --rm kylemanna/openvpn:2.4 ovpn_genconfig -u udp://公网IP
  5. #生成密钥文件
  6. docker run -v /opt/apps/openvpn:/etc/openvpn --rm -it kylemanna/openvpn:2.4 ovpn_initpki
  7. [root@VM-24-9-centos openvpn]# docker run -v /opt/apps/openvpn:/etc/openvpn --rm -it kylemanna/openvpn:2.4 ovpn_initpki
  8. init-pki complete; you may now create a CA or requests.
  9. Your newly created PKI dir is: /etc/openvpn/pki
  10. Using SSL: openssl OpenSSL 1.1.1g  21 Apr 2020
  11. Enter New CA Key Passphrase: 12345678
  12. Re-Enter New CA Key Passphrase: 12345678
  13. Generating RSA private key, 2048 bit long modulus (2 primes)
  14. ..........................+++++
  15. .....................................................................................+++++
  16. e is 65537 (0x010001)
  17. You are about to be asked to enter information that will be incorporated
  18. into your certificate request.
  19. What you are about to enter is what is called a Distinguished Name or a DN.
  20. There are quite a few fields but you can leave some blank
  21. For some fields there will be a default value,
  22. If you enter '.', the field will be left blank.
  23. -----
  24. Common Name (eg: your user, host, or server name) [Easy-RSA CA]:WEIHU
  25. CA creation complete and you may now import and sign cert requests.
  26. Your new CA certificate file for publishing is at:
  27. /etc/openvpn/pki/ca.crt
  28. Using SSL: openssl OpenSSL 1.1.1g  21 Apr 2020
  29. Generating DH parameters, 2048 bit long safe prime, generator 2
  30. This is going to take a long time
  31. .....................................................................................................................................+.......................................................................+...................+........................................+......................................................................+......................................................................+..............+........+..............................................................................................................................................................................................+.......................................................+................................................................................................................................................+...........................................+...................................................................+.................................................................................................................................................................................................................................................................................................................................................+..........................................................................................+..........+.......................................................+....................+.......................................................................................................................................................+...........................+.....................................................................................................................................................+.................................................................................+.............+.............................................+..............................................+...................................+.......................................................................+.......................................................................................+..........................+........................................................................+...........................................................................................................+...................................................................................................................................................................................................+................................................................................................................................................................................................................+.....................................................................+................................................................................+......................................+..................................................................................................................+.................................................................+........+.............+................................................................................................................................................................................................................+.....................................................................+......................................................+..............................+.....................................................................+..........................................................................................................................................................................................................................+...........................................+.........................+.................................................................................................................................................+................................................................................................................................+............................................................................................................................................+................................................................................................................................................................................+..........................................................................................+................................................................................+...........................................................+......................................+...................................................................................................................................................................................................................................................................................................................................................................................................................................+.............................................+................................................................................................................................................+.......................+.........................................................+.........................+..............................................................................................+.+.............................+.....................................................................................................................................................+..........+...........................+...+........................................+.....+...................................+...................................................................+.............+...................................+............................................................................................................................................................................................................................................................................................................................................................................................+................................+.................................................................................................................................................................+.............................+................................+..................................................+..............................................................................................................................+......................................................................................++*++*++*++*
  32. DH parameters of size 2048 created at /etc/openvpn/pki/dh.pem
  33. Using SSL: openssl OpenSSL 1.1.1g  21 Apr 2020
  34. Generating a RSA private key
  35. ........................+++++
  36. ........................................................+++++
  37. writing new private key to '/etc/openvpn/pki/easy-rsa-72.obdilb/tmp.OkeLiC'
  38. -----
  39. Using configuration from /etc/openvpn/pki/easy-rsa-72.obdilb/tmp.lAFMAn
  40. Enter pass phrase for /etc/openvpn/pki/private/ca.key:
  41. Check that the request matches the signature
  42. Signature ok
  43. The Subject's Distinguished Name is as follows
  44. commonName            :ASN.1 12:'82.157.165.162'
  45. Certificate is to be certified until Sep 17 00:59:32 2024 GMT (825 days)
  46. Write out database with 1 new entries
  47. Data Base Updated
  48. Using SSL: openssl OpenSSL 1.1.1g  21 Apr 2020
  49. Using configuration from /etc/openvpn/pki/easy-rsa-147.MolDfl/tmp.faLnEK
  50. Enter pass phrase for /etc/openvpn/pki/private/ca.key:12345678
  51. An updated CRL has been created.
  52. CRL file: /etc/openvpn/pki/crl.pem
  53. [root@VM-24-9-centos openvpn]#
  54. #生成客户端证书(这里的weihu改成你想要的名字)
  55. docker run -v /opt/apps/openvpn:/etc/openvpn --rm -it kylemanna/openvpn:2.4 easyrsa build-client-full weihu nopass
  56. [root@VM-24-9-centos openvpn]# docker run -v /opt/apps/openvpn:/etc/openvpn --rm -it kylemanna/openvpn:2.4 easyrsa build-client-full weihu nopass
  57. Using SSL: openssl OpenSSL 1.1.1g  21 Apr 2020
  58. Generating a RSA private key
  59. ...+++++
  60. ....................................................................................+++++
  61. writing new private key to '/etc/openvpn/pki/easy-rsa-1.hDjaFE/tmp.AjJCaO'
  62. -----
  63. Using configuration from /etc/openvpn/pki/easy-rsa-1.hDjaFE/tmp.LJIhlM
  64. Enter pass phrase for /etc/openvpn/pki/private/ca.key: 12345678
  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:'weihu'
  69. Certificate is to be certified until Sep 17 01:01:23 2024 GMT (825 days)
  70. Write out database with 1 new entries
  71. Data Base Updated
  72. [root@VM-24-9-centos openvpn]#
  73. #导出客户端配置
  74. mkdir -p /opt/apps/openvpn/conf
  75. docker run -v /opt/apps/openvpn:/etc/openvpn --rm kylemanna/openvpn:2.4 ovpn_getclient weihu> /opt/apps/openvpn/conf/weihu.ovpn
  76. #启动OpenV服务
  77. docker run --name openvpn -v /opt/apps/openvpn:/etc/openvpn -d -p 8000:1194/udp --cap-add=NET_ADMIN kylemanna/openvpn:2.4
  78. PS:
  79. 停止 openvpn
  80. docker stop openvpn
  81. 启动 openvpn
  82. docker start openvpn
  83. #设置防火墙 (这个先不加也可以用)
  84. iptables -t nat -A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE
  85. iptables -t nat -A POSTROUTING -s 172.17.0.2/32 -d 172.17.0.2/32 -p udp -m udp --dport 1194 -j MASQUERADE
  86. iptables -t nat -A DOCKER -i docker0 -j RETURN
  87. iptables -t nat -A DOCKER ! -i docker0 -p udp -m udp --dport 1194 -j DNAT --to-destination 172.17.0.2:1194
  88. iptables -t nat -A POSTROUTING -s 172.17.0.2/32 -d 172.17.0.2/32 -p udp -m udp --dport 1194 -j MASQUERADE
  89. ipconfig-save
  90. #创建用户脚本 user_create.sh
  91. #!/bin/bash
  92. read -p "please your username: " NAME
  93. docker run -v /opt/apps/openvpn:/etc/openvpn --rm -it kylemanna/openvpn:2.4 easyrsa build-client-full $NAME nopass
  94. docker run -v /opt/apps/openvpn:/etc/openvpn --rm kylemanna/openvpn:2.4 ovpn_getclient $NAME > /opt/apps/openvpn/conf/"$NAME".ovpn
  95. #删除用户脚本 user_del.sh
  96. #!/bin/bash
  97. read -p "Delete username: " DNAME
  98. docker run -v /opt/apps/openvpn:/etc/openvpn --rm -it kylemanna/openvpn:2.4 easyrsa revoke $DNAME
  99. docker run -v /opt/apps/openvpn:/etc/openvpn --rm -it kylemanna/openvpn:2.4 easyrsa gen-crl
  100. docker run -v /opt/apps/openvpn:/etc/openvpn --rm -it kylemanna/openvpn:2.4 rm -f /etc/openvpn/pki/reqs/"$DNAME".req
  101. docker run -v /opt/apps/openvpn:/etc/openvpn --rm -it kylemanna/openvpn:2.4 rm -f /etc/openvpn/pki/private/"$DNAME".key
  102. docker run -v /opt/apps/openvpn:/etc/openvpn --rm -it kylemanna/openvpn:2.4 rm -f /etc/openvpn/pki/issued/"$DNAME".crt
  103. #需要重启openvpn
  104. docker restart openvpn
复制代码
2.3 参考地址

   https://blog.csdn.net/qq_42761569/article/details/106538056
  码字不易,请点赞收藏。


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

络腮胡菲菲

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表