1、安装open 和easy-rsa(该包用来制作ca证书)
(1)安装epel 仓库源
wget http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -Uvh epel-release-6-8.noarch.rpm
(2)安装open
yum install open
(3)在github 上,下载最新的easy-rsa
https://github.com/OpenVPN/easy-rsa 下载包
mkdir open
cd open
unzip easy-rsa-3.0.5.zip
mv easy-rsa-3.0.5 easy-rsa
2、配置/etc/open/ 目录
(1)创建目录,并复制easy-rsa 目录
cp -a easy-rsa /etc/open/
(2)配置,编辑vars文件,根据自己环境配置
cd /etc/open/easy-rsa/easyrsa3
cp vars.example vars
cd /etc/open/easy-rsa/easyrsa3
vim vars
set_var EASYRSA_REQ_COUNTRY "CN"
set_var EASYRSA_REQ_PROVINCE "ZheJiang"
set_var EASYRSA_REQ_CITY "HangZhou"
set_var EASYRSA_REQ_ORG "ethnicity"
set_var EASYRSA_REQ_EMAIL "ops
set_var EASYRSA_REQ_EMAIL "[email protected]"
set_var EASYRSA_REQ_OU "My OpenVPN"
3、创建服务端证书及key
进入
/etc/open/easy-rsa/easyrsa3/目录
① 初始化
cd /etc/open/easy-rsa/easyrsa3/
./easyrsa init-pki
② 创建根证书
./easyrsa build-ca
注意:在上述部分需要输入PEM密码 PEM pass phrase,输入两次,此密码必须记住,不然以后不能为证书签名。还需要输入common name 通用名,这个你自己随便设置个独一无二的。
③ 创建服务器端证书
./easyrsa gen-req server nopass
该过程中需要输入common name,随意但是不要跟之前的根证书的一样
④ 签约服务端证书
./easyrsa sign server server
该命令中.需要你确认生成,要输入yes,还需要你提供我们当时创建CA时候的密码。如果你忘记了密码,那你就重头开始再来一次吧
⑤ 创建Diffie-Hellman,确保key穿越不安全网络的命令
./easyrsa gen-dh
4、创建客户端证书
① 进入root目录新建client文件夹,文件夹可随意命名,然后拷贝前面解压得到的easy-ras文件夹到client文件夹,进入下列目录
mkdir client
cp /etc/open/easy-rsa client/
cd client/easy-rsa/easyrsa3/
② 初始化
./easyrsa init-pki //需输入yes 确定
③ 创建客户端key及生成证书(记住生成是自己客户端登录输入的密码)
./easyrsa gen-req ethnicity //名字自己定义
④ 将的到的qingliu.req导入然后签约证书
a. 进入到
/etc/open/easy-rsa/easyrsa3/
cd /etc/open/easy-rsa/easyrsa3/
b. 导入req
./easyrsa import-req /root/client/easy-rsa/easyrsa3/pki/reqs/ethnicity.req ethnicity
c. 签约证书
./easyrsa sign client ethnicity
//这里生成client所以必须为client,along要与之前导入名字一致
上面签约证书跟server类似,就不截图了,但是期间还是要输入CA的密码
5、把服务器端必要文件放到etc/open/ 目录下
ca的证书、服务端的证书、秘钥
cp /etc/open/easy-rsa/easyrsa3/pki/ca.crt /etc/open/
cp /etc/open/easy-rsa/easyrsa3/pki/private/server.key /etc/open/
cp /etc/open/easy-rsa/easyrsa3/pki/issued/server.crt /etc/open/
cp /etc/open/easy-rsa/easyrsa3/pki/dh.pem /etc/open/
客户端的证书、
cp /etc/open/easy-rsa/easyrsa3/pki/ca.crt /root/client/
cp /etc/open/easy-rsa/easyrsa3/pki/issued/along.crt /root/client/
cp /root/client/easy-rsa/easyrsa3/pki/private/along.key /root/client
vim /etc/open/server.conf
local 0.0.0.0
port 1194
proto tcp
dev tun
ca /etc/open/ca.crt
cert /etc/open/server.crt
key /etc/open/server.key
dh /etc/open/dh.pem
server 10.222.1.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 8.8.8.8"
client-to-client
keepalive 10 120
comp-lzo
max-clients 100
push "route 172.19.208.0 255.255.240.0"
user open
group open
persist-key
persist-tun
status /var/log/open/open-status.log
log /var/log/open/open.log
#plugin /usr/lib64/open/plugins/open-plugin-auth-pam.so login
plugin /usr/lib64/open/plugin/lib/open-auth-ldap.so "/etc/open/auth/ldap.conf %u"
client-cert-not-required
username-as-common-name
script-security 3
verb 3
ldap配置文件
cat /etc/open/auth/ldap.conf
URL ldap://172.19.220.168:389
BindDN cn=admin,dc=ethnicity,dc=cn
Password xxxxxxx
Timeout 15
TLSEnable no
FollowReferrals no
BaseDN "ou=People,dc=ethnicity,dc=cn"
SearchFilter "uid=%u"
RequireGroup true
BaseDN "ou=Group,dc=ethnicity,dc=cn"
SearchFilter "(objectclass=groupOfUniqueNames)"
MemberAttribute uniqueMember
8、iptables 设置nat 规则和打开路由转发
iptables -t nat -A POSTROUTING -s 0.0.0.0/0 -j MASQUERADE
iptables -vnL -t nat
vim /etc/sysctl.conf //打开路由转发
net.ipv4.ip_forward = 1
sysctl -p
9、开启open 服务
open /etc/open/server.conf 开启服务
ss -nutl |grep 1194
添加用户的脚本
cat add_user.sh
#!/bin/bash
user=$1
cd /root/client/easy-rsa/easyrsa3/
./easyrsa init-pki
./easyrsa gen-req $user
rm -rf /etc/open/easy-rsa/easyrsa3/pki/reqs/$user.req
cd /etc/open/easy-rsa/easyrsa3/
./easyrsa import-req /root/client/easy-rsa/easyrsa3/pki/reqs/$user.req $user
./easyrsa sign client $user
cp -r /etc/open/easy-rsa/easyrsa3/pki/ca.crt /root/client/
cp /etc/open/easy-rsa/easyrsa3/pki/issued/$user.crt /root/client/
cp /root/client/easy-rsa/easyrsa3/pki/private/$user.key /root/client
最后附件client的配置
cat client.o
client
dev tun
proto tcp
resolv-retry infinite
nobind
persist-key
persist-tun
verb 3
comp-lzo
remote-cert-tls server
route-delay 2
cipher AES-256-CBC
remote xxxxxx1194
ca /etc/open/client/ca.crt
auth-user-pass /etc/open/client/pass.txt
auth-nocache
/etc/open/client/pass.txt 账号密码在两行