首页 > 系统 > CentOS > 正文

CentOS6.5/7.0 上 OpenVPN的安装

2024-06-28 16:00:27
字体:
来源:转载
供稿:网友

CentOS 上 OpenVPN的安装:

一、安装准备

?

1、安装openssl、gcc、libpam

#yum -y install openssl-devel openssl

#yum -y install gcc gcc-c++

#yum -y install pam-devel.x86_64

如遇Segmentation Fault,在/usr/local/lib下,删除链接libz.so和libz.so.1

#rm -f libz.so

#rm -f libz.so.1

安装完之后恢复这两个链接即可

#ln -s libz.so.1.2.7 libz.so.1

#ln -s libz.so.1 libz.so

二、OpenVPN服务端安装过程

下载lzo,下载链接为http://www.oberhumer.com/opensource/lzo/#download,下载完之后复制到/var/tmp目录下

1、lzo的安装

#cd /var/tmp    //安装目录

#gunzip zxvf lzo-2.04.tar.gz    //解压

#tar zxvf lzo-2.04.tar    //解压

#cd lzo-2.04

#./configure    

#make

#make install   #编译与安装

2、openvpn下载与安装

下载openvpn,下载链接为https://openvpn.net/index.php/open-source/downloads.html,下载完之后复制到/var/tmp目录下

#cd /var/tmp

#gunzip xvf openvpn-2.3.10.tar.gz

#tar xvf openvpn-2.3.10.tar

#./configure

#make

#make install

“lzo enabled but missing” -> lzo configure改为 “./configure --PRefix=/usr/local/”##CentOS 7 会遇到这个问题

“libpam required but missting” ->安装libpam“yum -y install pam-devel.x86_64”##CentOS 7 会遇到这个问题

安装好了检查一下openvpn版本

#openvpn --version

3、easy-sea安装

下载openvpn,下载链接为https://community.openvpn.net/openvpn/wiki/GettingEasyRsa,下载完之后复制到/var/tmp目录下

#cd /var/tmp

#gunzip xvf easy-rsa-2.2.0_master.tar.gz

#tar xvf easy-rsa-2.2.0_master.tar

#./configure

#make

#make install

安装路径

/usr/local/share/easy-esa/

三、制作证书

复制源文件至目标路径

#mkdir /etc/openvpn

#cp -rf /usr/local/share/easy-rsa /etc/openvpn/

0、配置vars

#vi /etc/openvpn/easy-rsa/vars

export KEY_SIZE=2048      //DH证书的长度设为2048,缺省为1024

export KEY_COUNTRY="CN"

export KEY_PROVINCE=“SH”

export KEY_CITY=“Shanghai”

export KEY_ORG=“jhrdc”

export KEY_EMAIL=“admin@jh.cn”

export KEY_OU=“jhrdc“

export KEY_NAME=jhrdc       //特别重要,“jhrdc”根据情况自己设定

#cd /etc/openvpn/easy-rsa

#source vars  //执行脚本生成证书时,需要设置一些环境变量

#./clean-all    //制作客户端证书时,不需要执行该脚本

1、生成根证书-CA证书,CA证书同时需要部署在服务器和客户端上

#./build-ca

#cp keys/ca.crt /etc/openvpn/

2、生成服务器端证书

#./build-key-server jhrdc

#./build-dh

#cp keys/jhrdc.crt keys/jhrdc.key keys/dh2048.pem /etc/openvpn/

3、Client端证书

#./build-key jhrdc    //使用服务器端的CN设定

Generating a 2048 bit RSA private key

................................................................................................+++

....+++

writing new private key to jhrdc.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)[JH]:

Organizational Unit Name (eg, section)[jhrdc]:

Common Name (eg, your name or your server's hostname)[jhrdc]:testchannel1   //给不同的客户端创建证书和私钥时,可以使用不同名字

Name [jhrdc]:testchannel1

Email Address [admin@jh.cn]:

Please enter the following 'extra' attributes

to be sent with your certificate request

A challenge passWord []:

如遇“TXT_DB error number 2”错误,重新touch一个keys/index.txt

生成的客户端证书位于/etc/openvpn/easy-rsa/keys,分别是jhrdc.crt、jhrdc.csr、jhrdc.key。这几个证书和前面所生成的服务器端的证书有所不同,仅可为客户端所使用,请将这几个证书文件和ca.crt打包,拷贝到客户端电脑备用。

注意,同一个证书,缺省配置情况下同时只能从一个客户端登陆

四、配置openvpn服务器

修改服务器配置文件

#cp /var/tmp/openvpn-2.3.10/sample/sample-config-files/server.conf /etc/openvpn/

#vi /etc/openvpn/server.conf

port 1194 //端口设为1194

proto tcp //协议设为tcp

ca ca.crt //ca.crt的路径,建议和server.conf在一个目录下

cert jhrdc.crt //jhrdc.crt的路径,建议和server.conf在一个目录下

key jhrdc.key //jhrdc.key的路径,建议和server.conf在一个目录下

server 10.8.0.0 255.255.255.0 //DHCP服务器的IP段,缺省是10.8.0.0

client-to-client //VPN客户端之间允许互相访问,如不需要客户端之间互相访问,则可注释该配置

max-clients 10 //并发的客户端链接,可设为10个

user nobody //服务启动时使用的系统用户,建议使用nobody

group nobody //服务启动时使用的系统用户组,建议使用nobody

五、启动openvpn的服务器端

#cd /etc/openvpn

#openvpn —config /etc/openvpn/server.conf —daemon

六、配置客户端

下载Windows客户端,下载链接为https://openvpn.net/index.php/download/community-downloads.html,

安装后,将第三步所生成的证书文件和配置文件放在一个目录里,并修改客户端配置文件,然后启动该配置文件即大功告成。配置文件简要说明

client

proto tcp

remote 111.111.111.111 1194  //VPN服务器的IP地址和端口号

ca /etc/openvpn/ca.crt //ca.crt的路径,建议和server.conf在一个目录下

cert /etc/openvpn/testchannel1.crt //testchannel1.crt的路径,建议和server.conf在一个目录下

key /etc/openvpn/testchannel1.key //testchannel1.key的路径,建议和server.conf在一个目录下

Please enter the following 'extra' attributes

to be sent with your certificate request

A challenge password []:Jing7VPN

An optional company name []:JingHang

七、将openvpn添加到启动脚本/etc/rc.d/rc.local里

CentOS 7里面需要给rc.local加上可执行权限,

需要ln -s /etc/rc.d/rc.local /etc/rc.local

=======

#!/bin/sh

#

# This script will be executed *after* all the other init scripts.

# You can put your own initialization stuff in here if you don't

# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

/usr/local/sbin/openvpn --config /etc/openvpn/client.conf --daemon

service iptables start

自启动时,server.conf里,证书的路径需要是绝对路径


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表