centOS6.5安装配置openVPN

来源:互联网 发布:淘宝卖家怎么发布宝贝 编辑:程序博客网 时间:2024/05/01 13:53

因为课程需要,要在linux系统上安装openVPN。我选择的服务端系统是centOS6.5,客户端系统是win7。都是64位的。


1) 准备
开始前可以用whereis xxx来判断是否已经安装程序

# yum -y install openssl openssl-devel lzo lzo-devel pam pam-devel automake pkgconfig

2) 下载安装openVPN

# sudo wget  http://swupdate.openvpn.org/community/releases/openvpn-2.3.8.tar.gz# sudo tar -zxc -f openvpn-2.3.8.tar.gz# cd openvpn-2.3.8# ./configure --prefix=/usr/local/openvpn# make && make install# mkdir -p /etc/openvpn//复制模板到openvpn配置文件# cp -rf sample /etc/openvpn//复制openvpn配置文件到主目录:# cp /etc/openvpn/sample/sample-config-files/server.conf /etc/openvpn/# cd ..

sudo ./configure时若出现error: libpam required but missing则表示你没安装libpam,安装之。

如果你的系统是32位
yum install pam-devel.i686
如果你的系统是64位
yum install pam-devel.x86_64

3) 下载easy-rsa

Easy-rsa
Starting with openvpn-2.3_alpha2 easy-rsa is no longer part of the OpenVPN source or binary packages. It can be downloaded separately from it’s GitHub project page.

:该包用来制作ca证书,服务端证书,客户端证书,openvpn2.3.8该版本源码不包含easy-rsa,所以需要单独下载安装用来配合openvpn实现证书生成。

# wget -c https://github.com/OpenVPN/easy-rsa/archive/master.zip# unzip master# mv easy-rsa-master easy-rsa# cp -rf  easy-rsa /etc/openvpn# cd /etc/openvpn/easy-rsa/easyrsa3/# cp vars.example vars# vim vars

修改配置中的下面字段,最后wq保存

set_var EASYRSA_REQ_COUNTRY "CN" //根据自己情况更改set_var EASYRSA_REQ_PROVINCE "Guangdong"set_var EASYRSA_REQ_CITY "Guangzhou"set_var EASYRSA_REQ_ORG "Sun Yat-sen"set_var EASYRSA_REQ_EMAIL "igeniuszhang@126.com"set_var EASYRSA_REQ_OU "My OpenVPN"

4)创建服务端证书及key
a. 进入/etc/openvpn/easy-rsa/easyrsa3/目录初始化

# cd /etc/openvpn/easy-rsa/easyrsa3/# ./easyrsa init-pki

这里写图片描述


b. 创建根证书

# ./easyrsa build-ca

这里写图片描述
注:在上述部分需要输入PEM密码 PEM pass phrase,输入两次,此密码必须记住,不然以后不能为证书签名。还需要输入common name 通用名,这个你自己随便设置个独一无二的。我这设置的是zhh


c. 创建服务端证书

# ./easyrsa gen-req server nopass

这里写图片描述
该过程中需要输入common name,随意但是不要跟之前的根证书的一样


d. 签约服务端证书:

# ./easyrsa sign server server

这里写图片描述
该命令中.需要你确认生成,要输入yes,还需要你提供我们当时创建CA时设置的密码。如果你忘记了密码,那你就重头开始再来一次吧。


e. 创建Diffie-Hellman,确保key穿越不安全网络的命令:

# ./easyrsa gen-dh

这里写图片描述
等待时间有点长,耐心点!

5)创建客户端证书

a. 进入root目录新建client文件夹,文件夹可随意命名,然后拷贝前面解压得到的easy-ras文件夹(我的在/usr/local/中)到client文件夹,进入下列目录

# cd /root/# mkdir client# cp -R /usr/local/easy-rsa/ client/# cd client/easy-rsa/easyrsa3/

b. 初始化

# ./easyrsa init-pki

init-pki


c. 创建客户端key及生成证书(记住生成证书时自己输入的密码)

# ./easyrsa gen-req zhh//该名字自己定义

gen-req


d. 将得到的zhh.req导入然后签约证书

# cd /etc/openvpn/easy-rsa/easyrsa3///导入req# ./easyrsa import-req /root/client/easy-rsa/easyrsa3/pki/reqs/zhh.req zhh

导入


签约证书

# ./easyrsa sign client zhh//其中生成client所以必须为client,zhh要与之前导入的名字一致

签约


e. 这步很重要,现在说一下我们上面都生成了什么东西
服务端:(etc/openvpn/easy-rsa/文件夹)

/etc/openvpn/easy-rsa/easyrsa3/pki/ca.crt/etc/openvpn/easy-rsa/easyrsa3/pki/reqs/server.req/etc/openvpn/easy-rsa/easyrsa3/pki/reqs/zhh.req/etc/openvpn/easy-rsa/easyrsa3/pki/private/ca.key/etc/openvpn/easy-rsa/easyrsa3/pki/private/server.key/etc/openvpn/easy-rsa/easyrsa3/pki/issued/server.crt/etc/openvpn/easy-rsa/easyrsa3/pki/issued/zhh.crt/etc/openvpn/easy-rsa/easyrsa3/pki/dh.pem

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

/root/client/easy-rsa/easyrsa3/pki/private/zhh.key/root/client/easy-rsa/easyrsa3/pki/reqs/zhh.req //这个文件被我们导入到了服务端文件所以那里也有

拷贝这些文件放入到相应位置。将下列文件放到/etc/openvpn/ 目录

# cp /etc/openvpn/easy-rsa/easyrsa3/pki/ca.crt /etc/openvpn# cp /etc/openvpn/easy-rsa/easyrsa3/pki/private/server.key /etc/openvpn# cp /etc/openvpn/easy-rsa/easyrsa3/pki/issued/server.crt /etc/openvpn# cp /etc/openvpn/easy-rsa/easyrsa3/pki/dh.pem /etc/openvpn

这样就将上述四个文件放入到了/etc/openvpn目录下。

将下列文件放到/root/client 目录下:

# cp /etc/openvpn/easy-rsa/easyrsa3/pki/ca.crt /root/client# cp /etc/openvpn/easy-rsa/easyrsa3/pki/issued/zhh.crt /root/client# cp /root/client/easy-rsa/easyrsa3/pki/private/zhh.key /root/client

这样就将上述三个文件复制到了/root/client目录,包括:ca.crt、zhh.crt、zhh.key。

6) 为服务端写配置文件
当你安装好了openvpn时候,他会提供一个server配置的文件例子,在
/usr/share/doc/openvpn-2.3.2/sample/sample-config-files/server.conf

将其复制到/etc/openvpn

# cp /usr/share/doc/openvpn-2.3.8/sample/sample-config-files/server.conf /etc/openvpn

之后配置:
# vim /etc/openvpn/server.conf

# 设置监听IP,默认是监听所有IP;local a.b.c.d# 设置监听端口,必须要对应的在防火墙里面打开port 1194# 设置用TCP还是UDP协议?;proto tcpproto udp# 设置创建tun的路由IP通道,还是创建tap的以太网通道# 路由IP容易控制,所以推荐使用它;但如果如IPX等必须# 使用第二层才能通过的通讯,则可以用tap方式,tap也# 就是以太网桥接;dev tapdev tun# Windows需要给网卡一个名称,这里设置,linux不需要;dev-node MyTap# 这里是重点,必须指定SSL/TLS root certificate (ca),# certificate(cert), and private key (key)# ca文件是服务端和客户端都必须使用的,但不需要ca.key# 服务端和客户端指定各自的.crt和.key# 请注意路径,可以使用以配置文件开始为根的相对路径,# 也可以使用绝对路径# 请小心存放.key密钥文件ca ca.crtcert server.crtkey server.key# This file should be kept secret# 指定Diffie hellman parameters.dh dh.pem# 配置VPN使用的网段,OpenVPN会自动提供基于该网段的DHCP# 服务,但不能和任何一方的局域网段重复,保证唯一server 10.8.0.0 255.255.255.0# 维持一个客户端和virtual IP的对应表,以方便客户端重新# 连接可以获得同样的IPifconfig-pool-persist ipp.txt# 配置为以太网桥模式,但需要使用系统的桥接功能# 这里不需要使用;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100# 为客户端创建对应的路由,以另其通达公司网内部服务器# 但记住,公司网内部服务器也需要有可用路由返回到客户端;push "route 192.168.20.0 255.255.255.0"push "route 172.24.30.0 255.255.255.0"# 为特定的客户端指定IP或指定路由,该路由通常是客户端后面的# 内网网段,而不是服务端连接的网段# ccd是/etc/openvpn下的目录,其中建有希望限制的客户端Common# Name为文件名的文件,并通过下面的命令写入固定IP地址# 例如Common Name为client1,则在/etc/openvpn/ccd/client1写有:# ifconfig-push 10.9.0.1 10.9.0.2;client-config-dir ccd;route 192.168.40.128 255.255.255.248# 为可以对不同的客户端设置防火墙等权限# 可以让其自动运行对应脚本,可参考man;learn-address ./script# 若客户端希望所有的流量都通过VPN传输,则可以使用该语句# 其会自动改变客户端的网关为VPN服务器,推荐关闭# 一旦设置,请小心服务端的DHCP设置问题;push "redirect-gateway"# 用OpenVPN的DHCP功能为客户端提供指定的DNS、WINS等;push "dhcp-option DNS 10.8.0.1";push "dhcp-option WINS 10.8.0.1"# 默认客户端之间是不能直接通讯的,除非把下面的语句注释掉client-to-client# 如果您希望有相同Common Name的客户端都可以登陆# 也可以注释下面的语句,推荐每个客户端都使用不用的Common Name# 常用于测试;duplicate-cn# 设置服务端检测的间隔和超时时间keepalive 10 120# 下面是一些对安全性增强的措施# For extra security beyond that provided# by SSL/TLS, create an "HMAC firewall"# to help block DoS attacks and UDP port flooding.## Generate with:# openvpn --genkey --secret ta.key## The server and each client must have# a copy of this key.# The second parameter should be 0# on the server and 1 on the clients.;tls-auth ta.key 0 # This file is secret# Select a cryptographic cipher.# This config item must be copied to# the client config file as well.;cipher BF-CBC # Blowfish (default);cipher AES-128-CBC # AES;cipher DES-EDE3-CBC # Triple-DES# 使用lzo压缩的通讯,服务端和客户端都必须配置comp-lzo# 设置最大用户数;max-clients 100# 让OpenVPN以nobody用户和组来运行(安全);user nobody;group nobody# The persist options will try to avoid# accessing certain resources on restart# that may no longer be accessible because# of the privilege downgrade.persist-keypersist-tun# 输出短日志,每分钟刷新一次,以显示当前的客户端status /var/log/openvpn/openvpn-status.log# 缺省日志会记录在系统日志中,但也可以导向到其他地方# 建议调试的使用先不要设置,调试完成后再定义log /var/log/openvpn/openvpn.loglog-append /var/log/openvpn/openvpn.log# 设置日志的级别## 0 is silent, except for fatal errors# 4 is reasonable for general usage# 5 and 6 can help to debug connection problems# 9 is extremely verboseverb 3# Silence repeating messages. At most 20# sequential messages of the same message# category will be output to the log.;mute 20

7)下载openvpn客户端,进行配置
a. 用pscp(这是我采用的工具,自行决定)将在服务端生成的客户端证书和Key下载到客户端电脑。包括下面三个文件:
ca.crt zhh.crt zhh.key

b. 去官网下载openvpn客户端进行安装,然后安装目录找到simple-config

F:\Program Files\OpenVPN\sample-config\client.ovpn
将client.ovpn 复制到F:\Program Files\OpenVPN\config下,我把客户端装在了F盘你根据自己情况选择。

将下载到的三个文件放入F:\Program Files\OpenVPN\config

c. 编辑client.ovpn:

# 定义是一个客户端client# 定义使用路由IP模式,与服务端一致;dev tapdev tun# 定义Windows下使用的网卡名称,linux不需要;dev-node MyTap# 定义使用的协议,与服务端一致;proto tcpproto udp# 指定服务端地址和端口,可以用多行指定多台服务器# 实现负载均衡(从上往下尝试)remote 192.168.100.90 1194;remote my-server-2 1194# 若上面配置了多台服务器,让客户端随机连接;remote-random# 解析服务器域名# Keep trying indefinitely to resolve the# host name of the OpenVPN server. Very useful# on machines which are not permanently connected# to the internet such as laptops.resolv-retry infinite# 客户端不需要绑定端口# Most clients do not need to bind to# a specific local port number.nobind# 也是为了让Openvpn也nobody运行(安全)# 注意:Windows不能设置;user nobody;group nobody# Try to preserve some state across restarts.persist-keypersist-tun# 若客户端通过HTTP Proxy,在这里设置# 要使用Proxy,不能使用UDP为VPN的通讯协议;http-proxy-retry # retry on connection failures;http-proxy [proxy server] [proxy port #]# 无线网络有很多多余的头文件,设置忽略它;mute-replay-warnings# 重点,就是指定ca和客户端的证书ca ca.crtcert zhh.crtkey zhh.key# 如果服务端打开了PAM认证模块,客户端需要另其有效;auth-user-pass# 一些安全措施# Verify server certificate by checking# that the certicate has the nsCertType# field set to "server". This is an# important precaution to protect against# a potential attack discussed here:# http://openvpn.net/howto.html#mitm## To use this feature, you will need to generate# your server certificates with the nsCertType# field set to "server". The build-key-server# script in the easy-rsa folder will do this.;ns-cert-type server# If a tls-auth key is used on the server# then every client must also have the key.;tls-auth ta.key 1# Select a cryptographic cipher.# If the cipher option is used on the server# then you must also specify it here.;cipher x# 使用lzo压缩,与服务端一致comp-lzo# Set log file verbosity.verb 3# Silence repeating messages;mute 20

8) 启动连接:
a. 服务端:

# openvpn --config /etc/openvpn/server.conf &

b. 启用客户端
打开openvpn GUI,启动连接后输入创建zhh.key时的密码。
若分配了ip则连接成功。
这里写图片描述
这里写图片描述

到此全部结束。只为备忘,网络方面的知识还不是很懂,往后慢慢学习。

参考:

  • 完整CentOS搭建OpenVPN服务环境图文教程
  • CentOS 6.3下利用OpenVPN部署远程VPN服务
  • CentOS系统使用openvpn搭建vpn服务器图文教程
0 0
原创粉丝点击