CentOS6下搭建OpenVPN服务器

• OpenVPN简介


OpenVPN是一个用于创建虚拟专用网络(Virtual Private Network)加密通道的免费开源软件。使用OpenVPN可以方便地在家庭、办公场所、住宿酒店等不同网络访问场所之间搭建类似于局域网的专用网络通道。OpenVPN使用方便,运行性能优秀,支持Solaris、Linux 2.2+(Linux 2.2+表示Linux 2.2及以上版本,下同)、OpenBSD 3.0+、FreeBSD、NetBSD、Mac OS X、Android和Windows 2000+的操作系统,并且采用了高强度的数据加密,再加上其开源免费的特性,使得OpenVPN成为中小型企业及个人的VPN首选产品。使用OpenVPN配合特定的代理服务器,可用于访问Youtube、FaceBook、Twitter等受限网站,也可用于突破公司的网络限制。

• 工具/原料

服务器端:CentOS6.5
客  户  端:Windows7
服务器端软件:epel-release-6-8.noarch.rpm,openvpn,easy-rsa
客户端软件: openvpn-install-2.3.4

• 服务器端安装及配置

1. 关闭SELINUX

 setenforce 0            //暂时关闭 sed  -i  ‘^SELINUX=/c\SELINUX=disabled‘   /etc/selinux/config           //重启有效 

2. 安装"EPEL"源

 wget  http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm          //下载centos6 32位的EPEL源 rpm -ivh epel-release-6-8.noarch.rpm          //安装EPEL源 yum makecache          //更新本地缓存 

3. 安装openvpn

yum -y install openvpn easy-rsa 

4. easy-rsa配置

 mkdir -p /etc/openvpn/easy-rsa/keys cp -rf/usr/share/easy-rsa/2.0/* /etc/openvpn/easy-rsa/ 

5.  创建CA证书和密钥

 vi /etc/openvpn/easy-rsa/vars                  //更改你自己的国家,省份,城市,邮箱等... source   ./vars                 //初始化证书的授权中心 ./clean-all                      //清除keys目录下面的文件 ./build-ca                  //创建ca证书 Generating a 1024 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) [CA]: Locality Name (eg, city) [HZ]: Organization Name (eg, company) [HZ]: Organizational Unit Name (eg, section) [changeme]: Common Name (eg, your name or your server‘s hostname) [changeme]: Name [changeme]: Email Address [[email protected]]: 

6.  创建服务端的证书和密钥

./build-key-server server Generating a 1024 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) [CA]: Locality Name (eg, city) [HZ]: Organization Name (eg, company) [HZ]: Organizational Unit Name (eg, section) [changeme]: Common Name (eg, your name or your server‘s hostname) [server]: Name [changeme]: 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/easy-rsa/2.0/openssl.cnf Check that the request matches the signature Signature ok The Subject‘s Distinguished Name is as follows countryName :PRINTABLE:‘CN‘ stateOrProvinceName :PRINTABLE:‘CA‘ localityName :PRINTABLE:‘HZ‘ organizationName :PRINTABLE:‘HZ‘ organizationalUnitName:PRINTABLE:‘changeme‘ commonName :PRINTABLE:‘server‘ name :PRINTABLE:‘changeme‘ emailAddress :IA5STRING:‘[email protected]‘ Certificate is to be certified until Mar 28 03:05:21 2022 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 Update 

7. 创建客户端的证书和密钥

./build-key client1 Generating a 1024 bit RSA private key ...++++++ ...............++++++ writing new private key to ‘client1.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) [CA]: Locality Name (eg, city) [HZ]: Organization Name (eg, company) [HZ]: Organizational Unit Name (eg, section) [changeme]: Common Name (eg, your name or your server‘s hostname) [client1]: Name [changeme]: 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/easy-rsa/2.0/openssl.cnf Check that the request matches the signature Signature ok The Subject‘s Distinguished Name is as follows countryName :PRINTABLE:‘CN‘ stateOrProvinceName :PRINTABLE:‘CA‘ localityName :PRINTABLE:‘HZ‘ organizationName :PRINTABLE:‘HZ‘ organizationalUnitName:PRINTABLE:‘changeme‘ commonName :PRINTABLE:‘client1‘ name :PRINTABLE:‘changeme‘ emailAddress :IA5STRING:‘[email protected]‘ Certificate is to be certified until Mar 28 03:21:06 2022 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 

8. 创建迪菲霍尔曼密钥交换参数

 ./build-dh 

9. 拷贝服务端证书、秘钥等

 cp /etc/openvpn/easy-rsa/keys/{server.crt,server.key,dh2048.pem,ca.crt} /etc/openvpn 

10. 配置VPN服务端

 cp /usr/share/doc/openvpn-2.3.*/sample/sample-config-files/server.conf/etc/openvpn/ //复制配置文件到/etc/openvpn 

修改服务端配置文件:

 cat server.conf | grep "^[^#|^;]"                           //列出未注释的内容 
 local 10.17.1.20    #监听地址 port 1194      #监听端口 proto tcp      #监听协议 dev tun      #采用路由隧道模式 ca ca.crt  #ca证书路径 cert server.crt    #服务器证书 key server.key    # This file should be kept secret 服务器密钥 dh dh2048.pem  #密钥交换协议文件 server 10.8.0.0 255.255.255.0        #给客户端分配地址池,注意:不能和VPN服务器内网网段有相同 ifconfig-pool-persist ipp.txt push "route 192.168.20.0 255.255.255.0"  #允许客户端访问内网 20.0 的网段。 push"dhcp-option DNS 8.8.8.8"              #dhcp分配dns client-to-client    #客户端之间互相通信 keepalive 10 120  #存活时间,10秒ping一次,120 如未收到响应则视为断线 comp-lzo        #传输数据压缩 max-clients 100  #最多允许 100 客户端连接 user nobody        #用户 group nobody      #用户组 persist-key persist-tun status        /var/log/openvpn/openvpn-status.log log        /var/log/openvpn/openvpn.log verb 3 

11. iptables配置

 清空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 

12. 启动openvpn

 service openvpn start 

13. 配置客户端

复制客户端配置文件client.ovpn:

 cp /usr/share/doc/openvpn-2.3.*/sample/sample-config-files/client.conf   /etc/openvpn/client.ovpn 

修改客户端配置文件:

 cat server.conf | grep "^[^#|^;]" 
 client dev tun proto tcp     //改为tcp remote 203.195.xxx.xxx 1194       //OpenVPN服务器的外网IP和端口 resolv-retry infinite nobind persist-key persist-tun ca ca.crt    //client1的证书 cert client.crt key client.key    //client1的密钥 ns-cert-type server comp-lzo verb 3 

• OpenVPN客户端配置

1. 拷贝服务器端/etc/openvpn/easy-rsa/keys/{ca.crt,client.crt,client,key}和/etc/openvpn/client.ovpn到Windows7客户端
2. 下载openvpn客户端安装
下载地址: http://pan.baidu.com/s/1ZsgpS
3. 把刚才复制过来的几个文件拷贝到openvpn客户端安装目录下面的config目录里面(C:\Program Files\OpenVPN\config)
4. 启动OpenVPN GUI
在电脑右下角的openvpn图标上右击,选择“Connect”。正常情况下应该能够连接成功,分配正常的IP。

原文来自:http://www.linuxprobe.com/centos6-setup-openvpn.html

更多Linux干货请访问:http://www.linuxprobe.com/

时间: 2024-08-25 11:29:08

CentOS6下搭建OpenVPN服务器的相关文章

CentOS6.6下搭建OpenVPN服务器

OpenVPN是用于创建虚拟专用网络(Virtual Private Network)加密通道的免费开源软件.官网 http://www.openvpn.net/. 一.服务器端安装及配置 服务器环境:干净的CentOS6.5 64位系统 OpenVPN版本:OpenVPN 2.3.2 x86_64-redhat-linux-gnu     1.安装前准备 # 关闭selinux setenforce 0 sed -i '/^SELINUX=/c\SELINUX=disabled' /etc/s

CentOS6.5 搭建OpenVPN服务器

前言: 之前搭建过程中找了5-6个教程一起看,真是累,难道就没有写的详细一点,一次成功的吗,基于此花了一下午制作了本教程,实际测试2遍均成功,懒人福音. 基础环境: 系统:Centos6.5 64X Openvpn:2.3.14 安装: Windows版 Linux版 问题: Windows版有路由推送.用户名密码验证方面的问题,不推荐使用.建议安装Linux版 附件中有密码检查脚本,配置文件等. 0基础环境 #关闭selinux setenforce 0 sed -i '/^SELINUX=/

Centos6.5搭建openvpn服务器

因为新版本openvpn里面没有包含最重要的证书制作部分:easy-rsa所以,需要事先下载好easyrsa,可以到GitHub上进行下载,配置过程将在下面第3步进行,本次部署使用了easy-rsa3,与easy-rsa2.0的操作完全不同,网上其它关于easy-rsa2.0的教程不适合本次部署 在部署openvpn之前,最好用ntpdate同步一下服务器的时间,否则生成证书的时间也不准确,会造成那个什么centificate error等的错误! 1.安装lzo lzo是致力于解压速度的一种数

Centos6下搭建svn服务器

最近在研究JSP,没有SVN的话,很不方便,家里和公司不同步,索性公司有多余的云服,就自己搭了一个 安装svn服务器 yum install subversion 创建版本库文件夹 mkdir -p /data/svn/svnrepos 创建版本库 svnadmin create /data/svn/svnrepos 查看目录内容 [[email protected] svnrepos]# cd /data/svn/svnrepos/[[email protected] svnrepos]# l

Centos6 下搭建wiki服务器

CentOS 6.4 系统及 Root 或者 sudo 权限,系统正常连接网络 使用到的软件: apache ,mysql-server ,php ,mediawiki,memcached 软件包的安装 首先,需要安装apache, php, mysql-server,mysql-client 等相关软件包 #yum install httpd php php-mysql php-gd php-xml mysql-server mysql libxml2memcached lrzsz 这里需要设

VPS CentOS-6 下 LNMP HTTP服务器的搭建

VPS CentOS-6 下 LNMP HTTP服务器的搭建 前言 恢复更新后的第一篇博文, 前段时间由于各种理由, 把博客更新给宕掉了, 个人独立博客的开发也搁浅了, 现在随着工作的逐步稳定, 决心把博客重新恢复更新, 继续记录着代码路上的一点一滴. 在上篇博文中提到, 笔者将把工作和学习环境全面转移到Linux, 转移也没什么目的, Windows用久了, 换个口味.目前使用的Linux环境是 Ubuntu 14.04, 该版本是一个长期支持版本, 对于想试一试Linux系统的朋友, 也是值

Centos6.8下搭建SVN服务器

1.Centos6.8下搭建SVN服务器 Subversion是一个自由,开源的版本控制系统.Subversion将文件存放在中心版本库里.这个版本库很像一个普通的文件服务器,不同的是,它可以记录每一次文件和目录的修改情况.这样就可以籍此将数据恢复到以前的版本,并可以查看数据的更改细节.Subversion是Apache基金会下的一个项目,官网 https://subversion.apache.org/ . 2.安装 yum -y install subversion 3.创建SVN版本库 m

CentOS6.5下搭建ftp服务器(三种认证模式:匿名用户、本地用户、虚拟用户)

CentOS 6.5下搭建ftp服务器 vsftpd(very secure ftp daemon,非常安全的FTP守护进程)是一款运行在Linux操作系统上的FTP服务程序,不仅完全开源而且免费,此外,还具有很高的安全性.传输速度,以及支持虚拟用户验证等其他FTP服务程序不具备的特点,下面开始搭建: 1.用root 进入系统 2.使用命令 rpm  -qa | grep vsftpd 查看系统是否安装了ftp,若安装了vsftp,使用这个命令会在屏幕上显示vsftpd的版本 3.如果安装了,可

搭建openVPN服务器

搭建openVPN服务器 概述: OpenVPN允许参与建立VPN的单点使用共享密钥,电子证书,用户名/密码来进行身份验证,它大量使用了OpenSSL加密码库中的SSLv3/TLSv1协议函式库. 目前OpenVPN能在Solaris, Linux, OpenBSD, FreeBSD, NetBSD, Mac OS X与Windows 2000/XP/Vista上运行,并包含了许多安全性的功能, 它并不是一个基于Web的VPN软件, 也不与IPsec及其它VPN软件包兼容.OpenVPN所有的通