http://zhuwanjun.com/?m=201409

 

CentOS安装OpenVPN

CentOS版本:6.4
OpenVPN版本:2.3.4

1.安装依赖包
yum install openssl-devel lzo-devel pam-devel gcc

2.下载安装包
从官网下载安装包openvpn-2.3.4.tar.gz,解压
./configure
make
make install

3.安装easy-rsa
新建/etc/openvpn
下载wget https://github.com/OpenVPN/easy-rsa/archive/release/2.x.zip
解压上述文件,进入解压后的目录 cp -r ./easy-rsa /etc/openvpn
修改/etc/openvpn/easy-rsa/2.0/var文件

# These are the default values for fields
# which will be placed in the certificate.
# Don't leave any of these fields blank.
export KEY_COUNTRY="xx"
export KEY_PROVINCE="xx"
export KEY_CITY="xx"
export KEY_ORG="xx"
export KEY_EMAIL="xxxx"
export KEY_OU="xx"

检查本机安装的openssl版本rpm -q openssl
执行结果:openssl-1.0.1e-16.el6_5.15.x86_64
重命名预置的openssl配置文件
cp /etc/openvpn/easy-rsa/2.0/openssl-1.0.0.cnf /etc/openvpn/easy-rsa/2.0/openssl.cnf

4.生成key
在/etc/openvpn/easy-rsa/2.0执行下列命令
. ./var
./clean-all
./build-ca server
./build-key-server server #服务端密钥
./build-key xxx #xxx为客户端名称
./build-dh #Diffie Hellman参数

5.开启包转发
vim /etc/sysctl.conf
修改net.ipv4.ip_forward = 0为net.ipv4.ip_forward = 1,保存
sysctl –p配置生效
防火墙修改
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT –to-source xx.xx.xx.xx (xx.xx.xx.xx为公网IP)

6.配置文件
从源码包中获取配置文件模板
cp /root/download/openvpn-2.3.4/sample/sample-config-files/server.conf /etc/openvpn/
修改配置文件
loacl xx.xx.xx.xx
ca /etc/openvpn/keys/ca.crt
cert /etc/openvpn/keys/server.crt
key /etc/openvpn/keys/server.key
dh /etc/openvpn/keys/dh2048.pem
push “redirect-gateway def1 bypass-dhcp”

7.服务端启动
screen启动:openvpn –config /etc/openvpn/server.conf
后台启动:openvpn –config /etc/openvpn/server.conf &

8.客户端配置
安装Openvpn客户端
将服务端生成的密钥文件
ca.crt
client.crt
client.key
放在客户端安装目录的config目录
修改客户端配置文件client.ovpn
remote xx.xx.xx.xx 1194 (服务端IP)
ca ca.crt
cert client.crt
key client.key

9.启动客户端
以管理员权限启动客户端OpenVPN GUI
点击Connect

10.固定客户端IP地址
打开配置
client-config-dir ccd
在/etc/openvpn新建ccd目录
在ccd目录中新建以客户端key为名称的文件,如client(无后缀名)
文件内容ifconfig-push 10.8.0.5 10.8.0.6
openvpn只支持255.255.255.252 的子网
252子网只有三个IP,客户端,服务器,广播地址
如子网10.8.0.36 主机10.8.0.37 10.8.37 广播地址10.8.0.39
子网10.8.0.40 主机10.8.0.41 10.8.42 广播地址10.8.0.43
子网10.8.0.44 主机10.8.0.45 10.8.46 广播地址10.8.0.47
子网10.8.0.48 主机10.8.0.49 10.8.50 广播地址10.8.0.51

Categories: OS

3 Comments

buy usa proxies · 05/21/2019 at 9:20 PM

But a smiling visitant here to share the love (:, btw great design.

Guqinz · 05/27/2019 at 1:15 AM

I was recommended this blog by my cousin. I’m not sure whether this post is written by him as no one else know such detailed about my difficulty. You are amazing! Thanks!

Laree Blackston · 06/18/2020 at 7:35 PM

There is definately a lot to find out about this issue. I really like all the points you’ve made.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *