OpenVPN搭建教程

系统环境:

CentOS  6.75

OpenVPN 2.3.11

安装过程

1、安装EPEL6的yum源并更新本地缓存

#rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
#yum makecache

2、关闭SElinux和防火墙

[[email protected] ~]# service iptables stop
[[email protected] ~]# chkconfig iptables off
vi /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
reboot   //重启server

3、安装openvpn 和easy-rsa

[[email protected] ~]# yum -y install openvpn easy-rsa

4、easy-rsa配置

[[email protected] ~]# mkdir -p /etc/openvpn/easy-rsa/keys
[[email protected] ~]# cp -rf /usr/share/easy-rsa/2.0/* /etc/openvpn/easy-rsa/

5、创建CA证书和密钥

a、vi /etc/openvpn/easy-rsa/vars    //修改以下内容

export KEY_COUNTRY="CN"      //所在国家
export KEY_PROVINCE="SH"     //所在省份
export KEY_CITY="Shanghai"    //所在省份
export KEY_ORG="OpenVPN ORG"    //所在组织
export KEY_EMAIL="[email protected]"  //邮箱
export KEY_OU="OpenVPN"    //所在单位

b、初始化证书的授权中心

[[email protected] easy-rsa]# pwd
/etc/openvpn/easy-rsa
[[email protected] easy-rsa]# source  ./vars 
NOTE: If you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/keys

c、清除已存在的证书文件

[[email protected] easy-rsa]# ./clean-all 
[[email protected] easy-rsa]# ls keys/
index.txt  serial

d、创建CA证书,  一路回车即可

[[email protected] easy-rsa]# ./build-ca 
Generating a 2048 bit RSA private key
..........................+++
............+++
writing new private key to ‘ca.key‘
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.‘, the field will be left blank.
-----
Country Name (2 letter code) [CN]:
State or Province Name (full name) [SH]:
Locality Name (eg, city) [Shanghai]:
Organization Name (eg, company) [OpenVPN ORG]:
Organizational Unit Name (eg, section) [OpenVPN]:
Common Name (eg, your name or your server‘s hostname) [OpenVPN ORG CA]:
Name [EasyRSA]:
Email Address [[email protected]]:
[[email protected] easy-rsa]#
[[email protected] easy-rsa]# ls keys/
ca.crt  ca.key  index.txt  serial

e、创建服务器端证书,server为证书名称

 ./build-key-server server
[[email protected] easy-rsa]# ./build-key-server server
Generating a 2048 bit RSA private key
..........+++
...............................+++
writing new private key to ‘server.key‘
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.‘, the field will be left blank.
-----
Country Name (2 letter code) [CN]:
State or Province Name (full name) [SH]:
Locality Name (eg, city) [Shanghai]:
Organization Name (eg, company) [OpenVPN ORG]:
Organizational Unit Name (eg, section) [OpenVPN]:
Common Name (eg, your name or your server‘s hostname) [server]:
Name [EasyRSA]:
Email Address [[email protected]]:
Please enter the following ‘extra‘ attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /etc/openvpn/easy-rsa/openssl-1.0.0.cnf
Check that the request matches the signature
Signature ok
The Subject‘s Distinguished Name is as follows
countryName           :PRINTABLE:‘CN‘
stateOrProvinceName   :PRINTABLE:‘SH‘
localityName          :PRINTABLE:‘Shanghai‘
organizationName      :PRINTABLE:‘OpenVPN ORG‘
organizationalUnitName:PRINTABLE:‘OpenVPN‘
commonName            :PRINTABLE:‘server‘
name                  :PRINTABLE:‘EasyRSA‘
emailAddress          :IA5STRING:‘[email protected]‘
Certificate is to be certified until Aug 13 02:14:17 2026 GMT (3650 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

注意: 以下两个地方设置为yes

Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
[[email protected] easy-rsa]# ls keys/
01.pem  ca.key     index.txt.attr  serial      server.crt  server.key
ca.crt  index.txt  index.txt.old   serial.old  server.csr

f、生成Diffie-Hellman文件

 ./build-dh
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
[[email protected] easy-rsa]# ls keys
01.pem  ca.key      index.txt       index.txt.old  serial.old  server.csr
ca.crt  dh2048.pem  index.txt.attr  serial         server.crt  server.key

g、生成TLS-auth密钥

这一步是可选操作,openvpn提供了TLS-auth功能,可以用来抵御Dos、UDP端口淹没攻击。出于安全考虑,可以启用该功能。执行以下命令来生成TLS-auth所需要的密钥文件

[[email protected] easy-rsa]# openvpn --genkey --secret keys/ta.key
[[email protected] easy-rsa]# ls keys/
01.pem  ca.key      index.txt       index.txt.old  serial.old  server.csr  ta.key
ca.crt  dh2048.pem  index.txt.attr  serial         server.crt  server.key

6、拷贝服务器端证书、密钥等

[[email protected] keys]# pwd
/etc/openvpn/easy-rsa/keys
[[email protected] keys]# cp server.crt server.key dh2048.pem ca.crt ta.key  /etc/openvpn/
[[email protected] keys]# cd /etc/openvpn/
[[email protected] openvpn]# ls
ca.crt  dh2048.pem  easy-rsa  server.crt  server.key  ta.key

7、修改服务器端配置文件

注意:OpenVPN推荐使用证书进行认证,安全性很高,但是配置起来很麻烦。还好它也能像pptp等vpn一样使用用户名/密码进行认证。不管何种认证方式,服务端的ca.crt, server.crt, server.key, dh1024.pem这四个证书都是要的。使用username/passwd方式,你需要在服务器配置文件中加入以下语句,取消客户端的证书认证,如果不加下面这条指令,则表示需要证书和用户名密码双重验证登录!

client-cert-not-required

然后加入auth-user-pass-verify,开启用户密码脚本:

auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env

加入script-security消除警告

script-security 3 system

checkpsw.sh脚本可以通过网络获取

wget http://openvpn.se/files/other/checkpsw.sh

如果下载失败,可以创建一个checkpsw.sh文件,注意复制文件到linux系统下换行符的问题

内容如下,只需要修改PASSFILE和LOG_FILE两个变量

  1. # cd /usr/local/openvpn/etc/
  2. # wget http://openvpn.se/files/other/checkpsw.sh
  3. # chmod +x checkpsw.sh
  4. # cat checkpsw.sh
  5. #!/bin/sh
  6. ###########################################################
  7. # checkpsw.sh (C) 2004 Mathias Sundman <mathias@openvpn.se>
  8. #
  9. # This script will authenticate OpenVPN users against
  10. # a plain text file. The passfile should simply contain
  11. # one row per user with the username first followed by
  12. # one or more space(s) or tab(s) and then the password.
  13. PASSFILE="/etc/openvpn/psw-file"
  14. LOG_FILE="/etc/openvpn/openvpn-password.log"
  15. TIME_STAMP=`date "+%Y-%m-%d %T"`
  16. ###########################################################
  17. if [ ! -r "${PASSFILE}" ]; then
  18. echo "${TIME_STAMP}: Could not open password file \"${PASSFILE}\" for reading." >>
  19. ${LOG_FILE}
  20. exit 1
  21. fi
  22. CORRECT_PASSWORD=`awk ‘!/^;/&&!/^#/&&$1=="‘${username}‘"{print $2;exit}‘ ${PASSFILE}`
  23. if [ "${CORRECT_PASSWORD}" = "" ]; then
  24. echo "${TIME_STAMP}: User does not exist: username=\"${username}\", password=\"${password}\"." >> ${LOG_FILE}
  25. exit 1
  26. fi
  27. if [ "${password}" = "${CORRECT_PASSWORD}" ]; then
  28. echo "${TIME_STAMP}: Successful authentication: username=\"${username}\"." >> ${LOG_FILE}
  29. exit 0
  30. fi
  31. echo "${TIME_STAMP}: Incorrect password: username=\"${username}\", password=\"${password}\"." >> ${LOG_FILE}
  32. exit 1

checkpsw.sh默认从文件/etc/openvpn/psw-file中读取用户名密码。

赋予checkpsw.sh文件可执行权限

chmod +x checkpsw.sh

psw-file中一行是一个账号,用户名和密码之间用空格隔开,如:

username   password

修改psw-file文件的权限,保证openvpn用户对该文件有读取权限

[[email protected] openvpn]# chmod 400 psw-file 
[[email protected] openvpn]# chown nobody.nobody psw-file 
[[email protected] openvpn]# ll
total 48
-rw-r--r-- 1 root   root    1732 Aug 15 11:03 ca.crt
-rw-r--r-- 1 root   root    1267 Aug 15 12:49 checkpsw.sh
-rw-r--r-- 1 root   root     424 Aug 15 11:03 dh2048.pem
drwxr-xr-x 3 root   root    4096 Aug 15 10:09 easy-rsa
-r-------- 1 nobody nobody    28 Aug 15 12:50 psw-file
-rw-r--r-- 1 root   root   10749 Aug 15 12:56 server.conf
-rw-r--r-- 1 root   root    5483 Aug 15 11:03 server.crt
-rw------- 1 root   root    1704 Aug 15 11:03 server.key
-rw------- 1 root   root     636 Aug 15 11:03 ta.key

拷贝server.conf 配置文件

[[email protected] openvpn]# cp /usr/share/doc/openvpn-2.3.11/sample/sample-config-files/server.conf /etc/openvpn/server.conf
[[email protected] openvpn]# ls
ca.crt  dh2048.pem  easy-rsa  server.conf  server.crt  server.key  ta.key

修改server.conf,配置如下:

[[email protected] openvpn]# cat server.conf|egrep -v "^#|^;|^$"
port 1194     //指定监听的本机端口
proto udp     //指定传输协议
dev tun       //指定通信隧道类型
ca ca.crt      //指定CA证书路径
cert server.crt  //指定服务器端证书路径
key server.key   //指定服务器端私钥文件路径
dh dh2048.pem     //指定迪菲赫尔曼参数的文件路径
server 10.8.0.0 255.255.255.0   //指定虚拟局域网段
ifconfig-pool-persist ipp.txt    //服务器给客户端分配的IP地址,下次客户端继续使用该IP
push "dhcp-option DNS 8.8.8.8"    //
client-to-client   //允许客户端与客户端相连接,默认情况下客户端只能与服务器相连接
keepalive 10 120    //每10秒ping一次,连接超时时间设为120秒
tls-auth ta.key 0 //开启TLS-auth,使用ta.key防御攻击。服务器端的第二个参数值为0,客户端的为1。
comp-lzo          //开启VPN连接压缩,如果服务器端开启,客户端也必须开启
persist-key       
persist-tun    //持久化选项可以尽量避免访问在重启时由于用户权限降低而无法访问的某些资源。
status openvpn-status.log  //指定记录OpenVPN状态的日志文件路径
log         openvpn.log
verb 3    //日志级别

client-cert-not-required //不使用客户端证书认证
username-as-common-name
script-security 3 system

auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env //


服务器端需要的文件,注意checkpsw.sh和psw-file两个文件的权限和属主属组

[[email protected] openvpn]# ll
total 76
-rw-r--r-- 1 root   root    1732 Aug 15 11:03 ca.crt
-rwxr-xr-x 1 root   root    1249 Aug 15 13:42 checkpsw.sh
-rw-r--r-- 1 root   root     424 Aug 15 11:03 dh2048.pem
drwxr-xr-x 3 root   root    4096 Aug 15 13:17 easy-rsa
-rw------- 1 root   root      23 Aug 15 15:25 ipp.txt
-rw------- 1 root   root    3813 Aug 15 15:17 openvpn.log
-rw-r--r-- 1 root   root     434 Aug 15 15:17 openvpn-password.log
-rw------- 1 root   root     371 Aug 15 15:28 openvpn-status.log
-r-------- 1 nobody nobody    28 Aug 15 12:50 psw-file
-rw-r--r-- 1 root   root   10601 Aug 15 15:15 server.conf
-rw-r--r-- 1 root   root   10724 Aug 15 14:40 server.conf.bak
-rw-r--r-- 1 root   root    5483 Aug 15 11:03 server.crt
-rw------- 1 root   root    1704 Aug 15 11:03 server.key
-rw------- 1 root   root     636 Aug 15 11:03 ta.key

8、修改客户端配置文件client.conf

注释掉客户端证书的配置

client.conf配置文件内容

client     //指定当前VPN是客户端
dev tun    //必须与服务器端的保持一致
proto udp  //必须与服务器端的保持一致
remote 172.16.100.225 1194  //指定连接的远程服务器的实际IP地址和端口号
resolv-retry infinite    //断线自动重新连接,在网络不稳定的情况下(例如:笔记本电脑无线网络)非常有用。
nobind     //不绑定特定的本地端口号

user nobody  
group nobody

persist-key
persist-tun

ca ca.crt    //指定CA证书的文件路径
remote-cert-tls server  //

tls-auth ta.key 1      //tls认证开启,必须与服务器一致

comp-lzo        //必须与服务器一致
verb 3  

auth-user-pass        //增加询问账户名密码

9、windows客户端的安装和配置

下载openvpn的客户端:http://www.openvpn.net/release/openvpn-2.0.9-install.exe

安装完成后,将服务器端的

ca.crt  client.conf   ta.key

三个文件拷贝到openvpn安装目录下的config目录中

登录

双击“C:\Program Files (x86)\OpenVPN\bin\openvpn-gui-1.0.3"图标,在桌面右下角弹出openvpn的图标,然后右键--client--Connect,输出账号密码登录

10、OpenVPN 服务器端的防火墙规则配置

清空iptables配置:
 iptables -F
 iptables -X

 配置openvpn的nat功能,将所有网段的包转发到eth0口:
iptables -t nat -A POSTROUTING  -o eth0 -j MASQUERADE

 添加FORWARD白名单:
 iptables -A FORWARD -i tun+ -j ACCEPT

 开启系统的路由功能:
 echo "1" > /proc/sys/net/ipv4/ip_forward

 service iptables save               //保存iptables配置
 service iptables restart            //重启iptables
cat /etc/sysconfig/iptables
# Generated by iptables-save v1.4.7 on Mon Aug 15 16:47:30 2016
*nat
:PREROUTING ACCEPT [38:8664]
:POSTROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A POSTROUTING -o eth0 -j MASQUERADE
COMMIT
# Completed on Mon Aug 15 16:47:30 2016
# Generated by iptables-save v1.4.7 on Mon Aug 15 16:47:30 2016
*filter
:INPUT ACCEPT [218:28319]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [95:13949]
-A FORWARD -i tun+ -j ACCEPT
COMMIT
# Completed on Mon Aug 15 16:47:30 2016

参考资料:

http://www.cnblogs.com/linuxprobe/p/5428098.html

http://www.cnblogs.com/electron/p/3488033.html

http://ylw6006.blog.51cto.com/470441/1009004

时间: 2024-08-10 15:05:11

OpenVPN搭建教程的相关文章

【技术宅拯救世界】在Windows Server2012上利用OpenVPN搭建自己的VPN服务器

写在前面的话:前段时间利用VPN免流特别火,我本来打算买一个,但后来发现其实都是用的OpenVPN搭建的,正好我手上有一个腾讯的云服务器,我一想不如就自己搭建一个吧,省点钱,结果就这样浪费了两天的时间,最大的问题在于网上关于用windows server搭建的教程异常的少,大部分都是cent os并且是脚本搭建的,我看了好多,看得我一头雾水...总算在我折腾服务器两天之后建好了属于自己的第一个VPN服务器,但是免流那块,四川联通失败,浪费了我两天的时间,但是为了避免以后搭建再次搭建VPN时发生问

【干货】ECS服务器OPENVPN搭建,方便管理所有内网服务器

[干货]ECS服务器OPENVPN搭建,方便管理所有内网服务器 使用场景 一台有外网的ECS服务器+N台无外网的ECS服务器,使用OPENVPN管理全部的ECS服务器(包括无外网的ECS服务器). 鉴于ECS服务器的带宽成本比较高的问题,撸主采用的是SLB(外网,据说最近计费方式变多了)+N台ECS服务器集群(包括一台2M外网ECS)+内网RDS服务来提供线上服务,但是客服沟通过得到的答案是只能在控制台用VNC来管理(客服MM确认可以自行搭建VPN来管理),对于熟悉习惯使用SSH的人来说使用管理

Win2012 R2 IIS8.5+PHP(FastCGI)+MySQL运行环境搭建教程

这篇文章主要介绍了Win2012 R2 IIS8.5+PHP(FastCGI)+MySQL运行环境搭建教程,需要的朋友可以参考下 准备篇 一.环境说明: 操作系统:Windows Server 2012 R2 PHP版本:php 5.5.8 MySQL版本:MySQL5.6.15 二.相关软件下载: 1.PHP下载地址: http://windows.php.net/downloads/releases/php-5.5.8-nts-Win32-VC11-x86.zip 2.MySQL下载地址:

Spring+SpringMvc+Mybatis框架集成搭建教程

一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼,网络上又没有很详细的讲解以及搭建的教程.闲来无事,我就利用空闲时间来写这样一个教程和搭建步骤,来帮助那些有问题的小伙伴,让你从此SSM搭建不再有问题. 二.教程目录 1.Spring+SpringMvc+Mybatis框架集成搭建教程一(项目创建) 2.Spring+SpringMvc+Mybat

新巴巴运动网上商城 项目 快速搭建 教程 The new babar sports online mall project quickly builds a tutorial

新巴巴运动网上商城 项目 快速搭建 教程 The new babar sports online mall project quickly builds a tutorial 作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:[email protected] E-mail: 313134555 @qq.com 搭建后台网站.启动 console 服务器 ,它的http 端口 是 8150 Build a background site.Start the console s

(一)phoneGap之环境搭建教程及其example分析

phoneGap之环境搭建教程及其example分析 一.环境搭建 与普通的开发android应用一样,phoneGap也同于原生android应用一样,环境相同,只是有部分不同,下面就我做理解,进行说明,不足之处还请大家及时提醒,谢谢!       一.安装 在安装PhoneGap开发环境之前,需要按顺序安装以下工具: 1.Java SDK java sdk,不安装的话不能正常安装Android SDK. 安装成功检测:启动DOS窗口start-->run-->cmd,在DOS窗口中键入:j

F.I.S本地环境的搭建教程

一.准备开发环境: 1.安装JRE 2.安装nodejs 最好是msi文件,比较省事. 3.(如果是PHP项目)安装php. 首先下载php(我的是5.5.15版本,win7 64位系统) zip,然后按如下步骤配置: a.将zip解压到一个简单明了的路径中,比如:D:/php b.将解压后的文件中的php5ts.dll放到system32文件夹中 c.修改php.ini-production 为php.ini d.修改php.ini的 on windows: 下面的 ;extension_di

Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境搭建教程

原文地址:http://www.osyunwei.com/archives/7378.html 搬运是为了自己找资料方便. 准备篇 一.环境说明: 操作系统:Windows Server 2012 R2 PHP版本:php 5.5.8 MySQL版本:MySQL5.6.15 二.相关软件下载: 1.PHP下载地址: http://windows.php.net/downloads/releases/php-5.5.8-nts-Win32-VC11-x86.zip 2.MySQL下载地址: htt

openvpn搭建文档

# openvpn搭建文档 ## 安装相关包文件 ### 安装openssl和lzo包,lzo包用于压缩通讯数据加快传输速度 ``` yum -y install openssl openssl-devel yum -y install lzo ``` ### 安装openvpn和easy-rsa ``` yum -y install openvpn easy-rsa ``` ## 服务器端生成相关文件 ### 修改vars文件 ``` cd /usr/share/easy-rsa/2.0/ v