电脑疯子技术论坛|电脑极客社区

微信扫一扫 分享朋友圈

已有 2191 人浏览分享

Centos6.5搭建openvpn服务器

[复制链接]
2191 0

因为新版本openvpn里面没有包含最重要的证书制作部分:easy-rsa所以,需要事先下载好easyrsa,可以到GitHub上进行下载,配置过程将在下面第3步进行,本次部署使用了easy-rsa

3,与easy-rsa2.0的操作完全不同,网上其它关于easy-rsa2.0的教程不适合本次部署

    在部署openvpn之前,最好用ntpdate同步一下服务器的时间,否则生成证书的时间也不准确,会造成那个什么centificate error等的错误!

1、安装lzo

    lzo是致力于解压速度的一种数据压缩算法

  1. [root@vpn ~]# wgethttp://www.oberhumer.com/opensource/lzo/download/lzo-2.09.tar.gz

  2. [root@vpn ~]# tar xf lzo-2.09.tar.gz

  3. [root@vpn ~]# cd lzo-2.09

  4. [root@vpn lzo-2.09]# ./configure && make && make install
复制代码


2、安装openvpn

  1. [root@vpn ~]# yum install -yopenssl-devel
  2. [root@vpn ~]# wgethttps://swupdate.openvpn.org/community/releases/openvpn-2.3.11.tar.gz

  3. [root@vpn ~]# tar zxvfopenvpn-2.3.11.tar.gz

  4. [root@vpn ~]# cd openvpn-2.3.11

  5. [root@vpn openvpn-2.3.4]# ./configure --with-lzo-headers=/usr/local/include/ --with-lzo-lib=/usr/local/lib

  6. [root@vpn openvpn-2.3.4]# make && make install

  7. [root@vpn openvpn-2.3.4]# which openvpn

  8. /usr/local/sbin/openvpn      #看到这里,说明安装openvpn成功
复制代码


3、配置easyrsa服务端

    openvpn-2.3.11软件包不包含证书(ca证书,服务端证书,客户端证书)制作工具,所以还需要单独下载easy-rsa,最新的为easy-rsa3

  1. [root@vpn ~]# wgethttps://github.com/OpenVPN/easy-rsa/archive/master.zip
  2. [root@vpn ~]# unzipmaster.zip

  3. [root@vpn ~]# mv easy-rsa-master easy-rsa

  4. [root@vpn ~]# cp -R easy-rsa/ /usr/local/share/doc/openvpn/

  5. [root@vpn ~]# cd/usr/local/share/doc/openvpn/easy-rsa/easyrsa3/

  6. [root@vpn easyrsa3]# cp vars.example vars

  7. [root@vpn easyrsa3]# vim vars

  8. set_var EASYRSA_REQ_COUNTRY "CN"

  9. set_var EASYRSA_REQ_PROVINCE "Beijing"

  10. set_var EASYRSA_REQ_CITY "Beijing"

  11. set_var EASYRSA_REQ_ORG "qiangshCertificate"

  12. set_var EASYRSA_REQ_EMAIL "503579266@qq.com"

  13. set_var EASYRSA_REQ_OU "My OpenVPN"
复制代码


4、创建服务端证书及key

(1)初始化

  1. [root@vpn easyrsa3]# ls

  2. easyrsa  openssl-1.0.cnf  vars  vars.example  x509-types

  3. [root@vpn easyrsa3]#

  4. [root@vpn easyrsa3]# ./easyrsa init-pki



  5. Note: using Easy-RSAconfiguration from: ./vars

  6. init-pki complete; you may nowcreate a CA or requests.

  7. Your newly created PKI dir is:/usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki
复制代码


(2)创建根证书
  1. [root@vpn easyrsa3]# ./easyrsa build-ca

  2. Note: using Easy-RSAconfiguration from: ./vars

  3. Generating a 2048 bit RSA privatekey

  4. ..+++

  5. ..........................+++

  6. writing new private key to'/usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/private/ca.key.GiibbqFhXm'

  7. Enter PEM pass phrase:                                 #输入密码,此密码用途证书签名

  8. Verifying - Enter PEM passphrase:                      #再次输入密码

  9. -----

  10. You are about to be asked toenter information that will be incorporated

  11. into your certificate request.

  12. What you are about to enter iswhat is called a Distinguished Name or a DN.

  13. There are quite a few fields butyou can leave some blank

  14. For some fields there will be adefault value,

  15. If you enter '.', the field willbe left blank.

  16. -----

  17. Common Name (eg: your user, host,or server name) [Easy-RSA CA]:qiangsh      #输入一个Common Name

  18. CA creation complete and you maynow import and sign cert requests.

  19. Your new CA certificate file forpublishing is at:

  20. /usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/ca.crt

复制代码


(3)创建服务器端证书

  1. [root@vpn easyrsa3]# ./easyrsa gen-req server nopass

  2. Note: using Easy-RSAconfiguration from: ./vars

  3. Generating a 2048 bit RSA privatekey

  4. .......................................+++

  5. ......................................+++

  6. writing new private key to '/usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/private/server.key.MIGrh2B6S8'

  7. -----

  8. You are about to be asked toenter information that will be incorporated

  9. into your certificate request.

  10. What you are about to enter iswhat is called a Distinguished Name or a DN.

  11. There are quite a few fields butyou can leave some blank

  12. For some fields there will be adefault value,

  13. If you enter '.', the field willbe left blank.

  14. -----

  15. Common Name (eg: your user, host,or server name) [server]:qiangsh-BJ     #该Common Name一定不要与创建根证书时的一样 !!!

  16. Keypair and certificate requestcompleted. Your files are:

  17. req:/usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/reqs/server.req

  18. key:/usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/private/server.key
复制代码


(4)签约服务器端证书


  1. [root@vpn easyrsa3]# ./easyrsa sign server server

  2. Note: using Easy-RSAconfiguration from: ./vars

  3. You are about to sign thefollowing certificate.

  4. Please check over the detailsshown below for accuracy. Note that this request

  5. has not been cryptographicallyverified. Please be sure it came from a trusted

  6. source or that you have verifiedthe request checksum with the sender.

  7. Request subject, to be signed asa server certificate for 3650 days:

  8. subject=

  9.     commonName               = qiangsh-BJ

  10. Type the word 'yes' to continue,or any other input to abort.

  11.   Confirm request details:yes          #输入yes继续

  12. Using configuration from/usr/local/share/doc/openvpn/easy-rsa/easyrsa3/openssl-1.0.cnf

  13. Enter pass phrase for/usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/private/ca.key:   #输入刚才创建根证书时的密码

  14. Check that the request matchesthe signature

  15. Signature ok

  16. The Subject's Distinguished Nameis as follows

  17. commonName           :PRINTABLE:'qiangsh-BJ'

  18. Certificate is to be certifieduntil Jun  6 07:19:45 2026 GMT (3650 days)

  19. Write out database with 1 newentries

  20. Data Base Updated

  21. Certificate created at: /usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/issued/server.crt
复制代码



(5)创建Diffie-Hellman,确保key穿越不安全网络的命令:
  1. [root@vpn easyrsa3]# ./easyrsa gen-dh

  2. Note: using Easy-RSAconfiguration from: ./vars

  3. Generating DH parameters, 2048bit long safe prime, generator 2

  4. This is going to take a long time

  5. ..........................................................................+...........................+.............................................................+...........................+.................................................................................................................................................................................................................................................+...............................................................................................................................+..+.................................................................+..........................................................................................+..............+...............................................................................................................................................................................+........................................................................................+...............................................................................+................................................+..........++*++*

  6. DH parameters of size 2048 created at/usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/dh.pem
复制代码


5、创建客户端证书
(1)在根目录下建立client目录

  1. [root@vpn easyrsa3]# cd

  2. [root@vpn ~]# mkdir client

  3. [root@vpn ~]# cp -R /mnt/easy-rsa/ client/
复制代码


(2)初始化

  1. [root@vpn ~]# cd client/easy-rsa/easyrsa3/

  2. [root@vpn easyrsa3]# ls

  3. easyrsa  openssl-1.0.cnf  vars  vars.example  x509-types

  4. [root@vpn easyrsa3]# ./easyrsa init-pki



  5. Note: using Easy-RSA configuration from: ./vars



  6. init-pki complete; you may now create a CA or requests.

  7. Your newly created PKI dir is: /root/client/easy-rsa/easyrsa3/pki
复制代码


(3)创建客户端key及生成证书

  1. [root@vpn easyrsa3]# ./easyrsa gen-req qiangsh

  2. Generating a 2048 bit RSA privatekey

  3. .......................+++

  4. ........................................................+++

  5. writing new private key to'/root/client/easy-rsa/easyrsa3/pki/private/qiangsh.key.LD7Wk6hmQq'

  6. Enter PEM pass phrase:                #输入密码

  7. Verifying - Enter PEM passphrase:     #再次输入密码     

  8. -----

  9. You are about to be asked toenter information that will be incorporated

  10. into your certificate request.

  11. What you are about to enter iswhat is called a Distinguished Name or a DN.

  12. There are quite a few fields butyou can leave some blank

  13. For some fields there will be adefault value,

  14. If you enter '.', the field willbe left blank.

  15. -----

  16. Common Name (eg: your user, host,or server name) [qiangsh]:qiangsh    #输入qiangsh

  17. Keypair and certificate request completed.Your files are:

  18. req:/root/client/easy-rsa/easyrsa3/pki/reqs/qiangsh.req

  19. key:/root/client/easy-rsa/easyrsa3/pki/private/qiangsh.key
复制代码


(4)将得到的qiangsh.req导入并签约证书

  1. [root@vpn ~]# cd/usr/local/share/doc/openvpn/easy-rsa/easyrsa3/

  2. [root@vpn easyrsa3]#   #导入req

  3. [root@vpn easyrsa3]#./easyrsa import-req /root/client/easy-rsa/easyrsa3/pki/reqs/qiangsh.reqqiangsh

  4. Note: using Easy-RSAconfiguration from: ./vars

  5. The request has been successfullyimported with a short name of: qiangsh

  6. You may now use this name toperform signing operations on this request.



  7. [root@vpn easyrsa3]#     #签约证书

  8. [root@vpn easyrsa3]# ./easyrsa sign client qiangsh

  9. Note: using Easy-RSAconfiguration from: ./vars

  10. You are about to sign thefollowing certificate.

  11. Please check over the detailsshown below for accuracy. Note that this request

  12. has not been cryptographicallyverified. Please be sure it came from a trusted

  13. source or that you have verifiedthe request checksum with the sender.

  14. Request subject, to be signed asa client certificate for 3650 days:

  15. subject=

  16.     commonName               = qiangsh

  17. Type the word 'yes' to continue,or any other input to abort.

  18.   Confirm request details:yes    #输入yes

  19. Using configuration from/usr/local/share/doc/openvpn/easy-rsa/easyrsa3/openssl-1.0.cnf

  20. Enter pass phrase for/usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/private/ca.key:   #输入创建根证书时的密码

  21. Check that the request matchesthe signature

  22. Signature ok

  23. The Subject's Distinguished Nameis as follows

  24. commonName           :PRINTABLE:'qiangsh'

  25. Certificate is to be certifieduntil Jun  6 07:50:02 2026 GMT (3650 days)

  26. Write out database with 1 newentries

  27. Data Base Updated

  28. Certificate created at:/usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/issued/qiangsh.crt  #签约成功
复制代码


(5)服务端及客户端生成的文件

服务端:(/usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/)文件夹

  1. /usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/ca.crt

  2. /usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/reqs/server.req

  3. /usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/reqs/qiangsh.req

  4. /usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/private/ca.key

  5. /usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/private/server.key

  6. /usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/issued/server.crt

  7. /usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/issued/qiangsh.crt

  8. /usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/dh.pem
复制代码

客户端:(/root/client/easy-rsa)

  1. /root/client/easy-rsa/easyrsa3/pki/private/qiangsh.key

  2. /root/client/easy-rsa/easyrsa3/pki/reqs/qiangsh.req  #这个文件被我们导入到了服务端文件,所以那里也有
复制代码


(6)拷贝服务器密钥及证书等到openvpn目录

  1. [root@vpn ~]# cp /usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/ca.crt /usr/local/share/doc/openvpn/

  2. [root@vpn ~]# cp /usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/private/server.key /usr/local/share/doc/openvpn/

  3. [root@vpn ~]# cp /usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/issued/server.crt /usr/local/share/doc/openvpn/

  4. [root@vpn ~]# cp /usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/dh.pem /usr/local/share/doc/openvpn/
复制代码


(7)拷贝客户端密钥及证书等到client目录

  1. [root@vpn ~]# cp /usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/ca.crt /root/client/

  2. [root@vpn ~]# cp /usr/local/share/doc/openvpn/easy-rsa/easyrsa3/pki/issued/qiangsh.crt /root/client/

  3. [root@vpn ~]# cp/root/client/easy-rsa/easyrsa3/pki/private/qiangsh.key /root/client/
复制代码


(8)为服务端编写配置文件

当安装好openvpn时候,它会提供一个server配置的文件例子

将此例子拷贝openvpn目录,然后配置


  1. [root@vpn ~]# cp /mnt/openvpn-2.3.11/sample/sample-config-files/server.conf /usr/local/share/doc/openvpn/

  2. [root@vpn ~]# vim /usr/local/share/doc/openvpn/server.conf

  3. local 192.168.1.100   #(自己vps IP)

  4. port 1194

  5. proto udp

  6. dev tun

  7. ca /usr/local/share/doc/openvpn/ca.crt

  8. cert /usr/local/share/doc/openvpn/server.crt

  9. key /usr/local/share/doc/openvpn/server.key # This file should be kept secret

  10. dh /usr/local/share/doc/openvpn/dh.pem

  11. server 10.8.0.0 255.255.255.0

  12. ifconfig-pool-persist ipp.txt

  13. push "redirect-gateway def1 bypass-dhcp"

  14. push "dhcp-option DNS 8.8.8.8"

  15. keepalive 10 120

  16. comp-lzo

  17. max-clients 100

  18. persist-key

  19. persist-tun

  20. status openvpn-status.log

  21. verb 3
复制代码


(9)开启系统转发功能

  1. [root@vpn ~]# vim /etc/sysctl.conf

  2. net.ipv4.ip_forward = 0  改成 net.ipv4.ip_forward = 1

  3. [root@vpn ~]# sysctl -p

  4. [root@vpn ~]# sysctl -a | grep net.ipv4.ip_forward

  5. net.ipv4.ip_forward = 1
复制代码


(10)封装出去的数据包(eth0是你的vps外网的网卡):

  1. /sbin/iptables -t nat -I POSTROUTING -s 10.8.0.0/255.255.255.0 -o eth0 -j MASQUERADE
复制代码



三、下载openvpn客户端,并进行配置

1、将客户端密钥及证书等拷出到windows备用

  1. [root@vpn ~]# cd client/

  2. [root@vpn client]# ls

  3. ca.crt  easy-rsa  nmshuishui.crt  nmshuishui.key    #带后缀的这三个
复制代码


2、安装openvpn-gui工具

(1)将C:\ProgramFiles\OpenVPN\sample-config\client.ovpn复制到C:\Program Files\OpenVPN\config
(2)将从linux中拷贝出来的三个密钥及证书放到D:\Program Files (x86)\OpenVPN\config下
(3)编辑C:\ProgramFiles\OpenVPN\config\client.ovpn,修改为


  1. client

  2. dev tun

  3. proto udp

  4. remote 192.168.1.100 1194

  5. resolv-retry infinite

  6. nobind

  7. persist-key

  8. persist-tun

  9. ca ca.crt //这里需要证书

  10. cert qiangsh.crt

  11. key qiangsh.key

  12. comp-lzo

  13. verb 3
复制代码


四、启动服务、测试

1、在vpn服务器上启动openvpn服务

[
  1. root@vpn ~]#/usr/local/sbin/openvpn --config /usr/local/share/doc/openvpn/server.conf &



  2. [root@vpn ~]# echo "/usr/local/sbin/openvpn--config /usr/local/share/doc/openvpn/server.conf &

  3. " >>/etc/rc.local  #设为开机启动
复制代码


2、在openvpn-gui上右键Connect输入密码连接

3、查看vpn状态


您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

关注

0

粉丝

9021

主题
精彩推荐
热门资讯
网友晒图
图文推荐

Powered by Pcgho! X3.4

© 2008-2022 Pcgho Inc.